Skip to content

Commit

Permalink
[BE] refactor: /stages/{stageId}/tickets API의 N+1을 해결한다.(#517) (#578)
Browse files Browse the repository at this point in the history
* refactor: findAllByStageId fetch 조인으로 변경

* refactor: fetch join 메서드 사용하도록 변경

* chore: Inner 조건 명시하도록 변경
  • Loading branch information
seokjin8678 authored Nov 14, 2023
1 parent a21401f commit 1eeb1c7
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ private Stage findStageById(Long stageId) {

@Transactional(readOnly = true)
public StageTicketsResponse findStageTickets(Long stageId) {
return StageTicketsResponse.from(ticketRepository.findAllByStageId(stageId));
return StageTicketsResponse.from(ticketRepository.findAllByStageIdWithFetch(stageId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@

public interface TicketRepository extends JpaRepository<Ticket, Long> {

List<Ticket> findAllByStageId(Long stageId);
@Query("""
SELECT t from Ticket t
INNER JOIN FETCH t.ticketAmount
WHERE t.stage.id = :stageId
""")
List<Ticket> findAllByStageIdWithFetch(@Param("stageId") Long stageId);

Optional<Ticket> findByTicketTypeAndStage(TicketType ticketType, Stage stage);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class TicketServiceTest {
TicketFixture.ticket().id(1L).ticketType(TicketType.STUDENT).stage(stage).build(),
TicketFixture.ticket().id(2L).ticketType(TicketType.VISITOR).stage(stage).build()
);
given(ticketRepository.findAllByStageId(stageId))
given(ticketRepository.findAllByStageIdWithFetch(stageId))
.willReturn(tickets);

// when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class TicketRepositoryTest {
ticketRepository.save(TicketFixture.ticket().stage(otherStage).build());

// when
List<Ticket> actual = ticketRepository.findAllByStageId(stage.getId());
List<Ticket> actual = ticketRepository.findAllByStageIdWithFetch(stage.getId());

// then
assertThat(actual).hasSize(2);
Expand Down

0 comments on commit 1eeb1c7

Please sign in to comment.