Skip to content

Commit

Permalink
Merge pull request #84 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 6712242 + cd90685 commit 797271c
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,9 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo
} else if (referer != null && referer.startsWith("http://localhost:3000/") && host.equals("hooking.shop")) {
targetUrl = "http://localhost:3000/oath-processor"; // 로컬 환경
} else if (referer != null && referer.startsWith("https://hooking.me/") && host.equals("hooking.shop")) {
targetUrl = "https://hooking.me/oath-processor"; // 배포
targetUrl = "https://hooking.me/oath-processor"; // 실배포 환경

}

// else if (referer != null && referer.startsWith("https://hooking.me/") && host.equals("hooking.shop")) {
// targetUrl = "https://hooking.me/oath-processor"; // 로컬 환경
// }

else {
// 기본적으로 로컬 개발 환경으로 설정
targetUrl = "http://localhost:3000/oath-processor"; // 로컬 환경
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ 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}/{index}","/example/login"

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

.anyRequest().authenticated()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ public HttpRes<List<BrandRes.BrandDto>> showAllBrand(){
}

// 해당 브랜드 상세정보 조회

@PostMapping("/{brand_id}/{index}")
public HttpRes<BrandRes.BrandDetailDto> showOneBrand(@PathVariable Long brand_id, @PathVariable int index){
public HttpRes<BrandRes.BrandDetailDto> showOneBrand(HttpServletRequest httpRequest,@PathVariable Long brand_id, @PathVariable int index){
BrandRes.BrandDetailDto brandDetailDto = brandService.getOneBrand(brand_id); // List<card> 가 전체 반환됨

// 전체 card 리스트를 가져옴
Expand All @@ -53,8 +54,21 @@ public HttpRes<BrandRes.BrandDetailDto> showOneBrand(@PathVariable Long brand_id

brandDetailDto.setCard(resultCards);


// 로그인이 안되어있을 경우 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);
}
}



// startIndex부터 30개의 카드를 잘라서 반환하는 메서드
private List<Card> getLimitedCardsByIndex(List<Card> cards, int startIndex) {
Expand Down
60 changes: 35 additions & 25 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 @@ -107,7 +105,7 @@ public CopySearchResponse copySearchList(HttpServletRequest httpRequest,@Request
moodResult.setTotalNum(moodCopyRes.size());
results.add(moodResult);

if(!textCopyRes.isEmpty()){
if (!textCopyRes.isEmpty()) {
setScrapCntWhenTokenNotProvided(httpRequest, textCopyRes);
Collections.shuffle(textCopyRes);
CopySearchResult copyResult = createCopySearchResult(textCopyRes);
Expand All @@ -126,7 +124,7 @@ public CopySearchResponse copySearchList(HttpServletRequest httpRequest,@Request
brandResult.setKeyword(q);
brandResult.setTotalNum(brandCopyRes.size());
results.add(brandResult);
} else if (!textCopyRes.isEmpty()){
} else if (!textCopyRes.isEmpty()) {
setScrapCntWhenTokenNotProvided(httpRequest, textCopyRes);
Collections.shuffle(textCopyRes);
CopySearchResult copyResult = createCopySearchResult(textCopyRes);
Expand All @@ -144,30 +142,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);

// if(user == null){
// List<CopyRes> notLoginCopyRes = copyRes.subList(0, endIndex);
// return new HttpRes<>(notLoginCopyRes);
// }
int startIndex = index * 30; //인덱싱
List<CopyRes> resultCopyRes = getLimitedCopyResByIndex(copyRes, startIndex);

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


Expand Down Expand Up @@ -214,12 +223,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) {
}






}
6 changes: 6 additions & 0 deletions src/main/java/shop/hooking/hooking/entity/Card.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import javax.persistence.Entity;
import javax.persistence.Table;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


@DynamicUpdate
Expand Down Expand Up @@ -43,9 +45,13 @@ public class Card {
@JoinColumn(name="brand_id")
private Brand brand;


@Column(name = "created_at")
private LocalDateTime createdAt;

@OneToMany(mappedBy = "card", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Scrap> scraps = new ArrayList<>();

@PrePersist
public void prePersist() {
this.scrapCnt= this.scrapCnt == null ? 0 : this.scrapCnt;
Expand Down

0 comments on commit 797271c

Please sign in to comment.