forked from Exagonal-Study/est-delivery
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from this-is-spear/회원-쿠폰-보유-상태를-변경한다
쿠폰 도메인에서 회원 상태 변경 방식을 변경한다.
- Loading branch information
Showing
21 changed files
with
164 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 27 additions & 22 deletions
49
...e/estdelivery/coupon/application/port/out/adapter/persistence/MemberPersistenceAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,51 @@ | ||
package com.example.estdelivery.coupon.application.port.out.adapter.persistence | ||
|
||
import com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity.MemberEntity | ||
import com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity.MemberCouponEntity | ||
import com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity.MemberCouponUseState | ||
import com.example.estdelivery.coupon.application.port.out.adapter.persistence.mapper.fromCoupon | ||
import com.example.estdelivery.coupon.application.port.out.adapter.persistence.mapper.toCoupon | ||
import com.example.estdelivery.coupon.application.port.out.adapter.persistence.repository.MemberRepository | ||
import com.example.estdelivery.coupon.application.port.out.adapter.persistence.repository.MemberCouponRepository | ||
import com.example.estdelivery.coupon.domain.coupon.Coupon | ||
import com.example.estdelivery.coupon.domain.coupon.CouponBook | ||
import com.example.estdelivery.coupon.domain.member.UnusedCouponBook | ||
import org.springframework.data.repository.findByIdOrNull | ||
import org.springframework.stereotype.Component | ||
import org.springframework.transaction.annotation.Transactional | ||
|
||
@Component | ||
class MemberPersistenceAdapter( | ||
private val memberRepository: MemberRepository | ||
private val memberCouponRepository: MemberCouponRepository, | ||
) { | ||
/** | ||
* 회원이 사용하지 않은 쿠폰 정보를 조회한다. 회원 정보가 없는 경우 회원 정보를 새롭게 추가한다. | ||
* `asSequence`를 이용해 사용자의 쿠폰 리스트를 중간마다 버퍼링하지 않고 최종적으로 버퍼링을 진행한다. | ||
*/ | ||
@Transactional(readOnly = true) | ||
fun findUnusedCouponByMemberId(memberId: Long) = | ||
memberRepository.findByIdOrNull(memberId)?.let { memberEntity -> | ||
memberEntity.unusedCoupons.map { | ||
toCoupon(it) | ||
}.let { | ||
UnusedCouponBook(CouponBook(it)) | ||
} | ||
} ?: memberRepository.save( | ||
MemberEntity( | ||
emptyList(), | ||
memberId | ||
) | ||
).let { | ||
UnusedCouponBook(CouponBook(emptyList())) | ||
} | ||
memberCouponRepository.findMembersUnusedCoupon(memberId) | ||
.asSequence() | ||
.map { it.coupon } | ||
.map { toCoupon(it) } | ||
.let { UnusedCouponBook(CouponBook(it.toList())) } | ||
|
||
/** | ||
* 추가된 쿠폰을 추가하거나 사용한 쿠폰만 찾아 상태를 변경한다. | ||
* `dirty checking`을 사용했으니 주의하자. | ||
*/ | ||
@Transactional | ||
fun updateUnusedCouponBook(memberId: Long, unusedCoupons: List<Coupon>) { | ||
val memberEntity = memberRepository.findByIdOrNull(memberId) ?: return | ||
memberEntity.unusedCoupons = unusedCoupons | ||
unusedCoupons | ||
.map { | ||
fromCoupon(it) | ||
}.toList() | ||
memberCouponRepository.findByMemberIdAndCoupon(memberId, fromCoupon(it)) | ||
?: memberCouponRepository.save( | ||
MemberCouponEntity( | ||
fromCoupon(it), | ||
memberId, | ||
MemberCouponUseState.UNUSED | ||
) | ||
) | ||
} | ||
.filterIsInstance<Coupon.UsedCoupon>() | ||
.map { memberCouponRepository.findByMemberIdAndCoupon(memberId, fromCoupon(it)) } | ||
.map { it!!.status = MemberCouponUseState.USED } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
.../estdelivery/coupon/application/port/out/adapter/persistence/entity/MemberCouponEntity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity | ||
|
||
import jakarta.persistence.Entity | ||
import jakarta.persistence.EnumType | ||
import jakarta.persistence.Enumerated | ||
import jakarta.persistence.GeneratedValue | ||
import jakarta.persistence.GenerationType | ||
import jakarta.persistence.Id | ||
import jakarta.persistence.JoinColumn | ||
import jakarta.persistence.ManyToOne | ||
|
||
@Entity | ||
class MemberCouponEntity( | ||
@ManyToOne | ||
@JoinColumn(name = "coupon_id") | ||
val coupon: CouponEntity, | ||
val memberId: Long, | ||
@Enumerated(EnumType.STRING) | ||
var status: MemberCouponUseState, | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
val id: Long = 0, | ||
) { | ||
override fun equals(other: Any?): Boolean { | ||
if (this === other) return true | ||
if (other !is MemberCouponEntity) return false | ||
|
||
if (other.id == 0L) return false | ||
if (id != other.id) return false | ||
|
||
return true | ||
} | ||
|
||
override fun hashCode(): Int { | ||
return id.hashCode() | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...stdelivery/coupon/application/port/out/adapter/persistence/entity/MemberCouponUseState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity | ||
|
||
enum class MemberCouponUseState { | ||
USED, | ||
UNUSED, | ||
EXPIRED, | ||
; | ||
} |
39 changes: 0 additions & 39 deletions
39
...xample/estdelivery/coupon/application/port/out/adapter/persistence/entity/MemberEntity.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
...very/coupon/application/port/out/adapter/persistence/repository/MemberCouponRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.example.estdelivery.coupon.application.port.out.adapter.persistence.repository | ||
|
||
import com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity.CouponEntity | ||
import com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity.MemberCouponEntity | ||
import org.springframework.data.jpa.repository.JpaRepository | ||
import org.springframework.data.jpa.repository.Query | ||
|
||
interface MemberCouponRepository : JpaRepository<MemberCouponEntity, Long> { | ||
@Query( | ||
""" | ||
SELECT mc | ||
FROM MemberCouponEntity mc | ||
WHERE mc.memberId = :memberId | ||
AND mc.status = com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity.MemberCouponUseState.UNUSED | ||
""" | ||
) | ||
fun findMembersUnusedCoupon(memberId: Long): List<MemberCouponEntity> | ||
fun findByMemberIdAndCoupon(memberId: Long, coupon: CouponEntity): MemberCouponEntity? | ||
} |
6 changes: 0 additions & 6 deletions
6
...stdelivery/coupon/application/port/out/adapter/persistence/repository/MemberRepository.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.