mentorenwahl/docker/backend/src/backend/db/teacher_vote.cr

50 lines
1.7 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
# 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/>.
2022-01-23 08:12:57 +00:00
module Backend
2022-01-08 12:29:22 +00:00
module Db
2022-02-07 20:10:04 +00:00
# Teacher vote model
2022-01-08 12:29:22 +00:00
class TeacherVote < Granite::Base
table teacher_votes
belongs_to :vote
belongs_to :teacher
2022-02-07 20:10:04 +00:00
# Teacher votes's ID
2022-01-08 12:29:22 +00:00
column id : Int64, primary: true
2022-02-07 20:10:04 +00:00
# Teacher vote's priority
2022-01-08 12:29:22 +00:00
column priority : Int32
2022-01-15 18:06:13 +00:00
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?
2022-01-08 12:29:22 +00:00
end
2022-01-15 18:06:13 +00:00
validate :priority, "must be positive" do |teacher_vote|
teacher_vote.priority >= 0
2022-01-08 12:29:22 +00:00
end
validate :priority, "must be less than the number of teachers" do |teacher_vote|
teacher_vote.priority < Teacher.count
end
2022-01-15 18:06:13 +00:00
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
2022-01-08 12:29:22 +00:00
end
end
end