Skip to content

Commit

Permalink
Merge pull request #44 from BOOK-TALK/#42-refactor-main
Browse files Browse the repository at this point in the history
#42 refactor main
  • Loading branch information
chanwoo7 authored Aug 9, 2024
2 parents 95bbf4d + 778808b commit c267ad7
Show file tree
Hide file tree
Showing 18 changed files with 202 additions and 294 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package com.book.backend.domain.book.controller;

import com.book.backend.domain.book.service.BookRequestValidate;
import com.book.backend.domain.book.service.BookService;

import com.book.backend.domain.openapi.dto.response.LoanItemSrchResponseDto;
import com.book.backend.domain.openapi.service.RequestValidate;
import com.book.backend.domain.openapi.dto.request.CustomHotTrendRequestDto;
import com.book.backend.domain.openapi.dto.request.LoanItemSrchRequestDto;

import com.book.backend.domain.openapi.dto.request.HotTrendRequestDto;
import com.book.backend.domain.openapi.dto.request.KeywordRequestDto;
import com.book.backend.domain.openapi.dto.response.CustomHotTrendResponseDto;
import com.book.backend.domain.openapi.dto.request.MonthlyKeywordsRequestDto;
import com.book.backend.domain.openapi.dto.response.HotTrendResponseDto;
import com.book.backend.domain.openapi.dto.response.KeywordResponseDto;
import com.book.backend.domain.openapi.dto.response.RecommendResponseDto;
import com.book.backend.domain.openapi.dto.request.RecommendRequestDto;
import com.book.backend.domain.openapi.dto.response.MonthlyKeywordsResponseDto;
import com.book.backend.domain.openapi.dto.response.RecommendListResponseDto;
import com.book.backend.domain.openapi.dto.request.RecommendListRequestDto;
import com.book.backend.global.ResponseTemplate;
import com.book.backend.global.log.RequestLogger;
import io.swagger.v3.oas.annotations.Operation;
Expand All @@ -38,24 +37,24 @@
@Slf4j
public class BookController {
private final BookService bookService;
private final BookRequestValidate bookRequestValidate;
private final ResponseTemplate responseTemplate;
private final RequestValidate requestValidate;

// 마니아(4), 다독자(5) 추천 API
@Operation(summary="책 추천", description="특정 책 코드를 입력으로 받아 해당 책 기반 추천 책 list를 반환합니다.",
parameters = {@Parameter(name = "isbn", description = "책 코드")},
responses = {@ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = RecommendResponseDto.class)),
description = RecommendResponseDto.description)})
responses = {@ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = RecommendListResponseDto.class)),
description = RecommendListResponseDto.description)})
@GetMapping("/recommend")
public ResponseEntity<?> recommend(@RequestParam String isbn) throws Exception {
RequestLogger.param(new String[]{"isbn"}, isbn);
bookRequestValidate.isValidIsbn(isbn);
requestValidate.isValidIsbn(isbn);

RecommendRequestDto requestDto = RecommendRequestDto.builder().isbn13(isbn).build();
LinkedList<RecommendResponseDto> response = bookService.recommend(requestDto);
RecommendListRequestDto requestDto = RecommendListRequestDto.builder().isbn13(isbn).build();
LinkedList<RecommendListResponseDto> response = bookService.recommend(requestDto);

HashSet<String> duplicateCheckSet = new HashSet<>();
LinkedList<RecommendResponseDto> duplicateRemovedList = bookService.duplicateChecker(response, duplicateCheckSet);
LinkedList<RecommendListResponseDto> duplicateRemovedList = bookService.duplicateChecker(response, duplicateCheckSet);
bookService.ensureRecommendationsCount(duplicateRemovedList, duplicateCheckSet);

return responseTemplate.success(duplicateRemovedList, HttpStatus.OK);
Expand All @@ -78,14 +77,14 @@ public ResponseEntity<?> hotTrend() throws Exception {

// 지난달 키워드 (17)
@Operation(summary="지난달 키워드", description="지난달 핵심 키워드 100개 중 랜덤으로 10개 키워드를 반환합니다.",
responses = {@ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = KeywordResponseDto.class)),
description = KeywordResponseDto.description)})
responses = {@ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = MonthlyKeywordsResponseDto.class)),
description = MonthlyKeywordsResponseDto.description)})
@GetMapping("/keyword")
public ResponseEntity<?> keywords() throws Exception {
KeywordRequestDto requestDto = new KeywordRequestDto();
MonthlyKeywordsRequestDto requestDto = new MonthlyKeywordsRequestDto();
requestDto.setSearchDt();

LinkedList<KeywordResponseDto> response = bookService.keywords(requestDto);
LinkedList<MonthlyKeywordsResponseDto> response = bookService.keywords(requestDto);

return responseTemplate.success(response, HttpStatus.OK);
}
Expand All @@ -100,8 +99,8 @@ public ResponseEntity<?> keywords() throws Exception {
@Parameter(name = "genreCode", description = "세부 장르 코드 (figma 참고)"),
@Parameter(name = "region", description = "시단위 지역코드 (figma 참고)"),
@Parameter(name = "libCode", description = "도서관 코드 (조회 -> https://www.data4library.kr/libDataL)")},
responses = {@ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = CustomHotTrendResponseDto.class)),
description = CustomHotTrendResponseDto.description)})
responses = {@ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = LoanItemSrchResponseDto.class)),
description = LoanItemSrchResponseDto.description)})
@GetMapping("/customHotTrend")
public ResponseEntity<?> customHotTrend(@RequestParam(required = false) String weekMonth,
@RequestParam(required = false) String peerAge,
Expand All @@ -112,9 +111,9 @@ public ResponseEntity<?> customHotTrend(@RequestParam(required = false) String w
@RequestParam(required = false) String libCode) throws Exception {
RequestLogger.param(new String[]{"weekMonth", "peerAge", "ageRange", "gender", "genreCode", "region", "libCode"},
weekMonth, peerAge, ageRange, gender, genreCode, region, libCode);
CustomHotTrendRequestDto requestDto = requestValidate.set_validCustomHotTrendRequest(weekMonth, peerAge, ageRange, gender, genreCode, region, libCode);
LoanItemSrchRequestDto requestDto = requestValidate.set_validLoanItemSrchRequest(weekMonth, peerAge, ageRange, gender, genreCode, region, libCode);

LinkedList<CustomHotTrendResponseDto> response = bookService.customHotTrend(requestDto);
LinkedList<LoanItemSrchResponseDto> response = bookService.loanItemSrch(requestDto);
return responseTemplate.success(response, HttpStatus.OK);
}
}

