Skip to content

Commit

Permalink
fix: 인기 순위 에러 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
Cho-D-YoungRae committed Apr 10, 2024
1 parent 5ee0fa2 commit 229e120
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,7 @@ List<Show> findAllWithByGenreAndEndDateGreaterThanEqualAndUseYnIsTrue(

@Lock(LockModeType.OPTIMISTIC)
Optional<Show> findWithLockById(ShowId id);

@EntityGraph(attributePaths = {"facility"})
List<Show> findAllWithFacilityByIdIn(List<ShowId> id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import org.cmc.curtaincall.web.show.response.BoxOfficeResponse;
import org.cmc.curtaincall.web.common.response.ListResult;
import org.cmc.curtaincall.web.common.response.With;
import org.cmc.curtaincall.web.show.response.ShowRank;
import org.cmc.curtaincall.web.show.response.ShowResponse;
import org.cmc.curtaincall.web.show.response.ShowReviewStatsDto;
import org.cmc.curtaincall.web.show.response.ShowReviewStatsResponse;
import org.springframework.validation.annotation.Validated;
Expand All @@ -15,6 +17,7 @@

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@RestController
Expand All @@ -25,19 +28,28 @@ public class BoxOfficeController {

private final ShowReviewStatsQueryService showReviewStatsQueryService;

private final ShowService showService;

@GetMapping("/box-office")
public ListResult<With<BoxOfficeResponse, ShowReviewStatsResponse>> getList(
public ListResult<With<With<ShowRank, ShowResponse>, ShowReviewStatsResponse>> getList(
@ModelAttribute @Validated BoxOfficeRequest request
) {
final List<BoxOfficeResponse> boxOfficeResponses = boxOfficeService.getList(request);
final List<ShowId> showIds = boxOfficeResponses.stream().map(BoxOfficeResponse::id).toList();
final Map<ShowId, ShowReviewStatsResponse> showIdToStats = showReviewStatsQueryService.getList(showIds)
.stream()
.collect(Collectors.toMap(ShowReviewStatsDto::showId, ShowReviewStatsResponse::of));
final Map<ShowId, ShowResponse> showIdToShow = showService.getList(showIds)
.stream()
.collect(Collectors.toMap(ShowResponse::id, Function.identity()));
return new ListResult<>(boxOfficeResponses.stream()
.map(show -> new With<>(
show, showIdToStats.getOrDefault(show.id(), new ShowReviewStatsResponse(0, 0L, 0D)
))).toList()
.filter(boxOffice -> showIdToShow.containsKey(boxOffice.id()))
.filter(boxOffice -> showIdToStats.containsKey(boxOffice.id()))
.map(boxOffice -> new With<>(new With<>(
new ShowRank(boxOffice.rank()),
showIdToShow.get(boxOffice.id())),
showIdToStats.get(boxOffice.id())
)).toList()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public List<ShowResponse> getList(final ShowListRequest request, final Pageable
).stream().map(ShowResponse::of).toList();
}

public List<ShowResponse> getList(final List<ShowId> showIds) {
return showRepository.findAllWithFacilityByIdIn(showIds).stream().map(ShowResponse::of).toList();
}

public List<ShowResponse> search(final Pageable pageable, final String keyword) {
return showRepository.findAllWithByNameStartsWithAndUseYnIsTrue(pageable, keyword)
.stream().map(ShowResponse::of).toList();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.cmc.curtaincall.web.show.config;

import org.cmc.curtaincall.domain.show.repository.ShowRepository;
import org.cmc.curtaincall.web.show.infra.KopisBoxOfficeService;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
Expand All @@ -11,7 +10,7 @@
public class BoxOfficeConfig {

@Bean
public KopisBoxOfficeService kopisBoxOfficeService(KopisProperties properties, ShowRepository showRepository) {
return new KopisBoxOfficeService(properties.getBaseUrl(), properties.getServiceKey(), showRepository);
public KopisBoxOfficeService kopisBoxOfficeService(KopisProperties properties) {
return new KopisBoxOfficeService(properties.getBaseUrl(), properties.getServiceKey());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

import lombok.extern.slf4j.Slf4j;
import org.cmc.curtaincall.domain.show.BoxOfficeGenre;
import org.cmc.curtaincall.domain.show.Show;
import org.cmc.curtaincall.domain.show.ShowId;
import org.cmc.curtaincall.domain.show.repository.ShowRepository;
import org.cmc.curtaincall.web.show.BoxOfficeService;
import org.cmc.curtaincall.web.show.request.BoxOfficeRequest;
import org.cmc.curtaincall.web.show.response.BoxOfficeResponse;
Expand All @@ -19,13 +16,10 @@

import java.time.Duration;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

@CacheConfig(cacheNames = "boxOffices")
Expand All @@ -41,41 +35,23 @@ public class KopisBoxOfficeService implements BoxOfficeService {

private final RestTemplate restTemplate;

public KopisBoxOfficeService(final String baseUrl, final String serviceKey, final ShowRepository showRepository) {
public KopisBoxOfficeService(final String baseUrl, final String serviceKey) {
this.serviceKey = serviceKey;
this.restTemplate = new RestTemplateBuilder()
.rootUri(baseUrl)
.setConnectTimeout(Duration.ofMillis(4000L))
.build();
this.showRepository = showRepository;
}

private final ShowRepository showRepository;

@Override
@Cacheable(key = "#request")
public List<BoxOfficeResponse> getList(final BoxOfficeRequest request) {
final List<KopisBoxOfficeResponse> boxOfficeResponses = requestEntity(request).getContent().stream()
.filter(response -> handledGenreName.contains(response.getGenreName()))
.toList();
final List<ShowId> showIds = boxOfficeResponses.stream()
.map(KopisBoxOfficeResponse::getShowId)
.toList();
final Map<ShowId, Show> showIdToShow = showRepository.findAllById(showIds)
.stream()
.collect(Collectors.toMap(Show::getId, Function.identity()));
return boxOfficeResponses.stream()
.filter(boxOffice -> showIdToShow.containsKey(boxOffice.getShowId()))
.map(boxOffice -> BoxOfficeResponse.builder()
.id(boxOffice.getShowId())
.name(showIdToShow.get(boxOffice.getShowId()).getName())
.startDate(showIdToShow.get(boxOffice.getShowId()).getStartDate())
.endDate(showIdToShow.get(boxOffice.getShowId()).getEndDate())
.facilityName(boxOffice.getFacilityName())
.poster(showIdToShow.get(boxOffice.getShowId()).getPoster())
.genre(showIdToShow.get(boxOffice.getShowId()).getGenre())
.showTimes(new ArrayList<>(showIdToShow.get(boxOffice.getShowId()).getShowTimes()))
.runtime(showIdToShow.get(boxOffice.getShowId()).getRuntime())
.rank(boxOffice.getRank())
.build()
).toList();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
package org.cmc.curtaincall.web.show.response;

import lombok.Builder;
import org.cmc.curtaincall.domain.show.ShowGenre;
import org.cmc.curtaincall.domain.show.ShowId;
import org.cmc.curtaincall.domain.show.ShowTime;

import java.time.LocalDate;
import java.util.List;

@Builder
public record BoxOfficeResponse(
ShowId id, // 공연 ID
String name, // 공연명
LocalDate startDate, // 공연 시작일
LocalDate endDate, // 공연 종료일
String facilityName, // 공연 시설명(공연장명)
String poster, // 공연 포스터 경로
ShowGenre genre, // 공연 장르명
List<ShowTime> showTimes,
String runtime,
int rank
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.cmc.curtaincall.web.show.response;

public record ShowRank(
int rank
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@

import org.cmc.curtaincall.domain.show.BoxOfficeGenre;
import org.cmc.curtaincall.domain.show.BoxOfficeType;
import org.cmc.curtaincall.domain.show.ShowDay;
import org.cmc.curtaincall.domain.show.ShowGenre;
import org.cmc.curtaincall.domain.show.ShowId;
import org.cmc.curtaincall.domain.show.ShowTime;
import org.cmc.curtaincall.web.common.AbstractWebTest;
import org.cmc.curtaincall.web.show.request.BoxOfficeRequest;
import org.cmc.curtaincall.web.show.response.BoxOfficeResponse;
import org.cmc.curtaincall.web.show.response.ShowResponse;
import org.cmc.curtaincall.web.show.response.ShowReviewStatsDto;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;

import static org.mockito.BDDMockito.given;
Expand All @@ -38,24 +42,38 @@ class BoxOfficeControllerDocsTest extends AbstractWebTest {
@MockBean
private ShowReviewStatsQueryService showReviewStatsQueryService;

@MockBean
private ShowService showService;

@Test
void getBoxOffice_Docs() throws Exception {
// given
var boxOfficeResponse = BoxOfficeResponse.builder()
.id(new ShowId("PF223822"))
.rank(1)
.build();
given(boxOfficeService.getList(new BoxOfficeRequest(
BoxOfficeType.WEEK, LocalDate.of(2023, 8, 17), BoxOfficeGenre.PLAY))
).willReturn(List.of(boxOfficeResponse));

final ShowResponse showResponse = ShowResponse.builder()
.id(new ShowId("PF223822"))
.name("그날밤, 너랑 나 [대학로]")
.startDate(LocalDate.of(2023, 8, 14))
.endDate(LocalDate.of(2023, 8, 31))
.facilityName("대학로 아트포레스트")
.poster("poster-url")
.genre(ShowGenre.PLAY)
.rank(1)
.showTimes(List.of(
new ShowTime(ShowDay.FRIDAY, LocalTime.of(1, 30))
))
.runtime("2시간 30분")
.build();
given(boxOfficeService.getList(new BoxOfficeRequest(
BoxOfficeType.WEEK, LocalDate.of(2023, 8, 17), BoxOfficeGenre.PLAY))
).willReturn(List.of(boxOfficeResponse));
given(showService.getList(List.of(new ShowId("PF223822")))).willReturn(List.of(showResponse));


final var showReviewStatsDto = ShowReviewStatsDto.builder()
.showId(new ShowId("PF220846"))
.showId(new ShowId("PF223822"))
.reviewCount(10)
.reviewGradeSum(48L)
.reviewGradeAvg(((double) 10) / 48)
Expand Down Expand Up @@ -93,6 +111,8 @@ void getBoxOffice_Docs() throws Exception {
fieldWithPath("poster").description("공연 포스터 경로"),
fieldWithPath("genre").type(ShowGenre.class.getSimpleName()).description("공연 장르명"),
fieldWithPath("showTimes").description("공연 시간"),
fieldWithPath("showTimes[].dayOfWeek").description("공연 요일"),
fieldWithPath("showTimes[].time").description("공연 시간"),
fieldWithPath("runtime").description("공연 시간"),
fieldWithPath("reviewCount").description("리뷰 수"),
fieldWithPath("reviewGradeSum").description("리뷰 점수 합"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.io.IOException;
import java.net.URI;
import java.time.LocalDate;
import java.util.Collections;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -56,8 +55,7 @@ void init() {
showRepository = mock(ShowRepository.class);
kopisBoxOfficeService = new KopisBoxOfficeService(
String.format("http://localhost:%s", mockWebServer.getPort()),
"test-service-key",
showRepository
"test-service-key"
);
}

Expand Down Expand Up @@ -104,11 +102,6 @@ void getList() throws InterruptedException {

final BoxOfficeResponse boxOfficeResponse = result.get(0);
assertThat(boxOfficeResponse.id()).isEqualTo(new ShowId("PF227565"));
assertThat(boxOfficeResponse.name()).isEqualTo("name");
assertThat(boxOfficeResponse.startDate()).isEqualTo(LocalDate.of(2023, 11, 9));
assertThat(boxOfficeResponse.endDate()).isEqualTo(LocalDate.of(2023, 11, 10));
assertThat(boxOfficeResponse.poster()).isEqualTo("poster");
assertThat(boxOfficeResponse.genre()).isEqualTo(ShowGenre.MUSICAL);
assertThat(boxOfficeResponse.rank()).isEqualTo(4);

final RecordedRequest recordedRequest = mockWebServer.takeRequest();
Expand All @@ -122,37 +115,4 @@ void getList() throws InterruptedException {
);
}

@Test
void getList_given_not_exists_show_then_skip() {
// given
mockWebServer.enqueue(new MockResponse()
.setBody("""
<?xml version="1.0" encoding="UTF-8"?>
<boxofs>
<boxof>
<prfplcnm>충무아트센터 대공연장</prfplcnm>
<seatcnt>1250</seatcnt>
<rnum>4</rnum>
<poster>/upload/pfmPoster/PF_PF227565_231012_095437.gif</poster>
<prfpd>2023.11.21~2024.02.25</prfpd>
<mt20id>PF227565</mt20id>
<prfnm>몬테크리스토</prfnm>
<cate>뮤지컬</cate>
<prfdtcnt>56</prfdtcnt>
<area>서울</area>
</boxof>
</boxofs>
""")
.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE)
);
given(showRepository.findAllById(List.of(new ShowId("PF227565")))).willReturn(Collections.emptyList());

// when
final BoxOfficeRequest request = new BoxOfficeRequest(
BoxOfficeType.WEEK, LocalDate.of(2023, 11, 9), null);
final List<BoxOfficeResponse> result = kopisBoxOfficeService.getList(request);

// then
assertThat(result).isEmpty();
}
}

0 comments on commit 229e120

Please sign in to comment.