mentorenwahl/docker/backend/src/backend/db/teacher_vote.cr
Dominic Grimm 5bc10f8aaf
Some checks failed
continuous-integration/drone/push Build is failing
Added worker
2022-01-23 09:12:57 +01:00

30 lines
866 B
Crystal

module Backend
module Db
class TeacherVote < Granite::Base
table teacher_votes
belongs_to :vote
belongs_to :teacher
column id : Int64, primary: true
column priority : Int32
validate :teacher, "must be vote unique" do |teacher_vote|
self.where(vote_id: teacher_vote.vote.id, teacher_id: teacher_vote.teacher.not_nil!.id).count.run.as(Int64).zero?
end
validate :priority, "must be positive" do |teacher_vote|
teacher_vote.priority >= 0
end
validate :priority, "must be less than the number of teachers" do |teacher_vote|
teacher_vote.priority < Teacher.count
end
validate :priority, "must be vote unique" do |teacher_vote|
self.where(vote_id: teacher_vote.vote.id, priority: teacher_vote.priority).count.run.as(Int64).zero?
end
end
end
end