@@ -5,13 +5,15 @@ import org.gitanimals.identity.core.AggregateRoot
55import org.gitanimals.identity.core.IdGenerator
66import org.gitanimals.identity.core.instant
77import org.gitanimals.identity.core.toZonedDateTime
8+ import org.slf4j.LoggerFactory
89import kotlin.math.max
910import kotlin.math.min
1011
1112@AggregateRoot
1213@Table(
1314 name = " users" , indexes = [
14- Index (columnList = " username" , unique = true )
15+ Index (columnList = " username" , unique = true ),
16+ Index (columnList = " entry_point, authentication_id" , unique = true ),
1517 ]
1618)
1719@Entity(name = " users" )
@@ -21,7 +23,7 @@ class User(
2123 val id : Long ,
2224
2325 @Column(name = " username" , nullable = false )
24- val name : String ,
26+ private var name : String ,
2527
2628 @Column(name = " points" , nullable = false )
2729 private var points : Long ,
@@ -36,17 +38,34 @@ class User(
3638
3739 @Column(name = " profile_image" , nullable = false )
3840 val profileImage : String ,
39-
40- @Enumerated(EnumType .STRING )
41- @Column(name = " entry_point" , columnDefinition = " VARCHAR(50) DEFAULT 'GITHUB'" )
42- val entryPoint : EntryPoint ,
41+
42+ @Embedded
43+ private val authInfo : UserAuthInfo ,
4344
4445 @Version
4546 private val version : Long? = null ,
4647) : AbstractTime() {
4748
49+ fun getName (): String = name
50+
4851 fun getPoints (): Long = points
4952
53+ fun getEntryPoint (): EntryPoint = authInfo.entryPoint
54+
55+ fun findAuthenticationId (): String? = authInfo.authenticationId
56+
57+ fun setAuthenticationId (authenticationId : String ) {
58+ if (authInfo.authenticationId != null ) {
59+ check(authInfo.authenticationId == authenticationId) {
60+ val message =
61+ " Different authenticationId input. saved authenticationId: \" ${authInfo.authenticationId} \" , input authenticationId: \" $authenticationId \" "
62+ logger.error(message)
63+ message
64+ }
65+ }
66+ authInfo.authenticationId = authenticationId
67+ }
68+
5069 fun givePoint (point : Long , reason : String ) {
5170 val current = instant().toZonedDateTime()
5271 pointHistories.removeAll { it.createdAt.toZonedDateTime().isBefore(current) }
@@ -74,23 +93,30 @@ class User(
7493 this .points + = point
7594 }
7695
96+ fun updateUsername (username : String ) {
97+ this .name = username
98+ }
99+
77100 companion object {
78- private val JOIN_POINT_THRESHOLD = 100_000L
79- private val PER_DAY_GIVE_POINT_THRESHOLD = 20000L
101+ private val logger = LoggerFactory .getLogger(User ::class .simpleName)
102+
103+ private const val JOIN_POINT_THRESHOLD = 100_000L
104+ private const val PER_DAY_GIVE_POINT_THRESHOLD = 20000L
80105
81106 fun newUser (
82107 name : String ,
83108 points : Long ,
84109 profileImage : String ,
85110 entryPoint : EntryPoint ,
111+ authenticationId : String ,
86112 ): User {
87113 return User (
88114 id = IdGenerator .generate(),
89115 name = name,
90116 points = min(points, JOIN_POINT_THRESHOLD ),
91117 pointHistories = mutableListOf (),
92118 profileImage = profileImage,
93- entryPoint = entryPoint,
119+ authInfo = UserAuthInfo ( entryPoint, authenticationId) ,
94120 )
95121 }
96122 }
0 commit comments