Skip to content

Commit

Permalink
refactor: (#101) wrap primitive type of User domain
Browse files Browse the repository at this point in the history
  • Loading branch information
leekyukin committed Dec 21, 2022
1 parent 286de20 commit 9627397
Show file tree
Hide file tree
Showing 27 changed files with 110 additions and 91 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.sexysisters.tojserverv2.common.security.auth

import com.sexysisters.tojserverv2.domain.user.User
import com.sexysisters.tojserverv2.domain.user.domain.User
import org.springframework.security.core.authority.SimpleGrantedAuthority
import org.springframework.security.core.userdetails.UserDetails

Expand All @@ -10,9 +10,9 @@ class AuthDetails(

override fun getAuthorities() = mutableListOf(SimpleGrantedAuthority(user.authority.name))

override fun getPassword() = this.user.password
override fun getPassword() = this.user.passwordValue()

override fun getUsername() = this.user.email
override fun getUsername() = this.user.emailValue()

override fun isAccountNonExpired() = true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import org.springframework.security.crypto.password.PasswordEncoder
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.time.Duration
import java.util.Date
import java.util.*

@Service
class AuthServiceImpl(
Expand All @@ -33,7 +33,7 @@ class AuthServiceImpl(
override fun login(command: AuthCommand.LoginRequest): AuthInfo.Token {
val email = command.email
val user = userReader.getUser(email)
checkPassword(command.password, user.password)
checkPassword(command.password, user.passwordValue())

val accessToken = jwtTokenProvider.createAccessToken(email)
val refreshToken = jwtTokenProvider.createRefreshToken(email)
Expand Down Expand Up @@ -63,7 +63,7 @@ class AuthServiceImpl(
val parsedAccessToken = jwtTokenProvider.parseToken(accessToken)!!
val remainTime = jwtTokenProvider.getExpiredTime(parsedAccessToken).time - Date().time
redisRepository.setBlackList(parsedAccessToken, Duration.ofMillis(remainTime))
redisRepository.deleteData(user.email)
redisRepository.deleteData(user.emailValue())
}

@Transactional(readOnly = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class AlreadyExistsStudentPolicy(
override fun check(student: Student, school: School) {
val hasStudent = studentRepository.checkAlreadyExists(
school = school,
grade = student.getGradeValue(),
classroom = student.getClassroomValue(),
number = student.getNumberValue(),
grade = student.gradeValue(),
classroom = student.classroomValue(),
number = student.numberValue(),
)
if (hasStudent) {
throw SchoolException.StudentAlreadyExists()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class InfoRangePolicy : SchoolPolicy {

override fun check(student: Student, school: School) {
val schoolDivision = school.division
val grade = student.getGradeValue()
val classroom = student.getClassroomValue()
val number = student.getNumberValue()
val grade = student.gradeValue()
val classroom = student.classroomValue()
val number = student.numberValue()

val isValidGrade = when (schoolDivision) {
Division.ELEMENTARY -> grade in 1..6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ package com.sexysisters.tojserverv2.domain.student.domain
import com.sexysisters.tojserverv2.domain.BaseTimeEntity
import com.sexysisters.tojserverv2.domain.approve.Approve
import com.sexysisters.tojserverv2.domain.school.domain.School
import com.sexysisters.tojserverv2.domain.user.User
import com.sexysisters.tojserverv2.domain.user.domain.User
import javax.persistence.*

@Entity
@Table(name = "tbl_student")
class Student(
@Embedded private val grade: Grade,
@Embedded private val classroom: Classroom,
@Embedded private val number: Number,
@Embedded private val age: Age,
@Embedded val grade: Grade,
@Embedded val classroom: Classroom,
@Embedded val number: Number,
@Embedded val age: Age,
) : BaseTimeEntity() {

@Enumerated(EnumType.STRING)
Expand All @@ -36,10 +36,10 @@ class Student(
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long = 0L

fun getGradeValue() = grade.value
fun getClassroomValue() = classroom.value
fun getNumberValue() = number.value
fun getAgeValue() = age.value
fun gradeValue() = grade.value
fun classroomValue() = classroom.value
fun numberValue() = number.value
fun ageValue() = age.value
}

fun Student.makeRelation(user: User) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.sexysisters.tojserverv2.domain.student.policy

import com.sexysisters.tojserverv2.domain.student.exception.StudentException
import com.sexysisters.tojserverv2.domain.user.User
import com.sexysisters.tojserverv2.domain.user.domain.User
import com.sexysisters.tojserverv2.domain.user.domain.hasStudent
import org.springframework.core.annotation.Order
import org.springframework.stereotype.Component

Expand All @@ -13,8 +14,7 @@ import org.springframework.stereotype.Component
class AlreadyCreatedPolicy : StudentPolicy {

override fun check(user: User) {
val hasStudent = user.student != null
if (hasStudent) {
if (user.hasStudent()) {
throw StudentException.AlreadyCreated()
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.sexysisters.tojserverv2.domain.student.policy

import com.sexysisters.tojserverv2.domain.user.User
import com.sexysisters.tojserverv2.domain.user.domain.User

interface StudentPolicy {
fun check(user: User)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.sexysisters.tojserverv2.domain.student.exception.StudentException
import com.sexysisters.tojserverv2.domain.teacher.*
import com.sexysisters.tojserverv2.domain.teacher.domain.*
import com.sexysisters.tojserverv2.domain.user.UserReader
import com.sexysisters.tojserverv2.domain.user.hasStudent
import com.sexysisters.tojserverv2.domain.user.domain.hasStudent
import com.sexysisters.tojserverv2.interfaces.teacher.dto.TeacherDtoMapper
import com.sexysisters.tojserverv2.interfaces.teacher.dto.TeacherResponse
import org.springframework.stereotype.Service
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.sexysisters.tojserverv2.domain.user

import com.sexysisters.tojserverv2.config.properties.S3Properties
import com.sexysisters.tojserverv2.domain.user.type.Nickname
import com.sexysisters.tojserverv2.domain.user.domain.*
import com.sexysisters.tojserverv2.domain.user.domain.Nickname
import org.springframework.security.crypto.password.PasswordEncoder

class UserCommand {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
package com.sexysisters.tojserverv2.domain.user

import org.mapstruct.InjectionStrategy
import org.mapstruct.Mapper
import org.mapstruct.ReportingPolicy
import com.sexysisters.tojserverv2.domain.user.domain.User
import org.mapstruct.*

@Mapper(
componentModel = "spring",
injectionStrategy = InjectionStrategy.CONSTRUCTOR,
unmappedTargetPolicy = ReportingPolicy.ERROR,
)
interface UserMapper {

@Mappings(
value = [
Mapping(source = "email.value", target = "email"),
Mapping(source = "nickname.value", target = "nickname"),
Mapping(source = "image.value", target = "profileImg"),
Mapping(source = "name.value", target = "name"),
]
)
fun of(user: User): UserInfo.Profile
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.sexysisters.tojserverv2.domain.user

import com.sexysisters.tojserverv2.domain.user.domain.User

interface UserReader {
fun getUser(email: String): User
fun getUser(id: Long): User
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.sexysisters.tojserverv2.domain.user

import com.sexysisters.tojserverv2.domain.user.domain.User

interface UserStore {
fun store(user: User): User
fun storeOAuthUser(user: User)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.sexysisters.tojserverv2.domain.user.domain

enum class Authority {
ADMIN, USER
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.sexysisters.tojserverv2.domain.user
package com.sexysisters.tojserverv2.domain.user.domain

import com.sexysisters.tojserverv2.domain.user.exception.UserException
import javax.persistence.Column
Expand All @@ -9,7 +9,7 @@ import javax.validation.constraints.NotNull
class Email(
@field:NotNull
@Column(name = "email")
private val value: String,
val value: String,
) {
init {
val EMAIL_FORMAT = "@"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.sexysisters.tojserverv2.domain.user
package com.sexysisters.tojserverv2.domain.user.domain

import com.sexysisters.tojserverv2.domain.user.exception.UserException
import javax.persistence.Column
Expand All @@ -9,7 +9,7 @@ import javax.validation.constraints.NotNull
class Image(
@field:NotNull
@Column(name = "image")
private val value: String,
val value: String,
) {
init {
if (value.isBlank()) throw UserException.UserNotValid()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.sexysisters.tojserverv2.domain.user
package com.sexysisters.tojserverv2.domain.user.domain

import com.sexysisters.tojserverv2.domain.user.exception.UserException
import javax.persistence.Column
Expand All @@ -9,7 +9,7 @@ import javax.validation.constraints.NotNull
class Name(
@field:NotNull
@Column(name = "name")
private val value: String,
val value: String,
) {
init {
val MAX_LENGTH = 20
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.sexysisters.tojserverv2.domain.user.type
package com.sexysisters.tojserverv2.domain.user.domain

import com.sexysisters.tojserverv2.domain.user.exception.UserException
import javax.persistence.Column
Expand All @@ -9,7 +9,7 @@ import javax.validation.constraints.NotNull
class Nickname(
@field:NotNull
@Column(name = "nickname", unique = true)
private val value: String,
val value: String,
) {
init {
val MAX_LENGTH = 21
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.sexysisters.tojserverv2.domain.user
package com.sexysisters.tojserverv2.domain.user.domain

import com.sexysisters.tojserverv2.domain.user.exception.UserException
import javax.persistence.Column
Expand All @@ -9,7 +9,7 @@ import javax.validation.constraints.NotNull
class Password(
@field:NotNull
@Column(name = "password")
private val value: String
val value: String
) {
init {
val MAX_LENGTH = 20
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.sexysisters.tojserverv2.domain.user
package com.sexysisters.tojserverv2.domain.user.domain

import com.sexysisters.tojserverv2.domain.BaseTimeEntity
import com.sexysisters.tojserverv2.domain.student.domain.Student
import com.sexysisters.tojserverv2.domain.user.type.Authority
import com.sexysisters.tojserverv2.domain.user.type.Nickname
import javax.persistence.*

@Entity
Expand All @@ -25,6 +23,12 @@ class User(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long = 0L

fun emailValue() = email.value
fun passwordValue() = password.value
fun nicknameValue() = nickname.value
fun imageValue() = image.value
fun nameValue() = name.value
}

fun User.updateInfo(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.sexysisters.tojserverv2.domain.user.service

import com.sexysisters.tojserverv2.common.util.api.oauth.dto.OAuthInfoResponse
import com.sexysisters.tojserverv2.domain.auth.AuthInfo
import com.sexysisters.tojserverv2.domain.user.User
import com.sexysisters.tojserverv2.domain.user.UserStore
import com.sexysisters.tojserverv2.domain.user.domain.*
import com.sexysisters.tojserverv2.infrastructure.jwt.JwtTokenProvider
import com.sexysisters.tojserverv2.infrastructure.oauth.OAuthExecutor
import org.springframework.stereotype.Service
Expand All @@ -20,18 +21,19 @@ class OAuthServiceImpl(
@Transactional
override fun googleLogin(code: String): AuthInfo.Token {
val oAuthResponse = googleAuthExecutor.execute(code)
val initUser = User(
nickname = oAuthResponse.name,
email = oAuthResponse.email,
profileImg = oAuthResponse.picture,
password = "OAUTH",
name = "이름"
)
val initUser = createUser(oAuthResponse)
userStore.storeOAuthUser(initUser)

return AuthInfo.Token(
accessToken = jwtTokenProvider.createAccessToken(initUser.email),
refreshToken = jwtTokenProvider.createRefreshToken(initUser.email),
accessToken = jwtTokenProvider.createAccessToken(initUser.emailValue()),
refreshToken = jwtTokenProvider.createRefreshToken(initUser.emailValue()),
)
}

private fun createUser(response: OAuthInfoResponse) = User(
nickname = Nickname(response.name),
email = Email(response.email),
image = Image(response.picture),
password = Password("OAUTH"),
name = Name("이름")
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import com.sexysisters.tojserverv2.domain.user.UserReader
import com.sexysisters.tojserverv2.domain.user.UserStore
import com.sexysisters.tojserverv2.domain.user.setEncodedPassword
import com.sexysisters.tojserverv2.domain.user.toEntity
import com.sexysisters.tojserverv2.domain.user.updateInfo
import com.sexysisters.tojserverv2.domain.user.updateProfileImg
import com.sexysisters.tojserverv2.domain.user.domain.updateInfo
import com.sexysisters.tojserverv2.domain.user.domain.updateProfileImg
import org.springframework.security.crypto.password.PasswordEncoder
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.sexysisters.tojserverv2.infrastructure.user

import com.sexysisters.tojserverv2.common.security.auth.AuthDetails
import com.sexysisters.tojserverv2.domain.user.User
import com.sexysisters.tojserverv2.domain.user.domain.User
import com.sexysisters.tojserverv2.domain.user.UserReader
import com.sexysisters.tojserverv2.domain.user.exception.UserException
import org.springframework.data.repository.findByIdOrNull
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.sexysisters.tojserverv2.infrastructure.user

import com.sexysisters.tojserverv2.domain.user.User
import com.sexysisters.tojserverv2.domain.user.domain.User
import org.springframework.data.jpa.repository.JpaRepository

interface UserRepository : JpaRepository<User, Long> {
Expand Down
Loading

0 comments on commit 9627397

Please sign in to comment.