Skip to content

Commit

Permalink
[refactor] 장르 컨트롤러에 pageNo, pageSize 파라미터 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
chanwoo7 authored and hyeesw committed Aug 10, 2024
1 parent 061423d commit 4d42e1a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,23 @@ public class GenreController {
@Operation(summary = "일주일 인기순", description = "장르 코드(2자리)를 입력받아 1주일 인기순 도서 리스트를 반환합니다.",
parameters = {
@Parameter(name = "genreCode", description = "세부 장르 코드 (필수, figma 참고)"),
@Parameter(name = "maxSize", description = "리스트 길이 제한 (선택)")},
@Parameter(name = "pageNo", description = "페이지 번호 (필수)"),
@Parameter(name = "pageSize", description = "페이지 당 요소 수 (필수)")},
responses = {@ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = LoanItemSrchResponseDto.class)),
description = LoanItemSrchResponseDto.description)})
@GetMapping("/aWeekTrend")
public ResponseEntity<?> aWeekTrend(@RequestParam String genreCode,
@RequestParam(required = false) Integer maxSize) throws Exception {
@RequestParam String pageNo,
@RequestParam String pageSize) throws Exception {
RequestLogger.param(new String[]{"kdcNum"}, genreCode);
requestValidate.isValidGenreCode(genreCode);

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

return responseTemplate.success(response, HttpStatus.OK);
}
Expand All @@ -51,17 +57,23 @@ public ResponseEntity<?> aWeekTrend(@RequestParam String genreCode,
@Operation(summary = "한 달 인기순", description = "장르 코드(2자리)를 입력받아 한 달 인기순 도서 리스트를 반환합니다.",
parameters = {
@Parameter(name = "genreCode", description = "세부 장르 코드 (필수, figma 참고)"),
@Parameter(name = "maxSize", description = "리스트 길이 제한 (선택)")},
@Parameter(name = "pageNo", description = "페이지 번호 (필수)"),
@Parameter(name = "pageSize", description = "페이지 당 요소 수 (필수)")},
responses = {@ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = LoanItemSrchResponseDto.class)),
description = LoanItemSrchResponseDto.description)})
@GetMapping("/aMonthTrend")
public ResponseEntity<?> aMonthTrend(@RequestParam String genreCode,
@RequestParam(required = false) Integer maxSize) throws Exception {
@RequestParam String pageNo,
@RequestParam String pageSize) throws Exception {
RequestLogger.param(new String[]{"kdcNum"}, genreCode);
requestValidate.isValidGenreCode(genreCode);

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

return responseTemplate.success(response, HttpStatus.OK);
}
Expand All @@ -71,17 +83,23 @@ public ResponseEntity<?> aMonthTrend(@RequestParam String genreCode,
"단, 월요일 또는 화요일이면 저번 주차로, 아니면 이번 주차로 계산됩니다.",
parameters = {
@Parameter(name = "genreCode", description = "세부 장르 코드 (필수, figma 참고)"),
@Parameter(name = "maxSize", description = "리스트 길이 제한 (선택)")},
@Parameter(name = "pageNo", description = "페이지 번호 (필수)"),
@Parameter(name = "pageSize", description = "페이지 당 요소 수 (필수)")},
responses = {@ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = LoanItemSrchResponseDto.class)),
description = LoanItemSrchResponseDto.description)})
@GetMapping("/thisWeekTrend")
public ResponseEntity<?> thisWeekTrend(@RequestParam String genreCode,
@RequestParam(required = false) Integer maxSize) throws Exception {
@RequestParam String pageNo,
@RequestParam String pageSize) throws Exception {
RequestLogger.param(new String[]{"kdcNum"}, genreCode);
requestValidate.isValidGenreCode(genreCode);

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

return responseTemplate.success(response, HttpStatus.OK);
}
Expand All @@ -100,29 +118,35 @@ public ResponseEntity<?> random(@RequestParam String genreCode,
RequestLogger.param(new String[]{"kdcNum"}, genreCode);
requestValidate.isValidGenreCode(genreCode);

LoanItemSrchRequestDto requestDto = LoanItemSrchRequestDto.builder().dtl_kdc(genreCode).build();
LoanItemSrchRequestDto requestDto = LoanItemSrchRequestDto.builder()
.dtl_kdc(genreCode)
.build();
LinkedList<LoanItemSrchResponseDto> response = genreService.random(requestDto, maxSize);

return responseTemplate.success(response, HttpStatus.OK);
}

