173 lines
5.5 KiB
Crystal
173 lines
5.5 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]
|
|
# Public configuration of the API for frontend pre-validation
|
|
def config : Config
|
|
Config.new
|
|
end
|
|
|
|
@[GraphQL::Field]
|
|
# Current authenticated user
|
|
def me(context : Context) : User?
|
|
context.authenticated!
|
|
|
|
User.new(context.user.not_nil!)
|
|
end
|
|
|
|
@[GraphQL::Field]
|
|
# Active tokens of the current user
|
|
def tokens(context : Context) : Array(Token)?
|
|
context.authenticated!
|
|
|
|
now = Time.utc
|
|
|
|
Db::Token.query.where { (user_id == context.user.not_nil!.id) & (exp > now) & active }.map { |t| Token.new(t) }
|
|
end
|
|
|
|
@[GraphQL::Field]
|
|
# User by ID
|
|
def user(context : Context, id : Int32) : User?
|
|
context.admin!
|
|
|
|
(user = context.loaders.user.load(id)) && User.new(user)
|
|
end
|
|
|
|
@[GraphQL::Field]
|
|
# All users
|
|
def users(context : Context) : Array(User)?
|
|
context.admin!
|
|
|
|
context.loaders.user.load(Db::User.query.select(:id).map(&.id)).map { |u| User.new(u.not_nil!) }
|
|
end
|
|
|
|
@[GraphQL::Field]
|
|
def user_by_username(context : Context, username : String) : User?
|
|
context.admin!
|
|
|
|
(user = context.loaders.user.load(Db::User.query.select(:id).find { var(:username) == username }.not_nil!.id)) && User.new(user)
|
|
end
|
|
|
|
@[GraphQL::Field]
|
|
# All admins
|
|
def admins(context : Context) : Array(User)?
|
|
context.admin!
|
|
|
|
# Db::User.query.where(admin: true).map { |user| User.new(user) }
|
|
context.loaders.user.load(Db::User.query.select(:id).where(admin: true).map(&.id)).map { |u| User.new(u.not_nil!) }
|
|
end
|
|
|
|
@[GraphQL::Field]
|
|
# Teacher by ID
|
|
def teacher(context : Context, id : Int32) : Teacher?
|
|
# Teacher.new(Db::Teacher.find!(id))
|
|
(teacher = context.loaders.teacher.load(id)) && Teacher.new(teacher)
|
|
end
|
|
|
|
@[GraphQL::Field]
|
|
# All teachers
|
|
def teachers(context : Context) : Array(Teacher)
|
|
# Db::Teacher.query.map { |teacher| Teacher.new(teacher) }
|
|
context.loaders.teacher.load(Db::Teacher.query.select(:id).map(&.id)).map { |t| Teacher.new(t.not_nil!) }
|
|
end
|
|
|
|
@[GraphQL::Field]
|
|
# Student by ID
|
|
def student(context : Context, id : Int32) : Student?
|
|
context.admin!
|
|
|
|
(student = context.loaders.student.load(id)) && Student.new(student)
|
|
end
|
|
|
|
@[GraphQL::Field]
|
|
# All students
|
|
def students(context : Context) : Array(Student)?
|
|
context.admin!
|
|
|
|
context.loaders.student.load(Db::Student.query.select(:id).map(&.id)).map { |s| Student.new(s.not_nil!) }
|
|
end
|
|
|
|
@[GraphQL::Field]
|
|
# Vote by ID
|
|
def vote(context : Context, id : Int32) : Vote?
|
|
context.admin!
|
|
|
|
# Vote.new(Db::Vote.find!(id))
|
|
(vote = context.loaders.vote.load(id)) && Vote.new(vote)
|
|
end
|
|
|
|
@[GraphQL::Field]
|
|
# All votes
|
|
def votes(context : Context) : Array(Vote)?
|
|
context.admin!
|
|
|
|
# Db::Vote.query.map { |vote| Vote.new(vote) }
|
|
context.loaders.vote.load(Db::Vote.query.select(:id).map(&.id)).map { |v| Vote.new(v.not_nil!) }
|
|
end
|
|
|
|
@[GraphQL::Field]
|
|
# All students voted
|
|
def all_students_voted(context : Context) : Bool?
|
|
context.admin!
|
|
|
|
votes = Db::Vote.query.count
|
|
students = Db::Student.query.count
|
|
|
|
students > 0 && votes >= students
|
|
end
|
|
|
|
@[GraphQL::Field]
|
|
# Students can vote
|
|
def students_can_vote : Bool
|
|
# teacher_role_count = Db::User.query.where(role: Db::UserRole::Teacher).count
|
|
|
|
# teacher_role_count > 0 && teacher_role_count == Db::Teacher.query.count
|
|
Db::Config.query.select(:can_vote).where { active == true }.first!.can_vote
|
|
end
|
|
|
|
@[GraphQL::Field]
|
|
# Teacher vote by ID
|
|
def teacher_vote(context : Context, id : Int32) : TeacherVote?
|
|
context.admin!
|
|
|
|
# TeacherVote.new(Db::TeacherVote.find!(id))
|
|
(tv = context.loaders.teacher_vote.load(id)) && TeacherVote.new(tv)
|
|
end
|
|
|
|
@[GraphQL::Field]
|
|
# All teacher votes
|
|
def teacher_votes(context : Context) : Array(TeacherVote)?
|
|
context.admin!
|
|
|
|
# Db::TeacherVote.query.map { |vote| TeacherVote.new(vote) }
|
|
context.loaders.teacher_vote.load(Db::TeacherVote.query.select(:id).map(&.id)).map { |tv| TeacherVote.new(tv.not_nil!) }
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|