mentorenwahl/docker/backend/src/backend/api/schema/query.cr

107 lines
2.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
@[GraphQL::Object]
class Query < GraphQL::BaseQuery
@[GraphQL::Field]
# Retuns true
def ok : Bool
true
end
@[GraphQL::Field]
# Current authenticated user
def me(context : Context) : User
context.authenticated!
User.new(context.user.not_nil!)
end
@[GraphQL::Field]
# User by ID
def user(context : Context, id : Int32) : User
context.admin!
User.new(id)
end
@[GraphQL::Field]
# All users
def users(context : Context) : Array(User)
context.admin!
Db::User.all.map { |user| User.new(user) }
end
@[GraphQL::Field]
# All admins
def admins(context : Context) : Array(User)
context.admin!
Db::User.where(admin: true).map { |user| User.new(user) }
end
@[GraphQL::Field]
# Teacher by ID
def teacher(id : Int32) : Teacher
Teacher.new(Db::Teacher.find!(id))
end
@[GraphQL::Field]
# All teachers
def teachers : Array(Teacher)
Db::Teacher.all.map { |teacher| Teacher.new(teacher) }
end
@[GraphQL::Field]
# Student by ID
def student(context : Context, id : Int32) : Student
context.admin!
Student.new(Db::Student.find!(id))
end
@[GraphQL::Field]
# All students
def students(context : Context) : Array(Student)
context.admin!
Db::Student.all.map { |student| Student.new(student) }
end
@[GraphQL::Field]
# Vote by ID
def vote(context : Context, id : Int32) : Vote
context.admin!
Vote.new(Db::Vote.find!(id))
end
@[GraphQL::Field]
# All votes
def votes(context : Context) : Array(Vote)
context.admin!
Db::Vote.all.map { |vote| Vote.new(vote) }
end
end
end
end
end