From 3ffc5b4ba365de8e651c96062460fb8ea1c8b6a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EA=B1=B4=EC=B0=BD?= <92219795+this-is-spear@users.noreply.github.com> Date: Wed, 15 May 2024 15:12:18 +0900 Subject: [PATCH 01/10] =?UTF-8?q?refactor=20:=20adapter=20=ED=8C=A8?= =?UTF-8?q?=ED=82=A4=EC=A7=80=20=EC=9D=B4=EB=8F=99=20=EB=B0=8F=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coupon/application/GiftCouponService.kt | 10 ++----- .../IssuePublishedCouponService.kt | 6 ++-- .../application/IssueRandomCouponService.kt | 4 +-- .../coupon/application/UseCouponService.kt | 4 +-- .../port/out/LoadMemberStatePort.kt | 2 +- .../port/out/UpdateMemberStatePort.kt | 2 +- .../port/out/adapter/MemberAdapter.kt | 24 +++++++++++++++ .../{ => adapter}/infra/EventInfraAdapter.kt | 6 ++-- .../{ => adapter}/infra/MemberInfraAdapter.kt | 4 +-- .../out/{ => adapter}/infra/RestClients.kt | 2 +- .../{ => adapter}/infra/ShopInfraAdapter.kt | 4 +-- .../out/{ => adapter}/infra/dto/EventState.kt | 2 +- .../port/out/adapter/infra/dto/MemberState.kt | 6 ++++ .../out/adapter/infra/dto/ShopOwnerState.kt | 6 ++++ .../out/{ => adapter}/infra/dto/ShopState.kt | 2 +- .../dto/UpdateParticipatedMembersState.kt | 2 +- .../persistence/CouponPersistenceAdapter.kt | 8 ++--- .../persistence/MemberPersistenceAdapter.kt | 26 +++++++++++++++++ .../ShopOwnerPersistenceAdapter.kt | 10 +++---- .../persistence/entity/CouponEntity.kt | 2 +- .../entity/CouponStateAmountType.kt | 6 ++++ .../persistence/entity/CouponStateType.kt | 7 +++++ .../persistence/entity/MemberCouponEntity.kt} | 5 ++-- .../persistence/entity/ShopEntity.kt | 6 ++-- .../persistence/entity/ShopOwnerEntity.kt | 2 +- .../persistence/mapper/CouponMapper.kt | 18 ++++++------ .../persistence/mapper/MemberMapper.kt | 13 ++++----- .../persistence/mapper/ShopOwnerMapper.kt | 8 ++--- .../repository/CouponRepository.kt | 7 +++++ .../repository/MemberRepository.kt | 7 +++++ .../repository/ShopOwnerRepository.kt | 11 +++++++ .../persistence/repository/ShopRepository.kt | 7 +++++ .../port/out/infra/dto/MemberState.kt | 6 ---- .../port/out/infra/dto/ShopOwnerState.kt | 6 ---- .../persistence/MemberPersistenceAdapter.kt | 29 ------------------- .../entity/CouponStateAmountType.kt | 6 ---- .../out/persistence/entity/CouponStateType.kt | 7 ----- .../repository/CouponRepository.kt | 7 ----- .../repository/MemberRepository.kt | 7 ----- .../repository/ShopOwnerRepository.kt | 11 ------- .../persistence/repository/ShopRepository.kt | 7 ----- .../coupon/domain/member/Member.kt | 6 +++- .../application/GiftCouponServiceTest.kt | 14 ++++----- .../IssuePublishedCouponServiceTest.kt | 12 ++++---- .../IssueRandomCouponServiceTest.kt | 4 +-- .../application/UseCouponServiceTest.kt | 4 +-- .../infra/EventInfraAdapterTest.kt | 4 +-- .../infra/MemberInfraAdapterTest.kt | 2 +- .../infra/ShopInfraAdapterTest.kt | 2 +- .../CouponPersistenceAdapterTest.kt | 6 ++-- .../MemberPersistenceAdapterTest.kt | 18 +++++------- .../ShopOwnerPersistenceAdapterTest.kt | 12 ++++---- 52 files changed, 207 insertions(+), 192 deletions(-) create mode 100644 coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/MemberAdapter.kt rename coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/{ => adapter}/infra/EventInfraAdapter.kt (72%) rename coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/{ => adapter}/infra/MemberInfraAdapter.kt (79%) rename coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/{ => adapter}/infra/RestClients.kt (89%) rename coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/{ => adapter}/infra/ShopInfraAdapter.kt (93%) rename coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/{ => adapter}/infra/dto/EventState.kt (84%) create mode 100644 coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/dto/MemberState.kt create mode 100644 coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/dto/ShopOwnerState.kt rename coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/{ => adapter}/infra/dto/ShopState.kt (55%) rename coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/{ => adapter}/infra/dto/UpdateParticipatedMembersState.kt (56%) rename coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/{ => adapter}/persistence/CouponPersistenceAdapter.kt (73%) create mode 100644 coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/MemberPersistenceAdapter.kt rename coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/{ => adapter}/persistence/ShopOwnerPersistenceAdapter.kt (69%) rename coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/{ => adapter}/persistence/entity/CouponEntity.kt (88%) create mode 100644 coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/CouponStateAmountType.kt create mode 100644 coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/CouponStateType.kt rename coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/{persistence/entity/MemberEntity.kt => adapter/persistence/entity/MemberCouponEntity.kt} (86%) rename coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/{ => adapter}/persistence/entity/ShopEntity.kt (91%) rename coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/{ => adapter}/persistence/entity/ShopOwnerEntity.kt (84%) rename coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/{ => adapter}/persistence/mapper/CouponMapper.kt (66%) rename coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/{ => adapter}/persistence/mapper/MemberMapper.kt (58%) rename coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/{ => adapter}/persistence/mapper/ShopOwnerMapper.kt (86%) create mode 100644 coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/repository/CouponRepository.kt create mode 100644 coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/repository/MemberRepository.kt create mode 100644 coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/repository/ShopOwnerRepository.kt create mode 100644 coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/repository/ShopRepository.kt delete mode 100644 coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/infra/dto/MemberState.kt delete mode 100644 coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/infra/dto/ShopOwnerState.kt delete mode 100644 coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/MemberPersistenceAdapter.kt delete mode 100644 coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/entity/CouponStateAmountType.kt delete mode 100644 coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/entity/CouponStateType.kt delete mode 100644 coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/repository/CouponRepository.kt delete mode 100644 coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/repository/MemberRepository.kt delete mode 100644 coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/repository/ShopOwnerRepository.kt delete mode 100644 coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/repository/ShopRepository.kt rename coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/{ => adapter}/infra/EventInfraAdapterTest.kt (84%) rename coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/{ => adapter}/infra/MemberInfraAdapterTest.kt (92%) rename coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/{ => adapter}/infra/ShopInfraAdapterTest.kt (93%) rename coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/{ => adapter}/persistence/CouponPersistenceAdapterTest.kt (89%) rename coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/{ => adapter}/persistence/MemberPersistenceAdapterTest.kt (71%) rename coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/{ => adapter}/persistence/ShopOwnerPersistenceAdapterTest.kt (88%) diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/GiftCouponService.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/GiftCouponService.kt index 9bf85fd..f33aec0 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/GiftCouponService.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/GiftCouponService.kt @@ -15,21 +15,17 @@ class GiftCouponService( updateMemberStatePort: UpdateMemberStatePort, private val transactionArea: TransactionArea, private val getReceiver: (GiftCouponCommand) -> Member = { - loadMemberStatePort.findById( - it.receiverId - ) + loadMemberStatePort.findMember(it.receiverId) }, private val getSender: (GiftCouponCommand) -> Member = { - loadMemberStatePort.findById( - it.senderId - ) + loadMemberStatePort.findMember(it.senderId) }, private val getCoupon: (GiftCouponCommand) -> Coupon = { loadCouponStatePort.findById( it.couponId ) }, - private val updateMember: (Member) -> Unit = { updateMemberStatePort.update(it) }, + private val updateMember: (Member) -> Unit = { updateMemberStatePort.updateMembersCoupon(it) }, ) : GiftCouponUseCase { /** * 선물 할 쿠폰과 선물 할 회원의 식별자를 입력해 쿠폰을 나눠준다. diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/IssuePublishedCouponService.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/IssuePublishedCouponService.kt index 4fae73c..83fda6d 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/IssuePublishedCouponService.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/IssuePublishedCouponService.kt @@ -20,9 +20,7 @@ class IssuePublishedCouponService( updateShopOwnerStatePort: UpdateShopOwnerStatePort, private val transactionArea: TransactionArea, private val getMember: (IssuePublishedCouponCommand) -> Member = { - loadMemberStatePort.findById( - it.memberId - ) + loadMemberStatePort.findMember(it.memberId) }, private val getCoupon: (IssuePublishedCouponCommand) -> Coupon = { loadCouponStatePort.findById( @@ -34,7 +32,7 @@ class IssuePublishedCouponService( it.shopId ) }, - private val updateMember: (Member) -> Unit = { updateMemberStatePort.update(it) }, + private val updateMember: (Member) -> Unit = { updateMemberStatePort.updateMembersCoupon(it) }, private val updateShopOwner: (ShopOwner) -> Unit = { updateShopOwnerStatePort.update(it) }, ) : IssuePublishedCouponUseCase { /** diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/IssueRandomCouponService.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/IssueRandomCouponService.kt index cb65b27..e5bec37 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/IssueRandomCouponService.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/IssueRandomCouponService.kt @@ -26,7 +26,7 @@ class IssueRandomCouponService( createCouponStatePort: CreateCouponStatePort, private val transactionArea: TransactionArea, private val getMember: (IssueEventCouponCommand) -> Member = { - loadMemberStatePort.findById(it.memberId) + loadMemberStatePort.findMember(it.memberId) }, private val getCouponIssueRandomCouponIssueEvent: (IssueEventCouponCommand) -> RandomCouponIssueEvent = { loadRandomCouponIssueEventStatePort.findById(it.eventId).toEvent() @@ -35,7 +35,7 @@ class IssueRandomCouponService( loadShopOwnerStatePort.findByShopId(it.shopId) }, private val createCoupon: (Coupon) -> Coupon = { createCouponStatePort.create(it) }, - private val updateMember: (Member) -> Unit = { updateMemberStatePort.update(it) }, + private val updateMember: (Member) -> Unit = { updateMemberStatePort.updateMembersCoupon(it) }, private val updateShopOwner: (ShopOwner) -> Unit = { updateShopOwnerStatePort.update(it) }, private val updateEvent: (RandomCouponIssueEvent) -> Unit = { updateRandomCouponIssueEventStatePort.update(UpdateRandomCouponIssueEventState.from(it)) diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/UseCouponService.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/UseCouponService.kt index f4920f5..eeea941 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/UseCouponService.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/UseCouponService.kt @@ -20,7 +20,7 @@ class UseCouponService( updateShopOwnerStatePort: UpdateShopOwnerStatePort, private val transactionArea: TransactionArea, private val getMember: (UseCouponCommand) -> Member = { - loadMemberStatePort.findById( + loadMemberStatePort.findMember( it.memberId ) }, @@ -34,7 +34,7 @@ class UseCouponService( it.shopId ) }, - private val updateMember: (Member) -> Unit = { updateMemberStatePort.update(it) }, + private val updateMember: (Member) -> Unit = { updateMemberStatePort.updateMembersCoupon(it) }, private val updateShopOwner: (ShopOwner) -> Unit = { updateShopOwnerStatePort.update(it) }, ) : UseCouponUseCase { /** diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/LoadMemberStatePort.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/LoadMemberStatePort.kt index 6c62f9e..b9b9742 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/LoadMemberStatePort.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/LoadMemberStatePort.kt @@ -3,5 +3,5 @@ package com.example.estdelivery.coupon.application.port.out import com.example.estdelivery.coupon.domain.member.Member interface LoadMemberStatePort { - fun findById(memberId: Long): Member + fun findMember(memberId: Long): Member } diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/UpdateMemberStatePort.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/UpdateMemberStatePort.kt index 865a163..9b4df45 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/UpdateMemberStatePort.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/UpdateMemberStatePort.kt @@ -3,5 +3,5 @@ package com.example.estdelivery.coupon.application.port.out import com.example.estdelivery.coupon.domain.member.Member interface UpdateMemberStatePort { - fun update(member: Member) + fun updateMembersCoupon(member: Member) } diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/MemberAdapter.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/MemberAdapter.kt new file mode 100644 index 0000000..5acb0b1 --- /dev/null +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/MemberAdapter.kt @@ -0,0 +1,24 @@ +package com.example.estdelivery.coupon.application.port.out.adapter + +import com.example.estdelivery.coupon.application.port.out.LoadMemberStatePort +import com.example.estdelivery.coupon.application.port.out.UpdateMemberStatePort +import com.example.estdelivery.coupon.application.port.out.adapter.infra.MemberInfraAdapter +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.MemberPersistenceAdapter +import com.example.estdelivery.coupon.domain.member.Member +import org.springframework.stereotype.Component + +@Component +class MemberAdapter( + private val memberInfraAdapter: MemberInfraAdapter, + private val memberPersistenceAdapter: MemberPersistenceAdapter, +): LoadMemberStatePort, UpdateMemberStatePort { + override fun findMember(memberId: Long): Member { + val member = memberInfraAdapter.findMember(memberId) + val membersCoupon = memberPersistenceAdapter.findMemberCouponById(memberId) + return member.have(membersCoupon) + } + + override fun updateMembersCoupon(member: Member) { + memberPersistenceAdapter.update(member) + } +} diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/infra/EventInfraAdapter.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/EventInfraAdapter.kt similarity index 72% rename from coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/infra/EventInfraAdapter.kt rename to coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/EventInfraAdapter.kt index 632fb15..e9d36b9 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/infra/EventInfraAdapter.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/EventInfraAdapter.kt @@ -1,7 +1,7 @@ -package com.example.estdelivery.coupon.application.port.out.infra +package com.example.estdelivery.coupon.application.port.out.adapter.infra -import com.example.estdelivery.coupon.application.port.out.infra.dto.EventState -import com.example.estdelivery.coupon.application.port.out.infra.dto.UpdateParticipatedMembersState +import com.example.estdelivery.coupon.application.port.out.adapter.infra.dto.EventState +import com.example.estdelivery.coupon.application.port.out.adapter.infra.dto.UpdateParticipatedMembersState import org.springframework.stereotype.Component import org.springframework.web.client.RestClient diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/infra/MemberInfraAdapter.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/MemberInfraAdapter.kt similarity index 79% rename from coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/infra/MemberInfraAdapter.kt rename to coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/MemberInfraAdapter.kt index 42c6032..b1c2a7a 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/infra/MemberInfraAdapter.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/MemberInfraAdapter.kt @@ -1,6 +1,6 @@ -package com.example.estdelivery.coupon.application.port.out.infra +package com.example.estdelivery.coupon.application.port.out.adapter.infra -import com.example.estdelivery.coupon.application.port.out.infra.dto.MemberState +import com.example.estdelivery.coupon.application.port.out.adapter.infra.dto.MemberState import com.example.estdelivery.coupon.domain.member.Member import org.springframework.stereotype.Component import org.springframework.web.client.RestClient diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/infra/RestClients.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/RestClients.kt similarity index 89% rename from coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/infra/RestClients.kt rename to coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/RestClients.kt index 314d06f..05c5579 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/infra/RestClients.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/RestClients.kt @@ -1,4 +1,4 @@ -package com.example.estdelivery.coupon.application.port.out.infra +package com.example.estdelivery.coupon.application.port.out.adapter.infra import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/infra/ShopInfraAdapter.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/ShopInfraAdapter.kt similarity index 93% rename from coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/infra/ShopInfraAdapter.kt rename to coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/ShopInfraAdapter.kt index c441bd5..020b472 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/infra/ShopInfraAdapter.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/ShopInfraAdapter.kt @@ -1,6 +1,6 @@ -package com.example.estdelivery.coupon.application.port.out.infra +package com.example.estdelivery.coupon.application.port.out.adapter.infra -import com.example.estdelivery.coupon.application.port.out.infra.dto.ShopOwnerState +import com.example.estdelivery.coupon.application.port.out.adapter.infra.dto.ShopOwnerState import com.example.estdelivery.coupon.domain.shop.HandOutCouponBook import com.example.estdelivery.coupon.domain.shop.PublishedCouponBook import com.example.estdelivery.coupon.domain.shop.PublishedEventCouponBook diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/infra/dto/EventState.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/dto/EventState.kt similarity index 84% rename from coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/infra/dto/EventState.kt rename to coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/dto/EventState.kt index 65914bc..fd0dbaf 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/infra/dto/EventState.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/dto/EventState.kt @@ -1,4 +1,4 @@ -package com.example.estdelivery.coupon.application.port.out.infra.dto +package com.example.estdelivery.coupon.application.port.out.adapter.infra.dto import com.example.estdelivery.coupon.domain.event.EventDiscountType import com.example.estdelivery.coupon.domain.event.ProbabilityRange diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/dto/MemberState.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/dto/MemberState.kt new file mode 100644 index 0000000..26e240e --- /dev/null +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/dto/MemberState.kt @@ -0,0 +1,6 @@ +package com.example.estdelivery.coupon.application.port.out.adapter.infra.dto + +class MemberState( + val id: Long, + val name: String, +) diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/dto/ShopOwnerState.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/dto/ShopOwnerState.kt new file mode 100644 index 0000000..2b4fd2c --- /dev/null +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/dto/ShopOwnerState.kt @@ -0,0 +1,6 @@ +package com.example.estdelivery.coupon.application.port.out.adapter.infra.dto + +class ShopOwnerState( + val shop: ShopState, + val id: Long, +) diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/infra/dto/ShopState.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/dto/ShopState.kt similarity index 55% rename from coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/infra/dto/ShopState.kt rename to coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/dto/ShopState.kt index 817180a..9246aa4 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/infra/dto/ShopState.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/dto/ShopState.kt @@ -1,4 +1,4 @@ -package com.example.estdelivery.coupon.application.port.out.infra.dto +package com.example.estdelivery.coupon.application.port.out.adapter.infra.dto class ShopState( val royalCustomers: List, diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/infra/dto/UpdateParticipatedMembersState.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/dto/UpdateParticipatedMembersState.kt similarity index 56% rename from coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/infra/dto/UpdateParticipatedMembersState.kt rename to coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/dto/UpdateParticipatedMembersState.kt index 7f1d025..26ecf1a 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/infra/dto/UpdateParticipatedMembersState.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/dto/UpdateParticipatedMembersState.kt @@ -1,4 +1,4 @@ -package com.example.estdelivery.coupon.application.port.out.infra.dto +package com.example.estdelivery.coupon.application.port.out.adapter.infra.dto data class UpdateParticipatedMembersState( val id: Long, diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/CouponPersistenceAdapter.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/CouponPersistenceAdapter.kt similarity index 73% rename from coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/CouponPersistenceAdapter.kt rename to coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/CouponPersistenceAdapter.kt index dded9c2..23b1cff 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/CouponPersistenceAdapter.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/CouponPersistenceAdapter.kt @@ -1,10 +1,10 @@ -package com.example.estdelivery.coupon.application.port.out.persistence +package com.example.estdelivery.coupon.application.port.out.adapter.persistence import com.example.estdelivery.coupon.application.port.out.CreateCouponStatePort import com.example.estdelivery.coupon.application.port.out.LoadCouponStatePort -import com.example.estdelivery.coupon.application.port.out.persistence.mapper.fromCoupon -import com.example.estdelivery.coupon.application.port.out.persistence.mapper.toCoupon -import com.example.estdelivery.coupon.application.port.out.persistence.repository.CouponRepository +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.CouponRepository import com.example.estdelivery.coupon.domain.coupon.Coupon import jakarta.transaction.Transactional import org.springframework.stereotype.Component diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/MemberPersistenceAdapter.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/MemberPersistenceAdapter.kt new file mode 100644 index 0000000..63cbf0a --- /dev/null +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/MemberPersistenceAdapter.kt @@ -0,0 +1,26 @@ +package com.example.estdelivery.coupon.application.port.out.adapter.persistence + +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.mapper.fromCouponMember +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.mapper.toMember +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.repository.MemberRepository +import com.example.estdelivery.coupon.domain.member.Member +import jakarta.transaction.Transactional +import org.springframework.data.repository.findByIdOrNull +import org.springframework.stereotype.Component + +@Component +class MemberPersistenceAdapter( + private val memberRepository: MemberRepository, +) { + fun findMemberCouponById(memberId: Long): Member { + return toMember( + memberRepository.findByIdOrNull(memberId) + ?: throw IllegalArgumentException("Member not found") + ) + } + + @Transactional + fun update(member: Member) { + memberRepository.save(fromCouponMember(member)) + } +} diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/ShopOwnerPersistenceAdapter.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/ShopOwnerPersistenceAdapter.kt similarity index 69% rename from coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/ShopOwnerPersistenceAdapter.kt rename to coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/ShopOwnerPersistenceAdapter.kt index 2899838..22b11ad 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/ShopOwnerPersistenceAdapter.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/ShopOwnerPersistenceAdapter.kt @@ -1,11 +1,11 @@ -package com.example.estdelivery.coupon.application.port.out.persistence +package com.example.estdelivery.coupon.application.port.out.adapter.persistence import com.example.estdelivery.coupon.application.port.out.LoadShopOwnerStatePort import com.example.estdelivery.coupon.application.port.out.UpdateShopOwnerStatePort -import com.example.estdelivery.coupon.application.port.out.persistence.mapper.fromShopOwner -import com.example.estdelivery.coupon.application.port.out.persistence.mapper.toShopOwner -import com.example.estdelivery.coupon.application.port.out.persistence.repository.ShopOwnerRepository -import com.example.estdelivery.coupon.application.port.out.persistence.repository.ShopRepository +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.mapper.fromShopOwner +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.mapper.toShopOwner +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.repository.ShopOwnerRepository +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.repository.ShopRepository import com.example.estdelivery.coupon.domain.shop.ShopOwner import jakarta.transaction.Transactional import org.springframework.stereotype.Component diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/entity/CouponEntity.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/CouponEntity.kt similarity index 88% rename from coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/entity/CouponEntity.kt rename to coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/CouponEntity.kt index 8c0e4d7..ab6aeb5 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/entity/CouponEntity.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/CouponEntity.kt @@ -1,4 +1,4 @@ -package com.example.estdelivery.coupon.application.port.out.persistence.entity +package com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity import jakarta.persistence.Column import jakarta.persistence.Entity diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/CouponStateAmountType.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/CouponStateAmountType.kt new file mode 100644 index 0000000..13fbbc4 --- /dev/null +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/CouponStateAmountType.kt @@ -0,0 +1,6 @@ +package com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity + +enum class CouponStateAmountType { + RATE, + FIX, +} diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/CouponStateType.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/CouponStateType.kt new file mode 100644 index 0000000..47a937f --- /dev/null +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/CouponStateType.kt @@ -0,0 +1,7 @@ +package com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity + +enum class CouponStateType { + PUBLISHED, + HANDOUT, + EVENT, +} diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/entity/MemberEntity.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/MemberCouponEntity.kt similarity index 86% rename from coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/entity/MemberEntity.kt rename to coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/MemberCouponEntity.kt index 9bc95ef..eac949f 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/entity/MemberEntity.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/MemberCouponEntity.kt @@ -1,4 +1,4 @@ -package com.example.estdelivery.coupon.application.port.out.persistence.entity +package com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity import jakarta.persistence.Column import jakarta.persistence.Entity @@ -12,8 +12,7 @@ import jakarta.persistence.Table @Entity @Table(name = "member") -class MemberEntity( - var name: String, +class MemberCouponEntity( @ManyToMany( targetEntity = CouponEntity::class ) diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/entity/ShopEntity.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/ShopEntity.kt similarity index 91% rename from coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/entity/ShopEntity.kt rename to coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/ShopEntity.kt index c3933c1..9cf90da 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/entity/ShopEntity.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/ShopEntity.kt @@ -1,4 +1,4 @@ -package com.example.estdelivery.coupon.application.port.out.persistence.entity +package com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity import jakarta.persistence.Column import jakarta.persistence.Entity @@ -50,14 +50,14 @@ class ShopEntity( ) var usedCouponBook: List, @ManyToMany( - targetEntity = MemberEntity::class + targetEntity = MemberCouponEntity::class ) @JoinTable( name = "use_coupon_book", joinColumns = [JoinColumn(name = "shop_id")], inverseJoinColumns = [JoinColumn(name = "member_id")], ) - var royalCustomers: List, + var royalCustomers: List, var name: String, @Id @Column(name = "shop_id") diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/entity/ShopOwnerEntity.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/ShopOwnerEntity.kt similarity index 84% rename from coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/entity/ShopOwnerEntity.kt rename to coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/ShopOwnerEntity.kt index a510fd9..991e243 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/entity/ShopOwnerEntity.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/ShopOwnerEntity.kt @@ -1,4 +1,4 @@ -package com.example.estdelivery.coupon.application.port.out.persistence.entity +package com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity import jakarta.persistence.Entity import jakarta.persistence.GeneratedValue diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/mapper/CouponMapper.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/mapper/CouponMapper.kt similarity index 66% rename from coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/mapper/CouponMapper.kt rename to coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/mapper/CouponMapper.kt index 1241ec0..93ca230 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/mapper/CouponMapper.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/mapper/CouponMapper.kt @@ -1,13 +1,13 @@ -package com.example.estdelivery.coupon.application.port.out.persistence.mapper +package com.example.estdelivery.coupon.application.port.out.adapter.persistence.mapper -import com.example.estdelivery.coupon.application.port.out.persistence.entity.CouponEntity -import com.example.estdelivery.coupon.application.port.out.persistence.entity.CouponStateAmountType -import com.example.estdelivery.coupon.application.port.out.persistence.entity.CouponStateAmountType.FIX -import com.example.estdelivery.coupon.application.port.out.persistence.entity.CouponStateAmountType.RATE -import com.example.estdelivery.coupon.application.port.out.persistence.entity.CouponStateType -import com.example.estdelivery.coupon.application.port.out.persistence.entity.CouponStateType.EVENT -import com.example.estdelivery.coupon.application.port.out.persistence.entity.CouponStateType.HANDOUT -import com.example.estdelivery.coupon.application.port.out.persistence.entity.CouponStateType.PUBLISHED +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity.CouponEntity +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity.CouponStateAmountType +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity.CouponStateAmountType.FIX +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity.CouponStateAmountType.RATE +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity.CouponStateType +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity.CouponStateType.EVENT +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity.CouponStateType.HANDOUT +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity.CouponStateType.PUBLISHED import com.example.estdelivery.coupon.domain.coupon.Coupon import com.example.estdelivery.coupon.domain.coupon.CouponType.IS_EVENT import com.example.estdelivery.coupon.domain.coupon.CouponType.IS_HAND_OUT diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/mapper/MemberMapper.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/mapper/MemberMapper.kt similarity index 58% rename from coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/mapper/MemberMapper.kt rename to coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/mapper/MemberMapper.kt index 659fd22..52f4198 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/mapper/MemberMapper.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/mapper/MemberMapper.kt @@ -1,23 +1,22 @@ -package com.example.estdelivery.coupon.application.port.out.persistence.mapper +package com.example.estdelivery.coupon.application.port.out.adapter.persistence.mapper -import com.example.estdelivery.coupon.application.port.out.persistence.entity.MemberEntity +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity.MemberCouponEntity import com.example.estdelivery.coupon.domain.coupon.CouponBook import com.example.estdelivery.coupon.domain.member.Member import com.example.estdelivery.coupon.domain.member.UnusedCouponBook -internal fun fromMember(member: Member): MemberEntity { - return MemberEntity( - member.name, +internal fun fromCouponMember(member: Member): MemberCouponEntity { + return MemberCouponEntity( member.showMyCouponBook() .map { fromCoupon(it) }, member.id, ) } -internal fun toMember(entity: MemberEntity): Member { +internal fun toMember(entity: MemberCouponEntity): Member { return Member( entity.id!!, - entity.name, + "", UnusedCouponBook( CouponBook(entity.unusedCoupons.map { toCoupon( diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/mapper/ShopOwnerMapper.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/mapper/ShopOwnerMapper.kt similarity index 86% rename from coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/mapper/ShopOwnerMapper.kt rename to coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/mapper/ShopOwnerMapper.kt index d8d6ad5..884e1ea 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/mapper/ShopOwnerMapper.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/mapper/ShopOwnerMapper.kt @@ -1,7 +1,7 @@ -package com.example.estdelivery.coupon.application.port.out.persistence.mapper +package com.example.estdelivery.coupon.application.port.out.adapter.persistence.mapper -import com.example.estdelivery.coupon.application.port.out.persistence.entity.ShopEntity -import com.example.estdelivery.coupon.application.port.out.persistence.entity.ShopOwnerEntity +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity.ShopEntity +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity.ShopOwnerEntity import com.example.estdelivery.coupon.domain.coupon.CouponBook import com.example.estdelivery.coupon.domain.shop.HandOutCouponBook import com.example.estdelivery.coupon.domain.shop.PublishedCouponBook @@ -65,7 +65,7 @@ internal fun fromShop(shop: Shop): ShopEntity { shop.showUsedCoupons() .map { fromCoupon(it) }, shop.showRoyalCustomers() - .map { fromMember(it) }, + .map { fromCouponMember(it) }, shop.name, shop.id, ) diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/repository/CouponRepository.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/repository/CouponRepository.kt new file mode 100644 index 0000000..f36b57c --- /dev/null +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/repository/CouponRepository.kt @@ -0,0 +1,7 @@ +package com.example.estdelivery.coupon.application.port.out.adapter.persistence.repository + +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity.CouponEntity +import org.springframework.data.jpa.repository.JpaRepository + +interface CouponRepository : + JpaRepository diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/repository/MemberRepository.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/repository/MemberRepository.kt new file mode 100644 index 0000000..12c877e --- /dev/null +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/repository/MemberRepository.kt @@ -0,0 +1,7 @@ +package com.example.estdelivery.coupon.application.port.out.adapter.persistence.repository + +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity.MemberCouponEntity +import org.springframework.data.jpa.repository.JpaRepository + +interface MemberRepository : + JpaRepository diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/repository/ShopOwnerRepository.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/repository/ShopOwnerRepository.kt new file mode 100644 index 0000000..b38a22c --- /dev/null +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/repository/ShopOwnerRepository.kt @@ -0,0 +1,11 @@ +package com.example.estdelivery.coupon.application.port.out.adapter.persistence.repository + +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity.ShopEntity +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity.ShopOwnerEntity +import java.util.Optional +import org.springframework.data.jpa.repository.JpaRepository + +interface ShopOwnerRepository : + JpaRepository { + fun findByShopEntity(shopEntity: ShopEntity): Optional +} diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/repository/ShopRepository.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/repository/ShopRepository.kt new file mode 100644 index 0000000..2c0616e --- /dev/null +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/repository/ShopRepository.kt @@ -0,0 +1,7 @@ +package com.example.estdelivery.coupon.application.port.out.adapter.persistence.repository + +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity.ShopEntity +import org.springframework.data.jpa.repository.JpaRepository + +interface ShopRepository : + JpaRepository diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/infra/dto/MemberState.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/infra/dto/MemberState.kt deleted file mode 100644 index b99a635..0000000 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/infra/dto/MemberState.kt +++ /dev/null @@ -1,6 +0,0 @@ -package com.example.estdelivery.coupon.application.port.out.infra.dto - -class MemberState( - val id: Long, - val name: String, -) diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/infra/dto/ShopOwnerState.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/infra/dto/ShopOwnerState.kt deleted file mode 100644 index 1c39449..0000000 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/infra/dto/ShopOwnerState.kt +++ /dev/null @@ -1,6 +0,0 @@ -package com.example.estdelivery.coupon.application.port.out.infra.dto - -class ShopOwnerState( - val shop: ShopState, - val id: Long, -) diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/MemberPersistenceAdapter.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/MemberPersistenceAdapter.kt deleted file mode 100644 index 2ad9757..0000000 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/MemberPersistenceAdapter.kt +++ /dev/null @@ -1,29 +0,0 @@ -package com.example.estdelivery.coupon.application.port.out.persistence - -import com.example.estdelivery.coupon.application.port.out.LoadMemberStatePort -import com.example.estdelivery.coupon.application.port.out.UpdateMemberStatePort -import com.example.estdelivery.coupon.application.port.out.persistence.mapper.fromMember -import com.example.estdelivery.coupon.application.port.out.persistence.mapper.toMember -import com.example.estdelivery.coupon.application.port.out.persistence.repository.MemberRepository -import com.example.estdelivery.coupon.domain.member.Member -import jakarta.transaction.Transactional -import org.springframework.stereotype.Component - -@Component -class MemberPersistenceAdapter( - private val memberRepository: MemberRepository, -) : LoadMemberStatePort, - UpdateMemberStatePort { - override fun findById(memberId: Long): Member { - return toMember( - memberRepository.findById( - memberId - ).orElseThrow() - ) - } - - @Transactional - override fun update(member: Member) { - memberRepository.save(fromMember(member)) - } -} diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/entity/CouponStateAmountType.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/entity/CouponStateAmountType.kt deleted file mode 100644 index 6c17833..0000000 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/entity/CouponStateAmountType.kt +++ /dev/null @@ -1,6 +0,0 @@ -package com.example.estdelivery.coupon.application.port.out.persistence.entity - -enum class CouponStateAmountType { - RATE, - FIX, -} diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/entity/CouponStateType.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/entity/CouponStateType.kt deleted file mode 100644 index 7051a57..0000000 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/entity/CouponStateType.kt +++ /dev/null @@ -1,7 +0,0 @@ -package com.example.estdelivery.coupon.application.port.out.persistence.entity - -enum class CouponStateType { - PUBLISHED, - HANDOUT, - EVENT, -} diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/repository/CouponRepository.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/repository/CouponRepository.kt deleted file mode 100644 index 25dc4df..0000000 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/repository/CouponRepository.kt +++ /dev/null @@ -1,7 +0,0 @@ -package com.example.estdelivery.coupon.application.port.out.persistence.repository - -import com.example.estdelivery.coupon.application.port.out.persistence.entity.CouponEntity -import org.springframework.data.jpa.repository.JpaRepository - -interface CouponRepository : - JpaRepository diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/repository/MemberRepository.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/repository/MemberRepository.kt deleted file mode 100644 index ae90273..0000000 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/repository/MemberRepository.kt +++ /dev/null @@ -1,7 +0,0 @@ -package com.example.estdelivery.coupon.application.port.out.persistence.repository - -import com.example.estdelivery.coupon.application.port.out.persistence.entity.MemberEntity -import org.springframework.data.jpa.repository.JpaRepository - -interface MemberRepository : - JpaRepository diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/repository/ShopOwnerRepository.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/repository/ShopOwnerRepository.kt deleted file mode 100644 index 879a75f..0000000 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/repository/ShopOwnerRepository.kt +++ /dev/null @@ -1,11 +0,0 @@ -package com.example.estdelivery.coupon.application.port.out.persistence.repository - -import com.example.estdelivery.coupon.application.port.out.persistence.entity.ShopEntity -import com.example.estdelivery.coupon.application.port.out.persistence.entity.ShopOwnerEntity -import java.util.Optional -import org.springframework.data.jpa.repository.JpaRepository - -interface ShopOwnerRepository : - JpaRepository { - fun findByShopEntity(shopEntity: ShopEntity): Optional -} diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/repository/ShopRepository.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/repository/ShopRepository.kt deleted file mode 100644 index 5cb4307..0000000 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/repository/ShopRepository.kt +++ /dev/null @@ -1,7 +0,0 @@ -package com.example.estdelivery.coupon.application.port.out.persistence.repository - -import com.example.estdelivery.coupon.application.port.out.persistence.entity.ShopEntity -import org.springframework.data.jpa.repository.JpaRepository - -interface ShopRepository : - JpaRepository diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/member/Member.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/member/Member.kt index 22a03a0..79104a1 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/member/Member.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/member/Member.kt @@ -5,7 +5,7 @@ import com.example.estdelivery.coupon.domain.coupon.Coupon class Member( val id: Long, val name: String, - private val unusedCouponBook: UnusedCouponBook = UnusedCouponBook(), + private var unusedCouponBook: UnusedCouponBook = UnusedCouponBook(), ) { fun useCoupon(coupon: Coupon) { unusedCouponBook.removeUsedCoupon(coupon) @@ -47,4 +47,8 @@ class Member( override fun toString(): String { return "Member(id=$id, name='$name', unUsedCouponBook=$unusedCouponBook)" } + + fun have(membersCoupon: Member): Member { + return Member(id, name, membersCoupon.unusedCouponBook) + } } diff --git a/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/GiftCouponServiceTest.kt b/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/GiftCouponServiceTest.kt index d8fa883..38a9e37 100644 --- a/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/GiftCouponServiceTest.kt +++ b/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/GiftCouponServiceTest.kt @@ -50,10 +50,10 @@ class GiftCouponServiceTest : FreeSpec({ val giftCouponCommand = GiftCouponCommand(1L, 주는자.id, 받는자.id) - every { loadMemberStatePort.findById(주는자.id) } returns 주는자 - every { loadMemberStatePort.findById(받는자.id) } returns 받는자 + every { loadMemberStatePort.findMember(주는자.id) } returns 주는자 + every { loadMemberStatePort.findMember(받는자.id) } returns 받는자 every { loadCouponStatePort.findById(giftCouponCommand.couponId) } returns coupon - every { updateMemberStatePort.update(any()) } returns Unit + every { updateMemberStatePort.updateMembersCoupon(any()) } returns Unit // when & then shouldNotThrow { @@ -68,8 +68,8 @@ class GiftCouponServiceTest : FreeSpec({ val giftCouponCommand = GiftCouponCommand(1L, 주는자.id, 받는자.id) - every { loadMemberStatePort.findById(주는자.id) } returns 주는자 - every { loadMemberStatePort.findById(받는자.id) } returns 받는자 + every { loadMemberStatePort.findMember(주는자.id) } returns 주는자 + every { loadMemberStatePort.findMember(받는자.id) } returns 받는자 // when & then shouldThrow { @@ -94,8 +94,8 @@ class GiftCouponServiceTest : FreeSpec({ val giftCouponCommand = GiftCouponCommand(1L, 주는자.id, 주는자.id) - every { loadMemberStatePort.findById(주는자.id) } returns 주는자 - every { loadMemberStatePort.findById(주는자.id) } returns 주는자 + every { loadMemberStatePort.findMember(주는자.id) } returns 주는자 + every { loadMemberStatePort.findMember(주는자.id) } returns 주는자 // when & then shouldThrow { diff --git a/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/IssuePublishedCouponServiceTest.kt b/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/IssuePublishedCouponServiceTest.kt index cdbc9d6..54bda1f 100644 --- a/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/IssuePublishedCouponServiceTest.kt +++ b/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/IssuePublishedCouponServiceTest.kt @@ -70,10 +70,10 @@ class IssuePublishedCouponServiceTest : FreeSpec({ val 프리퍼_주인_상태 = ShopOwner(가게, 1L) val 변경된_프리퍼_주인_상태 = slot() val 변경된_회원_상태 = slot() - every { loadMemberStatePort.findById(memberId) } returns 회원_상태 + every { loadMemberStatePort.findMember(memberId) } returns 회원_상태 every { loadCouponStatePort.findById(couponId) } returns 할인쿠폰 every { loadShopOwnerStatePort.findByShopId(shopId) } returns 프리퍼_주인_상태 - every { updateMemberStatePort.update(capture(변경된_회원_상태)) } returns Unit + every { updateMemberStatePort.updateMembersCoupon(capture(변경된_회원_상태)) } returns Unit every { updateShopOwnerStatePort.update(capture(변경된_프리퍼_주인_상태)) } returns Unit // when @@ -99,9 +99,9 @@ class IssuePublishedCouponServiceTest : FreeSpec({ ) val 프리퍼_주인_상태 = ShopOwner(게시된_쿠폰이_있는_프리퍼(게시된_고정_할인_쿠폰), 1L) - every { loadMemberStatePort.findById(memberId) } returns 회원_상태 + every { loadMemberStatePort.findMember(memberId) } returns 회원_상태 every { loadCouponStatePort.findById(couponId) } returns 게시된_고정_할인_쿠폰 - every { updateMemberStatePort.update(any()) } returns Unit + every { updateMemberStatePort.updateMembersCoupon(any()) } returns Unit every { loadShopOwnerStatePort.findByShopId(shopId) } returns 프리퍼_주인_상태 every { updateShopOwnerStatePort.update(any()) } returns Unit @@ -127,9 +127,9 @@ class IssuePublishedCouponServiceTest : FreeSpec({ ) val 프리퍼_주인_상태 = ShopOwner(새로_창업해서_아무것도_없는_프리퍼(), 1L) - every { loadMemberStatePort.findById(memberId) } returns 회원_상태 + every { loadMemberStatePort.findMember(memberId) } returns 회원_상태 every { loadCouponStatePort.findById(couponId) } returns 할인쿠폰 - every { updateMemberStatePort.update(any()) } returns Unit + every { updateMemberStatePort.updateMembersCoupon(any()) } returns Unit every { loadShopOwnerStatePort.findByShopId(shopId) } returns 프리퍼_주인_상태 every { updateShopOwnerStatePort.update(any()) } returns Unit diff --git a/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/IssueRandomCouponServiceTest.kt b/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/IssueRandomCouponServiceTest.kt index 85c7e6d..f46c7e0 100644 --- a/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/IssueRandomCouponServiceTest.kt +++ b/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/IssueRandomCouponServiceTest.kt @@ -86,10 +86,10 @@ class IssueRandomCouponServiceTest : FreeSpec({ val 이건창 = 이건창() // when - every { loadMemberStatePort.findById(1L) } returns 이건창 + every { loadMemberStatePort.findMember(1L) } returns 이건창 every { loadRandomCouponIssueEventStatePort.findById(1L) } returns 랜덤_쿠폰_뽑기_이벤트_상태 every { loadShopOwnerStatePort.findByShopId(1L) } returns 가게주인 - every { updateMemberStatePort.update(capture(상태가_변경된_회원)) } returns Unit + every { updateMemberStatePort.updateMembersCoupon(capture(상태가_변경된_회원)) } returns Unit every { updateShopOwnerStatePort.update(capture(상태가_변경된_가게주인)) } returns Unit every { updateRandomCouponIssueEventStatePort.update(capture(상태가_변경된_이벤트)) } returns Unit every { createCouponStatePort.create(any()) } returns 이벤트_쿠폰 diff --git a/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/UseCouponServiceTest.kt b/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/UseCouponServiceTest.kt index 890fa3b..8798b80 100644 --- a/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/UseCouponServiceTest.kt +++ b/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/UseCouponServiceTest.kt @@ -59,10 +59,10 @@ class UseCouponServiceTest : FreeSpec({ val 변경된_프리퍼_주인_상태 = slot() // when - every { loadMemberStatePort.findById(memberId) } returns 회원 + every { loadMemberStatePort.findMember(memberId) } returns 회원 every { loadCouponStatePort.findById(couponId) } returns 나눠준_비율_할인_쿠폰 every { loadShopOwnerStatePort.findByShopId(shopId) } returns 프리퍼_주인 - every { updateMemberStatePort.update(capture(변경된_회원_상태)) } returns Unit + every { updateMemberStatePort.updateMembersCoupon(capture(변경된_회원_상태)) } returns Unit every { updateShopOwnerStatePort.update(capture(변경된_프리퍼_주인_상태)) } returns Unit useCouponService.useCoupon(useCouponCommand) diff --git a/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/infra/EventInfraAdapterTest.kt b/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/EventInfraAdapterTest.kt similarity index 84% rename from coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/infra/EventInfraAdapterTest.kt rename to coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/EventInfraAdapterTest.kt index a1d7112..3f30ad8 100644 --- a/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/infra/EventInfraAdapterTest.kt +++ b/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/EventInfraAdapterTest.kt @@ -1,6 +1,6 @@ -package com.example.estdelivery.coupon.application.port.out.infra +package com.example.estdelivery.coupon.application.port.out.adapter.infra -import com.example.estdelivery.coupon.application.port.out.infra.dto.UpdateParticipatedMembersState +import com.example.estdelivery.coupon.application.port.out.adapter.infra.dto.UpdateParticipatedMembersState import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Test import org.springframework.beans.factory.annotation.Autowired diff --git a/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/infra/MemberInfraAdapterTest.kt b/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/MemberInfraAdapterTest.kt similarity index 92% rename from coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/infra/MemberInfraAdapterTest.kt rename to coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/MemberInfraAdapterTest.kt index 554c75e..f56cbd2 100644 --- a/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/infra/MemberInfraAdapterTest.kt +++ b/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/MemberInfraAdapterTest.kt @@ -1,4 +1,4 @@ -package com.example.estdelivery.coupon.application.port.out.infra +package com.example.estdelivery.coupon.application.port.out.adapter.infra import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test diff --git a/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/infra/ShopInfraAdapterTest.kt b/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/ShopInfraAdapterTest.kt similarity index 93% rename from coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/infra/ShopInfraAdapterTest.kt rename to coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/ShopInfraAdapterTest.kt index 63bd82c..5f1baa5 100644 --- a/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/infra/ShopInfraAdapterTest.kt +++ b/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/ShopInfraAdapterTest.kt @@ -1,4 +1,4 @@ -package com.example.estdelivery.coupon.application.port.out.infra +package com.example.estdelivery.coupon.application.port.out.adapter.infra import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test diff --git a/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/CouponPersistenceAdapterTest.kt b/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/CouponPersistenceAdapterTest.kt similarity index 89% rename from coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/CouponPersistenceAdapterTest.kt rename to coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/CouponPersistenceAdapterTest.kt index 3b36aae..3e7b008 100644 --- a/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/CouponPersistenceAdapterTest.kt +++ b/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/CouponPersistenceAdapterTest.kt @@ -1,7 +1,7 @@ -package com.example.estdelivery.coupon.application.port.out.persistence +package com.example.estdelivery.coupon.application.port.out.adapter.persistence -import com.example.estdelivery.coupon.application.port.out.persistence.mapper.fromCoupon -import com.example.estdelivery.coupon.application.port.out.persistence.repository.CouponRepository +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.mapper.fromCoupon +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.repository.CouponRepository import com.example.estdelivery.coupon.domain.fixture.게시된_고정_할인_쿠폰 import com.example.estdelivery.coupon.domain.fixture.게시할_쿠폰 import io.kotest.assertions.throwables.shouldThrow diff --git a/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/MemberPersistenceAdapterTest.kt b/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/MemberPersistenceAdapterTest.kt similarity index 71% rename from coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/MemberPersistenceAdapterTest.kt rename to coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/MemberPersistenceAdapterTest.kt index 4d197e5..2b7a709 100644 --- a/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/MemberPersistenceAdapterTest.kt +++ b/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/MemberPersistenceAdapterTest.kt @@ -1,7 +1,7 @@ -package com.example.estdelivery.coupon.application.port.out.persistence +package com.example.estdelivery.coupon.application.port.out.adapter.persistence -import com.example.estdelivery.coupon.application.port.out.persistence.mapper.fromMember -import com.example.estdelivery.coupon.application.port.out.persistence.repository.MemberRepository +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.mapper.fromCouponMember +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.repository.MemberRepository import com.example.estdelivery.coupon.domain.fixture.나눠준_비율_할인_쿠폰 import com.example.estdelivery.coupon.domain.fixture.일건창 import io.kotest.assertions.throwables.shouldThrow @@ -26,11 +26,11 @@ class MemberPersistenceAdapterTest : FreeSpec({ "회원을 찾는다." { // given val memberId = 1L - val memberEntity = fromMember(일건창()) + val memberEntity = fromCouponMember(일건창()) // when every { memberRepository.findById(memberId) } returns Optional.of(memberEntity) - val member = memberPersistenceAdapter.findById(memberId) + val member = memberPersistenceAdapter.findMemberCouponById(memberId) // then member shouldBe 일건창() @@ -44,8 +44,8 @@ class MemberPersistenceAdapterTest : FreeSpec({ every { memberRepository.findById(memberId) } returns Optional.empty() // then - shouldThrow { - memberPersistenceAdapter.findById(memberId) + shouldThrow { + memberPersistenceAdapter.findMemberCouponById(memberId) } } } @@ -57,9 +57,7 @@ class MemberPersistenceAdapterTest : FreeSpec({ // when member.receiveCoupon(나눠준_비율_할인_쿠폰) - every { memberRepository.save(any()) } returns fromMember( - member - ) + every { memberRepository.save(any()) } returns fromCouponMember(member) memberPersistenceAdapter.update(member) diff --git a/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/ShopOwnerPersistenceAdapterTest.kt b/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/ShopOwnerPersistenceAdapterTest.kt similarity index 88% rename from coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/ShopOwnerPersistenceAdapterTest.kt rename to coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/ShopOwnerPersistenceAdapterTest.kt index eb9b082..a4f069a 100644 --- a/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/persistence/ShopOwnerPersistenceAdapterTest.kt +++ b/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/ShopOwnerPersistenceAdapterTest.kt @@ -1,10 +1,10 @@ -package com.example.estdelivery.coupon.application.port.out.persistence +package com.example.estdelivery.coupon.application.port.out.adapter.persistence -import com.example.estdelivery.coupon.application.port.out.persistence.mapper.fromShop -import com.example.estdelivery.coupon.application.port.out.persistence.mapper.fromShopOwner -import com.example.estdelivery.coupon.application.port.out.persistence.mapper.toShopOwner -import com.example.estdelivery.coupon.application.port.out.persistence.repository.ShopOwnerRepository -import com.example.estdelivery.coupon.application.port.out.persistence.repository.ShopRepository +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.mapper.fromShop +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.mapper.fromShopOwner +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.mapper.toShopOwner +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.repository.ShopOwnerRepository +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.repository.ShopRepository import com.example.estdelivery.coupon.domain.fixture.게시할_쿠폰 import com.example.estdelivery.coupon.domain.fixture.새로_창업해서_아무것도_없는_프리퍼 import com.example.estdelivery.coupon.domain.fixture.이건창 From 55391a0d6e2bfb8cad2e8a370a901041ddc21116 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EA=B1=B4=EC=B0=BD?= <92219795+this-is-spear@users.noreply.github.com> Date: Wed, 15 May 2024 16:00:38 +0900 Subject: [PATCH 02/10] =?UTF-8?q?refactor=20:=20=EC=BF=A0=ED=8F=B0=20?= =?UTF-8?q?=EB=8F=84=EB=A9=94=EC=9D=B8=EC=97=90=EC=84=9C=20=ED=9A=8C?= =?UTF-8?q?=EC=9B=90=EC=9D=98=20=EC=BF=A0=ED=8F=B0=EB=B6=81=EB=A7=8C=20?= =?UTF-8?q?=EA=B4=80=EB=A6=AC=ED=95=98=EB=8F=84=EB=A1=9D=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../persistence/MemberPersistenceAdapter.kt | 40 +++++++---- .../persistence/entity/MemberCouponEntity.kt | 29 -------- .../adapter/persistence/entity/ShopEntity.kt | 13 ++-- .../persistence/entity/UnusedCouponEntity.kt | 21 ++++++ .../persistence/mapper/MemberMapper.kt | 28 -------- .../persistence/mapper/ShopOwnerMapper.kt | 8 +-- ...epository.kt => UnusedCouponRepository.kt} | 7 +- .../MemberPersistenceAdapterTest.kt | 68 ------------------ .../UnusedCouponPersistenceAdapterTest.kt | 72 +++++++++++++++++++ 9 files changed, 131 insertions(+), 155 deletions(-) delete mode 100644 coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/MemberCouponEntity.kt create mode 100644 coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/UnusedCouponEntity.kt delete mode 100644 coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/mapper/MemberMapper.kt rename coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/repository/{MemberRepository.kt => UnusedCouponRepository.kt} (53%) delete mode 100644 coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/MemberPersistenceAdapterTest.kt create mode 100644 coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/UnusedCouponPersistenceAdapterTest.kt diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/MemberPersistenceAdapter.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/MemberPersistenceAdapter.kt index 63cbf0a..08dfc3a 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/MemberPersistenceAdapter.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/MemberPersistenceAdapter.kt @@ -1,26 +1,38 @@ package com.example.estdelivery.coupon.application.port.out.adapter.persistence -import com.example.estdelivery.coupon.application.port.out.adapter.persistence.mapper.fromCouponMember -import com.example.estdelivery.coupon.application.port.out.adapter.persistence.mapper.toMember -import com.example.estdelivery.coupon.application.port.out.adapter.persistence.repository.MemberRepository -import com.example.estdelivery.coupon.domain.member.Member +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity.UnusedCouponEntity +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.UnusedCouponRepository +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 jakarta.transaction.Transactional -import org.springframework.data.repository.findByIdOrNull import org.springframework.stereotype.Component @Component class MemberPersistenceAdapter( - private val memberRepository: MemberRepository, + private val unusedCouponRepository: UnusedCouponRepository, ) { - fun findMemberCouponById(memberId: Long): Member { - return toMember( - memberRepository.findByIdOrNull(memberId) - ?: throw IllegalArgumentException("Member not found") - ) - } + fun findMemberCouponByMemberId(memberId: Long) = + unusedCouponRepository.findAllByMemberId(memberId) + .map { + toCoupon(it.coupon) + }.let { + UnusedCouponBook(CouponBook(it)) + } @Transactional - fun update(member: Member) { - memberRepository.save(fromCouponMember(member)) + fun updateUnusedCouponBook(memberId: Long, unusedCoupons: List) { + val collectedCoupons = unusedCouponRepository.findAllByMemberId(memberId) + .map { toCoupon(it.coupon) } + .toSet() + + unusedCoupons.filter { !collectedCoupons.contains(it) } + .map { + UnusedCouponEntity(memberId, fromCoupon(it)) + }.let { + unusedCouponRepository.saveAll(it) + } } } diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/MemberCouponEntity.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/MemberCouponEntity.kt deleted file mode 100644 index eac949f..0000000 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/MemberCouponEntity.kt +++ /dev/null @@ -1,29 +0,0 @@ -package com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity - -import jakarta.persistence.Column -import jakarta.persistence.Entity -import jakarta.persistence.GeneratedValue -import jakarta.persistence.GenerationType -import jakarta.persistence.Id -import jakarta.persistence.JoinColumn -import jakarta.persistence.JoinTable -import jakarta.persistence.ManyToMany -import jakarta.persistence.Table - -@Entity -@Table(name = "member") -class MemberCouponEntity( - @ManyToMany( - targetEntity = CouponEntity::class - ) - @JoinTable( - name = "member_coupon_book", - joinColumns = [JoinColumn(name = "member_id")], - inverseJoinColumns = [JoinColumn(name = "coupon_id")], - ) - var unusedCoupons: List, - @Id - @Column(name = "member_id") - @GeneratedValue(strategy = GenerationType.IDENTITY) - var id: Long? = null, -) diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/ShopEntity.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/ShopEntity.kt index 9cf90da..817eed7 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/ShopEntity.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/ShopEntity.kt @@ -1,6 +1,7 @@ package com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity import jakarta.persistence.Column +import jakarta.persistence.ElementCollection import jakarta.persistence.Entity import jakarta.persistence.GeneratedValue import jakarta.persistence.GenerationType @@ -49,15 +50,9 @@ class ShopEntity( inverseJoinColumns = [JoinColumn(name = "coupon_id")], ) var usedCouponBook: List, - @ManyToMany( - targetEntity = MemberCouponEntity::class - ) - @JoinTable( - name = "use_coupon_book", - joinColumns = [JoinColumn(name = "shop_id")], - inverseJoinColumns = [JoinColumn(name = "member_id")], - ) - var royalCustomers: List, + //TODO 쿠폰 도메인에서 회원 정보는 Long 으로 관리 + @ElementCollection + var royalCustomers: List, var name: String, @Id @Column(name = "shop_id") diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/UnusedCouponEntity.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/UnusedCouponEntity.kt new file mode 100644 index 0000000..6880fe6 --- /dev/null +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/UnusedCouponEntity.kt @@ -0,0 +1,21 @@ +package com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity + +import jakarta.persistence.Entity +import jakarta.persistence.GeneratedValue +import jakarta.persistence.GenerationType +import jakarta.persistence.Id +import jakarta.persistence.JoinColumn +import jakarta.persistence.OneToOne +import jakarta.persistence.Table + +@Entity +@Table(name = "member") +class UnusedCouponEntity( + val memberId: Long, + @OneToOne + @JoinColumn(name = "coupon_id") + val coupon: CouponEntity, + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + var id: Long? = null, +) diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/mapper/MemberMapper.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/mapper/MemberMapper.kt deleted file mode 100644 index 52f4198..0000000 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/mapper/MemberMapper.kt +++ /dev/null @@ -1,28 +0,0 @@ -package com.example.estdelivery.coupon.application.port.out.adapter.persistence.mapper - -import com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity.MemberCouponEntity -import com.example.estdelivery.coupon.domain.coupon.CouponBook -import com.example.estdelivery.coupon.domain.member.Member -import com.example.estdelivery.coupon.domain.member.UnusedCouponBook - -internal fun fromCouponMember(member: Member): MemberCouponEntity { - return MemberCouponEntity( - member.showMyCouponBook() - .map { fromCoupon(it) }, - member.id, - ) -} - -internal fun toMember(entity: MemberCouponEntity): Member { - return Member( - entity.id!!, - "", - UnusedCouponBook( - CouponBook(entity.unusedCoupons.map { - toCoupon( - it - ) - }), - ), - ) -} diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/mapper/ShopOwnerMapper.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/mapper/ShopOwnerMapper.kt index 884e1ea..ed4d6e1 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/mapper/ShopOwnerMapper.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/mapper/ShopOwnerMapper.kt @@ -3,6 +3,7 @@ package com.example.estdelivery.coupon.application.port.out.adapter.persistence. import com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity.ShopEntity import com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity.ShopOwnerEntity import com.example.estdelivery.coupon.domain.coupon.CouponBook +import com.example.estdelivery.coupon.domain.member.Member import com.example.estdelivery.coupon.domain.shop.HandOutCouponBook import com.example.estdelivery.coupon.domain.shop.PublishedCouponBook import com.example.estdelivery.coupon.domain.shop.PublishedEventCouponBook @@ -45,9 +46,8 @@ internal fun toShop(entity: ShopEntity): Shop { ) })), RoyalCustomers(entity.royalCustomers.map { - toMember( - it - ) + //TODO: Member를 어떻게 가져올지 고민해보기 + Member(it, "단골 손님") }), entity.name, entity.id!!, @@ -65,7 +65,7 @@ internal fun fromShop(shop: Shop): ShopEntity { shop.showUsedCoupons() .map { fromCoupon(it) }, shop.showRoyalCustomers() - .map { fromCouponMember(it) }, + .map { it.id }, shop.name, shop.id, ) diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/repository/MemberRepository.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/repository/UnusedCouponRepository.kt similarity index 53% rename from coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/repository/MemberRepository.kt rename to coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/repository/UnusedCouponRepository.kt index 12c877e..df31a31 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/repository/MemberRepository.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/repository/UnusedCouponRepository.kt @@ -1,7 +1,8 @@ package com.example.estdelivery.coupon.application.port.out.adapter.persistence.repository -import com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity.MemberCouponEntity +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity.UnusedCouponEntity import org.springframework.data.jpa.repository.JpaRepository -interface MemberRepository : - JpaRepository +interface UnusedCouponRepository : JpaRepository { + fun findAllByMemberId(memberId: Long): List +} diff --git a/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/MemberPersistenceAdapterTest.kt b/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/MemberPersistenceAdapterTest.kt deleted file mode 100644 index 2b7a709..0000000 --- a/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/MemberPersistenceAdapterTest.kt +++ /dev/null @@ -1,68 +0,0 @@ -package com.example.estdelivery.coupon.application.port.out.adapter.persistence - -import com.example.estdelivery.coupon.application.port.out.adapter.persistence.mapper.fromCouponMember -import com.example.estdelivery.coupon.application.port.out.adapter.persistence.repository.MemberRepository -import com.example.estdelivery.coupon.domain.fixture.나눠준_비율_할인_쿠폰 -import com.example.estdelivery.coupon.domain.fixture.일건창 -import io.kotest.assertions.throwables.shouldThrow -import io.kotest.core.spec.style.FreeSpec -import io.kotest.matchers.shouldBe -import io.mockk.every -import io.mockk.mockk -import io.mockk.verify -import java.util.Optional - -class MemberPersistenceAdapterTest : FreeSpec({ - - lateinit var memberRepository: MemberRepository - lateinit var memberPersistenceAdapter: MemberPersistenceAdapter - - beforeTest { - memberRepository = mockk() - memberPersistenceAdapter = MemberPersistenceAdapter(memberRepository) - } - - "findById" - { - "회원을 찾는다." { - // given - val memberId = 1L - val memberEntity = fromCouponMember(일건창()) - - // when - every { memberRepository.findById(memberId) } returns Optional.of(memberEntity) - val member = memberPersistenceAdapter.findMemberCouponById(memberId) - - // then - member shouldBe 일건창() - } - - "회원이 존재하지 않으면 예외가 발생한다." { - // given - val memberId = 1L - - // when - every { memberRepository.findById(memberId) } returns Optional.empty() - - // then - shouldThrow { - memberPersistenceAdapter.findMemberCouponById(memberId) - } - } - } - - "update" - { - "회원을 수정한다." { - // given - val member = 일건창() - // when - member.receiveCoupon(나눠준_비율_할인_쿠폰) - - every { memberRepository.save(any()) } returns fromCouponMember(member) - - memberPersistenceAdapter.update(member) - - // then - verify(exactly = 1) { memberRepository.save(any()) } - } - } -}) diff --git a/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/UnusedCouponPersistenceAdapterTest.kt b/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/UnusedCouponPersistenceAdapterTest.kt new file mode 100644 index 0000000..aff167b --- /dev/null +++ b/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/UnusedCouponPersistenceAdapterTest.kt @@ -0,0 +1,72 @@ +package com.example.estdelivery.coupon.application.port.out.adapter.persistence + +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity.UnusedCouponEntity +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.mapper.fromCoupon +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.repository.UnusedCouponRepository +import com.example.estdelivery.coupon.domain.fixture.나눠준_비율_할인_쿠폰 +import com.example.estdelivery.coupon.domain.fixture.일건창 +import io.kotest.core.spec.style.FreeSpec +import io.kotest.matchers.shouldBe +import io.mockk.every +import io.mockk.mockk +import io.mockk.slot + +class UnusedCouponPersistenceAdapterTest : FreeSpec({ + + lateinit var unusedCouponRepository: UnusedCouponRepository + lateinit var memberPersistenceAdapter: MemberPersistenceAdapter + + beforeTest { + unusedCouponRepository = mockk() + memberPersistenceAdapter = MemberPersistenceAdapter(unusedCouponRepository) + } + + "findById" - { + "회원이 가진 사용하지 않은 쿠폰을 찾는다." { + // given + val memberId = 1L + val unusedCouponBookEntity = UnusedCouponEntity(memberId, fromCoupon(나눠준_비율_할인_쿠폰), 1) + + // when + every { unusedCouponRepository.findAllByMemberId(memberId) } returns listOf(unusedCouponBookEntity) + val unusedCouponBook = memberPersistenceAdapter.findMemberCouponByMemberId(memberId) + + // then + unusedCouponBook.showUnusedCoupons().size shouldBe 1 + } + } + + "update" - { + "회원이 가진 사용하지 않은 쿠폰을 수정한다." { + // given + val member = 일건창() + // when + member.receiveCoupon(나눠준_비율_할인_쿠폰) + val savedCouponEntity = slot>() + + every { unusedCouponRepository.findAllByMemberId(any()) } returns emptyList() + every { unusedCouponRepository.saveAll(capture(savedCouponEntity)) } returns listOf(mockk()) + + memberPersistenceAdapter.updateUnusedCouponBook(member.id, member.showMyCouponBook()) + savedCouponEntity.captured.size shouldBe 1 + } + + "새롭게 등록하는 쿠폰만 등록한다" { + // given + val member = 일건창() + member.receiveCoupon(나눠준_비율_할인_쿠폰) + val savedCouponEntity = slot>() + + every { unusedCouponRepository.findAllByMemberId(member.id) } returns listOf( + UnusedCouponEntity(member.id, fromCoupon(나눠준_비율_할인_쿠폰), 1) + ) + every { unusedCouponRepository.saveAll(capture(savedCouponEntity)) } returns listOf(mockk()) + + // when + memberPersistenceAdapter.updateUnusedCouponBook(member.id, member.showMyCouponBook()) + + // then + savedCouponEntity.captured.size shouldBe 0 + } + } +}) From 6bc1f2a116571920f1c23ab724b76f91655b6332 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EA=B1=B4=EC=B0=BD?= <92219795+this-is-spear@users.noreply.github.com> Date: Wed, 15 May 2024 16:17:18 +0900 Subject: [PATCH 03/10] =?UTF-8?q?refactor=20:=20=ED=9A=8C=EC=9B=90=20?= =?UTF-8?q?=EA=B4=80=EB=A0=A8=20=EC=96=B4=EB=8C=91=ED=84=B0=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EA=B5=AC=ED=98=84=20=EB=B0=8F=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../port/out/adapter/MemberAdapter.kt | 6 +- .../persistence/MemberPersistenceAdapter.kt | 42 ++++++---- .../persistence/entity/MemberEntity.kt | 26 ++++++ .../persistence/entity/UnusedCouponEntity.kt | 21 ----- ...ouponRepository.kt => MemberRepository.kt} | 6 +- .../coupon/domain/member/Member.kt | 10 +-- .../port/out/adapter/MemberAdapterTest.kt | 81 +++++++++++++++++++ .../MemberPersistenceAdapterTest.kt | 51 ++++++++++++ .../UnusedCouponPersistenceAdapterTest.kt | 72 ----------------- 9 files changed, 191 insertions(+), 124 deletions(-) create mode 100644 coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/MemberEntity.kt delete mode 100644 coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/UnusedCouponEntity.kt rename coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/repository/{UnusedCouponRepository.kt => MemberRepository.kt} (53%) create mode 100644 coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/MemberAdapterTest.kt create mode 100644 coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/MemberPersistenceAdapterTest.kt delete mode 100644 coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/UnusedCouponPersistenceAdapterTest.kt diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/MemberAdapter.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/MemberAdapter.kt index 5acb0b1..eb3143b 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/MemberAdapter.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/MemberAdapter.kt @@ -11,14 +11,14 @@ import org.springframework.stereotype.Component class MemberAdapter( private val memberInfraAdapter: MemberInfraAdapter, private val memberPersistenceAdapter: MemberPersistenceAdapter, -): LoadMemberStatePort, UpdateMemberStatePort { +) : LoadMemberStatePort, UpdateMemberStatePort { override fun findMember(memberId: Long): Member { val member = memberInfraAdapter.findMember(memberId) - val membersCoupon = memberPersistenceAdapter.findMemberCouponById(memberId) + val membersCoupon = memberPersistenceAdapter.findUnusedCouponByMemberId(memberId) return member.have(membersCoupon) } override fun updateMembersCoupon(member: Member) { - memberPersistenceAdapter.update(member) + memberPersistenceAdapter.updateUnusedCouponBook(member.id, member.showMyCouponBook()) } } diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/MemberPersistenceAdapter.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/MemberPersistenceAdapter.kt index 08dfc3a..715e816 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/MemberPersistenceAdapter.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/MemberPersistenceAdapter.kt @@ -1,38 +1,46 @@ package com.example.estdelivery.coupon.application.port.out.adapter.persistence -import com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity.UnusedCouponEntity +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity.MemberEntity 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.UnusedCouponRepository +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.repository.MemberRepository 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 jakarta.transaction.Transactional +import org.springframework.data.repository.findByIdOrNull import org.springframework.stereotype.Component +import org.springframework.transaction.annotation.Transactional @Component class MemberPersistenceAdapter( - private val unusedCouponRepository: UnusedCouponRepository, + private val memberRepository: MemberRepository ) { - fun findMemberCouponByMemberId(memberId: Long) = - unusedCouponRepository.findAllByMemberId(memberId) - .map { - toCoupon(it.coupon) + /** + * 회원이 사용하지 않은 쿠폰 정보를 조회한다. 회원 정보가 없는 경우 회원 정보를 새롭게 추가한다. + */ + @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())) + } @Transactional fun updateUnusedCouponBook(memberId: Long, unusedCoupons: List) { - val collectedCoupons = unusedCouponRepository.findAllByMemberId(memberId) - .map { toCoupon(it.coupon) } - .toSet() - - unusedCoupons.filter { !collectedCoupons.contains(it) } + val memberEntity = memberRepository.findByIdOrNull(memberId) ?: return + memberEntity.unusedCoupons = unusedCoupons .map { - UnusedCouponEntity(memberId, fromCoupon(it)) - }.let { - unusedCouponRepository.saveAll(it) - } + fromCoupon(it) + }.toList() } } diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/MemberEntity.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/MemberEntity.kt new file mode 100644 index 0000000..1dab6ea --- /dev/null +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/MemberEntity.kt @@ -0,0 +1,26 @@ +package com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity + +import jakarta.persistence.Column +import jakarta.persistence.Entity +import jakarta.persistence.Id +import jakarta.persistence.JoinColumn +import jakarta.persistence.JoinTable +import jakarta.persistence.ManyToMany +import jakarta.persistence.Table + +@Entity +@Table(name = "member") +class MemberEntity( + @ManyToMany( + targetEntity = CouponEntity::class, + ) + @JoinTable( + name = "unused_coupon_book", + joinColumns = [JoinColumn(name = "member_id")], + inverseJoinColumns = [JoinColumn(name = "coupon_id")], + ) + var unusedCoupons: List, + @Id + @Column(name = "member_id") + val id: Long, +) diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/UnusedCouponEntity.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/UnusedCouponEntity.kt deleted file mode 100644 index 6880fe6..0000000 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/UnusedCouponEntity.kt +++ /dev/null @@ -1,21 +0,0 @@ -package com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity - -import jakarta.persistence.Entity -import jakarta.persistence.GeneratedValue -import jakarta.persistence.GenerationType -import jakarta.persistence.Id -import jakarta.persistence.JoinColumn -import jakarta.persistence.OneToOne -import jakarta.persistence.Table - -@Entity -@Table(name = "member") -class UnusedCouponEntity( - val memberId: Long, - @OneToOne - @JoinColumn(name = "coupon_id") - val coupon: CouponEntity, - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - var id: Long? = null, -) diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/repository/UnusedCouponRepository.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/repository/MemberRepository.kt similarity index 53% rename from coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/repository/UnusedCouponRepository.kt rename to coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/repository/MemberRepository.kt index df31a31..8002220 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/repository/UnusedCouponRepository.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/repository/MemberRepository.kt @@ -1,8 +1,6 @@ package com.example.estdelivery.coupon.application.port.out.adapter.persistence.repository -import com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity.UnusedCouponEntity +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity.MemberEntity import org.springframework.data.jpa.repository.JpaRepository -interface UnusedCouponRepository : JpaRepository { - fun findAllByMemberId(memberId: Long): List -} +interface MemberRepository : JpaRepository diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/member/Member.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/member/Member.kt index 79104a1..8447be9 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/member/Member.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/member/Member.kt @@ -5,7 +5,7 @@ import com.example.estdelivery.coupon.domain.coupon.Coupon class Member( val id: Long, val name: String, - private var unusedCouponBook: UnusedCouponBook = UnusedCouponBook(), + private val unusedCouponBook: UnusedCouponBook = UnusedCouponBook(), ) { fun useCoupon(coupon: Coupon) { unusedCouponBook.removeUsedCoupon(coupon) @@ -19,10 +19,6 @@ class Member( unusedCouponBook.addUnusedCoupon(coupon) } - fun hasCoupon(coupon: Coupon): Boolean { - return unusedCouponBook.showUnusedCoupons().contains(coupon) - } - fun sendCoupon( coupon: Coupon, target: Member, @@ -48,7 +44,7 @@ class Member( return "Member(id=$id, name='$name', unUsedCouponBook=$unusedCouponBook)" } - fun have(membersCoupon: Member): Member { - return Member(id, name, membersCoupon.unusedCouponBook) + fun have(unusedCouponBook: UnusedCouponBook): Member { + return Member(id, name, unusedCouponBook) } } diff --git a/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/MemberAdapterTest.kt b/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/MemberAdapterTest.kt new file mode 100644 index 0000000..ef21a6e --- /dev/null +++ b/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/MemberAdapterTest.kt @@ -0,0 +1,81 @@ +package com.example.estdelivery.coupon.application.port.out.adapter + +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity.MemberEntity +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.CouponRepository +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.repository.MemberRepository +import com.example.estdelivery.coupon.domain.coupon.Coupon +import com.example.estdelivery.coupon.domain.coupon.CouponType +import io.kotest.matchers.shouldBe +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.cloud.contract.stubrunner.spring.AutoConfigureStubRunner +import org.springframework.cloud.contract.stubrunner.spring.StubRunnerProperties + +@SpringBootTest +@AutoConfigureStubRunner( + stubsMode = StubRunnerProperties.StubsMode.LOCAL, + ids = ["com.example.estdelivery.member:member:1.0-SNAPSHOT:8081"] +) +class MemberAdapterTest( + @Autowired + private val memberAdapter: MemberAdapter, + @Autowired + private val memberRepository: MemberRepository, + @Autowired + private val couponRepository: CouponRepository, +) { + @BeforeEach + fun setUp() { + memberRepository.deleteAll() + couponRepository.deleteAll() + } + + @Test + fun `회원을 조회한다`() { + // given + val 할인_쿠폰 = couponRepository.save( + fromCoupon( + Coupon.RateDiscountCoupon( + 10, "10% 할인 쿠폰", "[USER_ID: 1][발급된 10% 할인 쿠폰]", CouponType.IS_PUBLISHED + ) + ) + ) + val 회원 = memberRepository.save(MemberEntity(listOf(할인_쿠폰), 1)) + + // when + val member = memberAdapter.findMember(회원.id) + + // then + member.id shouldBe 회원.id + member.showMyCouponBook().size shouldBe 회원.unusedCoupons.size + } + + @Test + fun `회원이 가진 쿠폰 정보를 수정한다`() { + // given + val 할인_쿠폰 = couponRepository.save( + fromCoupon( + Coupon.RateDiscountCoupon( + 10, "10% 할인 쿠폰", "[USER_ID: 1][발급된 10% 할인 쿠폰]", CouponType.IS_PUBLISHED + ) + ) + ) + val 회원 = memberRepository.save(MemberEntity(listOf(), 1)) + + // when + val member = memberAdapter.findMember(회원.id) + val previousCouponSize = member.showMyCouponBook().size + member.receiveCoupon(toCoupon(할인_쿠폰)) + memberAdapter.updateMembersCoupon(member) + val updatedMember = memberAdapter.findMember(1) + val afterCouponSize = updatedMember.showMyCouponBook().size + + // then + previousCouponSize shouldBe 0 + afterCouponSize shouldBe 1 + } +} diff --git a/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/MemberPersistenceAdapterTest.kt b/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/MemberPersistenceAdapterTest.kt new file mode 100644 index 0000000..be3f70f --- /dev/null +++ b/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/MemberPersistenceAdapterTest.kt @@ -0,0 +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.mapper.fromCoupon +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.repository.MemberRepository +import com.example.estdelivery.coupon.domain.fixture.나눠준_비율_할인_쿠폰 +import com.example.estdelivery.coupon.domain.fixture.일건창 +import io.kotest.core.spec.style.FreeSpec +import io.kotest.matchers.shouldBe +import io.mockk.every +import io.mockk.mockk +import org.springframework.data.repository.findByIdOrNull + +class MemberPersistenceAdapterTest : FreeSpec({ + val memberRepository = mockk() + lateinit var memberPersistenceAdapter: MemberPersistenceAdapter + + beforeTest { + memberPersistenceAdapter = MemberPersistenceAdapter(memberRepository) + } + + "findById" - { + "회원이 가진 사용하지 않은 쿠폰을 찾는다." { + // given + val memberId = 1L + val unusedCouponEntity = fromCoupon(나눠준_비율_할인_쿠폰) + + // when + every { memberRepository.findByIdOrNull(memberId) } returns MemberEntity( + listOf(unusedCouponEntity), + memberId + ) + val unusedCouponBook = memberPersistenceAdapter.findUnusedCouponByMemberId(memberId) + + // then + unusedCouponBook.showUnusedCoupons().size shouldBe 1 + } + } + + "update" - { + "회원이 가진 사용하지 않은 쿠폰을 수정한다." { + // given + val member = 일건창() + // when + member.receiveCoupon(나눠준_비율_할인_쿠폰) + + every { memberRepository.findByIdOrNull(member.id) } returns MemberEntity(listOf(), member.id) + memberPersistenceAdapter.updateUnusedCouponBook(member.id, member.showMyCouponBook()) + } + } +}) diff --git a/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/UnusedCouponPersistenceAdapterTest.kt b/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/UnusedCouponPersistenceAdapterTest.kt deleted file mode 100644 index aff167b..0000000 --- a/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/UnusedCouponPersistenceAdapterTest.kt +++ /dev/null @@ -1,72 +0,0 @@ -package com.example.estdelivery.coupon.application.port.out.adapter.persistence - -import com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity.UnusedCouponEntity -import com.example.estdelivery.coupon.application.port.out.adapter.persistence.mapper.fromCoupon -import com.example.estdelivery.coupon.application.port.out.adapter.persistence.repository.UnusedCouponRepository -import com.example.estdelivery.coupon.domain.fixture.나눠준_비율_할인_쿠폰 -import com.example.estdelivery.coupon.domain.fixture.일건창 -import io.kotest.core.spec.style.FreeSpec -import io.kotest.matchers.shouldBe -import io.mockk.every -import io.mockk.mockk -import io.mockk.slot - -class UnusedCouponPersistenceAdapterTest : FreeSpec({ - - lateinit var unusedCouponRepository: UnusedCouponRepository - lateinit var memberPersistenceAdapter: MemberPersistenceAdapter - - beforeTest { - unusedCouponRepository = mockk() - memberPersistenceAdapter = MemberPersistenceAdapter(unusedCouponRepository) - } - - "findById" - { - "회원이 가진 사용하지 않은 쿠폰을 찾는다." { - // given - val memberId = 1L - val unusedCouponBookEntity = UnusedCouponEntity(memberId, fromCoupon(나눠준_비율_할인_쿠폰), 1) - - // when - every { unusedCouponRepository.findAllByMemberId(memberId) } returns listOf(unusedCouponBookEntity) - val unusedCouponBook = memberPersistenceAdapter.findMemberCouponByMemberId(memberId) - - // then - unusedCouponBook.showUnusedCoupons().size shouldBe 1 - } - } - - "update" - { - "회원이 가진 사용하지 않은 쿠폰을 수정한다." { - // given - val member = 일건창() - // when - member.receiveCoupon(나눠준_비율_할인_쿠폰) - val savedCouponEntity = slot>() - - every { unusedCouponRepository.findAllByMemberId(any()) } returns emptyList() - every { unusedCouponRepository.saveAll(capture(savedCouponEntity)) } returns listOf(mockk()) - - memberPersistenceAdapter.updateUnusedCouponBook(member.id, member.showMyCouponBook()) - savedCouponEntity.captured.size shouldBe 1 - } - - "새롭게 등록하는 쿠폰만 등록한다" { - // given - val member = 일건창() - member.receiveCoupon(나눠준_비율_할인_쿠폰) - val savedCouponEntity = slot>() - - every { unusedCouponRepository.findAllByMemberId(member.id) } returns listOf( - UnusedCouponEntity(member.id, fromCoupon(나눠준_비율_할인_쿠폰), 1) - ) - every { unusedCouponRepository.saveAll(capture(savedCouponEntity)) } returns listOf(mockk()) - - // when - memberPersistenceAdapter.updateUnusedCouponBook(member.id, member.showMyCouponBook()) - - // then - savedCouponEntity.captured.size shouldBe 0 - } - } -}) From aedd7cbbe214e5e012814f3ad9f6e4d709f0ba5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EA=B1=B4=EC=B0=BD?= <92219795+this-is-spear@users.noreply.github.com> Date: Wed, 15 May 2024 22:08:46 +0900 Subject: [PATCH 04/10] =?UTF-8?q?refactor=20:=20=ED=9A=8C=EC=9B=90=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=EC=8B=9C=20ID=EB=8F=84=20=EB=8F=99=EC=9D=BC?= =?UTF-8?q?=ED=95=98=EA=B2=8C=20=EC=84=A4=EC=A0=95=EB=90=98=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test/kotlin/com/example/estdelivery/member/MemberBase.kt | 4 +++- member/src/test/resources/contracts/member/findMember.kts | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/member/src/test/kotlin/com/example/estdelivery/member/MemberBase.kt b/member/src/test/kotlin/com/example/estdelivery/member/MemberBase.kt index e3e36df..b8907c3 100644 --- a/member/src/test/kotlin/com/example/estdelivery/member/MemberBase.kt +++ b/member/src/test/kotlin/com/example/estdelivery/member/MemberBase.kt @@ -5,6 +5,7 @@ import com.example.estdelivery.member.dto.MemberResponse import com.example.estdelivery.member.service.MemberService import io.mockk.every import io.mockk.mockk +import io.mockk.slot import io.restassured.module.mockmvc.RestAssuredMockMvc import org.junit.jupiter.api.BeforeEach @@ -12,7 +13,8 @@ open class MemberBase { @BeforeEach fun setup() { val memberService = mockk() - every { memberService.findMemberById(1) } returns MemberResponse(1, "이건창") + val id = slot() + every { memberService.findMemberById(capture(id)) } answers { MemberResponse(id.captured, "이건창") } RestAssuredMockMvc.standaloneSetup(MemberController(memberService)) } } diff --git a/member/src/test/resources/contracts/member/findMember.kts b/member/src/test/resources/contracts/member/findMember.kts index e72cf08..b88d9ed 100644 --- a/member/src/test/resources/contracts/member/findMember.kts +++ b/member/src/test/resources/contracts/member/findMember.kts @@ -5,7 +5,7 @@ import org.springframework.cloud.contract.spec.ContractDsl.Companion.contract contract { request { method = GET - url = url(v(consumer(regex("\\/members\\/[0-9]{1,10}")), producer("/members/1"))) + url = url(v(regex("\\/members\\/[0-9]{1,10}"))) } response { status = OK @@ -13,7 +13,7 @@ contract { contentType = "application/json" } body = body( - "id" to 1, + "id" to value("${fromRequest().path(1)}"), "name" to "이건창" ) } From b60aa84cab6cfffc83f8944af048eaf08be9f8ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EA=B1=B4=EC=B0=BD?= <92219795+this-is-spear@users.noreply.github.com> Date: Wed, 15 May 2024 22:52:46 +0900 Subject: [PATCH 05/10] =?UTF-8?q?refactor=20:=20=EA=B0=80=EA=B2=8C=20?= =?UTF-8?q?=EA=B4=80=EB=A0=A8=20=EC=96=B4=EB=8C=91=ED=84=B0=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EA=B5=AC=ED=98=84=20=EB=B0=8F=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/HandoutCouponService.kt | 2 +- .../IssuePublishedCouponService.kt | 6 +- .../application/IssueRandomCouponService.kt | 2 +- .../application/PublishCouponService.kt | 2 +- .../coupon/application/UseCouponService.kt | 2 +- .../port/out/UpdateShopOwnerStatePort.kt | 4 +- .../port/out/adapter/ShopOwnerAdapter.kt | 80 +++++++++++++ .../out/adapter/infra/ShopInfraAdapter.kt | 54 +++------ .../ShopOwnerPersistenceAdapter.kt | 15 ++- .../persistence/entity/CouponEntity.kt | 17 ++- .../persistence/entity/MemberEntity.kt | 15 ++- .../adapter/persistence/entity/ShopEntity.kt | 22 ++-- .../persistence/entity/ShopOwnerEntity.kt | 21 +++- .../persistence/mapper/ShopOwnerMapper.kt | 11 +- .../repository/ShopOwnerRepository.kt | 3 +- .../coupon/domain/coupon/Coupon.kt | 4 + .../coupon/domain/coupon/CouponBook.kt | 4 + .../coupon/domain/member/UnusedCouponBook.kt | 4 + .../coupon/domain/shop/HandOutCouponBook.kt | 4 + .../coupon/domain/shop/PublishedCouponBook.kt | 4 + .../domain/shop/PublishedEventCouponBook.kt | 4 + .../coupon/domain/shop/RoyalCustomers.kt | 4 + .../estdelivery/coupon/domain/shop/Shop.kt | 10 +- .../coupon/domain/shop/ShopOwner.kt | 8 +- .../coupon/domain/shop/UsedCouponBook.kt | 4 + .../application/HandoutCouponServiceTest.kt | 12 +- .../IssuePublishedCouponServiceTest.kt | 12 +- .../IssueRandomCouponServiceTest.kt | 2 +- .../application/PublishCouponServiceTest.kt | 8 +- .../application/UseCouponServiceTest.kt | 4 +- .../port/out/adapter/ShopOwnerAdapterTest.kt | 106 ++++++++++++++++++ .../out/adapter/infra/ShopInfraAdapterTest.kt | 5 + .../coupon/domain/fixture/ShopFixture.kt | 8 +- .../coupon/domain/shop/ShopOwnerTest.kt | 9 +- .../coupon/domain/shop/ShopTest.kt | 1 + .../shop/controller/ShopOwnerController.kt | 10 ++ .../example/estdelivery/shop/entity/Shop.kt | 8 +- .../shop/service/ShopOwnerService.kt | 10 ++ .../com/example/estdelivery/shop/ShopBase.kt | 21 +++- .../contracts/shop/addRoyalCustomers.kts | 14 +++ .../contracts/shop/findShopOwnerByShopId.kts | 59 +++------- .../shop/findShopOwnerByShopOwnerId.kts | 59 +++------- 42 files changed, 452 insertions(+), 202 deletions(-) create mode 100644 coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/ShopOwnerAdapter.kt create mode 100644 coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/ShopOwnerAdapterTest.kt create mode 100644 shop/src/test/resources/contracts/shop/addRoyalCustomers.kts diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/HandoutCouponService.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/HandoutCouponService.kt index afc5298..ddbdc4e 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/HandoutCouponService.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/HandoutCouponService.kt @@ -23,7 +23,7 @@ class HandoutCouponService( }, private val notExistCoupon: (Long) -> Boolean = { loadCouponStatePort.exists(it).not() }, private val getCoupon: (Long) -> Coupon = { loadCouponStatePort.findById(it) }, - private val updateShopOwner: (ShopOwner) -> Unit = { updateShopOwnerStatePort.update(it) }, + private val updateShopOwner: (ShopOwner) -> Unit = { updateShopOwnerStatePort.updateShopOwnersCoupons(it) }, private val createCoupon: (Coupon) -> Coupon = { createCouponStatePort.create(it) }, ) : HandoutCouponUseCase { /** diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/IssuePublishedCouponService.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/IssuePublishedCouponService.kt index 83fda6d..bb26463 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/IssuePublishedCouponService.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/IssuePublishedCouponService.kt @@ -33,7 +33,9 @@ class IssuePublishedCouponService( ) }, private val updateMember: (Member) -> Unit = { updateMemberStatePort.updateMembersCoupon(it) }, - private val updateShopOwner: (ShopOwner) -> Unit = { updateShopOwnerStatePort.update(it) }, + private val updateRoyalCustomers: (ShopOwner, Member) -> Unit = { shopOwner, royalCustomer -> + updateShopOwnerStatePort.updateRoyalCustomers(shopOwner, royalCustomer) + }, ) : IssuePublishedCouponUseCase { /** * 1. 회원 정보를 조회한다. @@ -61,7 +63,7 @@ class IssuePublishedCouponService( ) { if (!shopOwner.showRoyalCustomersInShop().contains(member)) { shopOwner.addRoyalCustomersInShop(member) - updateShopOwner(shopOwner) + updateRoyalCustomers(shopOwner, member) } } } diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/IssueRandomCouponService.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/IssueRandomCouponService.kt index e5bec37..3ed5a06 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/IssueRandomCouponService.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/IssueRandomCouponService.kt @@ -36,7 +36,7 @@ class IssueRandomCouponService( }, private val createCoupon: (Coupon) -> Coupon = { createCouponStatePort.create(it) }, private val updateMember: (Member) -> Unit = { updateMemberStatePort.updateMembersCoupon(it) }, - private val updateShopOwner: (ShopOwner) -> Unit = { updateShopOwnerStatePort.update(it) }, + private val updateShopOwner: (ShopOwner) -> Unit = { updateShopOwnerStatePort.updateShopOwnersCoupons(it) }, private val updateEvent: (RandomCouponIssueEvent) -> Unit = { updateRandomCouponIssueEventStatePort.update(UpdateRandomCouponIssueEventState.from(it)) }, diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/PublishCouponService.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/PublishCouponService.kt index cb00aef..54480fa 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/PublishCouponService.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/PublishCouponService.kt @@ -24,7 +24,7 @@ class PublishCouponService( it.coupon ) }, - private val updateShopOwner: (ShopOwner) -> Unit = { updateShopOwnerStatePort.update(it) }, + private val updateShopOwner: (ShopOwner) -> Unit = { updateShopOwnerStatePort.updateShopOwnersCoupons(it) }, ) : PublishCouponUseCase { /** * 1. 가게 주인 정보를 조회한다. diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/UseCouponService.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/UseCouponService.kt index eeea941..f6825c0 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/UseCouponService.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/UseCouponService.kt @@ -35,7 +35,7 @@ class UseCouponService( ) }, private val updateMember: (Member) -> Unit = { updateMemberStatePort.updateMembersCoupon(it) }, - private val updateShopOwner: (ShopOwner) -> Unit = { updateShopOwnerStatePort.update(it) }, + private val updateShopOwner: (ShopOwner) -> Unit = { updateShopOwnerStatePort.updateShopOwnersCoupons(it) }, ) : UseCouponUseCase { /** * 1. 회원 정보를 조회한다. diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/UpdateShopOwnerStatePort.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/UpdateShopOwnerStatePort.kt index 49ce419..d0c72a2 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/UpdateShopOwnerStatePort.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/UpdateShopOwnerStatePort.kt @@ -1,7 +1,9 @@ package com.example.estdelivery.coupon.application.port.out +import com.example.estdelivery.coupon.domain.member.Member import com.example.estdelivery.coupon.domain.shop.ShopOwner interface UpdateShopOwnerStatePort { - fun update(shopOwner: ShopOwner) + fun updateShopOwnersCoupons(shopOwner: ShopOwner) + fun updateRoyalCustomers(shopOwner: ShopOwner, royalCustomer: Member) } diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/ShopOwnerAdapter.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/ShopOwnerAdapter.kt new file mode 100644 index 0000000..f102203 --- /dev/null +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/ShopOwnerAdapter.kt @@ -0,0 +1,80 @@ +package com.example.estdelivery.coupon.application.port.out.adapter + +import com.example.estdelivery.coupon.application.port.out.LoadShopOwnerStatePort +import com.example.estdelivery.coupon.application.port.out.UpdateShopOwnerStatePort +import com.example.estdelivery.coupon.application.port.out.adapter.infra.ShopInfraAdapter +import com.example.estdelivery.coupon.application.port.out.adapter.infra.dto.ShopOwnerState +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.ShopOwnerPersistenceAdapter +import com.example.estdelivery.coupon.domain.coupon.CouponBook +import com.example.estdelivery.coupon.domain.member.Member +import com.example.estdelivery.coupon.domain.shop.HandOutCouponBook +import com.example.estdelivery.coupon.domain.shop.PublishedCouponBook +import com.example.estdelivery.coupon.domain.shop.PublishedEventCouponBook +import com.example.estdelivery.coupon.domain.shop.RoyalCustomers +import com.example.estdelivery.coupon.domain.shop.Shop +import com.example.estdelivery.coupon.domain.shop.ShopOwner +import com.example.estdelivery.coupon.domain.shop.UsedCouponBook +import org.springframework.stereotype.Component + +@Component +class ShopOwnerAdapter( + memberAdapter: MemberAdapter, + shopInfraAdapter: ShopInfraAdapter, + shopOwnerPersistenceAdapter: ShopOwnerPersistenceAdapter, +) : LoadShopOwnerStatePort, UpdateShopOwnerStatePort { + + override fun findById(shopOwnerId: Long): ShopOwner { + return getShopOwner(shopOwnerId) + } + + override fun findByShopId(shopId: Long): ShopOwner { + return getShopOwnerByShopId(shopId) + } + + override fun updateShopOwnersCoupons(shopOwner: ShopOwner) { + updateShopOwnersCoupon(shopOwner) + } + + override fun updateRoyalCustomers(shopOwner: ShopOwner, royalCustomer: Member) { + updateRoyalCustomersInShopOwner(shopOwner, royalCustomer) + } + + private val getShopOwner: (Long) -> ShopOwner = { shopOwnerId -> + val shopOwnerState = shopInfraAdapter.findShopOwner(shopOwnerId) + getShopOwner(shopOwnerPersistenceAdapter, shopOwnerState, memberAdapter) + } + private val getShopOwnerByShopId: (Long) -> ShopOwner = { shopId -> + val shopOwnerState = shopInfraAdapter.findShopOwnerByShopId(shopId) + getShopOwner(shopOwnerPersistenceAdapter, shopOwnerState, memberAdapter) + } + private val updateShopOwnersCoupon: (ShopOwner) -> Unit = { shopOwner -> + shopOwnerPersistenceAdapter.update(shopOwner) + } + + private val updateRoyalCustomersInShopOwner: (ShopOwner, Member) -> Unit = { shopOwner, royalCustomer -> + shopInfraAdapter.addRoyalCustomers(shopOwner.showShop().id, royalCustomer.id) + } + + private fun getShopOwner( + shopOwnerPersistenceAdapter: ShopOwnerPersistenceAdapter, + shopOwnerState: ShopOwnerState, + memberAdapter: MemberAdapter + ): ShopOwner { + val shopOwnersCoupons = shopOwnerPersistenceAdapter.findByShopId(shopOwnerState.shop.id) + val royalCustomers = shopOwnerState.shop.royalCustomers.map { memberId -> + memberAdapter.findMember(memberId) + } + return ShopOwner( + Shop( + PublishedCouponBook(CouponBook(shopOwnersCoupons.showPublishedCouponsInShop())), + PublishedEventCouponBook(CouponBook(shopOwnersCoupons.showEventCouponInShop())), + HandOutCouponBook(CouponBook(shopOwnersCoupons.showHandOutCouponInShop())), + UsedCouponBook(CouponBook(shopOwnersCoupons.showUsedCouponBook())), + RoyalCustomers(royalCustomers), + shopOwnerState.shop.id, + shopOwnerState.shop.name, + ), + shopOwnerState.id, + ) + } +} diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/ShopInfraAdapter.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/ShopInfraAdapter.kt index 020b472..02cb6e1 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/ShopInfraAdapter.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/ShopInfraAdapter.kt @@ -1,61 +1,33 @@ package com.example.estdelivery.coupon.application.port.out.adapter.infra import com.example.estdelivery.coupon.application.port.out.adapter.infra.dto.ShopOwnerState -import com.example.estdelivery.coupon.domain.shop.HandOutCouponBook -import com.example.estdelivery.coupon.domain.shop.PublishedCouponBook -import com.example.estdelivery.coupon.domain.shop.PublishedEventCouponBook -import com.example.estdelivery.coupon.domain.shop.RoyalCustomers -import com.example.estdelivery.coupon.domain.shop.Shop -import com.example.estdelivery.coupon.domain.shop.ShopOwner -import com.example.estdelivery.coupon.domain.shop.UsedCouponBook import org.springframework.stereotype.Component import org.springframework.web.client.RestClient +import org.springframework.web.client.body @Component class ShopInfraAdapter( private val shopClient: RestClient, ) { - fun findShopOwner(shopOwnerId: Long): ShopOwner { + fun findShopOwner(shopOwnerId: Long): ShopOwnerState { return shopClient.get() .uri("/owners/{shopOwnerId}", shopOwnerId) .retrieve() - .body(ShopOwnerState::class.java)?.let { - ShopOwner( - id = it.id, - shop = it.shop.let { shopState -> - Shop( - PublishedCouponBook(), - PublishedEventCouponBook(), - HandOutCouponBook(), - UsedCouponBook(), - RoyalCustomers(), - shopState.name, - shopState.id, - ) - }, - ) - } ?: throw RuntimeException("ShopOwner not found") + .body(ShopOwnerState::class.java) + ?: throw RuntimeException("ShopOwner not found") } - fun findShopOwnerByShopId(shopId: Long): ShopOwner { + fun findShopOwnerByShopId(shopId: Long): ShopOwnerState { return shopClient.get() .uri("/owners/shop/{shopId}", shopId) .retrieve() - .body(ShopOwnerState::class.java)?.let { - ShopOwner( - id = it.id, - shop = it.shop.let { shopState -> - Shop( - PublishedCouponBook(), - PublishedEventCouponBook(), - HandOutCouponBook(), - UsedCouponBook(), - RoyalCustomers(), - shopState.name, - shopState.id, - ) - }, - ) - } ?: throw RuntimeException("ShopOwner not found") + .body(ShopOwnerState::class.java) + ?: throw RuntimeException("ShopOwner not found") + } + + fun addRoyalCustomers(shopId: Long, royalCustomer: Long) { + shopClient.post() + .uri("/owners/shop/{shopId}/royal-customers/{royalCustomer}", shopId, royalCustomer) + .retrieve() } } diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/ShopOwnerPersistenceAdapter.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/ShopOwnerPersistenceAdapter.kt index 22b11ad..362bd19 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/ShopOwnerPersistenceAdapter.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/ShopOwnerPersistenceAdapter.kt @@ -1,34 +1,33 @@ package com.example.estdelivery.coupon.application.port.out.adapter.persistence -import com.example.estdelivery.coupon.application.port.out.LoadShopOwnerStatePort -import com.example.estdelivery.coupon.application.port.out.UpdateShopOwnerStatePort import com.example.estdelivery.coupon.application.port.out.adapter.persistence.mapper.fromShopOwner import com.example.estdelivery.coupon.application.port.out.adapter.persistence.mapper.toShopOwner import com.example.estdelivery.coupon.application.port.out.adapter.persistence.repository.ShopOwnerRepository import com.example.estdelivery.coupon.application.port.out.adapter.persistence.repository.ShopRepository import com.example.estdelivery.coupon.domain.shop.ShopOwner -import jakarta.transaction.Transactional import org.springframework.stereotype.Component +import org.springframework.transaction.annotation.Transactional @Component class ShopOwnerPersistenceAdapter( private val shopOwnerRepository: ShopOwnerRepository, private val shopRepository: ShopRepository, -) : LoadShopOwnerStatePort, - UpdateShopOwnerStatePort { - override fun findById(shopOwnerId: Long): ShopOwner { +) { + @Transactional(readOnly = true) + fun findById(shopOwnerId: Long): ShopOwner { val shopOwnerEntity = shopOwnerRepository.findById(shopOwnerId).orElseThrow() return toShopOwner(shopOwnerEntity) } - override fun findByShopId(shopId: Long): ShopOwner { + @Transactional(readOnly = true) + fun findByShopId(shopId: Long): ShopOwner { val shopEntity = shopRepository.findById(shopId).orElseThrow() val shopOwnerEntity = shopOwnerRepository.findByShopEntity(shopEntity).orElseThrow() return toShopOwner(shopOwnerEntity) } @Transactional - override fun update(shopOwner: ShopOwner) { + fun update(shopOwner: ShopOwner) { shopOwnerRepository.save(fromShopOwner(shopOwner)) } } diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/CouponEntity.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/CouponEntity.kt index ab6aeb5..0171305 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/CouponEntity.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/CouponEntity.kt @@ -1,6 +1,5 @@ package com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity -import jakarta.persistence.Column import jakarta.persistence.Entity import jakarta.persistence.EnumType import jakarta.persistence.Enumerated @@ -20,7 +19,19 @@ class CouponEntity( var type: CouponStateType, var amount: Int, @Id - @Column(name = "coupon_id") @GeneratedValue(strategy = GenerationType.IDENTITY) var id: Long? = null, -) +) { + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (other !is CouponEntity) return false + + if (id != other.id) return false + + return true + } + + override fun hashCode(): Int { + return id?.hashCode() ?: 0 + } +} diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/MemberEntity.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/MemberEntity.kt index 1dab6ea..5f4faad 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/MemberEntity.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/MemberEntity.kt @@ -23,4 +23,17 @@ class MemberEntity( @Id @Column(name = "member_id") val id: Long, -) +) { + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (other !is MemberEntity) return false + + if (id != other.id) return false + + return true + } + + override fun hashCode(): Int { + return id.hashCode() + } +} diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/ShopEntity.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/ShopEntity.kt index 817eed7..35b81c5 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/ShopEntity.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/ShopEntity.kt @@ -1,7 +1,5 @@ package com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity -import jakarta.persistence.Column -import jakarta.persistence.ElementCollection import jakarta.persistence.Entity import jakarta.persistence.GeneratedValue import jakarta.persistence.GenerationType @@ -50,12 +48,20 @@ class ShopEntity( inverseJoinColumns = [JoinColumn(name = "coupon_id")], ) var usedCouponBook: List, - //TODO 쿠폰 도메인에서 회원 정보는 Long 으로 관리 - @ElementCollection - var royalCustomers: List, - var name: String, @Id - @Column(name = "shop_id") @GeneratedValue(strategy = GenerationType.IDENTITY) val id: Long? = null, -) +) { + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (other !is ShopEntity) return false + + if (id != other.id) return false + + return true + } + + override fun hashCode(): Int { + return id?.hashCode() ?: 0 + } +} diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/ShopOwnerEntity.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/ShopOwnerEntity.kt index 991e243..4be59b3 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/ShopOwnerEntity.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/entity/ShopOwnerEntity.kt @@ -1,5 +1,6 @@ package com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity +import jakarta.persistence.CascadeType import jakarta.persistence.Entity import jakarta.persistence.GeneratedValue import jakarta.persistence.GenerationType @@ -11,10 +12,26 @@ import jakarta.persistence.Table @Entity @Table(name = "shop_owner") class ShopOwnerEntity( - @OneToOne + @OneToOne( + cascade = [CascadeType.ALL], + orphanRemoval = true, + ) @JoinColumn(name = "shop_id") var shopEntity: ShopEntity, @Id @GeneratedValue(strategy = GenerationType.IDENTITY) var id: Long? = null, -) +) { + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (other !is ShopOwnerEntity) return false + + if (id != other.id) return false + + return true + } + + override fun hashCode(): Int { + return id?.hashCode() ?: 0 + } +} diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/mapper/ShopOwnerMapper.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/mapper/ShopOwnerMapper.kt index ed4d6e1..a8f513e 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/mapper/ShopOwnerMapper.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/mapper/ShopOwnerMapper.kt @@ -1,9 +1,9 @@ package com.example.estdelivery.coupon.application.port.out.adapter.persistence.mapper +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity.MemberEntity import com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity.ShopEntity import com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity.ShopOwnerEntity import com.example.estdelivery.coupon.domain.coupon.CouponBook -import com.example.estdelivery.coupon.domain.member.Member import com.example.estdelivery.coupon.domain.shop.HandOutCouponBook import com.example.estdelivery.coupon.domain.shop.PublishedCouponBook import com.example.estdelivery.coupon.domain.shop.PublishedEventCouponBook @@ -45,11 +45,7 @@ internal fun toShop(entity: ShopEntity): Shop { it ) })), - RoyalCustomers(entity.royalCustomers.map { - //TODO: Member를 어떻게 가져올지 고민해보기 - Member(it, "단골 손님") - }), - entity.name, + RoyalCustomers(), entity.id!!, ) } @@ -64,9 +60,6 @@ internal fun fromShop(shop: Shop): ShopEntity { .map { fromCoupon(it) }, shop.showUsedCoupons() .map { fromCoupon(it) }, - shop.showRoyalCustomers() - .map { it.id }, - shop.name, shop.id, ) } diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/repository/ShopOwnerRepository.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/repository/ShopOwnerRepository.kt index b38a22c..3dc1018 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/repository/ShopOwnerRepository.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/repository/ShopOwnerRepository.kt @@ -5,7 +5,6 @@ import com.example.estdelivery.coupon.application.port.out.adapter.persistence.e import java.util.Optional import org.springframework.data.jpa.repository.JpaRepository -interface ShopOwnerRepository : - JpaRepository { +interface ShopOwnerRepository : JpaRepository { fun findByShopEntity(shopEntity: ShopEntity): Optional } diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/coupon/Coupon.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/coupon/Coupon.kt index 165e533..57d0a32 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/coupon/Coupon.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/coupon/Coupon.kt @@ -42,4 +42,8 @@ sealed class Coupon( override fun hashCode(): Int { return id.hashCode() } + + override fun toString(): String { + return "Coupon(name='$name', description='$description', couponType=$couponType, id=$id)" + } } diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/coupon/CouponBook.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/coupon/CouponBook.kt index 645d94e..a05a4d4 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/coupon/CouponBook.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/coupon/CouponBook.kt @@ -16,4 +16,8 @@ class CouponBook( require(coupons.contains(coupon).not()) { "이미 존재하는 쿠폰입니다." } coupons = coupons + coupon } + + override fun toString(): String { + return "CouponBook(coupons=$coupons)" + } } diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/member/UnusedCouponBook.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/member/UnusedCouponBook.kt index e421206..028f30c 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/member/UnusedCouponBook.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/member/UnusedCouponBook.kt @@ -19,4 +19,8 @@ class UnusedCouponBook( require(unUsedCoupons.showCoupons().contains(coupon)) { "존재하지 않는 쿠폰입니다." } unUsedCoupons.deleteCoupon(coupon) } + + override fun toString(): String { + return "UnusedCouponBook(unUsedCoupons=$unUsedCoupons)" + } } diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/shop/HandOutCouponBook.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/shop/HandOutCouponBook.kt index d88eb3e..f59f4e8 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/shop/HandOutCouponBook.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/shop/HandOutCouponBook.kt @@ -17,4 +17,8 @@ class HandOutCouponBook( } handOutCoupons.addCoupon(coupon) } + + override fun toString(): String { + return "HandOutCouponBook(handOutCoupons=$handOutCoupons)" + } } diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/shop/PublishedCouponBook.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/shop/PublishedCouponBook.kt index bb73729..54465de 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/shop/PublishedCouponBook.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/shop/PublishedCouponBook.kt @@ -20,4 +20,8 @@ class PublishedCouponBook( require(publishedCoupons.showCoupons().contains(coupon)) { "게시된 쿠폰이 아닙니다." } return publishedCoupons.showCoupons().find { it == coupon }!! } + + override fun toString(): String { + return "PublishedCouponBook(publishedCoupons=$publishedCoupons)" + } } diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/shop/PublishedEventCouponBook.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/shop/PublishedEventCouponBook.kt index 2cd3a9a..f620947 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/shop/PublishedEventCouponBook.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/shop/PublishedEventCouponBook.kt @@ -13,4 +13,8 @@ class PublishedEventCouponBook( } fun showEventCoupons() = publishedEventCoupons.showCoupons() + + override fun toString(): String { + return "PublishedEventCouponBook(publishedEventCoupons=$publishedEventCoupons)" + } } diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/shop/RoyalCustomers.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/shop/RoyalCustomers.kt index a78601b..b8e3a94 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/shop/RoyalCustomers.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/shop/RoyalCustomers.kt @@ -22,4 +22,8 @@ class RoyalCustomers( fun showRoyalCustomers(): List { return customers.toList() } + + override fun toString(): String { + return "RoyalCustomers(customers=$customers)" + } } diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/shop/Shop.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/shop/Shop.kt index c33188a..c6e4d4c 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/shop/Shop.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/shop/Shop.kt @@ -10,8 +10,8 @@ class Shop( private val handOutCouponBook: HandOutCouponBook, private val usedCouponBook: UsedCouponBook, private val royalCustomers: RoyalCustomers, - val name: String, - internal val id: Long? = null, + val id: Long, + val name: String? = null, ) { fun publishCoupon(coupon: Coupon) { publishedCoupons.publishCoupon(coupon) @@ -60,5 +60,9 @@ class Shop( return id == other.id } - override fun hashCode() = id?.hashCode() ?: 0 + override fun hashCode() = id.hashCode() + + override fun toString(): String { + return "Shop(publishedCoupons=$publishedCoupons, publishedEventCoupons=$publishedEventCoupons, handOutCouponBook=$handOutCouponBook, usedCouponBook=$usedCouponBook, royalCustomers=$royalCustomers, id=$id, name=$name)" + } } diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/shop/ShopOwner.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/shop/ShopOwner.kt index b236c7d..419791a 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/shop/ShopOwner.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/shop/ShopOwner.kt @@ -5,7 +5,7 @@ import com.example.estdelivery.coupon.domain.member.Member class ShopOwner( private val shop: Shop, - val id: Long? = null, + val id: Long, ) { fun handOutCouponToRoyalCustomersInShop(coupon: Coupon) = shop.handOutCouponToRoyalCustomers(coupon) @@ -41,6 +41,10 @@ class ShopOwner( } override fun hashCode(): Int { - return id?.hashCode() ?: 0 + return id.hashCode() ?: 0 + } + + override fun toString(): String { + return "ShopOwner(shop=$shop, id=$id)" } } diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/shop/UsedCouponBook.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/shop/UsedCouponBook.kt index eedd2d8..517aa3c 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/shop/UsedCouponBook.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/shop/UsedCouponBook.kt @@ -18,4 +18,8 @@ class UsedCouponBook( fun showUsedCoupons(): List { return usedCoupons.showCoupons() } + + override fun toString(): String { + return "UsedCouponBook(usedCoupons=$usedCoupons)" + } } diff --git a/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/HandoutCouponServiceTest.kt b/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/HandoutCouponServiceTest.kt index 85c0556..a10e41e 100644 --- a/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/HandoutCouponServiceTest.kt +++ b/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/HandoutCouponServiceTest.kt @@ -45,8 +45,8 @@ class HandoutCouponServiceTest : FreeSpec({ val 프리퍼_주인 = ShopOwner(가게, 1L) val 변경된_프리퍼_주인_상태 = slot() - val shopId = 가게.id!! - val shopOwnerId = 프리퍼_주인.id!! + val shopId = 가게.id + val shopOwnerId = 프리퍼_주인.id val handoutCouponCommand = HandoutCouponCommand( shopOwnerId, @@ -57,7 +57,7 @@ class HandoutCouponServiceTest : FreeSpec({ every { loadCouponStatePort.exists(any()) } returns false every { createCouponStatePort.create(나눠줄_쿠폰) } returns 나눠준_비율_할인_쿠폰 every { loadCouponStatePort.findById(any()) } returns 나눠준_비율_할인_쿠폰 - every { updateShopOwnerStatePort.update(capture(변경된_프리퍼_주인_상태)) } returns Unit + every { updateShopOwnerStatePort.updateShopOwnersCoupons(capture(변경된_프리퍼_주인_상태)) } returns Unit // when handoutCouponService.handoutCoupon(handoutCouponCommand) @@ -76,8 +76,8 @@ class HandoutCouponServiceTest : FreeSpec({ val 프리퍼_주인_상태 = ShopOwner(가게, 1L) val 변경된_프리퍼_주인_상태 = slot() - val shopId = 가게.id!! - val shopOwnerId = 프리퍼_주인_상태.id!! + val shopId = 가게.id + val shopOwnerId = 프리퍼_주인_상태.id val handoutCouponCommand = HandoutCouponCommand( shopOwnerId, shopId, @@ -87,7 +87,7 @@ class HandoutCouponServiceTest : FreeSpec({ every { loadCouponStatePort.exists(any()) } returns false every { createCouponStatePort.create(나눠줄_쿠폰) } returns 나눠준_비율_할인_쿠폰 every { loadCouponStatePort.findById(any()) } returns 나눠준_비율_할인_쿠폰 - every { updateShopOwnerStatePort.update(capture(변경된_프리퍼_주인_상태)) } returns Unit + every { updateShopOwnerStatePort.updateShopOwnersCoupons(capture(변경된_프리퍼_주인_상태)) } returns Unit // when handoutCouponService.handoutCoupon(handoutCouponCommand) diff --git a/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/IssuePublishedCouponServiceTest.kt b/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/IssuePublishedCouponServiceTest.kt index 54bda1f..65272f7 100644 --- a/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/IssuePublishedCouponServiceTest.kt +++ b/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/IssuePublishedCouponServiceTest.kt @@ -59,7 +59,7 @@ class IssuePublishedCouponServiceTest : FreeSpec({ val 가게 = 게시된_쿠폰이_있는_프리퍼(할인쿠폰) val 회원_상태 = 이건창() val memberId = 회원_상태.id - val shopId = 가게.id!! + val shopId = 가게.id val couponId = 할인쿠폰.id!! val issuePublishedCouponCommand = IssuePublishedCouponCommand( @@ -74,7 +74,7 @@ class IssuePublishedCouponServiceTest : FreeSpec({ every { loadCouponStatePort.findById(couponId) } returns 할인쿠폰 every { loadShopOwnerStatePort.findByShopId(shopId) } returns 프리퍼_주인_상태 every { updateMemberStatePort.updateMembersCoupon(capture(변경된_회원_상태)) } returns Unit - every { updateShopOwnerStatePort.update(capture(변경된_프리퍼_주인_상태)) } returns Unit + every { updateShopOwnerStatePort.updateRoyalCustomers(capture(변경된_프리퍼_주인_상태), any()) } returns Unit // when issuePublishedCouponUseCase.issuePublishedCoupon(issuePublishedCouponCommand) @@ -89,7 +89,7 @@ class IssuePublishedCouponServiceTest : FreeSpec({ val 가게 = 게시된_쿠폰이_있는_프리퍼(게시된_고정_할인_쿠폰) val 회원_상태 = 이건창().apply { receiveCoupon(게시된_고정_할인_쿠폰) } val memberId = 회원_상태.id - val shopId = 가게.id!! + val shopId = 가게.id val couponId = 게시된_고정_할인_쿠폰.id!! val issuePublishedCouponCommand = IssuePublishedCouponCommand( @@ -103,7 +103,7 @@ class IssuePublishedCouponServiceTest : FreeSpec({ every { loadCouponStatePort.findById(couponId) } returns 게시된_고정_할인_쿠폰 every { updateMemberStatePort.updateMembersCoupon(any()) } returns Unit every { loadShopOwnerStatePort.findByShopId(shopId) } returns 프리퍼_주인_상태 - every { updateShopOwnerStatePort.update(any()) } returns Unit + every { updateShopOwnerStatePort.updateShopOwnersCoupons(any()) } returns Unit // when & then shouldThrow { @@ -117,7 +117,7 @@ class IssuePublishedCouponServiceTest : FreeSpec({ val 가게 = 게시된_쿠폰이_있는_프리퍼(할인쿠폰) val 회원_상태 = 이건창() val memberId = 회원_상태.id - val shopId = 가게.id!! + val shopId = 가게.id val couponId = 할인쿠폰.id!! val issuePublishedCouponCommand = IssuePublishedCouponCommand( @@ -131,7 +131,7 @@ class IssuePublishedCouponServiceTest : FreeSpec({ every { loadCouponStatePort.findById(couponId) } returns 할인쿠폰 every { updateMemberStatePort.updateMembersCoupon(any()) } returns Unit every { loadShopOwnerStatePort.findByShopId(shopId) } returns 프리퍼_주인_상태 - every { updateShopOwnerStatePort.update(any()) } returns Unit + every { updateShopOwnerStatePort.updateShopOwnersCoupons(any()) } returns Unit // when & then shouldThrow { diff --git a/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/IssueRandomCouponServiceTest.kt b/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/IssueRandomCouponServiceTest.kt index f46c7e0..8b66ea0 100644 --- a/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/IssueRandomCouponServiceTest.kt +++ b/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/IssueRandomCouponServiceTest.kt @@ -90,7 +90,7 @@ class IssueRandomCouponServiceTest : FreeSpec({ every { loadRandomCouponIssueEventStatePort.findById(1L) } returns 랜덤_쿠폰_뽑기_이벤트_상태 every { loadShopOwnerStatePort.findByShopId(1L) } returns 가게주인 every { updateMemberStatePort.updateMembersCoupon(capture(상태가_변경된_회원)) } returns Unit - every { updateShopOwnerStatePort.update(capture(상태가_변경된_가게주인)) } returns Unit + every { updateShopOwnerStatePort.updateShopOwnersCoupons(capture(상태가_변경된_가게주인)) } returns Unit every { updateRandomCouponIssueEventStatePort.update(capture(상태가_변경된_이벤트)) } returns Unit every { createCouponStatePort.create(any()) } returns 이벤트_쿠폰 diff --git a/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/PublishCouponServiceTest.kt b/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/PublishCouponServiceTest.kt index eac1ee9..16a4985 100644 --- a/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/PublishCouponServiceTest.kt +++ b/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/PublishCouponServiceTest.kt @@ -46,7 +46,7 @@ class PublishCouponServiceTest : FreeSpec({ // given val shopOwnerId = 1L val 가게 = 새로_창업해서_아무것도_없는_프리퍼() - val shopId = 가게.id!! + val shopId = 가게.id val publishCouponCommand = PublishCouponCommand( shopOwnerId, @@ -59,7 +59,7 @@ class PublishCouponServiceTest : FreeSpec({ // when every { loadShopOwnerPort.findById(shopOwnerId) } returns 프리퍼_주인 every { createCouponStatePort.create(게시할_쿠폰) } returns 게시된_고정_할인_쿠폰 - every { updateShopOwnerStatePort.update(capture(변경된_프리퍼_주인)) } returns Unit + every { updateShopOwnerStatePort.updateShopOwnersCoupons(capture(변경된_프리퍼_주인)) } returns Unit publishCouponService.publishCoupon(publishCouponCommand) @@ -70,7 +70,7 @@ class PublishCouponServiceTest : FreeSpec({ "게시된 쿠폰북에 동일한 쿠폰이 있을 수 없다." { // given val shopOwnerId = 1L - val shopId = 새로_창업해서_아무것도_없는_프리퍼().id!! + val shopId = 새로_창업해서_아무것도_없는_프리퍼().id val publishCouponCommand = PublishCouponCommand( shopOwnerId, @@ -84,8 +84,8 @@ class PublishCouponServiceTest : FreeSpec({ HandOutCouponBook(CouponBook(listOf())), UsedCouponBook(CouponBook(listOf())), RoyalCustomers(listOf()), - "프리퍼", shopId, + "프리퍼", ) val 프리퍼_주인_상태 = ShopOwner(이미_쿠폰을_게시한_프리퍼, shopOwnerId) diff --git a/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/UseCouponServiceTest.kt b/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/UseCouponServiceTest.kt index 8798b80..950ee00 100644 --- a/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/UseCouponServiceTest.kt +++ b/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/UseCouponServiceTest.kt @@ -47,7 +47,7 @@ class UseCouponServiceTest : FreeSpec({ val 프리퍼_주인 = ShopOwner(나눠준_쿠폰이_있는_프리퍼(나눠준_비율_할인_쿠폰), 1L) val 회원 = 나눠준_쿠폰을_가진_삼건창() val memberId = 회원.id - val shopId = 프리퍼_주인.showShop().id!! + val shopId = 프리퍼_주인.showShop().id val couponId = 나눠준_비율_할인_쿠폰.id!! val useCouponCommand = UseCouponCommand( @@ -63,7 +63,7 @@ class UseCouponServiceTest : FreeSpec({ every { loadCouponStatePort.findById(couponId) } returns 나눠준_비율_할인_쿠폰 every { loadShopOwnerStatePort.findByShopId(shopId) } returns 프리퍼_주인 every { updateMemberStatePort.updateMembersCoupon(capture(변경된_회원_상태)) } returns Unit - every { updateShopOwnerStatePort.update(capture(변경된_프리퍼_주인_상태)) } returns Unit + every { updateShopOwnerStatePort.updateShopOwnersCoupons(capture(변경된_프리퍼_주인_상태)) } returns Unit useCouponService.useCoupon(useCouponCommand) diff --git a/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/ShopOwnerAdapterTest.kt b/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/ShopOwnerAdapterTest.kt new file mode 100644 index 0000000..e2e5eab --- /dev/null +++ b/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/ShopOwnerAdapterTest.kt @@ -0,0 +1,106 @@ +package com.example.estdelivery.coupon.application.port.out.adapter + +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity.ShopEntity +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity.ShopOwnerEntity +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.CouponRepository +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.repository.MemberRepository +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.repository.ShopOwnerRepository +import com.example.estdelivery.coupon.domain.fixture.게시할_쿠폰 +import com.example.estdelivery.coupon.domain.fixture.나눠줄_쿠폰 +import io.kotest.matchers.shouldBe +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.cloud.contract.stubrunner.spring.AutoConfigureStubRunner +import org.springframework.cloud.contract.stubrunner.spring.StubRunnerProperties + +@SpringBootTest +@AutoConfigureStubRunner( + stubsMode = StubRunnerProperties.StubsMode.LOCAL, + ids = [ + "com.example.estdelivery.member:member:1.0-SNAPSHOT:8081", + "com.example.estdelivery.shop:shop:1.0-SNAPSHOT:8082", + ] +) +class ShopOwnerAdapterTest( + @Autowired + private val couponRepository: CouponRepository, + @Autowired + private val memberAdapter: MemberAdapter, + @Autowired + private val memberRepository: MemberRepository, + @Autowired + private val shopOwnerRepository: ShopOwnerRepository, + @Autowired + private val shopOwnerAdapter: ShopOwnerAdapter, +) { + + @BeforeEach + fun setUp() { + memberRepository.deleteAll() + shopOwnerRepository.deleteAll() + couponRepository.deleteAll() + } + + @Test + fun `가게 주인 식별자로 가게 주인을 찾는다`() { + val shopEntity = ShopEntity(listOf(), listOf(), listOf(), listOf()) + val shopOwnerEntity = ShopOwnerEntity(shopEntity) + + val ownerEntity = shopOwnerRepository.save(shopOwnerEntity) + + // when + val owner = shopOwnerAdapter.findById(ownerEntity.id!!) + + // then + owner.showShop().id shouldBe ownerEntity.shopEntity.id + } + + @Test + fun `가게 식별자로 가게 주인을 찾는다`() { + val shopEntity = ShopEntity(listOf(), listOf(), listOf(), listOf()) + val shopOwnerEntity = ShopOwnerEntity(shopEntity) + val ownerEntity = shopOwnerRepository.save(shopOwnerEntity) + + val owner = shopOwnerAdapter.findByShopId(ownerEntity.shopEntity.id!!) + + // then + owner.showShop().id shouldBe ownerEntity.shopEntity.id + } + + @Test + fun `수정된 쿠폰 목록을 저장한다`() { + val shopEntity = ShopEntity(listOf(), listOf(), listOf(), listOf()) + val shopOwnerEntity = ShopOwnerEntity(shopEntity) + val ownerEntity = shopOwnerRepository.save(shopOwnerEntity) + + val owner = shopOwnerAdapter.findById(ownerEntity.id!!) + val 게시할_쿠폰 = toCoupon(couponRepository.save(fromCoupon(게시할_쿠폰))) + val 나눠줄_쿠폰 = toCoupon(couponRepository.save(fromCoupon(나눠줄_쿠폰))) + + owner.publishCouponInShop(게시할_쿠폰) + owner.handOutCouponToRoyalCustomersInShop(나눠줄_쿠폰) + + shopOwnerAdapter.updateShopOwnersCoupons(owner) + + val updatedOwner = shopOwnerAdapter.findById(owner.id) + updatedOwner.showPublishedCouponsInShop() shouldBe listOf(게시할_쿠폰) + updatedOwner.showHandOutCouponInShop() shouldBe listOf(나눠줄_쿠폰) + } + + @Test + fun `단골 고객을 추가한다`() { + val shopEntity = ShopEntity(listOf(), listOf(), listOf(), listOf()) + val shopOwnerEntity = ShopOwnerEntity(shopEntity) + val ownerEntity = shopOwnerRepository.save(shopOwnerEntity) + val member = memberAdapter.findMember(10L) + val owner = shopOwnerAdapter.findById(ownerEntity.id!!) + + owner.addRoyalCustomersInShop(member) + + shopOwnerAdapter.updateRoyalCustomers(owner, member) + } +} diff --git a/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/ShopInfraAdapterTest.kt b/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/ShopInfraAdapterTest.kt index 5f1baa5..0973a2e 100644 --- a/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/ShopInfraAdapterTest.kt +++ b/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/ShopInfraAdapterTest.kt @@ -28,4 +28,9 @@ class ShopInfraAdapterTest( val shopOwner = shopInfraAdapter.findShopOwnerByShopId(2L) assertThat(shopOwner.id).isEqualTo(2L) } + + @Test + fun `가게 주인에게 단골 소님을 추가한다`() { + shopInfraAdapter.addRoyalCustomers(3L, 4L) + } } diff --git a/coupon/src/test/kotlin/com/example/estdelivery/coupon/domain/fixture/ShopFixture.kt b/coupon/src/test/kotlin/com/example/estdelivery/coupon/domain/fixture/ShopFixture.kt index 4688601..3b9a133 100644 --- a/coupon/src/test/kotlin/com/example/estdelivery/coupon/domain/fixture/ShopFixture.kt +++ b/coupon/src/test/kotlin/com/example/estdelivery/coupon/domain/fixture/ShopFixture.kt @@ -19,8 +19,8 @@ fun 새로_창업해서_아무것도_없는_프리퍼() = HandOutCouponBook(CouponBook(listOf())), UsedCouponBook(CouponBook(listOf())), RoyalCustomers(listOf()), - 가게_이름, 1L, + 가게_이름, ) fun 나눠준_쿠폰이_있는_프리퍼(vararg coupon: Coupon) = @@ -30,8 +30,8 @@ fun 나눠준_쿠폰이_있는_프리퍼(vararg coupon: Coupon) = HandOutCouponBook(CouponBook(listOf(*coupon))), UsedCouponBook(CouponBook(listOf())), RoyalCustomers(listOf(나눠준_쿠폰을_가진_삼건창(), 일건창(), 이건창())), - 가게_이름, 1L, + 가게_이름, ) fun 게시된_쿠폰이_있는_프리퍼(vararg coupon: Coupon) = @@ -41,8 +41,8 @@ fun 게시된_쿠폰이_있는_프리퍼(vararg coupon: Coupon) = HandOutCouponBook(CouponBook(listOf())), UsedCouponBook(CouponBook(listOf())), RoyalCustomers(listOf()), - "프리퍼", 1L, + "프리퍼", ) fun 단골이_있는_멋진_프리퍼(vararg member: Member) = @@ -52,6 +52,6 @@ fun 단골이_있는_멋진_프리퍼(vararg member: Member) = HandOutCouponBook(CouponBook(listOf())), UsedCouponBook(CouponBook(listOf())), RoyalCustomers(listOf(*member)), - "프리퍼", 1L, + "프리퍼", ) diff --git a/coupon/src/test/kotlin/com/example/estdelivery/coupon/domain/shop/ShopOwnerTest.kt b/coupon/src/test/kotlin/com/example/estdelivery/coupon/domain/shop/ShopOwnerTest.kt index f40be50..514173f 100644 --- a/coupon/src/test/kotlin/com/example/estdelivery/coupon/domain/shop/ShopOwnerTest.kt +++ b/coupon/src/test/kotlin/com/example/estdelivery/coupon/domain/shop/ShopOwnerTest.kt @@ -16,6 +16,7 @@ class ShopOwnerTest : FreeSpec({ HandOutCouponBook(), UsedCouponBook(), RoyalCustomers(), + 1, "프리퍼", ) @@ -34,8 +35,10 @@ class ShopOwnerTest : FreeSpec({ HandOutCouponBook(), UsedCouponBook(), 단골_리스트, + 2, "프리퍼", ), + 1 ) // when @@ -47,7 +50,7 @@ class ShopOwnerTest : FreeSpec({ "쿠폰을 가게에 게시한다." { // given - val 가게_주인 = ShopOwner(프리퍼) + val 가게_주인 = ShopOwner(프리퍼, 1) // when 가게_주인.publishCouponInShop(게시할_쿠폰) @@ -58,7 +61,7 @@ class ShopOwnerTest : FreeSpec({ "단골 회원을 가게에 추가한다." { // given - val 가게_주인 = ShopOwner(프리퍼) + val 가게_주인 = ShopOwner(프리퍼, 2) val 홍길동 = Member(1, "홍길동", UnusedCouponBook()) val 김철수 = Member(2, "김철수", UnusedCouponBook()) @@ -72,7 +75,7 @@ class ShopOwnerTest : FreeSpec({ "이벤트 쿠폰을 가게에 발행한다." { // given - val 가게_주인 = ShopOwner(프리퍼) + val 가게_주인 = ShopOwner(프리퍼, 3) // when 가게_주인.issueEventCouponInShop(이벤트_쿠폰) diff --git a/coupon/src/test/kotlin/com/example/estdelivery/coupon/domain/shop/ShopTest.kt b/coupon/src/test/kotlin/com/example/estdelivery/coupon/domain/shop/ShopTest.kt index dcaefa0..59e4946 100644 --- a/coupon/src/test/kotlin/com/example/estdelivery/coupon/domain/shop/ShopTest.kt +++ b/coupon/src/test/kotlin/com/example/estdelivery/coupon/domain/shop/ShopTest.kt @@ -30,6 +30,7 @@ class ShopTest : FreeSpec({ HandOutCouponBook(), UsedCouponBook(), 단골_리스트, + 1, "프리퍼", ) } diff --git a/shop/src/main/kotlin/com/example/estdelivery/shop/controller/ShopOwnerController.kt b/shop/src/main/kotlin/com/example/estdelivery/shop/controller/ShopOwnerController.kt index 23bb153..fe75d23 100644 --- a/shop/src/main/kotlin/com/example/estdelivery/shop/controller/ShopOwnerController.kt +++ b/shop/src/main/kotlin/com/example/estdelivery/shop/controller/ShopOwnerController.kt @@ -4,6 +4,7 @@ import com.example.estdelivery.shop.service.ShopOwnerService import org.springframework.http.MediaType import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.PathVariable +import org.springframework.web.bind.annotation.PostMapping import org.springframework.web.bind.annotation.RestController @RestController @@ -21,4 +22,13 @@ class ShopOwnerController( produces = [MediaType.APPLICATION_JSON_VALUE] ) fun findShopOwnerByShopId(@PathVariable id: Long) = shopOwnerService.findShopOwnerByShopId(id) + + @PostMapping( + value = ["/owners/shop/{shopId}/royal-customers/{royalCustomerId}"], + produces = [MediaType.APPLICATION_JSON_VALUE] + ) + fun addRoyalCustomers( + @PathVariable shopId: Long, + @PathVariable royalCustomerId: Long + ) = shopOwnerService.addRoyalCustomers(shopId, royalCustomerId) } diff --git a/shop/src/main/kotlin/com/example/estdelivery/shop/entity/Shop.kt b/shop/src/main/kotlin/com/example/estdelivery/shop/entity/Shop.kt index c005695..6a5ebd2 100644 --- a/shop/src/main/kotlin/com/example/estdelivery/shop/entity/Shop.kt +++ b/shop/src/main/kotlin/com/example/estdelivery/shop/entity/Shop.kt @@ -1,5 +1,6 @@ package com.example.estdelivery.shop.entity +import jakarta.persistence.CascadeType import jakarta.persistence.Column import jakarta.persistence.Entity import jakarta.persistence.GeneratedValue @@ -13,9 +14,12 @@ import jakarta.persistence.Table @Entity @Table(name = "shop") class Shop( - @OneToMany + @OneToMany( + targetEntity = RoyalCustomer::class, + cascade = [CascadeType.ALL], + ) @JoinColumn(name = "shop_id") - val royalCustomers: List, + var royalCustomers: List, val name: String, @Id @Column(name = "shop_id") diff --git a/shop/src/main/kotlin/com/example/estdelivery/shop/service/ShopOwnerService.kt b/shop/src/main/kotlin/com/example/estdelivery/shop/service/ShopOwnerService.kt index 6e772b7..32918da 100644 --- a/shop/src/main/kotlin/com/example/estdelivery/shop/service/ShopOwnerService.kt +++ b/shop/src/main/kotlin/com/example/estdelivery/shop/service/ShopOwnerService.kt @@ -2,6 +2,7 @@ package com.example.estdelivery.shop.service import com.example.estdelivery.shop.dto.ShopOwnerResponse import com.example.estdelivery.shop.dto.ShopResponse +import com.example.estdelivery.shop.entity.RoyalCustomer import com.example.estdelivery.shop.repository.ShopOwnerRepository import org.springframework.data.repository.findByIdOrNull import org.springframework.stereotype.Service @@ -44,4 +45,13 @@ class ShopOwnerService( ) ) } + + fun addRoyalCustomers(shopId: Long, royalCustomerId: Long) { + shopOwnerRepository.findByShopId(shopId) + ?.let { shopOwner -> + shopOwner.shop.royalCustomers += RoyalCustomer(royalCustomerId) + shopOwnerRepository.save(shopOwner) + } + ?: throw IllegalArgumentException("Shop owner not found") + } } diff --git a/shop/src/test/kotlin/com/example/estdelivery/shop/ShopBase.kt b/shop/src/test/kotlin/com/example/estdelivery/shop/ShopBase.kt index 1b2586c..1d165df 100644 --- a/shop/src/test/kotlin/com/example/estdelivery/shop/ShopBase.kt +++ b/shop/src/test/kotlin/com/example/estdelivery/shop/ShopBase.kt @@ -6,6 +6,7 @@ import com.example.estdelivery.shop.dto.ShopResponse import com.example.estdelivery.shop.service.ShopOwnerService import io.mockk.every import io.mockk.mockk +import io.mockk.slot import io.restassured.module.mockmvc.RestAssuredMockMvc import org.junit.jupiter.api.BeforeEach @@ -13,12 +14,20 @@ open class ShopBase { @BeforeEach fun setup() { val shopOwnerService = mockk() - val 첫_번째_가게 = ShopResponse(listOf(1, 2, 3), "첫 번째 가게", 1) - val 두_번째_가게 = ShopResponse(emptyList(), "두 번째 가게", 2) - every { shopOwnerService.findShopOwnerById(1) } returns ShopOwnerResponse(1, 첫_번째_가게) - every { shopOwnerService.findShopOwnerById(2) } returns ShopOwnerResponse(2, 두_번째_가게) - every { shopOwnerService.findShopOwnerByShopId(1) } returns ShopOwnerResponse(1, 첫_번째_가게) - every { shopOwnerService.findShopOwnerByShopId(2) } returns ShopOwnerResponse(2, 두_번째_가게) + val id = slot() + every { shopOwnerService.findShopOwnerById(capture(id)) } answers { + ShopOwnerResponse( + id.captured, + ShopResponse(listOf(1, 2, 3), "가게", id.captured) + ) + } + every { shopOwnerService.findShopOwnerByShopId(capture(id)) } answers { + ShopOwnerResponse( + id.captured, + ShopResponse(listOf(1, 2, 3), "가게", id.captured) + ) + } + every { shopOwnerService.addRoyalCustomers(any(), any()) } returns Unit RestAssuredMockMvc.standaloneSetup(ShopOwnerController(shopOwnerService)) } } diff --git a/shop/src/test/resources/contracts/shop/addRoyalCustomers.kts b/shop/src/test/resources/contracts/shop/addRoyalCustomers.kts new file mode 100644 index 0000000..9a04293 --- /dev/null +++ b/shop/src/test/resources/contracts/shop/addRoyalCustomers.kts @@ -0,0 +1,14 @@ +package shop + +import org.springframework.cloud.contract.spec.ContractDsl.Companion.contract + +contract { + name = "add royal customers" + request { + method = POST + url = url(v(regex("\\/owners\\/shop\\/[0-9]{1,10}\\/royal-customers\\/[0-9]{1,10}"))) + } + response { + status = OK + } +} diff --git a/shop/src/test/resources/contracts/shop/findShopOwnerByShopId.kts b/shop/src/test/resources/contracts/shop/findShopOwnerByShopId.kts index a10ec47..f31d8e1 100644 --- a/shop/src/test/resources/contracts/shop/findShopOwnerByShopId.kts +++ b/shop/src/test/resources/contracts/shop/findShopOwnerByShopId.kts @@ -2,47 +2,24 @@ package shop import org.springframework.cloud.contract.spec.ContractDsl.Companion.contract -listOf( - contract { - name = "get owner shop id is 1" - request { - method = GET - url = url("/owners/shop/1") - } - response { - status = OK - headers { - contentType = "application/json" - } - body = body( - "id" to 1, - "shop" to mapOf( - "royalCustomers" to listOf(1, 2, 3), - "name" to "첫 번째 가게", - "id" to 1 - ) - ) - } - }, - contract { - name = "get owner shop id is 2" - request { - method = GET - url = url("/owners/shop/2") +contract { + name = "get owner shop id" + request { + method = GET + url = url(v(regex("\\/owners\\/shop\\/[0-9]{1,10}"))) + } + response { + status = OK + headers { + contentType = "application/json" } - response { - status = OK - headers { - contentType = "application/json" - } - body = body( - "id" to 2, - "shop" to mapOf( - "royalCustomers" to emptyList(), - "name" to "두 번째 가게", - "id" to 2 - ) + body = body( + "id" to value("${fromRequest().path(2)}"), + "shop" to mapOf( + "royalCustomers" to listOf(1, 2, 3), + "name" to "가게", + "id" to value("${fromRequest().path(2)}") ) - } + ) } -) +} diff --git a/shop/src/test/resources/contracts/shop/findShopOwnerByShopOwnerId.kts b/shop/src/test/resources/contracts/shop/findShopOwnerByShopOwnerId.kts index 33b50aa..f32aab1 100644 --- a/shop/src/test/resources/contracts/shop/findShopOwnerByShopOwnerId.kts +++ b/shop/src/test/resources/contracts/shop/findShopOwnerByShopOwnerId.kts @@ -2,47 +2,24 @@ package shop import org.springframework.cloud.contract.spec.ContractDsl.Companion.contract -listOf( - contract { - name = "get owner id is 1" - request { - method = GET - url = url("/owners/1") - } - response { - status = OK - headers { - contentType = "application/json" - } - body = body( - "id" to 1, - "shop" to mapOf( - "royalCustomers" to listOf(1, 2, 3), - "name" to "첫 번째 가게", - "id" to 1 - ) - ) - } - }, - contract { - name = "get owner id is 2" - request { - method = GET - url = url("/owners/2") +contract { + name = "get owner id" + request { + method = GET + url = url(v(regex("\\/owners\\/[0-9]{1,10}"))) + } + response { + status = OK + headers { + contentType = "application/json" } - response { - status = OK - headers { - contentType = "application/json" - } - body = body( - "id" to 2, - "shop" to mapOf( - "royalCustomers" to emptyList(), - "name" to "두 번째 가게", - "id" to 2 - ) + body = body( + "id" to value("${fromRequest().path(1)}"), + "shop" to mapOf( + "royalCustomers" to listOf(1, 2, 3), + "name" to "가게", + "id" to value("${fromRequest().path(1)}") ) - } + ) } -) +} From b8589039ffcaee667a709d4de3ac1b77a1a406a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EA=B1=B4=EC=B0=BD?= <92219795+this-is-spear@users.noreply.github.com> Date: Wed, 15 May 2024 23:35:47 +0900 Subject: [PATCH 06/10] =?UTF-8?q?refactor=20:=20=EC=9D=B4=EB=B2=A4?= =?UTF-8?q?=ED=8A=B8=20=EA=B4=80=EB=A0=A8=20=EC=96=B4=EB=8C=91=ED=84=B0=20?= =?UTF-8?q?=EB=A1=9C=EC=A7=81=20=EA=B5=AC=ED=98=84=20=EB=B0=8F=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/IssueRandomCouponService.kt | 9 ++-- .../LoadRandomCouponIssueEventStatePort.kt | 4 +- .../UpdateRandomCouponIssueEventStatePort.kt | 5 +- .../port/out/adapter/EventAdapter.kt | 42 +++++++++++++++ .../out/adapter/infra/ShopInfraAdapter.kt | 1 - .../port/out/adapter/infra/dto/EventState.kt | 2 +- .../persistence/mapper/ShopOwnerMapper.kt | 1 - .../state/LoadRandomCouponIssueEventState.kt | 26 --------- .../UpdateRandomCouponIssueEventState.kt | 18 ------- .../IssueRandomCouponServiceTest.kt | 17 +++--- .../port/out/adapter/EventAdapterTest.kt | 54 +++++++++++++++++++ .../example/estdelivery/event/EventBase.kt | 34 ++++++------ .../resources/contracts/event/findEvent.kts | 4 +- .../contracts/event/participateMember.kts | 2 +- 14 files changed, 135 insertions(+), 84 deletions(-) create mode 100644 coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/EventAdapter.kt delete mode 100644 coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/state/LoadRandomCouponIssueEventState.kt delete mode 100644 coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/state/UpdateRandomCouponIssueEventState.kt create mode 100644 coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/EventAdapterTest.kt diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/IssueRandomCouponService.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/IssueRandomCouponService.kt index 3ed5a06..f160e94 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/IssueRandomCouponService.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/IssueRandomCouponService.kt @@ -9,7 +9,6 @@ import com.example.estdelivery.coupon.application.port.out.LoadShopOwnerStatePor import com.example.estdelivery.coupon.application.port.out.UpdateMemberStatePort import com.example.estdelivery.coupon.application.port.out.UpdateRandomCouponIssueEventStatePort import com.example.estdelivery.coupon.application.port.out.UpdateShopOwnerStatePort -import com.example.estdelivery.coupon.application.port.out.state.UpdateRandomCouponIssueEventState import com.example.estdelivery.coupon.application.utils.TransactionArea import com.example.estdelivery.coupon.domain.coupon.Coupon import com.example.estdelivery.coupon.domain.event.RandomCouponIssueEvent @@ -29,7 +28,7 @@ class IssueRandomCouponService( loadMemberStatePort.findMember(it.memberId) }, private val getCouponIssueRandomCouponIssueEvent: (IssueEventCouponCommand) -> RandomCouponIssueEvent = { - loadRandomCouponIssueEventStatePort.findById(it.eventId).toEvent() + loadRandomCouponIssueEventStatePort.findEvent(it.eventId) }, private val getShopOwner: (IssueEventCouponCommand) -> ShopOwner = { loadShopOwnerStatePort.findByShopId(it.shopId) @@ -37,8 +36,8 @@ class IssueRandomCouponService( private val createCoupon: (Coupon) -> Coupon = { createCouponStatePort.create(it) }, private val updateMember: (Member) -> Unit = { updateMemberStatePort.updateMembersCoupon(it) }, private val updateShopOwner: (ShopOwner) -> Unit = { updateShopOwnerStatePort.updateShopOwnersCoupons(it) }, - private val updateEvent: (RandomCouponIssueEvent) -> Unit = { - updateRandomCouponIssueEventStatePort.update(UpdateRandomCouponIssueEventState.from(it)) + private val participateEvent: (RandomCouponIssueEvent, Member) -> Unit = { event, member -> + updateRandomCouponIssueEventStatePort.participate(event, member) }, ) : IssueEventCouponUseCase { /** @@ -56,7 +55,7 @@ class IssueRandomCouponService( member.receiveCoupon(eventCoupon) updateMember(member) updateShopOwner(shopOwner) - updateEvent(event) + participateEvent(event, member) } } } diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/LoadRandomCouponIssueEventStatePort.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/LoadRandomCouponIssueEventStatePort.kt index 5740b65..4c584dd 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/LoadRandomCouponIssueEventStatePort.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/LoadRandomCouponIssueEventStatePort.kt @@ -1,7 +1,7 @@ package com.example.estdelivery.coupon.application.port.out -import com.example.estdelivery.coupon.application.port.out.state.LoadRandomCouponIssueEventState +import com.example.estdelivery.coupon.domain.event.RandomCouponIssueEvent interface LoadRandomCouponIssueEventStatePort { - fun findById(eventId: Long): LoadRandomCouponIssueEventState + fun findEvent(eventId: Long): RandomCouponIssueEvent } diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/UpdateRandomCouponIssueEventStatePort.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/UpdateRandomCouponIssueEventStatePort.kt index 935a941..c4197ec 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/UpdateRandomCouponIssueEventStatePort.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/UpdateRandomCouponIssueEventStatePort.kt @@ -1,7 +1,8 @@ package com.example.estdelivery.coupon.application.port.out -import com.example.estdelivery.coupon.application.port.out.state.UpdateRandomCouponIssueEventState +import com.example.estdelivery.coupon.domain.event.RandomCouponIssueEvent +import com.example.estdelivery.coupon.domain.member.Member interface UpdateRandomCouponIssueEventStatePort { - fun update(event: UpdateRandomCouponIssueEventState) + fun participate(event: RandomCouponIssueEvent, participatedMember: Member) } diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/EventAdapter.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/EventAdapter.kt new file mode 100644 index 0000000..7bddb75 --- /dev/null +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/EventAdapter.kt @@ -0,0 +1,42 @@ +package com.example.estdelivery.coupon.application.port.out.adapter + +import com.example.estdelivery.coupon.application.port.out.LoadRandomCouponIssueEventStatePort +import com.example.estdelivery.coupon.application.port.out.UpdateRandomCouponIssueEventStatePort +import com.example.estdelivery.coupon.application.port.out.adapter.infra.EventInfraAdapter +import com.example.estdelivery.coupon.application.port.out.adapter.infra.dto.UpdateParticipatedMembersState +import com.example.estdelivery.coupon.domain.event.DiscountAmountProbability +import com.example.estdelivery.coupon.domain.event.DiscountRange +import com.example.estdelivery.coupon.domain.event.RandomCouponIssueEvent +import com.example.estdelivery.coupon.domain.member.Member +import org.springframework.stereotype.Component + +@Component +class EventAdapter( + private val eventInfraAdapter: EventInfraAdapter, + private val memberAdapter: MemberAdapter, +) : LoadRandomCouponIssueEventStatePort, UpdateRandomCouponIssueEventStatePort { + override fun findEvent(eventId: Long) = eventInfraAdapter.findEvent(eventId).let { eventState -> + RandomCouponIssueEvent( + id = eventState.id, + description = eventState.description, + isProgress = eventState.isProgress, + discountType = eventState.discountType, + discountAmountProbability = DiscountAmountProbability( + intervalsProbability = eventState.intervalsProbability, + discountRange = DiscountRange(eventState.discountRangeMin, eventState.discountRangeMax) + ), + participatedMembers = eventState.participatedMembers.map { + memberAdapter.findMember(it) + }, + ) + } + + override fun participate(event: RandomCouponIssueEvent, participatedMember: Member) { + eventInfraAdapter.participateMember( + UpdateParticipatedMembersState( + id = event.id, + participatedMemberId = participatedMember.id + ) + ) + } +} diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/ShopInfraAdapter.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/ShopInfraAdapter.kt index 02cb6e1..a44caa4 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/ShopInfraAdapter.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/ShopInfraAdapter.kt @@ -3,7 +3,6 @@ package com.example.estdelivery.coupon.application.port.out.adapter.infra import com.example.estdelivery.coupon.application.port.out.adapter.infra.dto.ShopOwnerState import org.springframework.stereotype.Component import org.springframework.web.client.RestClient -import org.springframework.web.client.body @Component class ShopInfraAdapter( diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/dto/EventState.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/dto/EventState.kt index fd0dbaf..fda246a 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/dto/EventState.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/infra/dto/EventState.kt @@ -12,4 +12,4 @@ class EventState( val discountRangeMin: Int, val discountRangeMax: Int, val participatedMembers: List, -) \ No newline at end of file +) diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/mapper/ShopOwnerMapper.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/mapper/ShopOwnerMapper.kt index a8f513e..84e2d67 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/mapper/ShopOwnerMapper.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/persistence/mapper/ShopOwnerMapper.kt @@ -1,6 +1,5 @@ package com.example.estdelivery.coupon.application.port.out.adapter.persistence.mapper -import com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity.MemberEntity import com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity.ShopEntity import com.example.estdelivery.coupon.application.port.out.adapter.persistence.entity.ShopOwnerEntity import com.example.estdelivery.coupon.domain.coupon.CouponBook diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/state/LoadRandomCouponIssueEventState.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/state/LoadRandomCouponIssueEventState.kt deleted file mode 100644 index d2fa0f8..0000000 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/state/LoadRandomCouponIssueEventState.kt +++ /dev/null @@ -1,26 +0,0 @@ -package com.example.estdelivery.coupon.application.port.out.state - -import com.example.estdelivery.coupon.domain.event.DiscountAmountProbability -import com.example.estdelivery.coupon.domain.event.EventDiscountType -import com.example.estdelivery.coupon.domain.event.RandomCouponIssueEvent -import com.example.estdelivery.coupon.domain.member.Member - -class LoadRandomCouponIssueEventState( - private val id: Long, - private val description: String, - private val isProgress: Boolean, - private val disCountType: EventDiscountType, - private val probabilityRanges: DiscountAmountProbability, - private val participatedMembers: List, -) { - fun toEvent(): RandomCouponIssueEvent { - return RandomCouponIssueEvent( - id = id, - description = description, - isProgress = isProgress, - discountType = disCountType, - discountAmountProbability = probabilityRanges, - participatedMembers = participatedMembers, - ) - } -} diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/state/UpdateRandomCouponIssueEventState.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/state/UpdateRandomCouponIssueEventState.kt deleted file mode 100644 index b0f3e3e..0000000 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/port/out/state/UpdateRandomCouponIssueEventState.kt +++ /dev/null @@ -1,18 +0,0 @@ -package com.example.estdelivery.coupon.application.port.out.state - -import com.example.estdelivery.coupon.domain.event.RandomCouponIssueEvent -import com.example.estdelivery.coupon.domain.member.Member - -class UpdateRandomCouponIssueEventState( - val id: Long, - val participatedMembers: List, -) { - companion object { - fun from(event: RandomCouponIssueEvent): UpdateRandomCouponIssueEventState { - return UpdateRandomCouponIssueEventState( - event.id, - participatedMembers = event.showParticipatedMembers(), - ) - } - } -} diff --git a/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/IssueRandomCouponServiceTest.kt b/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/IssueRandomCouponServiceTest.kt index 8b66ea0..e82966b 100644 --- a/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/IssueRandomCouponServiceTest.kt +++ b/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/IssueRandomCouponServiceTest.kt @@ -8,13 +8,12 @@ import com.example.estdelivery.coupon.application.port.out.LoadShopOwnerStatePor import com.example.estdelivery.coupon.application.port.out.UpdateMemberStatePort import com.example.estdelivery.coupon.application.port.out.UpdateRandomCouponIssueEventStatePort import com.example.estdelivery.coupon.application.port.out.UpdateShopOwnerStatePort -import com.example.estdelivery.coupon.application.port.out.state.LoadRandomCouponIssueEventState -import com.example.estdelivery.coupon.application.port.out.state.UpdateRandomCouponIssueEventState import com.example.estdelivery.coupon.application.utils.TransactionArea import com.example.estdelivery.coupon.domain.event.DiscountAmountProbability import com.example.estdelivery.coupon.domain.event.DiscountRange import com.example.estdelivery.coupon.domain.event.EventDiscountType import com.example.estdelivery.coupon.domain.event.ProbabilityRange +import com.example.estdelivery.coupon.domain.event.RandomCouponIssueEvent import com.example.estdelivery.coupon.domain.fixture.새로_창업해서_아무것도_없는_프리퍼 import com.example.estdelivery.coupon.domain.fixture.이건창 import com.example.estdelivery.coupon.domain.fixture.이벤트_쿠폰 @@ -59,13 +58,13 @@ class IssueRandomCouponServiceTest : FreeSpec({ eventId = 1L, ) val 가게주인 = ShopOwner(새로_창업해서_아무것도_없는_프리퍼(), 2L) - val 랜덤_쿠폰_뽑기_이벤트_상태 = - LoadRandomCouponIssueEventState( + val 랜덤_쿠폰_뽑기_이벤트 = + RandomCouponIssueEvent( id = 1L, description = "고정 할인 쿠폰 제공 이벤트", isProgress = true, - disCountType = EventDiscountType.FIXED, - probabilityRanges = + discountType = EventDiscountType.FIXED, + discountAmountProbability = DiscountAmountProbability( listOf( ProbabilityRange(1000, 1500, 0.5), @@ -80,24 +79,22 @@ class IssueRandomCouponServiceTest : FreeSpec({ "이벤트로 고정 할인 쿠폰을 발급한다." { // given - val 상태가_변경된_이벤트 = slot() val 상태가_변경된_가게주인 = slot() val 상태가_변경된_회원 = slot() val 이건창 = 이건창() // when every { loadMemberStatePort.findMember(1L) } returns 이건창 - every { loadRandomCouponIssueEventStatePort.findById(1L) } returns 랜덤_쿠폰_뽑기_이벤트_상태 + every { loadRandomCouponIssueEventStatePort.findEvent(1L) } returns 랜덤_쿠폰_뽑기_이벤트 every { loadShopOwnerStatePort.findByShopId(1L) } returns 가게주인 every { updateMemberStatePort.updateMembersCoupon(capture(상태가_변경된_회원)) } returns Unit every { updateShopOwnerStatePort.updateShopOwnersCoupons(capture(상태가_변경된_가게주인)) } returns Unit - every { updateRandomCouponIssueEventStatePort.update(capture(상태가_변경된_이벤트)) } returns Unit + every { updateRandomCouponIssueEventStatePort.participate(any(), any()) } returns Unit every { createCouponStatePort.create(any()) } returns 이벤트_쿠폰 issueRandomCouponService.issueEventCoupon(이벤트_쿠폰_발급) // then - 상태가_변경된_이벤트.captured.participatedMembers.size shouldBe 1 상태가_변경된_가게주인.captured.showEventCouponInShop().size shouldBe 1 상태가_변경된_회원.captured.showMyCouponBook().size shouldBe 1 } diff --git a/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/EventAdapterTest.kt b/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/EventAdapterTest.kt new file mode 100644 index 0000000..2d8284e --- /dev/null +++ b/coupon/src/test/kotlin/com/example/estdelivery/coupon/application/port/out/adapter/EventAdapterTest.kt @@ -0,0 +1,54 @@ +package com.example.estdelivery.coupon.application.port.out.adapter + +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.repository.CouponRepository +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.repository.MemberRepository +import com.example.estdelivery.coupon.application.port.out.adapter.persistence.repository.ShopOwnerRepository +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.cloud.contract.stubrunner.spring.AutoConfigureStubRunner +import org.springframework.cloud.contract.stubrunner.spring.StubRunnerProperties + +@SpringBootTest +@AutoConfigureStubRunner( + stubsMode = StubRunnerProperties.StubsMode.LOCAL, + ids = [ + "com.example.estdelivery.member:member:1.0-SNAPSHOT:8081", + "com.example.estdelivery.event:event:1.0-SNAPSHOT:8083", + ] +) +class EventAdapterTest( + @Autowired + private val couponRepository: CouponRepository, + @Autowired + private val memberAdapter: MemberAdapter, + @Autowired + private val memberRepository: MemberRepository, + @Autowired + private val eventAdapter: EventAdapter, + @Autowired + private val shopOwnerRepository: ShopOwnerRepository, +) { + + @BeforeEach + fun setUp() { + memberRepository.deleteAll() + shopOwnerRepository.deleteAll() + couponRepository.deleteAll() + } + + @Test + fun `이벤트를 조회한다`() { + val event = eventAdapter.findEvent(1) + assertEquals(1L, event.id) + } + + @Test + fun `회원을 이벤트에 참가시킨다`() { + val event = eventAdapter.findEvent(1) + val member = memberAdapter.findMember(1) + eventAdapter.participate(event, member) + } +} diff --git a/event/src/test/kotlin/com/example/estdelivery/event/EventBase.kt b/event/src/test/kotlin/com/example/estdelivery/event/EventBase.kt index 7d7a23f..b599c9f 100644 --- a/event/src/test/kotlin/com/example/estdelivery/event/EventBase.kt +++ b/event/src/test/kotlin/com/example/estdelivery/event/EventBase.kt @@ -7,6 +7,7 @@ import com.example.estdelivery.event.entity.ProbabilityRange import com.example.estdelivery.event.service.EventService import io.mockk.every import io.mockk.mockk +import io.mockk.slot import io.restassured.module.mockmvc.RestAssuredMockMvc import org.junit.jupiter.api.BeforeEach @@ -14,22 +15,25 @@ open class EventBase { @BeforeEach fun setup() { val eventService = mockk() - every { eventService.findById(1) } returns EventResponse( - 1, - "이벤트 설명", - true, - EventDiscountType.FIXED, - listOf( - ProbabilityRange(1000, 1500, 0.5), - ProbabilityRange(1600, 2000, 0.3), - ProbabilityRange(2100, 2500, 0.2), - ), - 1000, - 2500, - listOf(1, 2, 3), - ) + val id = slot() + every { eventService.findById(capture(id)) } answers { + EventResponse( + id.captured, + "이벤트 설명", + true, + EventDiscountType.FIXED, + listOf( + ProbabilityRange(1000, 1500, 0.5), + ProbabilityRange(1600, 2000, 0.3), + ProbabilityRange(2100, 2500, 0.2), + ), + 1000, + 2500, + listOf(1, 2, 3), + ) + } - every { eventService.participate(1, 1) } returns Unit + every { eventService.participate(any(), any()) } returns Unit RestAssuredMockMvc.standaloneSetup(EventController(eventService)) } } diff --git a/event/src/test/resources/contracts/event/findEvent.kts b/event/src/test/resources/contracts/event/findEvent.kts index 21afb0f..718aa47 100644 --- a/event/src/test/resources/contracts/event/findEvent.kts +++ b/event/src/test/resources/contracts/event/findEvent.kts @@ -5,7 +5,7 @@ import org.springframework.cloud.contract.spec.ContractDsl.Companion.contract contract { request { method = GET - url = url("/events/1") + url = url(v(regex("/events/[0-9]{1,9}"))) } response { status = OK @@ -13,7 +13,7 @@ contract { contentType = "application/json" } body = body( - "id" to 1, + "id" to value("${fromRequest().path(1)}"), "description" to "이벤트 설명", "isProgress" to true, "discountType" to "FIXED", diff --git a/event/src/test/resources/contracts/event/participateMember.kts b/event/src/test/resources/contracts/event/participateMember.kts index f9a0920..858a3a7 100644 --- a/event/src/test/resources/contracts/event/participateMember.kts +++ b/event/src/test/resources/contracts/event/participateMember.kts @@ -4,7 +4,7 @@ import org.springframework.cloud.contract.spec.ContractDsl.Companion.contract contract { request { method = PUT - url = url("/events/1/participants/1") + url = url(v(regex("/events/[0-9]{1,9}/participants/[0-9]{1,9}"))) } response { status = OK From f9553e519f65a4ebff1cd23772fd4bb43b33b3e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EA=B1=B4=EC=B0=BD?= <92219795+this-is-spear@users.noreply.github.com> Date: Wed, 15 May 2024 23:39:44 +0900 Subject: [PATCH 07/10] =?UTF-8?q?refactor=20:=20=EB=B9=84=EC=A6=88?= =?UTF-8?q?=EB=8B=88=EC=8A=A4=20=EB=A1=9C=EC=A7=81=20=EC=84=9C=EB=B9=84?= =?UTF-8?q?=EC=8A=A4=20=EB=B9=88=20=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coupon/application/GiftCouponService.kt | 27 ++++++++------ .../application/HandoutCouponService.kt | 21 ++++++----- .../IssuePublishedCouponService.kt | 37 ++++++++++--------- .../application/IssueRandomCouponService.kt | 33 +++++++++-------- .../application/PublishCouponService.kt | 25 +++++++------ .../coupon/application/UseCouponService.kt | 37 ++++++++++--------- 6 files changed, 99 insertions(+), 81 deletions(-) diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/GiftCouponService.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/GiftCouponService.kt index f33aec0..bcc5521 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/GiftCouponService.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/GiftCouponService.kt @@ -8,24 +8,14 @@ import com.example.estdelivery.coupon.application.port.out.UpdateMemberStatePort import com.example.estdelivery.coupon.application.utils.TransactionArea import com.example.estdelivery.coupon.domain.coupon.Coupon import com.example.estdelivery.coupon.domain.member.Member +import org.springframework.stereotype.Service +@Service class GiftCouponService( loadMemberStatePort: LoadMemberStatePort, loadCouponStatePort: LoadCouponStatePort, updateMemberStatePort: UpdateMemberStatePort, private val transactionArea: TransactionArea, - private val getReceiver: (GiftCouponCommand) -> Member = { - loadMemberStatePort.findMember(it.receiverId) - }, - private val getSender: (GiftCouponCommand) -> Member = { - loadMemberStatePort.findMember(it.senderId) - }, - private val getCoupon: (GiftCouponCommand) -> Coupon = { - loadCouponStatePort.findById( - it.couponId - ) - }, - private val updateMember: (Member) -> Unit = { updateMemberStatePort.updateMembersCoupon(it) }, ) : GiftCouponUseCase { /** * 선물 할 쿠폰과 선물 할 회원의 식별자를 입력해 쿠폰을 나눠준다. @@ -53,4 +43,17 @@ class GiftCouponService( updateMember(sender) } } + + private val getReceiver: (GiftCouponCommand) -> Member = { + loadMemberStatePort.findMember(it.receiverId) + } + private val getSender: (GiftCouponCommand) -> Member = { + loadMemberStatePort.findMember(it.senderId) + } + private val getCoupon: (GiftCouponCommand) -> Coupon = { + loadCouponStatePort.findById( + it.couponId + ) + } + private val updateMember: (Member) -> Unit = { updateMemberStatePort.updateMembersCoupon(it) } } diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/HandoutCouponService.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/HandoutCouponService.kt index ddbdc4e..5867560 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/HandoutCouponService.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/HandoutCouponService.kt @@ -9,22 +9,15 @@ import com.example.estdelivery.coupon.application.port.out.UpdateShopOwnerStateP import com.example.estdelivery.coupon.application.utils.TransactionArea import com.example.estdelivery.coupon.domain.coupon.Coupon import com.example.estdelivery.coupon.domain.shop.ShopOwner +import org.springframework.stereotype.Service +@Service class HandoutCouponService( loadShopOwnerStatePort: LoadShopOwnerStatePort, loadCouponStatePort: LoadCouponStatePort, updateShopOwnerStatePort: UpdateShopOwnerStatePort, createCouponStatePort: CreateCouponStatePort, private val transactionArea: TransactionArea, - private val getShopOwner: (HandoutCouponCommand) -> ShopOwner = { - loadShopOwnerStatePort.findById( - it.shopOwnerId - ) - }, - private val notExistCoupon: (Long) -> Boolean = { loadCouponStatePort.exists(it).not() }, - private val getCoupon: (Long) -> Coupon = { loadCouponStatePort.findById(it) }, - private val updateShopOwner: (ShopOwner) -> Unit = { updateShopOwnerStatePort.updateShopOwnersCoupons(it) }, - private val createCoupon: (Coupon) -> Coupon = { createCouponStatePort.create(it) }, ) : HandoutCouponUseCase { /** * 1. 가게 주인 정보를 조회한다. @@ -50,4 +43,14 @@ class HandoutCouponService( return getCoupon(id) } + + private val getShopOwner: (HandoutCouponCommand) -> ShopOwner = { + loadShopOwnerStatePort.findById( + it.shopOwnerId + ) + } + private val notExistCoupon: (Long) -> Boolean = { loadCouponStatePort.exists(it).not() } + private val getCoupon: (Long) -> Coupon = { loadCouponStatePort.findById(it) } + private val updateShopOwner: (ShopOwner) -> Unit = { updateShopOwnerStatePort.updateShopOwnersCoupons(it) } + private val createCoupon: (Coupon) -> Coupon = { createCouponStatePort.create(it) } } diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/IssuePublishedCouponService.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/IssuePublishedCouponService.kt index bb26463..370b9c0 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/IssuePublishedCouponService.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/IssuePublishedCouponService.kt @@ -11,7 +11,9 @@ import com.example.estdelivery.coupon.application.utils.TransactionArea import com.example.estdelivery.coupon.domain.coupon.Coupon import com.example.estdelivery.coupon.domain.member.Member import com.example.estdelivery.coupon.domain.shop.ShopOwner +import org.springframework.stereotype.Service +@Service class IssuePublishedCouponService( loadMemberStatePort: LoadMemberStatePort, loadCouponStatePort: LoadCouponStatePort, @@ -19,23 +21,6 @@ class IssuePublishedCouponService( updateMemberStatePort: UpdateMemberStatePort, updateShopOwnerStatePort: UpdateShopOwnerStatePort, private val transactionArea: TransactionArea, - private val getMember: (IssuePublishedCouponCommand) -> Member = { - loadMemberStatePort.findMember(it.memberId) - }, - private val getCoupon: (IssuePublishedCouponCommand) -> Coupon = { - loadCouponStatePort.findById( - it.couponId - ) - }, - private val getShopOwner: (IssuePublishedCouponCommand) -> ShopOwner = { - loadShopOwnerStatePort.findByShopId( - it.shopId - ) - }, - private val updateMember: (Member) -> Unit = { updateMemberStatePort.updateMembersCoupon(it) }, - private val updateRoyalCustomers: (ShopOwner, Member) -> Unit = { shopOwner, royalCustomer -> - updateShopOwnerStatePort.updateRoyalCustomers(shopOwner, royalCustomer) - }, ) : IssuePublishedCouponUseCase { /** * 1. 회원 정보를 조회한다. @@ -66,4 +51,22 @@ class IssuePublishedCouponService( updateRoyalCustomers(shopOwner, member) } } + + private val getMember: (IssuePublishedCouponCommand) -> Member = { + loadMemberStatePort.findMember(it.memberId) + } + private val getCoupon: (IssuePublishedCouponCommand) -> Coupon = { + loadCouponStatePort.findById( + it.couponId + ) + } + private val getShopOwner: (IssuePublishedCouponCommand) -> ShopOwner = { + loadShopOwnerStatePort.findByShopId( + it.shopId + ) + } + private val updateMember: (Member) -> Unit = { updateMemberStatePort.updateMembersCoupon(it) } + private val updateRoyalCustomers: (ShopOwner, Member) -> Unit = { shopOwner, royalCustomer -> + updateShopOwnerStatePort.updateRoyalCustomers(shopOwner, royalCustomer) + } } diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/IssueRandomCouponService.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/IssueRandomCouponService.kt index f160e94..3d0eae7 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/IssueRandomCouponService.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/IssueRandomCouponService.kt @@ -14,7 +14,9 @@ import com.example.estdelivery.coupon.domain.coupon.Coupon import com.example.estdelivery.coupon.domain.event.RandomCouponIssueEvent import com.example.estdelivery.coupon.domain.member.Member import com.example.estdelivery.coupon.domain.shop.ShopOwner +import org.springframework.stereotype.Service +@Service class IssueRandomCouponService( loadMemberStatePort: LoadMemberStatePort, loadRandomCouponIssueEventStatePort: LoadRandomCouponIssueEventStatePort, @@ -24,21 +26,6 @@ class IssueRandomCouponService( updateRandomCouponIssueEventStatePort: UpdateRandomCouponIssueEventStatePort, createCouponStatePort: CreateCouponStatePort, private val transactionArea: TransactionArea, - private val getMember: (IssueEventCouponCommand) -> Member = { - loadMemberStatePort.findMember(it.memberId) - }, - private val getCouponIssueRandomCouponIssueEvent: (IssueEventCouponCommand) -> RandomCouponIssueEvent = { - loadRandomCouponIssueEventStatePort.findEvent(it.eventId) - }, - private val getShopOwner: (IssueEventCouponCommand) -> ShopOwner = { - loadShopOwnerStatePort.findByShopId(it.shopId) - }, - private val createCoupon: (Coupon) -> Coupon = { createCouponStatePort.create(it) }, - private val updateMember: (Member) -> Unit = { updateMemberStatePort.updateMembersCoupon(it) }, - private val updateShopOwner: (ShopOwner) -> Unit = { updateShopOwnerStatePort.updateShopOwnersCoupons(it) }, - private val participateEvent: (RandomCouponIssueEvent, Member) -> Unit = { event, member -> - updateRandomCouponIssueEventStatePort.participate(event, member) - }, ) : IssueEventCouponUseCase { /** * 1. 회원 정보를 조회한다. @@ -58,4 +45,20 @@ class IssueRandomCouponService( participateEvent(event, member) } } + + private val getMember: (IssueEventCouponCommand) -> Member = { + loadMemberStatePort.findMember(it.memberId) + } + private val getCouponIssueRandomCouponIssueEvent: (IssueEventCouponCommand) -> RandomCouponIssueEvent = { + loadRandomCouponIssueEventStatePort.findEvent(it.eventId) + } + private val getShopOwner: (IssueEventCouponCommand) -> ShopOwner = { + loadShopOwnerStatePort.findByShopId(it.shopId) + } + private val createCoupon: (Coupon) -> Coupon = { createCouponStatePort.create(it) } + private val updateMember: (Member) -> Unit = { updateMemberStatePort.updateMembersCoupon(it) } + private val updateShopOwner: (ShopOwner) -> Unit = { updateShopOwnerStatePort.updateShopOwnersCoupons(it) } + private val participateEvent: (RandomCouponIssueEvent, Member) -> Unit = { event, member -> + updateRandomCouponIssueEventStatePort.participate(event, member) + } } diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/PublishCouponService.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/PublishCouponService.kt index 54480fa..3cee459 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/PublishCouponService.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/PublishCouponService.kt @@ -8,23 +8,14 @@ import com.example.estdelivery.coupon.application.port.out.UpdateShopOwnerStateP import com.example.estdelivery.coupon.application.utils.TransactionArea import com.example.estdelivery.coupon.domain.coupon.Coupon import com.example.estdelivery.coupon.domain.shop.ShopOwner +import org.springframework.stereotype.Service +@Service class PublishCouponService( loadShopOwnerPort: LoadShopOwnerStatePort, createCouponStatePort: CreateCouponStatePort, updateShopOwnerStatePort: UpdateShopOwnerStatePort, private val transactionArea: TransactionArea, - private val findShopOwner: (PublishCouponCommand) -> ShopOwner = { - loadShopOwnerPort.findById( - it.shopOwnerId - ) - }, - private val createCoupon: (PublishCouponCommand) -> Coupon = { - createCouponStatePort.create( - it.coupon - ) - }, - private val updateShopOwner: (ShopOwner) -> Unit = { updateShopOwnerStatePort.updateShopOwnersCoupons(it) }, ) : PublishCouponUseCase { /** * 1. 가게 주인 정보를 조회한다. @@ -42,4 +33,16 @@ class PublishCouponService( updateShopOwner(shopOwner) } } + + private val findShopOwner: (PublishCouponCommand) -> ShopOwner = { + loadShopOwnerPort.findById( + it.shopOwnerId + ) + } + private val createCoupon: (PublishCouponCommand) -> Coupon = { + createCouponStatePort.create( + it.coupon + ) + } + private val updateShopOwner: (ShopOwner) -> Unit = { updateShopOwnerStatePort.updateShopOwnersCoupons(it) } } diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/UseCouponService.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/UseCouponService.kt index f6825c0..c5c54dd 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/UseCouponService.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/application/UseCouponService.kt @@ -11,7 +11,9 @@ import com.example.estdelivery.coupon.application.utils.TransactionArea import com.example.estdelivery.coupon.domain.coupon.Coupon import com.example.estdelivery.coupon.domain.member.Member import com.example.estdelivery.coupon.domain.shop.ShopOwner +import org.springframework.stereotype.Service +@Service class UseCouponService( loadMemberStatePort: LoadMemberStatePort, loadCouponStatePort: LoadCouponStatePort, @@ -19,23 +21,6 @@ class UseCouponService( updateMemberStatePort: UpdateMemberStatePort, updateShopOwnerStatePort: UpdateShopOwnerStatePort, private val transactionArea: TransactionArea, - private val getMember: (UseCouponCommand) -> Member = { - loadMemberStatePort.findMember( - it.memberId - ) - }, - private val getCoupon: (UseCouponCommand) -> Coupon = { - loadCouponStatePort.findById( - it.couponId - ) - }, - private val getShopOwner: (UseCouponCommand) -> ShopOwner = { - loadShopOwnerStatePort.findByShopId( - it.shopId - ) - }, - private val updateMember: (Member) -> Unit = { updateMemberStatePort.updateMembersCoupon(it) }, - private val updateShopOwner: (ShopOwner) -> Unit = { updateShopOwnerStatePort.updateShopOwnersCoupons(it) }, ) : UseCouponUseCase { /** * 1. 회원 정보를 조회한다. @@ -58,4 +43,22 @@ class UseCouponService( updateShopOwner(shopOwner) } } + + private val getMember: (UseCouponCommand) -> Member = { + loadMemberStatePort.findMember( + it.memberId + ) + } + private val getCoupon: (UseCouponCommand) -> Coupon = { + loadCouponStatePort.findById( + it.couponId + ) + } + private val getShopOwner: (UseCouponCommand) -> ShopOwner = { + loadShopOwnerStatePort.findByShopId( + it.shopId + ) + } + private val updateMember: (Member) -> Unit = { updateMemberStatePort.updateMembersCoupon(it) } + private val updateShopOwner: (ShopOwner) -> Unit = { updateShopOwnerStatePort.updateShopOwnersCoupons(it) } } From 9b6e5efabac1d4bb10be329bd3167a2f84a30239 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EA=B1=B4=EC=B0=BD?= <92219795+this-is-spear@users.noreply.github.com> Date: Wed, 15 May 2024 23:40:45 +0900 Subject: [PATCH 08/10] =?UTF-8?q?refactor=20:=20ShopOwner=20=EC=97=98?= =?UTF-8?q?=EB=B9=84=EC=8A=A4=20=EC=97=B0=EC=82=B0=EC=9E=90=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/example/estdelivery/coupon/domain/shop/ShopOwner.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/shop/ShopOwner.kt b/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/shop/ShopOwner.kt index 419791a..4fdee8e 100644 --- a/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/shop/ShopOwner.kt +++ b/coupon/src/main/kotlin/com/example/estdelivery/coupon/domain/shop/ShopOwner.kt @@ -41,7 +41,7 @@ class ShopOwner( } override fun hashCode(): Int { - return id.hashCode() ?: 0 + return id.hashCode() } override fun toString(): String { From 69ef4929044cbdc549db1cf7a37f760de370cc7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EA=B1=B4=EC=B0=BD?= <92219795+this-is-spear@users.noreply.github.com> Date: Wed, 15 May 2024 23:41:36 +0900 Subject: [PATCH 09/10] =?UTF-8?q?test=20:=20=EC=82=AC=EC=9A=A9=ED=95=98?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EB=8A=94=20=EB=B3=80=EC=88=98=20=EC=84=A0?= =?UTF-8?q?=EC=96=B8=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coupon/domain/event/DiscountAmountProbabilityTest.kt | 2 +- .../event/EqualDistributionRandomDoubleGeneratorKtTest.kt | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/coupon/src/test/kotlin/com/example/estdelivery/coupon/domain/event/DiscountAmountProbabilityTest.kt b/coupon/src/test/kotlin/com/example/estdelivery/coupon/domain/event/DiscountAmountProbabilityTest.kt index 14037f7..89c2cee 100644 --- a/coupon/src/test/kotlin/com/example/estdelivery/coupon/domain/event/DiscountAmountProbabilityTest.kt +++ b/coupon/src/test/kotlin/com/example/estdelivery/coupon/domain/event/DiscountAmountProbabilityTest.kt @@ -75,7 +75,7 @@ class DiscountAmountProbabilityTest : FreeSpec({ ), DiscountRange(10, 30), ) - checkAll { a -> + checkAll { val discountAmount = discountAmountProbability.getAmountBetween() collect(discountAmount) diff --git a/coupon/src/test/kotlin/com/example/estdelivery/coupon/domain/event/EqualDistributionRandomDoubleGeneratorKtTest.kt b/coupon/src/test/kotlin/com/example/estdelivery/coupon/domain/event/EqualDistributionRandomDoubleGeneratorKtTest.kt index 9cec801..4961bbe 100644 --- a/coupon/src/test/kotlin/com/example/estdelivery/coupon/domain/event/EqualDistributionRandomDoubleGeneratorKtTest.kt +++ b/coupon/src/test/kotlin/com/example/estdelivery/coupon/domain/event/EqualDistributionRandomDoubleGeneratorKtTest.kt @@ -5,7 +5,7 @@ import io.kotest.property.checkAll class EqualDistributionRandomDoubleGeneratorKtTest : FreeSpec({ "난수 값 생성 분포도를 확인한다." { - checkAll { a -> + checkAll { val random = generateDouble() when (random) { in 0.0..0.1 -> collect("statistic", "0.0..0.1") @@ -21,7 +21,7 @@ class EqualDistributionRandomDoubleGeneratorKtTest : FreeSpec({ } } - checkAll { a -> + checkAll { val random = generateInt(0, 10) collect(random) } From ba93bdf61c83e80e6c18666dc419609a50a3498d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EA=B1=B4=EC=B0=BD?= <92219795+this-is-spear@users.noreply.github.com> Date: Wed, 15 May 2024 23:42:45 +0900 Subject: [PATCH 10/10] =?UTF-8?q?chore=20:=20=EC=95=A1=EC=85=98=20?= =?UTF-8?q?=ED=8C=8C=EC=9D=BC=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/actions_build.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/.github/workflows/actions_build.yml b/.github/workflows/actions_build.yml index 49515af..764e787 100644 --- a/.github/workflows/actions_build.yml +++ b/.github/workflows/actions_build.yml @@ -38,6 +38,16 @@ jobs: - name: ☑️ Validate Gradle wrapper uses: gradle/wrapper-validation-action@e6e38bacfdf1a337459f332974bb2327a31aaf4b + - name: ✅ Contract Test with Gradle + uses: gradle/gradle-build-action@67421db6bd0bf253fb4bd25b31ebb98943c375e1 + with: + arguments: contractTest + + - name: ✅ Publish To Maven Local + uses: gradle/gradle-build-action@67421db6bd0bf253fb4bd25b31ebb98943c375e1 + with: + arguments: publishToMavenLocal + - name: ✅ Test with Gradle uses: gradle/gradle-build-action@67421db6bd0bf253fb4bd25b31ebb98943c375e1 with: