diff --git a/src/main/java/shop/hooking/hooking/controller/CopyController.java b/src/main/java/shop/hooking/hooking/controller/CopyController.java index 029a90d..34fa026 100644 --- a/src/main/java/shop/hooking/hooking/controller/CopyController.java +++ b/src/main/java/shop/hooking/hooking/controller/CopyController.java @@ -40,7 +40,6 @@ public class CopyController { private final CardRepository cardRepository; - private final CardJpaRepository cardJpaRepository; @@ -50,12 +49,8 @@ public class CopyController { @GetMapping("/{index}") public ResponseEntity>> copyList(HttpServletRequest httpRequest, @PathVariable int index) { - // 브랜드에서 카피 가져오기 - List tempCopyRes = copyService.getCopyResFromBrands(); - Collections.shuffle(tempCopyRes); - int startIndex = index * 30; - List resultCopyRes = copyService.getLimitedCopyResByIndex(tempCopyRes, startIndex); - copyService.setScrapCntWhenTokenNotProvided(httpRequest, resultCopyRes); + int limit = 30; + List resultCopyRes = copyService.getCopyListFromBrandsAndSetScrapCnt(httpRequest,index,limit); return ResponseEntity.ok(new HttpRes<>(resultCopyRes)); } @@ -107,33 +102,6 @@ public ResponseEntity> copyScrap(HttpServletRequest httpRequest, } - // 크롤링 with 파이썬 -// @PostMapping("/crawling") -// public HttpRes saveCrawling(@RequestBody CrawlingReq crawlingReq) { -// List dataList = crawlingReq.getData(); -// -// -// for (CrawlingData data : dataList) { -// String text = data.getText(); -// String url = data.getUrl(); -// LocalDateTime createdAt = data.getCreatedAt(); -// Long brandId = data.getBrandId(); -// -// -// Brand brand = brandRepository.findBrandById(brandId); -// -// Card card = new Card(); -// -// card.setText(text); -// card.setCreatedAt(createdAt); -// card.setBrand(brand); -// card.setUrl(url); -// -// cardRepository.save(card); -// } -// -// return new HttpRes<>("크롤링 데이터가 저장되었습니다."); -// } @PostMapping("/crawling") public ResponseEntity> saveCrawling(@RequestBody CrawlingReq crawlingReq) { @@ -147,18 +115,17 @@ public ResponseEntity> saveCrawling(@RequestBody CrawlingReq cra // 카피라이팅 필터링 @GetMapping("/filter/{index}") - public ResponseEntity> searchFilterCard(HttpServletRequest httpRequest,@PathVariable int index,CardSearchCondition condition) { - List results = cardJpaRepository.filter(condition); - int startIndex = index * 30; //인덱싱 - List resultCopyRes = copyService.getLimitedCopyResByIndex(results, startIndex); - copyService.setScrapCntWhenTokenNotProvided(httpRequest, resultCopyRes); - if(resultCopyRes.isEmpty()){ + public ResponseEntity> searchFilterCard(HttpServletRequest httpRequest, @PathVariable int index, CardSearchCondition condition) { + List resultCopyRes = copyService.searchFilterCard(httpRequest, index, condition); + if (resultCopyRes.isEmpty()) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null); } return ResponseEntity.status(HttpStatus.OK).body(resultCopyRes); } + + // 카피라이팅 스크랩 취소 (soft delete) @PostMapping ("/scrap/cancle") public ResponseEntity> cancelScrap(HttpServletRequest httpRequest, @RequestBody CopyReq copyReq){ diff --git a/src/main/java/shop/hooking/hooking/service/CopyService.java b/src/main/java/shop/hooking/hooking/service/CopyService.java index 0e15a9a..57606d5 100644 --- a/src/main/java/shop/hooking/hooking/service/CopyService.java +++ b/src/main/java/shop/hooking/hooking/service/CopyService.java @@ -7,6 +7,7 @@ import org.springframework.stereotype.Service; import shop.hooking.hooking.config.BrandType; import shop.hooking.hooking.config.MoodType; +import shop.hooking.hooking.dto.CardSearchCondition; import shop.hooking.hooking.dto.request.CopyReq; import shop.hooking.hooking.dto.request.CrawlingData; import shop.hooking.hooking.dto.response.CopyRes; @@ -124,6 +125,14 @@ public CopySearchRes copySearchList(HttpServletRequest httpRequest, String q, in } + public List getCopyScrapListAndSortByCreatedAt(HttpServletRequest httpRequest, int index, User user) { + List copyRes = getCopyScrapList(user); + int startIndex = index * 30; + List resultCopyRes = getLimitedCopyResByIndex(copyRes, startIndex); + resultCopyRes.sort((copy1, copy2) -> copy1.getCreatedAt().compareTo(copy2.getCreatedAt())); + return resultCopyRes; + } + // 검색 쿼리 // @Transactional @@ -210,18 +219,23 @@ public CopyRes createCopyRes(Card card) { return new CopyRes(id, brand,text,scrapCnt,createdAt); } - public List getCopyResFromBrands() { + public List getCopyListFromBrandsAndSetScrapCnt(HttpServletRequest httpRequest, int index, int limit) { Long[] brandIds = {1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L}; List tempCopyRes = new ArrayList<>(); for (Long brandId : brandIds) { List copyRes = getCopyList(brandId); tempCopyRes.addAll(copyRes); } - return tempCopyRes; + Collections.shuffle(tempCopyRes); + int startIndex = index * limit; + List resultCopyRes = getLimitedCopyResByIndex(tempCopyRes, startIndex); + setScrapCntWhenTokenNotProvided(httpRequest, resultCopyRes); + return resultCopyRes; } + public void setScrapCntWhenTokenNotProvided(HttpServletRequest httpRequest, List copyResList) { String token = httpRequest.getHeader("X-AUTH-TOKEN"); if (token == null) { @@ -275,6 +289,15 @@ public CopyRes createScrapRes(Scrap scrap) { return new CopyRes(id, brand,text,scrapCnt,createdAt); } + public List searchFilterCard(HttpServletRequest httpRequest, int index, CardSearchCondition condition) { + List results = cardJpaRepository.filter(condition); + int startIndex = index * 30; + List resultCopyRes = getLimitedCopyResByIndex(results, startIndex); + setScrapCntWhenTokenNotProvided(httpRequest, resultCopyRes); + return resultCopyRes; + } + + @Transactional public boolean saveCopy(User user, Card card) throws IOException {