mentorenwahl/backend/src/backend/api/schema/helpers.cr

43 lines
1.4 KiB
Crystal
Raw Normal View History

2022-02-10 07:43:47 +00:00
# Mentorenwahl: A fullstack application for assigning mentors to students based on their whishes.
# Copyright (C) 2022 Dominic Grimm
2022-03-07 13:06:02 +00:00
#
2022-02-10 07:43:47 +00:00
# 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.
2022-03-07 13:06:02 +00:00
#
2022-02-10 07:43:47 +00:00
# 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.
2022-03-07 13:06:02 +00:00
#
2022-02-10 07:43:47 +00:00
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
2022-01-23 08:12:57 +00:00
module Backend
module Api
2022-01-23 08:12:57 +00:00
module Schema
2022-02-09 13:35:35 +00:00
# Schema helper macros
2022-01-23 08:12:57 +00:00
module Helpers
2022-03-07 17:10:55 +00:00
# Defines DB model field helper functions
2022-11-21 18:48:53 +00:00
macro db_object(type, id_type)
2022-03-07 17:10:55 +00:00
def initialize(@model : {{ type }})
end
2022-01-23 08:12:57 +00:00
def self.from_id(id : Int32) : self
new({{ type }}.query.find! { var(:id) == id })
2022-03-07 17:10:55 +00:00
end
2022-01-23 08:12:57 +00:00
2022-03-07 17:10:55 +00:00
{% space_name = type.names.last.underscore.gsub(/_/, " ").capitalize %}
2022-03-07 17:10:55 +00:00
@[GraphQL::Field]
# {{ space_name }}'s ID
2022-11-21 18:48:53 +00:00
def id : {{ id_type }}
@model.id
2022-01-23 08:12:57 +00:00
end
end
end
end
end
end