Skip to content

Commit

Permalink
[refactor] 검색 코드
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiwon committed Jul 28, 2023
1 parent 10ab640 commit 9fd8ecb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 52 deletions.
91 changes: 40 additions & 51 deletions src/main/java/shop/hooking/hooking/controller/CopyController.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import shop.hooking.hooking.dto.request.CrawlingData;
import shop.hooking.hooking.dto.request.CrawlingReq;
import shop.hooking.hooking.dto.response.CopyRes;
import shop.hooking.hooking.dto.response.CopySearchResponse;
import shop.hooking.hooking.dto.response.CopySearchRes;
import shop.hooking.hooking.dto.response.CopySearchResult;
import shop.hooking.hooking.entity.Brand;
import shop.hooking.hooking.entity.Card;
Expand Down Expand Up @@ -60,33 +60,31 @@ public ResponseEntity<HttpRes<List<CopyRes>>> copyList(HttpServletRequest httpRe
tempCopyRes.addAll(copyRes);
}

Collections.shuffle(tempCopyRes); //섞임
// 랜덤
Collections.shuffle(tempCopyRes);

if (tempCopyRes.isEmpty()) {
String errorMessage = "검색 결과를 찾을 수 없습니다.";
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
}

// 요청한 index에 따라 30개의 다른 결과를 생성
int startIndex = index * 30; //인덱싱
// 인덱싱
int startIndex = index * 30;
List<CopyRes> resultCopyRes = getLimitedCopyResByIndex(tempCopyRes, startIndex);

setScrapCntWhenTokenNotProvided(httpRequest, resultCopyRes);

return ResponseEntity.ok(new HttpRes<>(resultCopyRes));
}



@GetMapping("/search/{index}")
public ResponseEntity<CopySearchResponse> copySearchList(HttpServletRequest httpRequest,
@RequestParam(name = "keyword") String q,
@PathVariable int index) {
public ResponseEntity<CopySearchRes> copySearchList(HttpServletRequest httpRequest,
@RequestParam(name = "keyword") String q,
@PathVariable int index) {

int startIndex = index * 30;


CopySearchResponse response = new CopySearchResponse();
CopySearchRes response = new CopySearchRes();
List<CopySearchResult> results = new ArrayList<>();

if (q.isEmpty()) { // 검색 결과가 없다면
Expand All @@ -97,9 +95,8 @@ public ResponseEntity<CopySearchResponse> copySearchList(HttpServletRequest http
.body(response);
}

if(q.equals("애프터블로우")){
q="애프터 블로우";
}
q = checkKeyword(q);


// 검색 결과 처리 로직...
MoodType moodType = MoodType.fromKeyword(q);
Expand All @@ -113,55 +110,29 @@ public ResponseEntity<CopySearchResponse> copySearchList(HttpServletRequest http
moodCopyRes = cardJpaRepository.searchMood(q);
setScrapCntWhenTokenNotProvided(httpRequest, moodCopyRes);
Collections.shuffle(moodCopyRes);
CopySearchResult moodResult = new CopySearchResult();
moodResult.setType("mood");
moodResult.setKeyword(q); // 현재는 전체 카드 수가 나옴
moodResult.setTotalNum(moodCopyRes.size());
moodResult.setData(getLimitedCopyResByIndex(moodCopyRes,index));
results.add(moodResult);

results.add(createCopySearchResult("mood", q, moodCopyRes, index));
if (!textCopyRes.isEmpty()) {
setScrapCntWhenTokenNotProvided(httpRequest, textCopyRes);
Collections.shuffle(textCopyRes);
CopySearchResult copyResult = new CopySearchResult();
copyResult.setType("copy");
copyResult.setKeyword(q);
copyResult.setTotalNum(textCopyRes.size());
copyResult.setData(getLimitedCopyResByIndex(textCopyRes,index));
setIndicesForCopyRes(textCopyRes, q);
results.add(copyResult);
results.add(createCopySearchResult("copy", q, textCopyRes, index));
}
} else if (BrandType.containsKeyword(q)) {
brandCopyRes = cardJpaRepository.searchBrand(q);
setScrapCntWhenTokenNotProvided(httpRequest, brandCopyRes);
Collections.shuffle(brandCopyRes);
CopySearchResult brandResult = new CopySearchResult();
brandResult.setType("brand");
brandResult.setKeyword(q);
brandResult.setTotalNum(brandCopyRes.size());
brandResult.setData(getLimitedCopyResByIndex(brandCopyRes,index));
results.add(brandResult);
results.add(createCopySearchResult("brand", q, brandCopyRes, index));
} else if (!textCopyRes.isEmpty()) {
setScrapCntWhenTokenNotProvided(httpRequest, textCopyRes);
Collections.shuffle(textCopyRes);
CopySearchResult copyResult = new CopySearchResult();
copyResult.setType("copy");
copyResult.setKeyword(q);
copyResult.setTotalNum(textCopyRes.size());
copyResult.setData(getLimitedCopyResByIndex(textCopyRes,index));
setIndicesForCopyRes(textCopyRes, q);
results.add(copyResult);
results.add(createCopySearchResult("copy", q, textCopyRes, index));
}

// 검색 결과가 없다면
if (results.isEmpty()) {
String errorMessage = "검색 결과를 찾을 수 없습니다.";
response.setCode(HttpStatus.BAD_REQUEST.value());
response.setMessage(errorMessage);
response.setData(results);
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(response);
return getBadRequestResponseEntity(response, results);
}


response.setCode(HttpStatus.OK.value());
response.setMessage("요청에 성공하였습니다.");
response.setData(results);
Expand Down Expand Up @@ -255,6 +226,14 @@ public ResponseEntity<List<CopyRes>> searchFilterCard(HttpServletRequest httpReq
return ResponseEntity.status(HttpStatus.OK).body(resultCopyRes);
}

private String checkKeyword(String q) {
if (q.equals("애프터블로우")) {
q = "애프터 블로우";
}
return q;
}


private void setScrapCntWhenTokenNotProvided(HttpServletRequest httpRequest, List<CopyRes> copyResList) {
String token = httpRequest.getHeader("X-AUTH-TOKEN");
if (token == null) {
Expand All @@ -264,14 +243,24 @@ private void setScrapCntWhenTokenNotProvided(HttpServletRequest httpRequest, Lis
}
}

private CopySearchResult createCopySearchResult(List<CopyRes> copyResList) {

private CopySearchResult createCopySearchResult(String type, String keyword, List<CopyRes> copyResList, int index) {
CopySearchResult result = new CopySearchResult();
int endIndex = Math.min(30, copyResList.size());
List<CopyRes> limitedCopyRes = copyResList.subList(0, endIndex);
result.setData(limitedCopyRes);
result.setType(type);
result.setKeyword(keyword);
result.setTotalNum(copyResList.size());
result.setData(getLimitedCopyResByIndex(copyResList, index));
return result;
}

private ResponseEntity<CopySearchRes> getBadRequestResponseEntity(CopySearchRes response, List<CopySearchResult> results) {
response.setCode(HttpStatus.BAD_REQUEST.value());
response.setMessage("검색 결과를 찾을 수 없습니다.");
response.setData(results);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
}



private void setIndicesForCopyRes(List<CopyRes> copyResList, String keyword) {
for (CopyRes copyRes : copyResList) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@Getter
@Setter
@NoArgsConstructor
public class CopySearchResponse {
public class CopySearchRes {
private int code;
private String message;
private List<CopySearchResult> data;
Expand Down

0 comments on commit 9fd8ecb

Please sign in to comment.