Skip to content

Commit

Permalink
[feat] 인덱스 처리
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiwon committed Jul 27, 2023
1 parent 7325d39 commit 842a659
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.requestMatchers(CorsUtils::isPreFlightRequest).permitAll()


.antMatchers("**/oath-processor/**","/kakaologin","/copy/{index}", "/copy/search","/copy/filter","/copy/crawling", "/brand", "/brand/{brand_id}","/example/login"
.antMatchers("**/oath-processor/**","/kakaologin","/copy/{index}", "/copy/search/{index}","/copy/filter/{index}","/copy/crawling", "/brand", "/brand/{brand_id}","/example/login"

).permitAll()

Expand Down
55 changes: 35 additions & 20 deletions src/main/java/shop/hooking/hooking/controller/CopyController.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,17 @@ public HttpRes<List<CopyRes>> copyList(HttpServletRequest httpRequest,@PathVaria

}

private List<CopyRes> getLimitedCopyResByIndex(List<CopyRes> copyResList, int startIndex) {
int endIndex = Math.min(startIndex + 30, copyResList.size());
return copyResList.subList(startIndex, endIndex);
}


@Cacheable("copySearchCache")
@GetMapping("/search")
public CopySearchResponse copySearchList(HttpServletRequest httpRequest,@RequestParam(name = "keyword") String q) {
@GetMapping("/search/{index}")
public CopySearchResponse copySearchList(HttpServletRequest httpRequest,
@RequestParam(name = "keyword") String q,
@PathVariable int index) {
CopySearchResponse response = new CopySearchResponse();
List<CopySearchResult> results = new ArrayList<>();

if (q.isEmpty()) { //검색 결과가 없다면
if (q.isEmpty()) { // 검색 결과가 없다면
response.setCode(HttpStatus.BAD_REQUEST.value());
response.setMessage("검색 결과를 찾을 수 없습니다.");
response.setData(results);
Expand All @@ -106,7 +104,7 @@ public CopySearchResponse copySearchList(HttpServletRequest httpRequest,@Request
moodResult.setKeyword(q);
results.add(moodResult);

if(!textCopyRes.isEmpty()){
if (!textCopyRes.isEmpty()) {
setScrapCntWhenTokenNotProvided(httpRequest, textCopyRes);
Collections.shuffle(textCopyRes);
CopySearchResult copyResult = createCopySearchResult(textCopyRes);
Expand All @@ -123,7 +121,7 @@ public CopySearchResponse copySearchList(HttpServletRequest httpRequest,@Request
brandResult.setType("brand");
brandResult.setKeyword(q);
results.add(brandResult);
} else if (!textCopyRes.isEmpty()){
} else if (!textCopyRes.isEmpty()) {
setScrapCntWhenTokenNotProvided(httpRequest, textCopyRes);
Collections.shuffle(textCopyRes);
CopySearchResult copyResult = createCopySearchResult(textCopyRes);
Expand All @@ -140,25 +138,41 @@ public CopySearchResponse copySearchList(HttpServletRequest httpRequest,@Request
return response;
}

// 요청한 index에 따라 30개씩 다른 결과를 생성
int startIndex = index * 30;
List<CopySearchResult> resultCopyRes = getLimitedCopyResByIndex2(results, startIndex);

response.setCode(HttpStatus.OK.value());
response.setMessage("요청에 성공하였습니다.");
response.setData(results);
response.setData(resultCopyRes);

return response;
}

private List<CopyRes> getLimitedCopyResByIndex(List<CopyRes> copyResList, int startIndex) {
int endIndex = Math.min(startIndex + 30, copyResList.size());
return copyResList.subList(startIndex, endIndex);
}

private List<CopySearchResult> getLimitedCopyResByIndex2(List<CopySearchResult> copyResList, int startIndex) {
int endIndex = Math.min(startIndex + 30, copyResList.size());
return copyResList.subList(startIndex, endIndex);
}




@CrossOrigin(origins = "https://hooking.shop, https://hooking-dev.netlify.app/, https://hooking.netlify.app/, http://localhost:3000/, http://localhost:3001/")
@GetMapping("/scrap")
public HttpRes<List<CopyRes>> copyScrapList(HttpServletRequest httpRequest) {
@GetMapping("/scrap/{index}")
public HttpRes<List<CopyRes>> copyScrapList(HttpServletRequest httpRequest,@PathVariable int index) {
User user = jwtTokenProvider.getUserInfoByToken(httpRequest);

List<CopyRes> copyRes = copyService.getCopyScrapList(user);

int endIndex = Math.min(30, copyRes.size());
List<CopyRes> limitedCopyRes = copyRes.subList(0, endIndex);
int startIndex = index * 30; //인덱싱
List<CopyRes> resultCopyRes = getLimitedCopyResByIndex(copyRes, startIndex);

return new HttpRes<>(limitedCopyRes);
return new HttpRes<>(resultCopyRes);
}


Expand Down Expand Up @@ -205,12 +219,13 @@ public HttpRes<String> saveCrawling(@RequestBody CrawlingReq crawlingReq) {
}


@GetMapping("/filter")
public HttpRes<List<CopyRes>> searchFilterCard(HttpServletRequest httpRequest,CardSearchCondition condition) {
@GetMapping("/filter/{index}")
public HttpRes<List<CopyRes>> searchFilterCard(HttpServletRequest httpRequest,@PathVariable int index,CardSearchCondition condition) {
List<CopyRes> results = cardJpaRepository.filter(condition);
List<CopyRes> limitedResults = getLimitedCopyRes(results,30);
setScrapCntWhenTokenNotProvided(httpRequest, limitedResults);
return new HttpRes<>(limitedResults);
int startIndex = index * 30; //인덱싱
List<CopyRes> resultCopyRes = getLimitedCopyResByIndex(results, startIndex);
setScrapCntWhenTokenNotProvided(httpRequest, resultCopyRes);
return new HttpRes<>(resultCopyRes);
}

private void setScrapCntWhenTokenNotProvided(HttpServletRequest httpRequest, List<CopyRes> copyResList) {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/shop/hooking/hooking/dto/HttpRes.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ public HttpRes(T data) {
}






}

0 comments on commit 842a659

Please sign in to comment.