From 94c40e1877a0380b98c39bcb2ea569f68aca2cda Mon Sep 17 00:00:00 2001 From: Jiwon Date: Sun, 30 Jul 2023 01:04:46 +0900 Subject: [PATCH] =?UTF-8?q?[refactor]=20isScrap=20=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hooking/hooking/controller/CopyController.java | 11 ++++++++++- .../shop/hooking/hooking/dto/response/CopyRes.java | 2 +- .../shop/hooking/hooking/service/CopyService.java | 9 +++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/main/java/shop/hooking/hooking/controller/CopyController.java b/src/main/java/shop/hooking/hooking/controller/CopyController.java index 5849a21..3342494 100644 --- a/src/main/java/shop/hooking/hooking/controller/CopyController.java +++ b/src/main/java/shop/hooking/hooking/controller/CopyController.java @@ -52,9 +52,10 @@ public class CopyController { // 페이지네이션 @GetMapping("/{index}") public ResponseEntity>> copyList(HttpServletRequest httpRequest, @PathVariable int index) { - + User user = jwtTokenProvider.getUserInfoByToken(httpRequest); int limit = 30; List resultCopyRes = copyService.getCopyListFromBrandsAndSetScrapCnt(httpRequest,index,limit); + copyService.setIsScrapWithUser(user, resultCopyRes); return ResponseEntity.ok(new HttpRes<>(resultCopyRes)); } @@ -65,7 +66,13 @@ public ResponseEntity>> copyList(HttpServletRequest httpRe public ResponseEntity copySearchList(HttpServletRequest httpRequest, @RequestParam(name = "keyword") String q, @PathVariable int index) { + User user = jwtTokenProvider.getUserInfoByToken(httpRequest); CopySearchRes response = copyService.copySearchList(httpRequest, q, index); + List copySearchResults = response.getData(); + for(CopySearchResult copySearchResult : copySearchResults){ + List copyRes = copySearchResult.getData(); + copyService.setIsScrapWithUser(user, copyRes); + } if (response.getCode() == HttpStatus.BAD_REQUEST.value()) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response); } else { @@ -126,10 +133,12 @@ public ResponseEntity> saveCrawling(@RequestBody CrawlingReq cra // 카피라이팅 필터링 @GetMapping("/filter/{index}") public ResponseEntity> searchFilterCard(HttpServletRequest httpRequest, @PathVariable int index, CardSearchCondition condition) { + User user = jwtTokenProvider.getUserInfoByToken(httpRequest); List resultCopyRes = copyService.searchFilterCard(httpRequest, index, condition); if (resultCopyRes.isEmpty()) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null); } + copyService.setIsScrapWithUser(user,resultCopyRes); return ResponseEntity.status(HttpStatus.OK).body(resultCopyRes); } diff --git a/src/main/java/shop/hooking/hooking/dto/response/CopyRes.java b/src/main/java/shop/hooking/hooking/dto/response/CopyRes.java index 0f0b852..757c384 100644 --- a/src/main/java/shop/hooking/hooking/dto/response/CopyRes.java +++ b/src/main/java/shop/hooking/hooking/dto/response/CopyRes.java @@ -24,6 +24,7 @@ public class CopyRes implements Comparable{ private LocalDateTime createdAt; private List index; private LocalDateTime scrapTime; + private Integer isScrap; @Builder @QueryProjection @@ -42,7 +43,6 @@ public CopyRes(Long id, Brand brand, String text, LocalDateTime createdAt) { this.text = text; this.scrapCnt = 0; this.createdAt = createdAt; - } //왜안됨..? diff --git a/src/main/java/shop/hooking/hooking/service/CopyService.java b/src/main/java/shop/hooking/hooking/service/CopyService.java index f91f1b5..bf8bba9 100644 --- a/src/main/java/shop/hooking/hooking/service/CopyService.java +++ b/src/main/java/shop/hooking/hooking/service/CopyService.java @@ -53,7 +53,16 @@ public List getCopyList(Long brandId) { return copyResList; } + public void setIsScrapWithUser(User user, List copyResList) { + List scraps = scrapRepository.findScrapByUser(user); + // cardList의 id와 scraps의 card_id를 비교하여 isScrap 값을 설정 + for (CopyRes copyRes : copyResList) { + long cardId = copyRes.getId(); + boolean isScrapFound = scraps.stream().anyMatch(scrap -> scrap.getCard().getId() == cardId); + copyRes.setIsScrap(isScrapFound ? 1 : 0); + } + } @Transactional public void saveCrawlingData(List dataList) {