Skip to content

Commit

Permalink
feat: 쿼리 최적화 (#598)
Browse files Browse the repository at this point in the history
  • Loading branch information
Arachneee authored Sep 25, 2024
1 parent e0b5080 commit f20eb8e
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -11,15 +10,6 @@
@Repository
public interface BillDetailRepository extends JpaRepository<BillDetail, Long> {

List<BillDetail> findAllByBill(Bill bill);

@Query("""
select bd from BillDetail bd
join fetch bd.member
where bd.bill.id = :billId
""")
List<BillDetail> findAllByBillId(Long billId);

@Modifying
@Query("delete from BillDetail bd where bd.member = :member")
void deleteAllByMember(@Param("member") Member member);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Bill, Long> {

@Query("""
select b
from Bill b
join fetch b.billDetails bd
join fetch bd.member
where b.event = :event
""")
List<Bill> 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<Bill> findFirstByEventOrderByIdDesc(Event event);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<StepAppResponse> 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("지출 내역을 생성한다.")
Expand Down Expand Up @@ -109,7 +151,9 @@ void saveBillTest1() {

billService.saveBill(event.getToken(), request);

List<BillDetail> billDetails = billDetailRepository.findAllByBillId(1L);
List<Bill> bills = billRepository.findAllByEvent(event);

List<BillDetail> billDetails = bills.get(0).getBillDetails();

assertThat(billDetails)
.hasSize(2)
Expand Down Expand Up @@ -195,8 +239,8 @@ void updateBill2() {

billService.updateBill(event.getToken(), bill.getId(), request);

Bill updatedBill = billRepository.findById(bill.getId()).get();
List<BillDetail> billDetails = billDetailRepository.findAllByBill(updatedBill);
Bill updatedBill = billRepository.findAllByEvent(event).get(0);
List<BillDetail> billDetails = updatedBill.getBillDetails();

assertThat(billDetails).hasSize(4)
.extracting(BillDetail::getPrice)
Expand Down Expand Up @@ -272,7 +316,8 @@ void updateBillDetailsTest2() {

billService.updateBillDetails(event1.getToken(), bill.getId(), request);

List<BillDetail> foundBillDetails = billDetailRepository.findAllByBill(bill);
Bill foundBill = billRepository.findAllByEvent(event1).get(0);
List<BillDetail> foundBillDetails = foundBill.getBillDetails();

assertThat(foundBillDetails).hasSize(2)
.extracting(BillDetail::getId, BillDetail::getPrice)
Expand Down

0 comments on commit f20eb8e

Please sign in to comment.