Skip to content

Commit

Permalink
Merge pull request #87 from Hooking-CEOS/feature/copy
Browse files Browse the repository at this point in the history
Feature/copy
  • Loading branch information
JiwonKim08 authored Jul 27, 2023
2 parents 1344c07 + eb264c1 commit efe6b71
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 34 deletions.
Empty file added .github/workflows/main.yml
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,21 @@ public HttpRes<BrandRes.BrandDetailDto> showOneBrand(HttpServletRequest httpRequ
// index와 30을 곱하여 startIndex 계산
int startIndex = index * 30;


// startIndex부터 30개씩의 카드를 잘라서 resultCards 리스트에 저장
List<Card> resultCards = getLimitedCardsByIndex(cards, startIndex);

if (resultCards.isEmpty()) {
return new HttpRes<>(400, "카드가 없습니다.");
}


brandDetailDto.setCard(resultCards);


// 로그인이 안되어있을 경우 scrapCnt를 0으로 설정
//로그인이 안되어있을 경우 scrapCnt를 0으로 설정
if (jwtTokenProvider.getUserInfoByToken(httpRequest) == null) {
setScrapCntWhenTokenNotProvided(brandDetailDto.getCard());
}

return new HttpRes<>(brandDetailDto);
}


private void setScrapCntWhenTokenNotProvided(List<Card> cardList) {
for (Card card : cardList) {
card.setScrapCnt(0);
Expand Down
58 changes: 32 additions & 26 deletions src/main/java/shop/hooking/hooking/controller/CopyController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import shop.hooking.hooking.config.BrandType;
import shop.hooking.hooking.config.MoodType;
Expand Down Expand Up @@ -49,7 +50,7 @@ public class CopyController {
// 전체 카피라이팅 조회
// 페이지네이션 구현
@GetMapping("/{index}") //copy/0=> 0-30 copy/1=>0~30 copy/2=>60~90 copy/9 => 270~300
public HttpRes<List<CopyRes>> copyList(HttpServletRequest httpRequest,@PathVariable int index) {
public ResponseEntity<List<CopyRes>> copyList(HttpServletRequest httpRequest, @PathVariable int index) {
Long[] brandIds = {2L, 3L, 4L, 12L, 15L, 17L, 21L, 24L, 25L, 28L};

List<CopyRes> tempCopyRes = new ArrayList<>();
Expand All @@ -61,33 +62,38 @@ public HttpRes<List<CopyRes>> copyList(HttpServletRequest httpRequest,@PathVaria

Collections.shuffle(tempCopyRes); //섞임

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

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

setScrapCntWhenTokenNotProvided(httpRequest,resultCopyRes);

return new HttpRes<>(resultCopyRes);
setScrapCntWhenTokenNotProvided(httpRequest, resultCopyRes);

return ResponseEntity.status(HttpStatus.OK).body(resultCopyRes);
}



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

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

// 검색 결과 처리 로직...
MoodType moodType = MoodType.fromKeyword(q);
List<CopyRes> moodCopyRes = new ArrayList<>();
List<CopyRes> textCopyRes = new ArrayList<>();
Expand Down Expand Up @@ -136,10 +142,12 @@ public CopySearchResponse copySearchList(HttpServletRequest httpRequest,
}

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

// 요청한 index에 따라 30개씩 다른 결과를 생성
Expand All @@ -150,9 +158,11 @@ public CopySearchResponse copySearchList(HttpServletRequest httpRequest,
response.setMessage("요청에 성공하였습니다.");
response.setData(resultCopyRes);

return response;
return ResponseEntity.status(HttpStatus.OK)
.body(response);
}


private List<CopyRes> getLimitedCopyResByIndex(List<CopyRes> copyResList, int startIndex) {
int endIndex = Math.min(startIndex + 30, copyResList.size());
return copyResList.subList(startIndex, endIndex);
Expand All @@ -168,34 +178,30 @@ private List<CopySearchResult> getLimitedCopyResByIndex2(List<CopySearchResult>

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

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

if (copyRes.isEmpty()) {
return new HttpRes<>(400,"스크랩한 내용이 없습니다.");
}

int startIndex = index * 30; //인덱싱
int startIndex = index * 30;
List<CopyRes> resultCopyRes = getLimitedCopyResByIndex(copyRes, startIndex);

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



@CrossOrigin(origins = "https://hooking.shop, https://hooking-dev.netlify.app/, https://hooking.netlify.app/, http://localhost:3000/, http://localhost:3001/")
@PostMapping("/scrap")
public HttpRes<String> copyScrap(HttpServletRequest httpRequest, @RequestBody CopyReq copyReq) throws IOException {
public ResponseEntity<String> copyScrap(HttpServletRequest httpRequest, @RequestBody CopyReq copyReq) throws IOException {
User user = jwtTokenProvider.getUserInfoByToken(httpRequest);
Card card = cardRepository.findCardById(copyReq.getCardId());
boolean isScrap = copyService.saveCopy(user, card);
if(isScrap){
return new HttpRes<>("스크랩을 완료하였습니다.");
}


return new HttpRes<>(HttpStatus.BAD_REQUEST.value(),"중복 스크랩이 불가능합니다.");
if (isScrap) {
return ResponseEntity.ok("스크랩을 완료하였습니다.");
} else {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("중복 스크랩이 불가능합니다.");
}
}


Expand Down
3 changes: 2 additions & 1 deletion src/main/java/shop/hooking/hooking/exception/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public enum ErrorCode {

//400 BAD_REQUEST : 잘못된 요청
CANNOT_EMPTY_CONTENT(BAD_REQUEST, "내용이 비어있을 수 없습니다."),
CANNOT_DUPLICATE_SCRAP(BAD_REQUEST, "스크랩을 중복해서 저장할 수 없습니다."),
INVALID_VALUE(BAD_REQUEST, "올바르지 않은 값입니다."),
INVALID_IMAGE_FILE(BAD_REQUEST, "잘못된 이미지 파일입니다."),
INVALID_SESSION_USER(BAD_REQUEST, "세션 유저가 비어있습니다."),
Expand All @@ -25,7 +26,7 @@ public enum ErrorCode {

//404 NOT_FOUND : Resource를 찾을 수 없음
USER_NOT_FOUND(NOT_FOUND, "해당 유저 정보를 찾을 수 없습니다."),

COPY_NOT_FOUND(NOT_FOUND, "해당 카피를 찾을 수 없습니다."),
//500 INTERNAL_SERVER_ERROR : 서버 내 문제
EXCEPTION(INTERNAL_SERVER_ERROR, "서버 내에 알 수 없는 오류가 발생했습니다."),
IO_EXCEPTION(INTERNAL_SERVER_ERROR, "이미지 업로드/다운로드 중 알 수 없는 오류가 발생했습니다.");
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher
# jackson:
# serialization:
# fail-on-empty-beans: false

app:
deployment:
Expand Down

0 comments on commit efe6b71

Please sign in to comment.