mentorenwahl/docker/backend/src/mw/schema/query.cr
2022-01-08 13:29:22 +01:00

88 lines
1.9 KiB
Crystal

require "graphql"
module MW
module Schema
@[GraphQL::Object]
class Query < GraphQL::BaseQuery
@[GraphQL::Field]
def ok : Bool
true
end
@[GraphQL::Field]
def me(context : Context) : User
context.authenticated!
User.new(context.user.not_nil!)
end
@[GraphQL::Field]
def user(context : Context, id : Int32) : User
context.admin!
User.new(id)
end
@[GraphQL::Field({UserRole::Admin})]
def users(context : Context) : Array(User)
puts "AUTHORIZED ALLOWED ROLES: #{{{ @def.annotation(GraphQL::Field)[0] }}}"
context.admin!
Db::User.all.map { |user| User.new(user) }
end
@[GraphQL::Field]
def admin(context : Context, id : Int32) : Admin
context.admin!
Admin.new(Db::Admin.find!(id))
end
@[GraphQL::Field]
def admins(context : Context) : Array(Admin)
context.admin!
Db::Admin.all.map { |admin| Admin.new(admin) }
end
@[GraphQL::Field]
def teacher(id : Int32) : Teacher
Teacher.new(Db::Teacher.find!(id))
end
@[GraphQL::Field]
def teachers : Array(Teacher)
Db::Teacher.all.map { |teacher| Teacher.new(teacher) }
end
@[GraphQL::Field]
def student(context : Context, id : Int32) : Student
context.admin!
Student.new(Db::Student.find!(id))
end
@[GraphQL::Field]
def students(context : Context) : Array(Student)
context.admin!
Db::Student.all.map { |student| Student.new(student) }
end
@[GraphQL::Field]
def vote(context : Context, id : Int32) : Vote
context.admin!
Vote.new(Db::Vote.find!(id))
end
@[GraphQL::Field]
def votes(context : Context) : Array(Vote)
context.admin!
Db::Vote.all.map { |vote| Vote.new(vote) }
end
end
end
end