mentorenwahl/backend/src/backend/db/user.cr

46 lines
1 KiB
Crystal
Raw Normal View History

2022-11-21 18:48:53 +00:00
module Backend::Db
Clear.enum UserRole, :teacher, :student
2022-11-21 18:48:53 +00:00
struct UserRole
# API representation of the enum
def to_api : Api::Schema::UserRole
case self
when Student
Api::Schema::UserRole::Student
when Teacher
Api::Schema::UserRole::Teacher
else
raise "Invalid enum value for UserRole"
end
2022-11-21 18:48:53 +00:00
end
2022-11-21 18:48:53 +00:00
# DB representation of the enum
def self.from_api(role : Api::Schema::UserRole) : self
role.to_db
2022-03-20 14:13:15 +00:00
end
2022-11-21 18:48:53 +00:00
end
2022-03-20 14:13:15 +00:00
2022-11-21 18:48:53 +00:00
class User
include Clear::Model
self.table = :users
2022-01-08 12:29:22 +00:00
2022-11-21 18:48:53 +00:00
primary_key type: :serial
2022-02-07 20:10:04 +00:00
2022-11-21 18:48:53 +00:00
column username : String
2023-01-29 10:49:13 +00:00
column password_hash : Crypto::Bcrypt::Password
column first_name : String
column last_name : String
2022-11-21 18:48:53 +00:00
column role : UserRole
column admin : Bool = false
2022-11-21 18:48:53 +00:00
has_one student : Student?, foreign_key: :user_id
has_one teacher : Teacher?, foreign_key: :user_id
2022-11-13 17:41:53 +00:00
2022-11-21 18:48:53 +00:00
has_many tokens : Token, foreign_key: :user_id
2023-01-29 10:49:13 +00:00
def password=(x : String)
self.password_hash = Crypto::Bcrypt::Password.create(x)
end
2022-01-08 12:29:22 +00:00
end
end