From 9fd8ecbe16333e732fbd91fb00a586f60fa18c85 Mon Sep 17 00:00:00 2001 From: Jiwon Date: Fri, 28 Jul 2023 13:49:12 +0900 Subject: [PATCH] =?UTF-8?q?[refactor]=20=EA=B2=80=EC=83=89=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hooking/controller/CopyController.java | 91 ++++++++----------- ...SearchResponse.java => CopySearchRes.java} | 2 +- 2 files changed, 41 insertions(+), 52 deletions(-) rename src/main/java/shop/hooking/hooking/dto/response/{CopySearchResponse.java => CopySearchRes.java} (90%) diff --git a/src/main/java/shop/hooking/hooking/controller/CopyController.java b/src/main/java/shop/hooking/hooking/controller/CopyController.java index b665aac..ab3e04e 100644 --- a/src/main/java/shop/hooking/hooking/controller/CopyController.java +++ b/src/main/java/shop/hooking/hooking/controller/CopyController.java @@ -12,7 +12,7 @@ import shop.hooking.hooking.dto.request.CrawlingData; import shop.hooking.hooking.dto.request.CrawlingReq; import shop.hooking.hooking.dto.response.CopyRes; -import shop.hooking.hooking.dto.response.CopySearchResponse; +import shop.hooking.hooking.dto.response.CopySearchRes; import shop.hooking.hooking.dto.response.CopySearchResult; import shop.hooking.hooking.entity.Brand; import shop.hooking.hooking.entity.Card; @@ -60,33 +60,31 @@ public ResponseEntity>> copyList(HttpServletRequest httpRe tempCopyRes.addAll(copyRes); } - Collections.shuffle(tempCopyRes); //섞임 + // 랜덤 + Collections.shuffle(tempCopyRes); if (tempCopyRes.isEmpty()) { String errorMessage = "검색 결과를 찾을 수 없습니다."; return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null); } - // 요청한 index에 따라 30개의 다른 결과를 생성 - int startIndex = index * 30; //인덱싱 + // 인덱싱 + int startIndex = index * 30; List resultCopyRes = getLimitedCopyResByIndex(tempCopyRes, startIndex); - setScrapCntWhenTokenNotProvided(httpRequest, resultCopyRes); - return ResponseEntity.ok(new HttpRes<>(resultCopyRes)); } @GetMapping("/search/{index}") - public ResponseEntity copySearchList(HttpServletRequest httpRequest, - @RequestParam(name = "keyword") String q, - @PathVariable int index) { + public ResponseEntity copySearchList(HttpServletRequest httpRequest, + @RequestParam(name = "keyword") String q, + @PathVariable int index) { - int startIndex = index * 30; - CopySearchResponse response = new CopySearchResponse(); + CopySearchRes response = new CopySearchRes(); List results = new ArrayList<>(); if (q.isEmpty()) { // 검색 결과가 없다면 @@ -97,9 +95,8 @@ public ResponseEntity copySearchList(HttpServletRequest http .body(response); } - if(q.equals("애프터블로우")){ - q="애프터 블로우"; - } + q = checkKeyword(q); + // 검색 결과 처리 로직... MoodType moodType = MoodType.fromKeyword(q); @@ -113,55 +110,29 @@ public ResponseEntity copySearchList(HttpServletRequest http moodCopyRes = cardJpaRepository.searchMood(q); setScrapCntWhenTokenNotProvided(httpRequest, moodCopyRes); Collections.shuffle(moodCopyRes); - CopySearchResult moodResult = new CopySearchResult(); - moodResult.setType("mood"); - moodResult.setKeyword(q); // 현재는 전체 카드 수가 나옴 - moodResult.setTotalNum(moodCopyRes.size()); - moodResult.setData(getLimitedCopyResByIndex(moodCopyRes,index)); - results.add(moodResult); - + results.add(createCopySearchResult("mood", q, moodCopyRes, index)); if (!textCopyRes.isEmpty()) { setScrapCntWhenTokenNotProvided(httpRequest, textCopyRes); Collections.shuffle(textCopyRes); - CopySearchResult copyResult = new CopySearchResult(); - copyResult.setType("copy"); - copyResult.setKeyword(q); - copyResult.setTotalNum(textCopyRes.size()); - copyResult.setData(getLimitedCopyResByIndex(textCopyRes,index)); - setIndicesForCopyRes(textCopyRes, q); - results.add(copyResult); + results.add(createCopySearchResult("copy", q, textCopyRes, index)); } } else if (BrandType.containsKeyword(q)) { brandCopyRes = cardJpaRepository.searchBrand(q); setScrapCntWhenTokenNotProvided(httpRequest, brandCopyRes); Collections.shuffle(brandCopyRes); - CopySearchResult brandResult = new CopySearchResult(); - brandResult.setType("brand"); - brandResult.setKeyword(q); - brandResult.setTotalNum(brandCopyRes.size()); - brandResult.setData(getLimitedCopyResByIndex(brandCopyRes,index)); - results.add(brandResult); + results.add(createCopySearchResult("brand", q, brandCopyRes, index)); } else if (!textCopyRes.isEmpty()) { setScrapCntWhenTokenNotProvided(httpRequest, textCopyRes); Collections.shuffle(textCopyRes); - CopySearchResult copyResult = new CopySearchResult(); - copyResult.setType("copy"); - copyResult.setKeyword(q); - copyResult.setTotalNum(textCopyRes.size()); - copyResult.setData(getLimitedCopyResByIndex(textCopyRes,index)); - setIndicesForCopyRes(textCopyRes, q); - results.add(copyResult); + results.add(createCopySearchResult("copy", q, textCopyRes, index)); } + // 검색 결과가 없다면 if (results.isEmpty()) { - String errorMessage = "검색 결과를 찾을 수 없습니다."; - response.setCode(HttpStatus.BAD_REQUEST.value()); - response.setMessage(errorMessage); - response.setData(results); - return ResponseEntity.status(HttpStatus.BAD_REQUEST) - .body(response); + return getBadRequestResponseEntity(response, results); } + response.setCode(HttpStatus.OK.value()); response.setMessage("요청에 성공하였습니다."); response.setData(results); @@ -255,6 +226,14 @@ public ResponseEntity> searchFilterCard(HttpServletRequest httpReq return ResponseEntity.status(HttpStatus.OK).body(resultCopyRes); } + private String checkKeyword(String q) { + if (q.equals("애프터블로우")) { + q = "애프터 블로우"; + } + return q; + } + + private void setScrapCntWhenTokenNotProvided(HttpServletRequest httpRequest, List copyResList) { String token = httpRequest.getHeader("X-AUTH-TOKEN"); if (token == null) { @@ -264,14 +243,24 @@ private void setScrapCntWhenTokenNotProvided(HttpServletRequest httpRequest, Lis } } - private CopySearchResult createCopySearchResult(List copyResList) { + + private CopySearchResult createCopySearchResult(String type, String keyword, List copyResList, int index) { CopySearchResult result = new CopySearchResult(); - int endIndex = Math.min(30, copyResList.size()); - List limitedCopyRes = copyResList.subList(0, endIndex); - result.setData(limitedCopyRes); + result.setType(type); + result.setKeyword(keyword); + result.setTotalNum(copyResList.size()); + result.setData(getLimitedCopyResByIndex(copyResList, index)); return result; } + private ResponseEntity getBadRequestResponseEntity(CopySearchRes response, List results) { + response.setCode(HttpStatus.BAD_REQUEST.value()); + response.setMessage("검색 결과를 찾을 수 없습니다."); + response.setData(results); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response); + } + + private void setIndicesForCopyRes(List copyResList, String keyword) { for (CopyRes copyRes : copyResList) { diff --git a/src/main/java/shop/hooking/hooking/dto/response/CopySearchResponse.java b/src/main/java/shop/hooking/hooking/dto/response/CopySearchRes.java similarity index 90% rename from src/main/java/shop/hooking/hooking/dto/response/CopySearchResponse.java rename to src/main/java/shop/hooking/hooking/dto/response/CopySearchRes.java index e896b2b..e479e42 100644 --- a/src/main/java/shop/hooking/hooking/dto/response/CopySearchResponse.java +++ b/src/main/java/shop/hooking/hooking/dto/response/CopySearchRes.java @@ -9,7 +9,7 @@ @Getter @Setter @NoArgsConstructor -public class CopySearchResponse { +public class CopySearchRes { private int code; private String message; private List data;