From ca8e157074a31a85605d6ba6df24b387c8566794 Mon Sep 17 00:00:00 2001 From: Jiwon Date: Sun, 30 Jul 2023 00:47:19 +0900 Subject: [PATCH] =?UTF-8?q?[feat]=20brand=20isScrap=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hooking/hooking/controller/BrandController.java | 10 ++++++---- .../hooking/hooking/controller/CopyController.java | 1 + .../shop/hooking/hooking/dto/response/BrandRes.java | 1 + .../shop/hooking/hooking/dto/response/CopyRes.java | 4 ++-- .../shop/hooking/hooking/service/BrandService.java | 10 ++++++++++ .../java/shop/hooking/hooking/service/CopyService.java | 6 ------ 6 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/main/java/shop/hooking/hooking/controller/BrandController.java b/src/main/java/shop/hooking/hooking/controller/BrandController.java index 1d58d5b..fd13a3f 100644 --- a/src/main/java/shop/hooking/hooking/controller/BrandController.java +++ b/src/main/java/shop/hooking/hooking/controller/BrandController.java @@ -37,23 +37,25 @@ public ResponseEntity>> showAllBrand(){ } @CrossOrigin(origins = "https://hooking.shop, https://hooking-dev.netlify.app/, https://hooking.netlify.app/, http://localhost:3000/, http://localhost:3001/") - @PostMapping("/{brand_id}/{index}") - public ResponseEntity> showOneBrand(HttpServletRequest httpRequest, @PathVariable Long brand_id, @PathVariable int index){ + @GetMapping("/{brand_id}/{index}") + public ResponseEntity> getOneBrand(HttpServletRequest httpRequest, @PathVariable Long brand_id, @PathVariable int index){ + User user = jwtTokenProvider.getUserInfoByToken(httpRequest); BrandRes.BrandDetailDto brandDetailDto = brandService.getOneBrand(brand_id); List cards = brandDetailDto.getCard(); + int startIndex = index * 30; List resultCards = brandService.getLimitedCardsByIndex(cards, startIndex); if (resultCards.isEmpty()) { - String errorMessage = "카드가 없습니다."; - return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new HttpRes<>(HttpStatus.BAD_REQUEST.value(), errorMessage)); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new HttpRes<>(HttpStatus.BAD_REQUEST.value(), "카드가 없습니다.")); } brandDetailDto.setCard(resultCards); brandService.setScrapCntWhenTokenNotProvided(httpRequest, resultCards); + brandService.setIsScrapWithUser(user, resultCards); return ResponseEntity.ok(new HttpRes<>(brandDetailDto)); } diff --git a/src/main/java/shop/hooking/hooking/controller/CopyController.java b/src/main/java/shop/hooking/hooking/controller/CopyController.java index e11913b..5849a21 100644 --- a/src/main/java/shop/hooking/hooking/controller/CopyController.java +++ b/src/main/java/shop/hooking/hooking/controller/CopyController.java @@ -86,6 +86,7 @@ public ResponseEntity>> copyScrapList(HttpServletRequest h for(CopyRes copyRes1 : resultCopyRes){ Scrap scrap = scrapRepository.findByUserAndCardId(user, copyRes1.getId()); copyRes1.setScrapTime(scrap.getCreatedAt()); + } Collections.sort(resultCopyRes); diff --git a/src/main/java/shop/hooking/hooking/dto/response/BrandRes.java b/src/main/java/shop/hooking/hooking/dto/response/BrandRes.java index 01eb965..4ca7a9b 100644 --- a/src/main/java/shop/hooking/hooking/dto/response/BrandRes.java +++ b/src/main/java/shop/hooking/hooking/dto/response/BrandRes.java @@ -49,6 +49,7 @@ public static class cardDto{ private String text; private Integer scrapCnt; private LocalDateTime createdAt; + private Integer isScrap; } } 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 1691ecd..0f0b852 100644 --- a/src/main/java/shop/hooking/hooking/dto/response/CopyRes.java +++ b/src/main/java/shop/hooking/hooking/dto/response/CopyRes.java @@ -22,9 +22,7 @@ public class CopyRes implements Comparable{ private String text; private Integer scrapCnt; private LocalDateTime createdAt; - private List index; - private LocalDateTime scrapTime; @Builder @@ -35,6 +33,7 @@ public CopyRes(Long id, Brand brand, String text, Integer scrapCnt,LocalDateTime this.text = text; this.scrapCnt = scrapCnt; this.createdAt = createdAt; + } public CopyRes(Long id, Brand brand, String text, LocalDateTime createdAt) { @@ -43,6 +42,7 @@ 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/BrandService.java b/src/main/java/shop/hooking/hooking/service/BrandService.java index 74b6465..bcf5009 100644 --- a/src/main/java/shop/hooking/hooking/service/BrandService.java +++ b/src/main/java/shop/hooking/hooking/service/BrandService.java @@ -112,6 +112,16 @@ public List getLimitedCardsByIndex(List card return cards.subList(startIndex, endIndex); } + public void setIsScrapWithUser(User user, List cardList) { + List scraps = scrapRepository.findScrapByUser(user); + + // cardList의 id와 scraps의 card_id를 비교하여 isScrap 값을 설정 + for (BrandRes.cardDto cardDto : cardList) { + long cardId = cardDto.getId(); + boolean isScrapFound = scraps.stream().anyMatch(scrap -> scrap.getCard().getId() == cardId); + cardDto.setIsScrap(isScrapFound ? 1 : 0); + } + } public void setScrapCntWhenTokenNotProvided(HttpServletRequest httpRequest, List cardList) { diff --git a/src/main/java/shop/hooking/hooking/service/CopyService.java b/src/main/java/shop/hooking/hooking/service/CopyService.java index 57606d5..f91f1b5 100644 --- a/src/main/java/shop/hooking/hooking/service/CopyService.java +++ b/src/main/java/shop/hooking/hooking/service/CopyService.java @@ -268,12 +268,6 @@ public String checkKeyword(String q) { return q; } - public 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); - } @Transactional public CopyRes createScrapRes(Scrap scrap) {