diff --git a/server/src/main/java/server/haengdong/domain/bill/Bill.java b/server/src/main/java/server/haengdong/domain/bill/Bill.java index 69be84523..6face8274 100644 --- a/server/src/main/java/server/haengdong/domain/bill/Bill.java +++ b/server/src/main/java/server/haengdong/domain/bill/Bill.java @@ -33,7 +33,6 @@ public class Bill { private static final int MAX_TITLE_LENGTH = 30; private static final long MIN_PRICE = 1L; private static final long MAX_PRICE = 10_000_000L; - private static final long DEFAULT_PRICE = 0L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) diff --git a/server/src/main/java/server/haengdong/domain/bill/BillDetailRepository.java b/server/src/main/java/server/haengdong/domain/bill/BillDetailRepository.java index d49c01ab2..7d656783b 100644 --- a/server/src/main/java/server/haengdong/domain/bill/BillDetailRepository.java +++ b/server/src/main/java/server/haengdong/domain/bill/BillDetailRepository.java @@ -1,6 +1,5 @@ package server.haengdong.domain.bill; -import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; @@ -11,15 +10,6 @@ @Repository public interface BillDetailRepository extends JpaRepository { - List findAllByBill(Bill bill); - - @Query(""" - select bd from BillDetail bd - join fetch bd.member - where bd.bill.id = :billId - """) - List findAllByBillId(Long billId); - @Modifying @Query("delete from BillDetail bd where bd.member = :member") void deleteAllByMember(@Param("member") Member member); diff --git a/server/src/main/java/server/haengdong/domain/bill/BillRepository.java b/server/src/main/java/server/haengdong/domain/bill/BillRepository.java index 2c0b16956..bbafbb4db 100644 --- a/server/src/main/java/server/haengdong/domain/bill/BillRepository.java +++ b/server/src/main/java/server/haengdong/domain/bill/BillRepository.java @@ -3,13 +3,30 @@ import java.util.List; import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; import server.haengdong.domain.event.Event; @Repository public interface BillRepository extends JpaRepository { + @Query(""" + select b + from Bill b + join fetch b.billDetails bd + join fetch bd.member + where b.event = :event + """) List findAllByEvent(Event event); + @Query(""" + select b + from Bill b + join fetch b.billDetails bd + join fetch bd.member + where b.event = :event + order by b.id desc + limit 1 + """) Optional findFirstByEventOrderByIdDesc(Event event); } diff --git a/server/src/test/java/server/haengdong/application/BillServiceTest.java b/server/src/test/java/server/haengdong/application/BillServiceTest.java index 72fd0192f..347e6712a 100644 --- a/server/src/test/java/server/haengdong/application/BillServiceTest.java +++ b/server/src/test/java/server/haengdong/application/BillServiceTest.java @@ -22,10 +22,10 @@ import server.haengdong.domain.bill.BillDetail; import server.haengdong.domain.bill.BillDetailRepository; import server.haengdong.domain.bill.BillRepository; -import server.haengdong.domain.member.Member; -import server.haengdong.domain.member.MemberRepository; import server.haengdong.domain.event.Event; import server.haengdong.domain.event.EventRepository; +import server.haengdong.domain.member.Member; +import server.haengdong.domain.member.MemberRepository; import server.haengdong.exception.HaengdongException; import server.haengdong.support.fixture.Fixture; @@ -51,25 +51,67 @@ class BillServiceTest extends ServiceTestSupport { void findSteps() { Event event = Fixture.EVENT1; eventRepository.save(event); - Member member = new Member(event, "토다리"); - memberRepository.save(member); - Bill bill = Bill.create(event, "행동대장 회식", 100000L, List.of(member)); - billRepository.save(bill); + Member member1 = new Member(event, "토다리"); + Member member2 = new Member(event, "쿠키"); + Member member3 = new Member(event, "소하"); + Member member4 = new Member(event, "웨디"); + memberRepository.saveAll(List.of(member1, member2, member3, member4)); + Bill bill1 = Bill.create(event, "행동대장 회식1", 100000L, List.of(member1, member2, member3)); + Bill bill2 = Bill.create(event, "행동대장 회식2", 200000L, List.of(member1, member2, member3, member4)); + Bill bill3 = Bill.create(event, "행동대장 회식3", 300000L, List.of(member1, member2, member3, member4)); + Bill bill4 = Bill.create(event, "행동대장 회식4", 400000L, List.of(member2, member3, member4)); + billRepository.saveAll(List.of(bill1, bill2, bill3, bill4)); List steps = billService.findSteps(event.getToken()); + assertThat(steps).hasSize(3); + assertThat(steps.get(0).bills()).hasSize(1) .extracting(BillAppResponse::id, BillAppResponse::title, BillAppResponse::price, BillAppResponse::isFixed) .containsExactlyInAnyOrder( - tuple(bill.getId(), bill.getTitle(), bill.getPrice(), bill.isFixed()) + tuple(bill1.getId(), bill1.getTitle(), bill1.getPrice(), bill1.isFixed()) + ); + + assertThat(steps.get(0).members()).hasSize(3) + .extracting(MemberAppResponse::id, MemberAppResponse::name) + .containsExactlyInAnyOrder( + tuple(member1.getId(), member1.getName()), + tuple(member2.getId(), member2.getName()), + tuple(member3.getId(), member3.getName()) + ); + + assertThat(steps.get(1).bills()).hasSize(2) + .extracting(BillAppResponse::id, BillAppResponse::title, BillAppResponse::price, + BillAppResponse::isFixed) + .containsExactlyInAnyOrder( + tuple(bill2.getId(), bill2.getTitle(), bill2.getPrice(), bill2.isFixed()), + tuple(bill3.getId(), bill3.getTitle(), bill3.getPrice(), bill3.isFixed()) ); - assertThat(steps.get(0).members()).hasSize(1) + assertThat(steps.get(1).members()).hasSize(4) .extracting(MemberAppResponse::id, MemberAppResponse::name) .containsExactlyInAnyOrder( - tuple(member.getId(), member.getName()) + tuple(member1.getId(), member1.getName()), + tuple(member2.getId(), member2.getName()), + tuple(member3.getId(), member3.getName()), + tuple(member4.getId(), member4.getName()) ); + + assertThat(steps.get(2).bills()).hasSize(1) + .extracting(BillAppResponse::id, BillAppResponse::title, BillAppResponse::price, + BillAppResponse::isFixed) + .containsExactlyInAnyOrder( + tuple(bill4.getId(), bill4.getTitle(), bill4.getPrice(), bill4.isFixed()) + ); + + assertThat(steps.get(2).members()).hasSize(3) + .extracting(MemberAppResponse::id, MemberAppResponse::name) + .containsExactlyInAnyOrder( + tuple(member2.getId(), member2.getName()), + tuple(member3.getId(), member3.getName()), + tuple(member4.getId(), member4.getName()) + ); } @DisplayName("지출 내역을 생성한다.") @@ -109,7 +151,9 @@ void saveBillTest1() { billService.saveBill(event.getToken(), request); - List billDetails = billDetailRepository.findAllByBillId(1L); + List bills = billRepository.findAllByEvent(event); + + List billDetails = bills.get(0).getBillDetails(); assertThat(billDetails) .hasSize(2) @@ -195,8 +239,8 @@ void updateBill2() { billService.updateBill(event.getToken(), bill.getId(), request); - Bill updatedBill = billRepository.findById(bill.getId()).get(); - List billDetails = billDetailRepository.findAllByBill(updatedBill); + Bill updatedBill = billRepository.findAllByEvent(event).get(0); + List billDetails = updatedBill.getBillDetails(); assertThat(billDetails).hasSize(4) .extracting(BillDetail::getPrice) @@ -272,7 +316,8 @@ void updateBillDetailsTest2() { billService.updateBillDetails(event1.getToken(), bill.getId(), request); - List foundBillDetails = billDetailRepository.findAllByBill(bill); + Bill foundBill = billRepository.findAllByEvent(event1).get(0); + List foundBillDetails = foundBill.getBillDetails(); assertThat(foundBillDetails).hasSize(2) .extracting(BillDetail::getId, BillDetail::getPrice)