Skip to content

Commit

Permalink
Merge pull request #65 from BOOK-TALK/#62-fix-period-trend
Browse files Browse the repository at this point in the history
[refactor] aWeekTrend, aMonthTrend API에 커스텀 페이지네이션 적용
  • Loading branch information
chanwoo7 authored Aug 18, 2024
2 parents 2ae8b22 + ab512b0 commit bbdaf72
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@ public ResponseEntity<?> aWeekTrend(@RequestParam String genreCode,

LoanItemSrchRequestDto requestDto = LoanItemSrchRequestDto.builder()
.dtl_kdc(genreCode)
.pageNo(pageNo)
.pageSize(pageSize)
.build();
LinkedList<LoanItemSrchResponseDto> response = genreService.periodToNowTrend(requestDto, 7);
LinkedList<LoanItemSrchResponseDto> response = genreService.periodToNowTrend(requestDto, 7, pageNo, pageSize);

return responseTemplate.success(response, HttpStatus.OK);
}
Expand All @@ -70,10 +68,8 @@ public ResponseEntity<?> aMonthTrend(@RequestParam String genreCode,

LoanItemSrchRequestDto requestDto = LoanItemSrchRequestDto.builder()
.dtl_kdc(genreCode)
.pageNo(pageNo)
.pageSize(pageSize)
.build();
LinkedList<LoanItemSrchResponseDto> response = genreService.periodToNowTrend(requestDto, 30);
LinkedList<LoanItemSrchResponseDto> response = genreService.periodToNowTrend(requestDto, 30, pageNo, pageSize);

return responseTemplate.success(response, HttpStatus.OK);
}
Expand All @@ -96,10 +92,8 @@ public ResponseEntity<?> thisWeekTrend(@RequestParam String genreCode,

LoanItemSrchRequestDto requestDto = LoanItemSrchRequestDto.builder()
.dtl_kdc(genreCode)
.pageNo(pageNo)
.pageSize(pageSize)
.build();
LinkedList<LoanItemSrchResponseDto> response = genreService.thisWeekTrend(requestDto);
LinkedList<LoanItemSrchResponseDto> response = genreService.thisWeekTrend(requestDto, pageNo, pageSize);

return responseTemplate.success(response, HttpStatus.OK);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ public class GenreResponseParser {
private final ResponseParser responseParser;
private static final int NEW_TREND_YEAR_OFFSET = 2; // 최근 트렌드 연도 범위

public LinkedList<LoanItemSrchResponseDto> periodTrend(JSONObject jsonResponse) {
public LinkedList<LoanItemSrchResponseDto> periodTrend(JSONObject jsonResponse,
String filteredPageNo, String filteredPageSize) {
log.trace("GenreResponseParser > periodTrend()");

return filterResponses(jsonResponse, null, null);
LinkedList<LoanItemSrchResponseDto> filteredResponse = filterResponses(jsonResponse, null, null);
return responseParser.customPageFilter(filteredResponse, filteredPageNo, filteredPageSize);
}

public LinkedList<LoanItemSrchResponseDto> random(JSONObject jsonResponse, Integer maxSize) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.book.backend.domain.openapi.dto.response.LoanItemSrchResponseDto;
import com.book.backend.domain.openapi.service.OpenAPI;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.minidev.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -21,6 +22,7 @@
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
@Slf4j
public class GenreService {
private final GenreRepository genreRepository;
private final OpenAPI openAPI;
Expand All @@ -41,17 +43,24 @@ public Genre findByMainKdcNumAndSubKdcNum(String mainKdcNum, String subKdcNum) {
.orElseThrow(() -> new IllegalArgumentException("KDC 번호가" + mainKdcNum + subKdcNum + "인 장르를 찾을 수 없습니다."));
}

public LinkedList<LoanItemSrchResponseDto> periodToNowTrend(LoanItemSrchRequestDto requestDto, Integer dayPeriod) throws Exception {
public LinkedList<LoanItemSrchResponseDto> periodToNowTrend(LoanItemSrchRequestDto requestDto, Integer dayPeriod,
String filteredPageNo, String filteredPageSize) throws Exception {
log.trace("GenreService > periodToNowTrend()");

LocalDate today = LocalDate.now();
LocalDate startDt = today.minusDays(dayPeriod + 1);
LocalDate endDt = today.minusDays(1);

return periodTrend(requestDto, startDt, endDt);
return periodTrend(requestDto, startDt, endDt, filteredPageNo, filteredPageSize);
}

public LinkedList<LoanItemSrchResponseDto> thisWeekTrend(LoanItemSrchRequestDto requestDto) throws Exception {
public LinkedList<LoanItemSrchResponseDto> thisWeekTrend(LoanItemSrchRequestDto requestDto,
String filteredPageNo, String filteredPageSize) throws Exception {
log.trace("GenreService > thisWeekTrend()");

LocalDate today = LocalDate.now();
LocalDate startDt, endDt;

// 월요일 또는 화요일이면 저번주로, 아니면 이번주로 계산
if (today.getDayOfWeek() == DayOfWeek.MONDAY || today.getDayOfWeek() == DayOfWeek.TUESDAY) {
startDt = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)).minusDays(7);
Expand All @@ -61,21 +70,27 @@ public LinkedList<LoanItemSrchResponseDto> thisWeekTrend(LoanItemSrchRequestDto
endDt = today.minusDays(1);
}

return periodTrend(requestDto, startDt, endDt);
return periodTrend(requestDto, startDt, endDt, filteredPageNo, filteredPageSize);
}

// periodToNowTrend, thisWeekTrend에 의해 호출됨
public LinkedList<LoanItemSrchResponseDto> periodTrend(LoanItemSrchRequestDto requestDto, LocalDate startDt, LocalDate endDt) throws Exception {
public LinkedList<LoanItemSrchResponseDto> periodTrend(LoanItemSrchRequestDto requestDto, LocalDate startDt, LocalDate endDt,
String filteredPageNo, String filteredPageSize) throws Exception {
log.trace("GenreService > periodTrend()");

String subUrl = "loanItemSrch";

requestDto.setPageSize("500"); // 커스텀 페이지네이션 적용하기 전 페이지 크기 설정
requestDto.setStartDt(startDt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
requestDto.setEndDt(endDt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));

JSONObject JsonResponse = openAPI.connect(subUrl, requestDto, new LoanItemSrchResponseDto());
return new LinkedList<>(genreResponseParser.periodTrend(JsonResponse));
return new LinkedList<>(genreResponseParser.periodTrend(JsonResponse, filteredPageNo, filteredPageSize));
}

public LinkedList<LoanItemSrchResponseDto> random(LoanItemSrchRequestDto requestDto, Integer maxSize) throws Exception {
log.trace("GenreService > random()");

String subUrl = "loanItemSrch";

requestDto.setPageSize("500"); // 셔플할 리스트의 페이지 크기 설정
Expand All @@ -87,9 +102,11 @@ public LinkedList<LoanItemSrchResponseDto> random(LoanItemSrchRequestDto request

public LinkedList<LoanItemSrchResponseDto> newTrend(LoanItemSrchRequestDto requestDto,
String filteredPageNo, String filteredPageSize) throws Exception {
log.trace("GenreService > newTrend()");

String subUrl = "loanItemSrch";

requestDto.setPageSize("1500"); // 연도로 필터링하기 전 페이지 크기 설정
requestDto.setPageSize("1500"); // 커스텀 페이지네이션 적용하기 전 페이지 크기 설정
LocalDate today = LocalDate.now();
int currentYear = Integer.parseInt(today.format(DateTimeFormatter.ofPattern("yyyy")));

Expand Down

0 comments on commit bbdaf72

Please sign in to comment.