Skip to content

Commit

Permalink
Merge branch 'main' into jiwon
Browse files Browse the repository at this point in the history
  • Loading branch information
JiwonKim08 authored Feb 18, 2024
2 parents bc2dff4 + bf9caf6 commit 4189dc4
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 37 deletions.
26 changes: 15 additions & 11 deletions src/main/java/shop/hooking/hooking/controller/CopyController.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import shop.hooking.hooking.dto.request.CrawlingReqDto;
import shop.hooking.hooking.dto.request.FolderReqDto;
import shop.hooking.hooking.dto.request.ScrapReqDto;
import shop.hooking.hooking.dto.request.RandomSeedDto;
import shop.hooking.hooking.dto.response.CopyResDto;
import shop.hooking.hooking.dto.response.CopySearchResDto;
import shop.hooking.hooking.entity.Card;
Expand All @@ -34,9 +35,9 @@ public class CopyController {

@Operation(summary = "전체 카피라이팅 조회하기")
@GetMapping("/{index}")
public ResponseEntity<List<CopyResDto>> getCopyList(HttpServletRequest httpRequest, @PathVariable int index) {
public ResponseEntity<List<CopyResDto>> getCopyList(HttpServletRequest httpRequest, @PathVariable int index,@RequestBody(required = false) RandomSeedDto randomSeedDto) {
try {
List<CopyResDto> copyResList = copyService.getCopyList(httpRequest, index);
List<CopyResDto> copyResList = copyService.getCopyList(httpRequest, index,randomSeedDto.getRandomSeed());
return ResponseEntity.ok(copyResList);
} catch (IllegalArgumentException ex) {
throw new OutOfIndexException();
Expand All @@ -46,24 +47,27 @@ public ResponseEntity<List<CopyResDto>> getCopyList(HttpServletRequest httpReque


@Operation(summary = "브랜드 카피라이팅 검색하기")
@GetMapping("/search/brand/{index}")
@PostMapping("/search/brand/{index}")
public ResponseEntity<CopySearchResDto> searchBrandList(HttpServletRequest httpRequest,
@RequestParam(name = "keyword") String q, @PathVariable int index) {
return ResponseEntity.ok(copyService.searchBrandList(httpRequest, q, index));
@RequestParam(name = "keyword") String q,
@PathVariable int index, @RequestBody(required = false) RandomSeedDto randomSeedDto) {
return ResponseEntity.ok(copyService.searchBrandList(httpRequest, q, index, randomSeedDto.getRandomSeed()));
}

@Operation(summary = "키워드 카피라이팅 검색하기")
@GetMapping("/search/text/{index}")
@PostMapping("/search/text/{index}")
public ResponseEntity<CopySearchResDto> searchCopyList(HttpServletRequest httpRequest,
@RequestParam(name = "keyword") String q, @PathVariable int index) {
return ResponseEntity.ok(copyService.searchCopyList(httpRequest, q, index));
@RequestParam(name = "keyword") String q,
@PathVariable int index,@RequestBody(required = false) RandomSeedDto randomSeedDto) {
return ResponseEntity.ok(copyService.searchCopyList(httpRequest, q, index,randomSeedDto.getRandomSeed()));
}

@Operation(summary = "무드 카피라이팅 검색하기")
@GetMapping("/search/mood/{index}")
@PostMapping("/search/mood/{index}")
public ResponseEntity<CopySearchResDto> searchMoodList(HttpServletRequest httpRequest,
@RequestParam(name = "keyword") String q, @PathVariable int index) {
return ResponseEntity.ok(copyService.searchMoodList(httpRequest, q, index));
@RequestParam(name = "keyword") String q,
@PathVariable int index,@RequestBody(required = false) RandomSeedDto randomSeedDto) {
return ResponseEntity.ok(copyService.searchMoodList(httpRequest, q, index, randomSeedDto.getRandomSeed()));
}


Expand Down
10 changes: 10 additions & 0 deletions src/main/java/shop/hooking/hooking/dto/request/RandomSeedDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package shop.hooking.hooking.dto.request;

import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class RandomSeedDto {
private Long randomSeed;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ public class CopySearchResDto {
private String type;
private List<CopyResDto> data;

private Long randomSeed;


@Builder
public CopySearchResDto(int totalNum, String keyword, String type, List<CopyResDto> data) {
public CopySearchResDto(int totalNum, String keyword, String type, List<CopyResDto> data, Long randomSeed) {
this.totalNum = totalNum;
this.keyword = keyword;
this.type = type;
this.data = data;
this.randomSeed=randomSeed;
}

}
106 changes: 81 additions & 25 deletions src/main/java/shop/hooking/hooking/service/CopyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import shop.hooking.hooking.config.enumtype.BrandType;
import shop.hooking.hooking.config.enumtype.MoodType;
import shop.hooking.hooking.dto.request.ScrapReqDto;
import shop.hooking.hooking.dto.request.RandomSeedDto;

import shop.hooking.hooking.dto.response.CopyResDto;
import shop.hooking.hooking.global.jwt.JwtTokenProvider;
import shop.hooking.hooking.dto.CardSearchCondition;
Expand Down Expand Up @@ -35,14 +37,15 @@ public class CopyService {
private final ContainRepository containRepository;

//상속 -> 일반 부모 , 카카오 자식
public List<CopyResDto> getCopyList(HttpServletRequest httpRequest, int index) {
public List<CopyResDto> getCopyList(HttpServletRequest httpRequest, int index, Long randomSeedDto) {
User user = jwtTokenProvider.getUserInfoByToken(httpRequest);
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<CopyResDto> tempCopyRes = new ArrayList<>();
for (Long brandId : brandIds) {
List<CopyResDto> copyRes = getTopEightCopy(brandId);
tempCopyRes.addAll(copyRes);
}

Collections.shuffle(tempCopyRes);
List<CopyResDto> resultCopyRes = getCopyByIndex(tempCopyRes, index);
setScrapCnt(httpRequest, resultCopyRes);
Expand Down Expand Up @@ -95,48 +98,83 @@ public List<CopyResDto> getTopEightCopy(Long brandId) {
return copyResList;
}

public CopySearchResDto searchBrandList(HttpServletRequest httpRequest, String q, int index) {
public CopySearchResDto searchBrandList(HttpServletRequest httpRequest, String q, int index, Long randomSeedDto) {
User user = jwtTokenProvider.getUserInfoByToken(httpRequest);

q = checkKeyword(q);

List<CopyResDto> brandCopyRes;

if (BrandType.containsKeyword(q)) {
brandCopyRes = cardJpaRepository.searchBrand(q);
setScrapCnt(httpRequest, brandCopyRes);
setIsScrap(user, brandCopyRes);
Collections.shuffle(brandCopyRes);

CopySearchResDto brandSearchResult = createCopySearchResult("brand", q, brandCopyRes, index);
return brandSearchResult;
// index가 0이면 백엔드에서 랜덤 시드값 생성
if (index == 0) {
Long seed = new Random().nextLong(); // 랜덤 시드값 생성
brandCopyRes = cardJpaRepository.searchBrand(q);
setScrapCnt(httpRequest, brandCopyRes);
setIsScrap(user, brandCopyRes);
Collections.shuffle(brandCopyRes, new Random(seed)); // 생성한 시드값으로 섞기

// 생성한 랜덤 시드값과 함께 결과 반환
CopySearchResDto brandSearchResult = createCopySearchResult("brand", q, brandCopyRes, index, seed);
return brandSearchResult;
} else if (index > 0) {
// index가 1 이상이면 프론트엔드에서 전달한 시드값 사용
// 프론트엔드에서 전달받은 시드값으로 섞기
// 여기서는 seed를 프론트엔드에서 전달받는다고 가정합니다.
Long seed = randomSeedDto; // 프론트엔드에서 전달받은 시드값
brandCopyRes = cardJpaRepository.searchBrand(q);
setScrapCnt(httpRequest, brandCopyRes);
setIsScrap(user, brandCopyRes);
Collections.shuffle(brandCopyRes, new Random(seed)); // 전달받은 시드값으로 섞기

CopySearchResDto brandSearchResult = createCopySearchResult("brand", q,brandCopyRes, index, seed);
return brandSearchResult;
}
}

// 검색 결과가 없다면
throw new CardNotFoundException();
}



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

public CopySearchResDto searchMoodList(HttpServletRequest httpRequest, String q, int index) {
public CopySearchResDto searchMoodList(HttpServletRequest httpRequest, String q, int index, Long randomSeedDto) {
User user = jwtTokenProvider.getUserInfoByToken(httpRequest);

List<CopyResDto> moodCopyRes;

if (MoodType.containsKeyword(q)) {
moodCopyRes = cardJpaRepository.searchMood(q);
setScrapCnt(httpRequest, moodCopyRes);
setIsScrap(user, moodCopyRes);
Collections.shuffle(moodCopyRes);

CopySearchResDto moodSearchResult = createCopySearchResult("mood", q, moodCopyRes, index);
return moodSearchResult;
// index가 0이면 백엔드에서 랜덤 시드값 생성
if (index == 0) {
Long seed = new Random().nextLong(); // 랜덤 시드값 생성
moodCopyRes = cardJpaRepository.searchMood(q);
setScrapCnt(httpRequest, moodCopyRes);
setIsScrap(user, moodCopyRes);
Collections.shuffle(moodCopyRes, new Random(seed)); // 생성한 시드값으로 섞기

// 생성한 랜덤 시드값과 함께 결과 반환
CopySearchResDto moodSearchResult = createCopySearchResult("mood", q, moodCopyRes, index, seed);
return moodSearchResult;
} else if (index > 0) {
// index가 1 이상이면 프론트엔드에서 전달한 시드값 사용
// 프론트엔드에서 전달받은 시드값으로 섞기
// 여기서는 seed를 프론트엔드에서 전달받는다고 가정합니다.
Long seed = randomSeedDto; // 프론트엔드에서 전달받은 시드값
moodCopyRes = cardJpaRepository.searchMood(q);
setScrapCnt(httpRequest, moodCopyRes);
setIsScrap(user, moodCopyRes);
Collections.shuffle(moodCopyRes, new Random(seed)); // 전달받은 시드값으로 섞기

CopySearchResDto moodSearchResult = createCopySearchResult("mood", q, moodCopyRes, index,seed);
return moodSearchResult;
}
}

// 검색 결과가 없다면
Expand All @@ -145,20 +183,36 @@ public CopySearchResDto searchMoodList(HttpServletRequest httpRequest, String q,



public CopySearchResDto searchCopyList(HttpServletRequest httpRequest, String q, int index) {
public CopySearchResDto searchCopyList(HttpServletRequest httpRequest, String q, int index, Long randomSeedDto) {
User user = jwtTokenProvider.getUserInfoByToken(httpRequest);

List<CopyResDto> textCopyRes;

textCopyRes = cardJpaRepository.searchCopy(q);

if (!textCopyRes.isEmpty()) {
setScrapCnt(httpRequest, textCopyRes);
setIsScrap(user, textCopyRes);
Collections.shuffle(textCopyRes);

CopySearchResDto textSearchResult = createCopySearchResult("text", q, textCopyRes, index);
return textSearchResult;
// index가 0이면 백엔드에서 랜덤 시드값 생성
if (index == 0) {
Long seed = new Random().nextLong(); // 랜덤 시드값 생성
setScrapCnt(httpRequest, textCopyRes);
setIsScrap(user, textCopyRes);
Collections.shuffle(textCopyRes, new Random(seed)); // 생성한 시드값으로 섞기

// 생성한 랜덤 시드값과 함께 결과 반환
CopySearchResDto textSearchResult = createCopySearchResult("text", q, textCopyRes, index, seed);
return textSearchResult;
} else if (index > 0) {
// index가 1 이상이면 프론트엔드에서 전달한 시드값 사용
// 프론트엔드에서 전달받은 시드값으로 섞기
// 여기서는 seed를 프론트엔드에서 전달받는다고 가정합니다.
Long seed = randomSeedDto; // 프론트엔드에서 전달받은 시드값
setScrapCnt(httpRequest, textCopyRes);
setIsScrap(user, textCopyRes);
Collections.shuffle(textCopyRes, new Random(seed)); // 전달받은 시드값으로 섞기

CopySearchResDto textSearchResult = createCopySearchResult("text", q, textCopyRes, index,seed);
return textSearchResult;
}
}

// 검색 결과가 없다면
Expand All @@ -167,6 +221,7 @@ public CopySearchResDto searchCopyList(HttpServletRequest httpRequest, String q,




@Transactional
public List<CopyResDto> getScrapList(HttpServletRequest httpRequest, int index) {
User user = jwtTokenProvider.getUserInfoByToken(httpRequest);
Expand Down Expand Up @@ -287,14 +342,15 @@ public CopyResDto createCopyRes(Card card) {



public CopySearchResDto createCopySearchResult(String type, String keyword, List<CopyResDto> copyResList, int index) {
public CopySearchResDto createCopySearchResult(String type, String keyword, List<CopyResDto> copyResList, int index,Long seed) {
List<CopyResDto> slicedCopyResList = getCopyByIndex(copyResList, index);

return CopySearchResDto.builder()
.type(type)
.keyword(keyword)
.totalNum(copyResList.size())
.data(slicedCopyResList)
.randomSeed(seed)
.build();
}

Expand Down

0 comments on commit 4189dc4

Please sign in to comment.