Created documentation for DB models
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing

This commit is contained in:
Dominic Grimm 2022-02-07 21:10:04 +01:00
parent 4ae471734c
commit eed1042529
7 changed files with 29 additions and 1 deletions

View file

@ -9,4 +9,4 @@ prod:
shards build --static --release --no-debug --verbose -s -p -t
docs:
crystal docs --project-name "Mentorenwahl Backend"
crystal docs --project-name "Mentorenwahl Backend" -D granite_docs

View file

@ -4,6 +4,7 @@ require "granite/adapter/pg"
require "./db/*"
module Backend
# Database model definitions
module Db
Granite::Connections << Granite::Adapter::Pg.new(name: "pg", url: Backend.config.db.url)
end

View file

@ -1,12 +1,16 @@
module Backend
module Db
# Student model
class Student < Granite::Base
table students
belongs_to :user
has_one :vote
# Student's ID
column id : Int64, primary: true
# Student is at SKIF
column skif : Bool
end
end

View file

@ -1,13 +1,19 @@
module Backend
module Db
# Teacher model
class Teacher < Granite::Base
table teachers
belongs_to :user
has_many teacher_votes : TeacherVote
# Teacher's ID
column id : Int64, primary: true
# Teacher's max students count
column max_students : Int32
# Teacher is at SKIF
column skif : Bool
end
end

View file

@ -1,12 +1,16 @@
module Backend
module Db
# Teacher vote model
class TeacherVote < Granite::Base
table teacher_votes
belongs_to :vote
belongs_to :teacher
# Teacher votes's ID
column id : Int64, primary: true
# Teacher vote's priority
column priority : Int32
validate :teacher, "must be vote unique" do |teacher_vote|

View file

@ -1,28 +1,40 @@
module Backend
module Db
# User model
class User < Granite::Base
table users
has_one :teacher
has_one :student
# User's ID
column id : Int64, primary: true
# User's LDAP username
column username : String
# User's role
column role : String
# User is admin
column admin : Bool = false
# User's first name
def firstname : String
Ldap.user(Ldap.uid(@username.not_nil!)).first["givenName"].first
end
# User's last name
def lastname : String
Ldap.user(Ldap.uid(@username.not_nil!)).first["sn"].first
end
# User's full name
def name : String
Ldap.user(Ldap.uid(@username.not_nil!)).first["cn"].first
end
# User's email
def email : String
Ldap.user(Ldap.uid(@username.not_nil!)).first["mail"].first
end

View file

@ -1,5 +1,6 @@
module Backend
module Db
# Possible roles a user can have
enum UserRole
Teacher
Student