155 lines
4.2 KiB
Crystal
155 lines
4.2 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]
|
|
# User by ID
|
|
def user(context : Context, id : Int32) : User?
|
|
context.admin!
|
|
|
|
User.from_id(id)
|
|
end
|
|
|
|
@[GraphQL::Field]
|
|
# All users
|
|
def users(context : Context) : Array(User)?
|
|
context.admin!
|
|
|
|
Db::User.query.map { |user| User.new(user) }
|
|
end
|
|
|
|
@[GraphQL::Field]
|
|
def user_by_username(context : Context, username : String) : User?
|
|
context.admin!
|
|
|
|
User.new(Db::User.query.find { var(:username) == username }.not_nil!)
|
|
end
|
|
|
|
@[GraphQL::Field]
|
|
# All admins
|
|
def admins(context : Context) : Array(User)?
|
|
context.admin!
|
|
|
|
Db::User.query.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.query.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.query.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.query.map { |vote| Vote.new(vote) }
|
|
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
|
|
end
|
|
|
|
@[GraphQL::Field]
|
|
# Teacher vote by ID
|
|
def teacher_vote(context : Context, id : Int32) : TeacherVote?
|
|
context.admin!
|
|
|
|
TeacherVote.new(Db::TeacherVote.find!(id))
|
|
end
|
|
|
|
@[GraphQL::Field]
|
|
# All teacher votes
|
|
def teacher_votes(context : Context) : Array(TeacherVote)?
|
|
context.admin!
|
|
|
|
Db::TeacherVote.query.map { |vote| TeacherVote.new(vote) }
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|