30 lines
866 B
Crystal
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
|