11package com .softwaremill .bootzooka .user
22
3- import com . augustnagro . magnum .{ DbCodec , DbTx , Frag , PostgresDbType , Repo , Spec , SqlName , SqlNameMapper , Table , TableInfo , sql }
3+ import ma . chinespirit . parlance .{ DbTx , EntityMeta , Frag , Postgres , QueryBuilder , Repo , SqlName , SqlNameMapper , Table , TableInfo , sql , unsafeAsWhere }
44import com .password4j .{Argon2Function , Password }
55import com .softwaremill .bootzooka .infrastructure .Codecs .given
66import com .softwaremill .bootzooka .user .User .PasswordHashing
@@ -12,37 +12,35 @@ import ox.discard
1212import java .time .Instant
1313
1414class UserModel :
15- private val userRepo = Repo [User , User , Id [User ]]
15+ private val userRepo = Repo [User , User , Id [User ]]()
1616 private val u = TableInfo [User , User , Id [User ]]
1717
18- export userRepo .{insert , findById }
18+ export userRepo .{rawInsert as insert , findById }
1919
20- def findByEmail (email : LowerCased )(using DbTx ): Option [User ] = findBy(
21- Spec [User ].where(sql " ${u.emailLowerCase} = $email" )
22- )
23- def findByLogin (login : LowerCased )(using DbTx ): Option [User ] = findBy(
24- Spec [User ].where(sql " ${u.loginLowerCase} = $login" )
25- )
26- def findByLoginOrEmail (loginOrEmail : LowerCased )(using DbTx ): Option [User ] =
27- findBy(Spec [User ].where(sql " ${u.loginLowerCase} = ${loginOrEmail : String } OR ${u.emailLowerCase} = $loginOrEmail" ))
20+ def findByEmail (email : LowerCased )(using DbTx [Postgres ]): Option [User ] =
21+ findBy(sql " ${u.emailLowerCase} = $email" )
22+ def findByLogin (login : LowerCased )(using DbTx [Postgres ]): Option [User ] =
23+ findBy(sql " ${u.loginLowerCase} = $login" )
24+ def findByLoginOrEmail (loginOrEmail : LowerCased )(using DbTx [Postgres ]): Option [User ] =
25+ findBy(sql " ${u.loginLowerCase} = ${loginOrEmail : String } OR ${u.emailLowerCase} = $loginOrEmail" )
2826
29- private def findBy (by : Spec [ User ] )(using DbTx ): Option [User ] =
30- userRepo.findAll(by).headOption
27+ private def findBy (condition : Frag )(using DbTx [ Postgres ] ): Option [User ] =
28+ QueryBuilder .from[ User ].where(condition.unsafeAsWhere).first()
3129
32- def updatePassword (userId : Id [User ], newPassword : Hashed )(using DbTx ): Unit =
30+ def updatePassword (userId : Id [User ], newPassword : Hashed )(using DbTx [ Postgres ] ): Unit =
3331 sql """ UPDATE $u SET ${u.passwordHash} = $newPassword WHERE ${u.id} = $userId""" .update.run().discard
3432
35- def updateLogin (userId : Id [User ], newLogin : String , newLoginLowerCase : LowerCased )(using DbTx ): Unit =
33+ def updateLogin (userId : Id [User ], newLogin : String , newLoginLowerCase : LowerCased )(using DbTx [ Postgres ] ): Unit =
3634 sql """ UPDATE $u SET ${u.login} = $newLogin, login_lowercase = ${newLoginLowerCase : String } WHERE ${u.id} = $userId""" .update
3735 .run()
3836 .discard
3937
40- def updateEmail (userId : Id [User ], newEmail : LowerCased )(using DbTx ): Unit =
38+ def updateEmail (userId : Id [User ], newEmail : LowerCased )(using DbTx [ Postgres ] ): Unit =
4139 sql """ UPDATE $u SET ${u.emailLowerCase} = $newEmail WHERE ${u.id} = $userId""" .update.run().discard
4240
4341end UserModel
4442
45- @ Table (PostgresDbType , SqlNameMapper .CamelToSnakeCase )
43+ @ Table (SqlNameMapper .CamelToSnakeCase )
4644@ SqlName (" users" )
4745case class User (
4846 id : Id [User ],
@@ -51,7 +49,7 @@ case class User(
5149 @ SqlName (" email_lowercase" ) emailLowerCase : LowerCased ,
5250 @ SqlName (" password" ) passwordHash : Hashed ,
5351 createdOn : Instant
54- ):
52+ ) derives EntityMeta :
5553 def verifyPassword (password : String ): PasswordVerificationStatus =
5654 if Password .check(password, passwordHash).`with`(PasswordHashing .Argon2 ) then PasswordVerificationStatus .Verified
5755 else PasswordVerificationStatus .VerificationFailed
0 commit comments