module Backend::Db Clear.enum UserRole, :teacher, :student 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 end # DB representation of the enum def self.from_api(role : Api::Schema::UserRole) : self role.to_db end end class User include Clear::Model self.table = :users primary_key type: :serial column username : String column password_hash : Crypto::Bcrypt::Password column first_name : String column last_name : String column role : UserRole column admin : Bool = false has_one student : Student?, foreign_key: :user_id has_one teacher : Teacher?, foreign_key: :user_id has_many tokens : Token, foreign_key: :user_id def password=(x : String) self.password_hash = Crypto::Bcrypt::Password.create(x) end end end