Skip to content

Commit

Permalink
Merge pull request #94 from Hooking-CEOS/feature/brand
Browse files Browse the repository at this point in the history
카카오 분기 처리
  • Loading branch information
CYJhub authored Jul 28, 2023
2 parents fc2d078 + f0f80a2 commit cbb0b62
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo
}
else {
// 기본적으로 로컬 개발 환경으로 설정
targetUrl = "http://localhost:3000/oath-processor"; // 로컬 환경
targetUrl = redirectUrl; // 로컬 환경
}

// 쿼리 파라미터를 추가하여 targetUrl 생성
Expand Down
40 changes: 3 additions & 37 deletions src/main/java/shop/hooking/hooking/controller/BrandController.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ public class BrandController {

private final JwtTokenProvider jwtTokenProvider;

private final UserRepository userRepository;


// 전체 브랜드 기본정보 조회
@CrossOrigin(origins = "https://hooking.shop, https://hooking-dev.netlify.app/, https://hooking.netlify.app/, http://localhost:3000/, http://localhost:3001/")
@GetMapping("")
public ResponseEntity<HttpRes<List<BrandRes.BrandDto>>> showAllBrand(){
Expand All @@ -40,56 +36,26 @@ public ResponseEntity<HttpRes<List<BrandRes.BrandDto>>> showAllBrand(){
return ResponseEntity.ok(new HttpRes<>(brandDtoList));
}

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

@CrossOrigin(origins = "https://hooking.shop, https://hooking-dev.netlify.app/, https://hooking.netlify.app/, http://localhost:3000/, http://localhost:3001/")
@PostMapping("/{brand_id}/{index}")
public ResponseEntity<HttpRes<BrandRes.BrandDetailDto>> showOneBrand(HttpServletRequest httpRequest, @PathVariable Long brand_id, @PathVariable int index){
BrandRes.BrandDetailDto brandDetailDto = brandService.getOneBrand(brand_id); // List<card> 가 전체 반환됨

// 전체 card 리스트를 가져옴
BrandRes.BrandDetailDto brandDetailDto = brandService.getOneBrand(brand_id);
List<Card> cards = brandDetailDto.getCard();


// index와 30을 곱하여 startIndex 계산
int startIndex = index * 30;

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


List<Card> resultCards = brandService.getLimitedCardsByIndex(cards, startIndex);

if (resultCards.isEmpty()) {
String errorMessage = "카드가 없습니다.";
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new HttpRes<>(HttpStatus.BAD_REQUEST.value(), errorMessage));
}

brandDetailDto.setCard(resultCards);


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

return ResponseEntity.ok(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) {
int endIndex = Math.min(startIndex + 30, cards.size());
return cards.subList(startIndex, endIndex);
}

// // 해당 브랜드 팔로우
// @CrossOrigin(origins = "https://hooking.shop, https://hooking-dev.netlify.app/, https://hooking.netlify.app/, http://localhost:3000, http://localhost:3001")
// @PostMapping("/{brand_id}/follow")
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/shop/hooking/hooking/service/BrandService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import shop.hooking.hooking.entity.*;
import shop.hooking.hooking.repository.*;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.time.LocalDateTime;
import java.util.ArrayList;
Expand Down Expand Up @@ -95,6 +96,26 @@ public BrandRes.BrandDetailDto getOneBrand(Long id) {
return brandDetailDto;
}

public List<Card> getLimitedCardsByIndex(List<Card> cards, int startIndex) {
int endIndex = Math.min(startIndex + 30, cards.size());
return cards.subList(startIndex, endIndex);
}

public void setScrapCntWhenTokenNotProvided(List<Card> cardList) {
for (Card card : cardList) {
card.setScrapCnt(0);
}
}

public void setScrapCntWhenTokenNotProvided(HttpServletRequest httpRequest, List<Card> cardList) {
String token = httpRequest.getHeader("X-AUTH-TOKEN");
if (token == null) {
for (Card card : cardList) {
card.setScrapCnt(0);
}
}
}

// public boolean followBrand(Long brandId, User user){
//
// Brand brand = brandRepository.findBrandById(brandId);
Expand Down

0 comments on commit cbb0b62

Please sign in to comment.