Skip to content

Commit

Permalink
[refactor] isScrap 처리
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiwon committed Jul 29, 2023
1 parent 2c8be5d commit 94c40e1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ public class CopyController {
// 페이지네이션
@GetMapping("/{index}")
public ResponseEntity<HttpRes<List<CopyRes>>> copyList(HttpServletRequest httpRequest, @PathVariable int index) {

User user = jwtTokenProvider.getUserInfoByToken(httpRequest);
int limit = 30;
List<CopyRes> resultCopyRes = copyService.getCopyListFromBrandsAndSetScrapCnt(httpRequest,index,limit);
copyService.setIsScrapWithUser(user, resultCopyRes);
return ResponseEntity.ok(new HttpRes<>(resultCopyRes));

}
Expand All @@ -65,7 +66,13 @@ public ResponseEntity<HttpRes<List<CopyRes>>> copyList(HttpServletRequest httpRe
public ResponseEntity<CopySearchRes> copySearchList(HttpServletRequest httpRequest,
@RequestParam(name = "keyword") String q,
@PathVariable int index) {
User user = jwtTokenProvider.getUserInfoByToken(httpRequest);
CopySearchRes response = copyService.copySearchList(httpRequest, q, index);
List<CopySearchResult> copySearchResults = response.getData();
for(CopySearchResult copySearchResult : copySearchResults){
List<CopyRes> copyRes = copySearchResult.getData();
copyService.setIsScrapWithUser(user, copyRes);
}
if (response.getCode() == HttpStatus.BAD_REQUEST.value()) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
} else {
Expand Down Expand Up @@ -126,10 +133,12 @@ public ResponseEntity<HttpRes<String>> saveCrawling(@RequestBody CrawlingReq cra
// 카피라이팅 필터링
@GetMapping("/filter/{index}")
public ResponseEntity<List<CopyRes>> searchFilterCard(HttpServletRequest httpRequest, @PathVariable int index, CardSearchCondition condition) {
User user = jwtTokenProvider.getUserInfoByToken(httpRequest);
List<CopyRes> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class CopyRes implements Comparable<CopyRes>{
private LocalDateTime createdAt;
private List<Integer> index;
private LocalDateTime scrapTime;
private Integer isScrap;

@Builder
@QueryProjection
Expand All @@ -42,7 +43,6 @@ public CopyRes(Long id, Brand brand, String text, LocalDateTime createdAt) {
this.text = text;
this.scrapCnt = 0;
this.createdAt = createdAt;

}

//왜안됨..?
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/shop/hooking/hooking/service/CopyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,16 @@ public List<CopyRes> getCopyList(Long brandId) {
return copyResList;
}

public void setIsScrapWithUser(User user, List<CopyRes> copyResList) {
List<Scrap> 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<CrawlingData> dataList) {
Expand Down

0 comments on commit 94c40e1

Please sign in to comment.