/**
* 신작 인기순 - 중주제 KDC 번호(2자리) 입력 시 2년 내 출판된 인기 도서 목록 리턴
*/
// 신작 인기순
@Operation(summary = "신작 인기순", description = "장르 코드(2자리)를 입력받아 최근 2년 내 출판된 인기 도서 리스트를 반환합니다.",
parameters = {
@Parameter(name = "genreCode", description = "세부 장르 코드 (필수, figma 참고)"),
@Parameter(name = "maxSize", description = "리스트 길이 제한 (선택)")},
@Parameter(name = "pageNo", description = "페이지 번호 (필수)"),
@Parameter(name = "pageSize", description = "페이지 당 요소 수 (필수)")},
responses = {@ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = LoanItemSrchResponseDto.class)),
description = LoanItemSrchResponseDto.description)})
@GetMapping("/newTrend")
public ResponseEntity<?> newTrend(@RequestParam String genreCode,
@RequestParam(required = false) Integer maxSize) throws Exception {
@RequestParam String pageNo,
@RequestParam String pageSize) throws Exception {
RequestLogger.param(new String[]{"kdcNum"}, genreCode);
requestValidate.isValidGenreCode(genreCode);

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

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

public LinkedList<LoanItemSrchResponseDto> periodTrend(JSONObject jsonResponse, Integer maxSize) {
return filterResponses(jsonResponse, maxSize, null);
public LinkedList<LoanItemSrchResponseDto> periodTrend(JSONObject jsonResponse) {
return filterResponses(jsonResponse, null, null);
}

public LinkedList<LoanItemSrchResponseDto> random(JSONObject jsonResponse, Integer maxSize) {
LinkedList<LoanItemSrchResponseDto> filteredResponses = filterResponses(jsonResponse, maxSize, null);
LinkedList<LoanItemSrchResponseDto> filteredResponses = filterResponses(jsonResponse, null, maxSize);
return RandomPicker.randomPick(filteredResponses, maxSize);
}

public LinkedList<LoanItemSrchResponseDto> newTrend(JSONObject jsonResponse, int currentYear, Integer maxSize) {
return filterResponses(jsonResponse, maxSize, currentYear);
public LinkedList<LoanItemSrchResponseDto> newTrend(JSONObject jsonResponse, int currentYear) {
return filterResponses(jsonResponse, currentYear, null);
}

private LinkedList<LoanItemSrchResponseDto> filterResponses(JSONObject jsonResponse, Integer maxSize, Integer yearThreshold) {
private LinkedList<LoanItemSrchResponseDto> filterResponses(JSONObject jsonResponse, Integer yearThreshold, Integer maxSize) {
LinkedList<LoanItemSrchResponseDto> loanTrendResponseList = responseParser.loanTrend(jsonResponse);
LinkedList<LoanItemSrchResponseDto> responseList = new LinkedList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ public Genre findByMainKdcNumAndSubKdcNum(String mainKdcNum, String subKdcNum) {
.orElseThrow(() -> new IllegalArgumentException("KDC 번호가" + mainKdcNum + subKdcNum + "인 장르를 찾을 수 없습니다."));
}

public LinkedList<LoanItemSrchResponseDto> periodToNowTrend(LoanItemSrchRequestDto requestDto, Integer dayPeriod, Integer maxSize) throws Exception {
public LinkedList<LoanItemSrchResponseDto> periodToNowTrend(LoanItemSrchRequestDto requestDto, Integer dayPeriod) throws Exception {
LocalDate today = LocalDate.now();
LocalDate startDt = today.minusDays(dayPeriod + 1);
LocalDate endDt = today.minusDays(1);

return periodTrend(requestDto, startDt, endDt, maxSize);
return periodTrend(requestDto, startDt, endDt);
}

public LinkedList<LoanItemSrchResponseDto> thisWeekTrend(LoanItemSrchRequestDto requestDto, Integer maxSize) throws Exception {
public LinkedList<LoanItemSrchResponseDto> thisWeekTrend(LoanItemSrchRequestDto requestDto) throws Exception {
LocalDate today = LocalDate.now();
LocalDate startDt, endDt;
// 월요일 또는 화요일이면 저번주로, 아니면 이번주로 계산
Expand All @@ -61,18 +61,18 @@ public LinkedList<LoanItemSrchResponseDto> thisWeekTrend(LoanItemSrchRequestDto
endDt = today.minusDays(1);
}

return periodTrend(requestDto, startDt, endDt, maxSize);
return periodTrend(requestDto, startDt, endDt);
}

// periodToNowTrend, thisWeekTrend에 의해 호출됨
public LinkedList<LoanItemSrchResponseDto> periodTrend(LoanItemSrchRequestDto requestDto, LocalDate startDt, LocalDate endDt, Integer maxSize) throws Exception {
public LinkedList<LoanItemSrchResponseDto> periodTrend(LoanItemSrchRequestDto requestDto, LocalDate startDt, LocalDate endDt) throws Exception {
String subUrl = "loanItemSrch";

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, maxSize));
return new LinkedList<>(genreResponseParser.periodTrend(JsonResponse));
}

public LinkedList<LoanItemSrchResponseDto> random(LoanItemSrchRequestDto requestDto, Integer maxSize) throws Exception {
Expand All @@ -85,7 +85,7 @@ public LinkedList<LoanItemSrchResponseDto> random(LoanItemSrchRequestDto request
return new LinkedList<>(genreResponseParser.random(JsonResponse, maxSize));
}

public LinkedList<LoanItemSrchResponseDto> newTrend(LoanItemSrchRequestDto requestDto, Integer maxSize) throws Exception {
public LinkedList<LoanItemSrchResponseDto> newTrend(LoanItemSrchRequestDto requestDto) throws Exception {
String subUrl = "loanItemSrch";

requestDto.setPageSize("1500"); // 연도로 필터링하기 전 페이지 크기 설정
Expand All @@ -94,7 +94,7 @@ public LinkedList<LoanItemSrchResponseDto> newTrend(LoanItemSrchRequestDto reque

JSONObject JsonResponse = openAPI.connect(subUrl, requestDto, new LoanItemSrchResponseDto());

return new LinkedList<>(genreResponseParser.newTrend(JsonResponse, currentYear, maxSize));
return new LinkedList<>(genreResponseParser.newTrend(JsonResponse, currentYear));
}

}

0 comments on commit 4d42e1a

Please sign in to comment.