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

155 lines
4.2 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
@[GraphQL::Object]
class Query < GraphQL::BaseQuery
@[GraphQL::Field]
2022-02-09 13:35:35 +00:00
# Retuns true
2022-01-23 08:12:57 +00:00
def ok : Bool
true
end
2022-03-13 10:54:42 +00:00
@[GraphQL::Field]
# Public configuration of the API for frontend pre-validation
def config : Config
Config.new
end
2022-01-23 08:12:57 +00:00
@[GraphQL::Field]
2022-02-09 13:35:35 +00:00
# Current authenticated user
2022-11-12 21:50:06 +00:00
def me(context : Context) : User?
2022-01-23 08:12:57 +00:00
context.authenticated!
User.new(context.user.not_nil!)
end
@[GraphQL::Field]
2022-02-09 13:35:35 +00:00
# User by ID
2022-11-12 21:50:06 +00:00
def user(context : Context, id : Int32) : User?
2022-01-23 08:12:57 +00:00
context.admin!
User.from_id(id)
2022-01-23 08:12:57 +00:00
end
@[GraphQL::Field]
2022-02-09 13:35:35 +00:00
# All users
2022-11-12 21:50:06 +00:00
def users(context : Context) : Array(User)?
2022-01-23 08:12:57 +00:00
context.admin!
Db::User.query.map { |user| User.new(user) }
2022-01-23 08:12:57 +00:00
end
2022-07-28 12:05:10 +00:00
@[GraphQL::Field]
2022-11-12 21:50:06 +00:00
def user_by_username(context : Context, username : String) : User?
2022-07-28 12:05:10 +00:00
context.admin!
User.new(Db::User.query.find { var(:username) == username }.not_nil!)
end
2022-01-23 08:12:57 +00:00
@[GraphQL::Field]
2022-02-09 13:35:35 +00:00
# All admins
2022-11-12 21:50:06 +00:00
def admins(context : Context) : Array(User)?
2022-01-23 08:12:57 +00:00
context.admin!
Db::User.query.where(admin: true).map { |user| User.new(user) }
2022-01-23 08:12:57 +00:00
end
@[GraphQL::Field]
2022-02-09 13:35:35 +00:00
# Teacher by ID
2022-01-23 08:12:57 +00:00
def teacher(id : Int32) : Teacher
Teacher.new(Db::Teacher.find!(id))
end
@[GraphQL::Field]
2022-02-09 13:35:35 +00:00
# All teachers
2022-01-23 08:12:57 +00:00
def teachers : Array(Teacher)
Db::Teacher.query.map { |teacher| Teacher.new(teacher) }
2022-01-23 08:12:57 +00:00
end
@[GraphQL::Field]
2022-02-09 13:35:35 +00:00
# Student by ID
2022-11-12 21:50:06 +00:00
def student(context : Context, id : Int32) : Student?
2022-01-23 08:12:57 +00:00
context.admin!
Student.new(Db::Student.find!(id))
end
@[GraphQL::Field]
2022-02-09 13:35:35 +00:00
# All students
2022-11-12 21:50:06 +00:00
def students(context : Context) : Array(Student)?
2022-01-23 08:12:57 +00:00
context.admin!
Db::Student.query.map { |student| Student.new(student) }
2022-01-23 08:12:57 +00:00
end
@[GraphQL::Field]
2022-02-09 13:35:35 +00:00
# Vote by ID
2022-11-12 21:50:06 +00:00
def vote(context : Context, id : Int32) : Vote?
2022-01-23 08:12:57 +00:00
context.admin!
Vote.new(Db::Vote.find!(id))
end
@[GraphQL::Field]
2022-02-09 13:35:35 +00:00
# All votes
2022-11-12 21:50:06 +00:00
def votes(context : Context) : Array(Vote)?
2022-01-23 08:12:57 +00:00
context.admin!
Db::Vote.query.map { |vote| Vote.new(vote) }
2022-01-23 08:12:57 +00:00
end
2022-02-13 08:40:06 +00:00
2022-04-14 14:44:49 +00:00
@[GraphQL::Field]
# All students voted
2022-11-12 21:50:06 +00:00
def all_students_voted(context : Context) : Bool?
2022-04-14 14:44:49 +00:00
context.admin!
2022-07-28 12:05:10 +00:00
votes = Db::Vote.query.count
students = Db::Student.query.count
students > 0 && votes >= students
2022-04-14 14:44:49 +00:00
end
2022-11-04 20:23:36 +00:00
@[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
2022-02-13 08:40:06 +00:00
@[GraphQL::Field]
# Teacher vote by ID
2022-11-12 21:50:06 +00:00
def teacher_vote(context : Context, id : Int32) : TeacherVote?
2022-02-13 08:40:06 +00:00
context.admin!
TeacherVote.new(Db::TeacherVote.find!(id))
end
@[GraphQL::Field]
# All teacher votes
2022-11-12 21:50:06 +00:00
def teacher_votes(context : Context) : Array(TeacherVote)?
2022-02-13 08:40:06 +00:00
context.admin!
Db::TeacherVote.query.map { |vote| TeacherVote.new(vote) }
2022-02-13 08:40:06 +00:00
end
2022-01-23 08:12:57 +00:00
end
end
end
end