Skip to content

Commit 85d9918

Browse files
committed
refactor: 도메인에서 Entity 의존성 제거 및 Mapper 분리
1 parent 92f45a6 commit 85d9918

File tree

18 files changed

+112
-127
lines changed

18 files changed

+112
-127
lines changed

domain-nearpick/src/main/kotlin/com/nearpick/app/domain/address/entity/UserAddressEntity.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import org.springframework.data.annotation.LastModifiedBy
1111
import org.springframework.data.annotation.LastModifiedDate
1212
import org.springframework.data.jpa.domain.support.AuditingEntityListener
1313
import java.time.LocalDateTime
14-
import java.util.UUID
1514

1615
@Entity
1716
@Table(name = "user_address")
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.nearpick.app.domain.address.mapper
2+
3+
import com.nearpick.app.domain.address.entity.UserAddressEntity
4+
import com.nearpick.app.domain.address.service.UserAddress
5+
import org.springframework.stereotype.Component
6+
import java.util.UUID
7+
8+
@Component
9+
class UserAddressMapper {
10+
fun toEntity(domain: UserAddress): UserAddressEntity {
11+
return UserAddressEntity(
12+
id = domain.id ?: UUID.randomUUID().toString(),
13+
userId = domain.userId,
14+
name = domain.name,
15+
receiverName = domain.receiverName,
16+
phoneNumber = domain.phoneNumber,
17+
fullAddress = domain.fullAddress,
18+
addressDetail = domain.addressDetail,
19+
province = domain.province,
20+
district = domain.district,
21+
neighborhood = domain.neighborhood,
22+
street = domain.street,
23+
buildingNumber = domain.buildingNumber,
24+
isDefault = domain.isDefault ?: false
25+
)
26+
}
27+
28+
fun toDomain(entity: UserAddressEntity): UserAddress {
29+
return UserAddress(
30+
id = entity.id,
31+
userId = entity.userId,
32+
name = entity.name,
33+
receiverName = entity.receiverName,
34+
phoneNumber = entity.phoneNumber,
35+
fullAddress = entity.fullAddress,
36+
addressDetail = entity.addressDetail,
37+
province = entity.province,
38+
district = entity.district,
39+
neighborhood = entity.neighborhood,
40+
street = entity.street,
41+
buildingNumber = entity.buildingNumber,
42+
isDefault = entity.isDefault
43+
)
44+
}
45+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.nearpick.app.domain.address.mapper
2+
3+
import com.nearpick.app.domain.address.dto.UserAddressResponse
4+
import com.nearpick.app.domain.address.service.UserAddress
5+
import org.springframework.stereotype.Component
6+
7+
@Component
8+
class UserAddressResponseMapper {
9+
fun toResponse(address: UserAddress): UserAddressResponse {
10+
return UserAddressResponse(
11+
id = address.id,
12+
name = address.name,
13+
receiverName = address.receiverName,
14+
phoneNumber = address.phoneNumber,
15+
fullAddress = address.fullAddress,
16+
addressDetail = address.addressDetail,
17+
isDefault = address.isDefault
18+
)
19+
}
20+
}

domain-nearpick/src/main/kotlin/com/nearpick/app/domain/address/service/UserAddress.kt

Lines changed: 14 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,10 @@ package com.nearpick.app.domain.address.service
22

33
import com.nearpick.app.common.exception.InvalidPhoneNumberException
44
import com.nearpick.app.common.validator.Validator
5-
import com.nearpick.app.domain.address.dto.CreateUserAddressRequest
6-
import com.nearpick.app.domain.address.dto.UpdateUserAddressRequest
7-
import com.nearpick.app.domain.address.dto.UserAddressResponse
8-
import com.nearpick.app.domain.address.entity.UserAddressEntity
9-
import java.util.*
105

116

127
class UserAddress(
13-
val id: String?= null,
8+
val id: String? = null,
149
val userId: String,
1510
var name: String? = null,
1611
var receiverName: String? = null,
@@ -22,7 +17,7 @@ class UserAddress(
2217
val neighborhood: String? = null,
2318
val street: String? = null,
2419
val buildingNumber: String? = null,
25-
var isDefault: Boolean? = false,
20+
var isDefault: Boolean = false,
2621
) {
2722
init {
2823
val num = phoneNumber
@@ -31,62 +26,18 @@ class UserAddress(
3126
}
3227
}
3328

34-
fun update(request: UpdateUserAddressRequest) {
35-
this.name = request.name ?: this.name
36-
this.receiverName = request.receiverName ?: this.receiverName
37-
this.phoneNumber = request.phoneNumber ?: this.phoneNumber
38-
this.addressDetail = request.addressDetail ?: this.addressDetail
39-
this.isDefault = request.isDefault ?: this.isDefault
40-
}
41-
42-
fun toEntity(): UserAddressEntity {
43-
return UserAddressEntity(
44-
id = id?: UUID.randomUUID().toString(),
45-
userId = userId,
46-
name = name,
47-
receiverName = receiverName,
48-
phoneNumber = phoneNumber,
49-
fullAddress = fullAddress,
50-
addressDetail = addressDetail,
51-
province = province,
52-
district = district,
53-
neighborhood = neighborhood,
54-
street = street,
55-
buildingNumber = buildingNumber,
56-
isDefault = isDefault ?: false
57-
)
58-
}
59-
60-
companion object {
61-
fun from(userAddressEntity: UserAddressEntity): UserAddress {
62-
return UserAddress(
63-
id = userAddressEntity.id,
64-
userId = userAddressEntity.userId,
65-
name = userAddressEntity.name,
66-
receiverName = userAddressEntity.receiverName,
67-
phoneNumber = userAddressEntity.phoneNumber,
68-
fullAddress = userAddressEntity.fullAddress,
69-
addressDetail = userAddressEntity.addressDetail,
70-
province = userAddressEntity.province,
71-
district = userAddressEntity.district,
72-
neighborhood = userAddressEntity.neighborhood,
73-
street = userAddressEntity.street,
74-
buildingNumber = userAddressEntity.buildingNumber,
75-
isDefault = userAddressEntity.isDefault
76-
)
77-
}
78-
79-
fun toResponse(address: UserAddressEntity): UserAddressResponse {
80-
return UserAddressResponse(
81-
id = address.id,
82-
name = address.name,
83-
receiverName = address.receiverName,
84-
phoneNumber = address.phoneNumber,
85-
fullAddress = address.fullAddress,
86-
addressDetail = address.addressDetail,
87-
isDefault = address.isDefault
88-
)
89-
}
29+
fun update(
30+
name: String?,
31+
receiverName: String?,
32+
phoneNumber: String?,
33+
addressDetail: String?,
34+
isDefault: Boolean?
35+
) {
36+
this.name = name ?: this.name
37+
this.receiverName = receiverName ?: this.receiverName
38+
this.phoneNumber = phoneNumber ?: this.phoneNumber
39+
this.addressDetail = addressDetail ?: this.addressDetail
40+
this.isDefault = isDefault ?: this.isDefault
9041
}
9142
}
9243

domain-nearpick/src/main/kotlin/com/nearpick/app/domain/address/service/UserAddressServiceImpl.kt

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@ import com.nearpick.app.common.exception.UserAddressNotFoundException
44
import com.nearpick.app.domain.address.dto.CreateUserAddressRequest
55
import com.nearpick.app.domain.address.dto.UpdateUserAddressRequest
66
import com.nearpick.app.domain.address.dto.UserAddressResponse
7+
import com.nearpick.app.domain.address.mapper.UserAddressMapper
8+
import com.nearpick.app.domain.address.mapper.UserAddressResponseMapper
79
import com.nearpick.app.domain.address.repository.UserAddressRepository
810
import org.springframework.transaction.annotation.Transactional
911
import org.springframework.stereotype.Service
1012

1113
@Service
1214
@Transactional(readOnly = false)
1315
open class UserAddressServiceImpl(
14-
private val userAddressRepository: UserAddressRepository
16+
private val userAddressRepository: UserAddressRepository,
17+
private val userAddressMapper: UserAddressMapper,
18+
private val userAddressResponseMapper: UserAddressResponseMapper,
1519
) : UserAddressService {
1620

1721

@@ -32,29 +36,41 @@ open class UserAddressServiceImpl(
3236
isDefault = false
3337
)
3438

35-
return UserAddress.toResponse(userAddressRepository.save(userAddress.toEntity()))
39+
val entity = userAddressMapper.toEntity(userAddress)
40+
userAddressRepository.save(entity)
41+
42+
return userAddressResponseMapper.toResponse(userAddress)
3643
}
3744

3845
@Transactional(readOnly = true)
3946
override fun findAllUserAddressByUserId(userId: String): List<UserAddressResponse> {
40-
return userAddressRepository.findAllByUserId(userId).map { UserAddress.toResponse(it) }
47+
return userAddressRepository.findAllByUserId(userId)
48+
.map { userAddressResponseMapper.toResponse(userAddressMapper.toDomain(it)) }
4149
}
4250

4351
override fun updateUserAddress(id: String, userId: String, request: UpdateUserAddressRequest): UserAddressResponse {
44-
val userAddressEntity =
45-
userAddressRepository.findByIdAndUserId(id, userId) ?: throw UserAddressNotFoundException(id, userId)
46-
val userAddress = UserAddress.from(userAddressEntity)
52+
val entity = userAddressRepository.findByIdAndUserId(id, userId)
53+
?: throw UserAddressNotFoundException(id, userId)
54+
55+
val userAddress = userAddressMapper.toDomain(entity)
4756

48-
userAddress.update(request)
57+
userAddress.update(
58+
request.name,
59+
request.receiverName,
60+
request.phoneNumber,
61+
request.addressDetail,
62+
request.isDefault
63+
)
4964

50-
return UserAddress.toResponse(userAddressRepository.save(userAddress.toEntity()))
65+
val updatedEntity = userAddressMapper.toEntity(userAddress)
66+
userAddressRepository.save(updatedEntity)
67+
68+
return userAddressResponseMapper.toResponse(userAddress)
5169
}
5270

5371
override fun deleteUserAddress(id: String, userId: String) {
54-
val address = userAddressRepository.findByIdAndUserId(id, userId) ?: throw UserAddressNotFoundException(
55-
id,
56-
userId
57-
)
72+
val address = userAddressRepository.findByIdAndUserId(id, userId)
73+
?: throw UserAddressNotFoundException(id, userId)
5874

5975
userAddressRepository.delete(address)
6076
}

domain-nearpick/src/main/kotlin/com/nearpick/app/domain/brand/entity/BrandEntity.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package com.nearpick.app.domain.brand.entity
22

3-
import com.nearpick.app.domain.brand.dto.BrandResponse
4-
import com.nearpick.app.domain.brand.dto.CreateBrandRequest
5-
import com.nearpick.app.domain.brand.dto.GetBrandDetailResponse
63
import com.nearpick.app.domain.user.entity.UserEntity
74
import jakarta.persistence.Column
85
import jakarta.persistence.Entity
@@ -18,7 +15,6 @@ import org.springframework.data.annotation.LastModifiedBy
1815
import org.springframework.data.annotation.LastModifiedDate
1916
import org.springframework.data.jpa.domain.support.AuditingEntityListener
2017
import java.time.LocalDateTime
21-
import java.util.UUID
2218

2319
@Entity
2420
@Table(name = "Brand")

domain-nearpick/src/main/kotlin/com/nearpick/app/domain/brand/service/BrandServiceImpl.kt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,16 @@ package com.nearpick.app.domain.brand.service
33
import com.nearpick.app.common.exception.BrandAlreadyExistsException
44
import com.nearpick.app.domain.brand.dto.BrandResponse
55
import com.nearpick.app.domain.brand.dto.CreateBrandRequest
6-
import com.nearpick.app.domain.brand.entity.BrandEntity
76
import com.nearpick.app.domain.brand.repository.BrandRepository
87
import com.nearpick.app.common.exception.BrandNotFoundException
9-
import com.nearpick.app.common.exception.UserNotFoundException
108
import com.nearpick.app.domain.brand.dto.GetBrandDetailResponse
119
import com.nearpick.app.domain.brand.dto.UpdateBrandRequest
1210
import com.nearpick.app.domain.brand.mapper.BrandMapper
1311
import com.nearpick.app.domain.brand.mapper.BrandResponseMapper
1412
import com.nearpick.app.domain.user.mapper.UserMapper
1513
import com.nearpick.app.domain.user.mapper.UserResponseMapper
16-
import com.nearpick.app.domain.user.repository.UserRepository
1714
import org.springframework.transaction.annotation.Transactional
1815
import org.springframework.stereotype.Service
19-
import kotlin.jvm.optionals.getOrElse
2016

2117
@Service
2218
@Transactional(readOnly = false)
@@ -49,8 +45,7 @@ open class BrandServiceImpl(
4945
val entity = brandMapper.toEntity(brand)
5046
brandRepository.save(entity)
5147

52-
val savedBrand = brandMapper.toDomain(entity)
53-
return brandResponseMapper.toResponse(savedBrand)
48+
return brandResponseMapper.toResponse(brand)
5449
}
5550

5651
@Transactional(readOnly = true)

domain-nearpick/src/main/kotlin/com/nearpick/app/domain/product/entity/ProductEntity.kt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
package com.nearpick.app.domain.product.entity
22

33
import com.nearpick.app.domain.brand.entity.BrandEntity
4-
import com.nearpick.app.domain.brand.service.Brand
5-
import com.nearpick.app.domain.product.dto.CreateProductRequest
6-
import com.nearpick.app.domain.product.dto.GetProductDetailResponse
7-
import com.nearpick.app.domain.product.dto.ProductResponse
84
import com.nearpick.app.domain.product.enum.ProductStatus
95
import com.nearpick.app.domain.product.enum.ProductType
106
import com.nearpick.app.domain.user.entity.UserEntity
11-
import com.nearpick.app.domain.user.service.User
127
import jakarta.persistence.Column
138
import jakarta.persistence.Entity
149
import jakarta.persistence.EntityListeners
@@ -26,7 +21,6 @@ import org.springframework.data.annotation.LastModifiedDate
2621
import org.springframework.data.jpa.domain.support.AuditingEntityListener
2722
import java.math.BigInteger
2823
import java.time.LocalDateTime
29-
import java.util.UUID
3024

3125
@Entity
3226
@Table(name = "product")

domain-nearpick/src/main/kotlin/com/nearpick/app/domain/product/repository/ProductRepository.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.nearpick.app.domain.product.repository
22

3-
import com.nearpick.app.domain.brand.entity.BrandEntity
43
import com.nearpick.app.domain.product.entity.ProductEntity
54
import org.springframework.data.jpa.repository.JpaRepository
65

domain-nearpick/src/main/kotlin/com/nearpick/app/domain/product/service/ProductServiceImpl.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ open class ProductServiceImpl(
5050
val entity = productMapper.toEntity(product)
5151
productRepository.save(entity)
5252

53-
val savedProduct = productMapper.toDomain(entity)
54-
return productResponseMapper.toResponse(savedProduct)
53+
return productResponseMapper.toResponse(product)
5554
}
5655

5756
@Transactional(readOnly = true)
@@ -92,6 +91,7 @@ open class ProductServiceImpl(
9291
request.productType,
9392
request.reservationDeadline
9493
)
94+
9595
val updatedEntity = productMapper.toEntity(product)
9696
productRepository.save(updatedEntity)
9797

0 commit comments

Comments
 (0)