96 lines
2.7 KiB
Crystal
96 lines
2.7 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 leverage helpers
|
|
module ObjectDbInit
|
|
# Defines a DB model specific initializer
|
|
macro db_init(type)
|
|
def initialize(obj : {{ type }})
|
|
initialize(obj.id.not_nil!)
|
|
end
|
|
end
|
|
end
|
|
|
|
# DB model finder helpers
|
|
module ObjectFinders
|
|
# Defines finder
|
|
macro finders(type)
|
|
# Finds object by ID
|
|
def find : {{ type }}?
|
|
{{ type }}.find(@id)
|
|
end
|
|
|
|
# :ditto:
|
|
def find! : {{ type }}
|
|
obj = find
|
|
raise "#{{{ type }}} not found" unless obj
|
|
|
|
obj
|
|
end
|
|
end
|
|
end
|
|
|
|
# DB model field helpers
|
|
module DbObject
|
|
# Defines DB model field helper functions
|
|
macro db_object(type)
|
|
{% space_name = type.names.last.underscore.gsub(/_/, " ").capitalize %}
|
|
|
|
include ::Backend::Api::Schema::Helpers::ObjectDbInit
|
|
include ::Backend::Api::Schema::Helpers::ObjectFinders
|
|
|
|
db_init {{ type }}
|
|
finders {{ type }}
|
|
|
|
property id
|
|
|
|
def initialize(@id : Int32)
|
|
end
|
|
|
|
def initialize(obj : {{ type }})
|
|
@id = obj.id.not_nil!.to_i
|
|
end
|
|
|
|
@[GraphQL::Field]
|
|
# {{ space_name }}'s ID
|
|
def id : Int32
|
|
@id
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|