mentorenwahl/docker/backend/src/backend/api/schema/helpers.cr
Dominic Grimm 3a19d1d8db
All checks were successful
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing
Fixed file license headers
2022-03-07 14:06:02 +01:00

61 lines
1.8 KiB
Crystal

# Mentorenwahl: A fullstack application for assigning mentors to students based on their whishes.
# Copyright (C) 2022 Dominic Grimm
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
module Backend
module Api
module Schema
# Schema helper macros
module Helpers
# Object helpers
module ObjectMacros
# Defines field property and GraphQL specific getter
macro field(type)
property {{ type.var }} {% if type.value %} = {{ type.value }}{% end %}
@[GraphQL::Field]
def {{ type.var }} : {{ type.type }}
@{{ type.var }}
end
end
end
# DB model field helpers
module DbObject
# Defines DB model field helper functions
macro db_object(type)
private property model
def initialize(@model : {{ type }})
end
def initialize(id : Int32)
@model = {{ type }}.find!(id)
end
{% space_name = type.names.last.underscore.gsub(/_/, " ").capitalize %}
@[GraphQL::Field]
# {{ space_name }}'s ID
def id : Int32
@model.id.not_nil!.to_i
end
end
end
end
end
end
end