This file was deleted.

50 changes: 25 additions & 25 deletions src/main/java/com/book/backend/domain/book/service/BookService.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package com.book.backend.domain.book.service;

import com.book.backend.domain.openapi.dto.request.CustomHotTrendRequestDto;
import com.book.backend.domain.openapi.dto.request.HotTrendRequestDto;
import com.book.backend.domain.openapi.dto.request.KeywordRequestDto;
import com.book.backend.domain.openapi.dto.request.RecommendRequestDto;
import com.book.backend.domain.openapi.dto.response.CustomHotTrendResponseDto;
import com.book.backend.domain.openapi.dto.request.LoanItemSrchRequestDto;
import com.book.backend.domain.openapi.dto.request.MonthlyKeywordsRequestDto;
import com.book.backend.domain.openapi.dto.request.RecommendListRequestDto;
import com.book.backend.domain.openapi.dto.response.HotTrendResponseDto;
import com.book.backend.domain.openapi.dto.response.KeywordResponseDto;
import com.book.backend.domain.openapi.dto.response.RecommendResponseDto;
import com.book.backend.domain.openapi.dto.response.LoanItemSrchResponseDto;
import com.book.backend.domain.openapi.dto.response.MonthlyKeywordsResponseDto;
import com.book.backend.domain.openapi.dto.response.RecommendListResponseDto;
import com.book.backend.domain.openapi.service.OpenAPI;
import com.book.backend.domain.openapi.service.ResponseParser;
import com.book.backend.global.log.RequestLogger;

