Skip to content

Commit

Permalink
Merge pull request #100 from Hooking-CEOS/feature/copy
Browse files Browse the repository at this point in the history
Feature/copy
  • Loading branch information
JiwonKim08 authored Jul 29, 2023
2 parents d18d1dd + 81cc58d commit d87057d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 42 deletions.
47 changes: 7 additions & 40 deletions src/main/java/shop/hooking/hooking/controller/CopyController.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public class CopyController {

private final CardRepository cardRepository;

private final CardJpaRepository cardJpaRepository;



Expand All @@ -50,12 +49,8 @@ public class CopyController {
@GetMapping("/{index}")
public ResponseEntity<HttpRes<List<CopyRes>>> copyList(HttpServletRequest httpRequest, @PathVariable int index) {

// 브랜드에서 카피 가져오기
List<CopyRes> tempCopyRes = copyService.getCopyResFromBrands();
Collections.shuffle(tempCopyRes);
int startIndex = index * 30;
List<CopyRes> resultCopyRes = copyService.getLimitedCopyResByIndex(tempCopyRes, startIndex);
copyService.setScrapCntWhenTokenNotProvided(httpRequest, resultCopyRes);
int limit = 30;
List<CopyRes> resultCopyRes = copyService.getCopyListFromBrandsAndSetScrapCnt(httpRequest,index,limit);
return ResponseEntity.ok(new HttpRes<>(resultCopyRes));

}
Expand Down Expand Up @@ -107,33 +102,6 @@ public ResponseEntity<HttpRes<String>> copyScrap(HttpServletRequest httpRequest,
}


// 크롤링 with 파이썬
// @PostMapping("/crawling")
// public HttpRes<String> saveCrawling(@RequestBody CrawlingReq crawlingReq) {
// List<CrawlingData> 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<HttpRes<String>> saveCrawling(@RequestBody CrawlingReq crawlingReq) {
Expand All @@ -147,18 +115,17 @@ public ResponseEntity<HttpRes<String>> saveCrawling(@RequestBody CrawlingReq cra

// 카피라이팅 필터링
@GetMapping("/filter/{index}")
public ResponseEntity<List<CopyRes>> searchFilterCard(HttpServletRequest httpRequest,@PathVariable int index,CardSearchCondition condition) {
List<CopyRes> results = cardJpaRepository.filter(condition);
int startIndex = index * 30; //인덱싱
List<CopyRes> resultCopyRes = copyService.getLimitedCopyResByIndex(results, startIndex);
copyService.setScrapCntWhenTokenNotProvided(httpRequest, resultCopyRes);
if(resultCopyRes.isEmpty()){
public ResponseEntity<List<CopyRes>> searchFilterCard(HttpServletRequest httpRequest, @PathVariable int index, CardSearchCondition condition) {
List<CopyRes> 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<HttpRes<String>> cancelScrap(HttpServletRequest httpRequest, @RequestBody CopyReq copyReq){
Expand Down
27 changes: 25 additions & 2 deletions src/main/java/shop/hooking/hooking/service/CopyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -124,6 +125,14 @@ public CopySearchRes copySearchList(HttpServletRequest httpRequest, String q, in
}


public List<CopyRes> getCopyScrapListAndSortByCreatedAt(HttpServletRequest httpRequest, int index, User user) {
List<CopyRes> copyRes = getCopyScrapList(user);
int startIndex = index * 30;
List<CopyRes> resultCopyRes = getLimitedCopyResByIndex(copyRes, startIndex);
resultCopyRes.sort((copy1, copy2) -> copy1.getCreatedAt().compareTo(copy2.getCreatedAt()));
return resultCopyRes;
}


// 검색 쿼리
// @Transactional
Expand Down Expand Up @@ -210,18 +219,23 @@ public CopyRes createCopyRes(Card card) {
return new CopyRes(id, brand,text,scrapCnt,createdAt);
}

public List<CopyRes> getCopyResFromBrands() {
public List<CopyRes> 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<CopyRes> tempCopyRes = new ArrayList<>();
for (Long brandId : brandIds) {
List<CopyRes> copyRes = getCopyList(brandId);
tempCopyRes.addAll(copyRes);
}
return tempCopyRes;
Collections.shuffle(tempCopyRes);
int startIndex = index * limit;
List<CopyRes> resultCopyRes = getLimitedCopyResByIndex(tempCopyRes, startIndex);
setScrapCntWhenTokenNotProvided(httpRequest, resultCopyRes);
return resultCopyRes;
}




public void setScrapCntWhenTokenNotProvided(HttpServletRequest httpRequest, List<CopyRes> copyResList) {
String token = httpRequest.getHeader("X-AUTH-TOKEN");
if (token == null) {
Expand Down Expand Up @@ -275,6 +289,15 @@ public CopyRes createScrapRes(Scrap scrap) {
return new CopyRes(id, brand,text,scrapCnt,createdAt);
}

public List<CopyRes> searchFilterCard(HttpServletRequest httpRequest, int index, CardSearchCondition condition) {
List<CopyRes> results = cardJpaRepository.filter(condition);
int startIndex = index * 30;
List<CopyRes> resultCopyRes = getLimitedCopyResByIndex(results, startIndex);
setScrapCntWhenTokenNotProvided(httpRequest, resultCopyRes);
return resultCopyRes;
}



@Transactional
public boolean saveCopy(User user, Card card) throws IOException {
Expand Down

0 comments on commit d87057d

Please sign in to comment.