import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
Expand All @@ -27,45 +27,45 @@
public class BookService {
private final OpenAPI openAPI;

public LinkedList<RecommendResponseDto> recommend(RecommendRequestDto requestDto) throws Exception {
public LinkedList<RecommendListResponseDto> recommend(RecommendListRequestDto requestDto) throws Exception {
log.trace("recommend()");
String subUrl = "recommandList";

LinkedList<RecommendResponseDto> responseList = new LinkedList<>();
LinkedList<RecommendListResponseDto> responseList = new LinkedList<>();

requestDto.setType("mania");
JSONObject maniaJsonResponse = openAPI.connect(subUrl, requestDto, new RecommendResponseDto());
JSONObject maniaJsonResponse = openAPI.connect(subUrl, requestDto, new RecommendListResponseDto());
ResponseParser maniaResponseParser = new ResponseParser();
responseList.addAll(maniaResponseParser.recommend(maniaJsonResponse));

requestDto.setType("reader");
JSONObject readerJsonResponse = openAPI.connect(subUrl, requestDto, new RecommendResponseDto());
JSONObject readerJsonResponse = openAPI.connect(subUrl, requestDto, new RecommendListResponseDto());
ResponseParser readerResponseParser = new ResponseParser();
responseList.addAll(readerResponseParser.recommend(readerJsonResponse));

return responseList;
}

/* 추천 책 수가 5보다 작으면 추천된 isbn 으로 recommend() 다시 호출 */
public void ensureRecommendationsCount(LinkedList<RecommendResponseDto> list, HashSet<String> set) throws Exception {
public void ensureRecommendationsCount(LinkedList<RecommendListResponseDto> list, HashSet<String> set) throws Exception {
log.trace("ensureRecommendationsCount()");
LinkedList<RecommendResponseDto> originalList = new LinkedList<>(list);
Iterator<RecommendResponseDto> iterator = originalList.iterator();
LinkedList<RecommendListResponseDto> originalList = new LinkedList<>(list);
Iterator<RecommendListResponseDto> iterator = originalList.iterator();
while (originalList.size() < 5 && iterator.hasNext()) {
RecommendRequestDto newRequestDto = RecommendRequestDto.builder().isbn13(iterator.next().getIsbn13()).build(); // 추천된 다른 책의 isbn13
LinkedList<RecommendResponseDto> newRecommendList = recommend(newRequestDto);
RecommendListRequestDto newRequestDto = RecommendListRequestDto.builder().isbn13(iterator.next().getIsbn13()).build(); // 추천된 다른 책의 isbn13
LinkedList<RecommendListResponseDto> newRecommendList = recommend(newRequestDto);
// 기존에 추가된 책인지 확인
for(RecommendResponseDto dto : newRecommendList){
for(RecommendListResponseDto dto : newRecommendList){
String key = dto.getBookname() + dto.getAuthors();
if(set.add(key)) list.add(dto);
}
}
}

public LinkedList<RecommendResponseDto> duplicateChecker(LinkedList<RecommendResponseDto> list, HashSet<String> set){
public LinkedList<RecommendListResponseDto> duplicateChecker(LinkedList<RecommendListResponseDto> list, HashSet<String> set){
log.trace("duplicateChecker()");
LinkedList<RecommendResponseDto> duplicateRemovedList = new LinkedList<>();
for(RecommendResponseDto dto : list){
LinkedList<RecommendListResponseDto> duplicateRemovedList = new LinkedList<>();
for(RecommendListResponseDto dto : list){
String key = dto.getBookname() + dto.getAuthors();
if(set.add(key)) duplicateRemovedList.add(dto);
}
Expand All @@ -80,20 +80,20 @@ public LinkedList<HotTrendResponseDto> hotTrend(HotTrendRequestDto requestDto) t
return responseParser.hotTrend(jsonResponse);
}

public LinkedList<KeywordResponseDto> keywords(KeywordRequestDto requestDto) throws Exception{
public LinkedList<MonthlyKeywordsResponseDto> keywords(MonthlyKeywordsRequestDto requestDto) throws Exception{
log.trace("keywords()");
String subUrl = "monthlyKeywords";
JSONObject jsonResponse = openAPI.connect(subUrl, requestDto, new KeywordResponseDto());
JSONObject jsonResponse = openAPI.connect(subUrl, requestDto, new MonthlyKeywordsResponseDto());
ResponseParser responseParser = new ResponseParser();
return responseParser.keywords(jsonResponse);
}

public LinkedList<CustomHotTrendResponseDto> customHotTrend(CustomHotTrendRequestDto requestDto) throws Exception{
public LinkedList<LoanItemSrchResponseDto> loanItemSrch(LoanItemSrchRequestDto requestDto) throws Exception{
log.trace("customHotTrend()");
String subUrl = "loanItemSrch";
requestDto.setPageSize("10"); // 10개만 출력하도록 제한
JSONObject jsonResponse = openAPI.connect(subUrl, requestDto, new CustomHotTrendResponseDto());
JSONObject jsonResponse = openAPI.connect(subUrl, requestDto, new LoanItemSrchResponseDto());
ResponseParser responseParser = new ResponseParser();
return responseParser.customHotTrend(jsonResponse);
return responseParser.loanItemSrch(jsonResponse);
}
}
Loading

0 comments on commit c267ad7

Please sign in to comment.