Skip to content

Commit

Permalink
Merge pull request #97 from Hooking-CEOS/feature/brand
Browse files Browse the repository at this point in the history
[feat] 브랜드 오류 처리
  • Loading branch information
CYJhub authored Jul 29, 2023
2 parents 1e56422 + 5913fed commit 2bbfd59
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public ResponseEntity<HttpRes<List<BrandRes.BrandDto>>> showAllBrand(){
@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> cards = brandDetailDto.getCard();
List<BrandRes.cardDto> cards = brandDetailDto.getCard();
int startIndex = index * 30;
List<Card> resultCards = brandService.getLimitedCardsByIndex(cards, startIndex);
List<BrandRes.cardDto> resultCards = brandService.getLimitedCardsByIndex(cards, startIndex);

if (resultCards.isEmpty()) {
String errorMessage = "카드가 없습니다.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ public ResponseEntity<HttpRes<List<CopyRes>>> copyScrapList(HttpServletRequest h
List<CopyRes> copyRes = copyService.getCopyScrapList(user);
int startIndex = index * 30;
List<CopyRes> resultCopyRes = copyService.getLimitedCopyResByIndex(copyRes, startIndex);
resultCopyRes.sort((copy1, copy2) -> copy2.getCreatedAt().compareTo(copy1.getCreatedAt()));

return ResponseEntity.ok(new HttpRes<>(resultCopyRes));
}
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/shop/hooking/hooking/dto/response/BrandRes.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,19 @@ public static class BrandDetailDto {
String brandIntro; // 브랜드 한줄소개
List<String> randomCard; // 랜덤 카피라이팅 카드에서 본문만 3개
List<String> mood; // 브랜드 무드 3개
List<Card> card; // 브랜드 전체 카피라이팅 카드
List<cardDto> card; // 브랜드 전체 카피라이팅 카드
}

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public static class cardDto{
private Long id;
private String brandName;
private String text;
private Integer scrapCnt;
private LocalDateTime createdAt;
}

}
20 changes: 15 additions & 5 deletions src/main/java/shop/hooking/hooking/service/BrandService.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ public BrandRes.BrandDetailDto getOneBrand(Long id) {
Brand brand = brandRepository.findBrandById(id);

List<Card> cards = cardRepository.findCardsByBrandId(brand.getId());
List<BrandRes.cardDto> cardDtos = new ArrayList<>();
for(Card card : cards){
BrandRes.cardDto cardDto = new BrandRes.cardDto();
cardDto.setId(card.getId());
cardDto.setBrandName(card.getBrand().getBrandName());
cardDto.setText(card.getText());
cardDto.setCreatedAt(card.getCreatedAt());
cardDto.setScrapCnt(card.getScrapCnt());
cardDtos.add(cardDto);
}

cards.forEach(card -> card.setScrapCnt((int) scrapRepository.findByCardId(card.getId()).stream().count()));

Expand All @@ -89,14 +99,14 @@ public BrandRes.BrandDetailDto getOneBrand(Long id) {
.brandIntro(brand.getBrandIntro())
.brandLink(brand.getBrandLink())
.randomCard(cardTexts)
.card(cards)
.card(cardDtos)
.mood(Arrays.asList(moodOne.getMoodName(),moodZero.getMoodName(),moodTwo.getMoodName()))
.build();

return brandDetailDto;
}

public List<Card> getLimitedCardsByIndex(List<Card> cards, int startIndex) {
public List<BrandRes.cardDto> getLimitedCardsByIndex(List<BrandRes.cardDto> cards, int startIndex) {
int endIndex = Math.min(startIndex + 30, cards.size());
return cards.subList(startIndex, endIndex);
}
Expand All @@ -107,11 +117,11 @@ public void setScrapCntWhenTokenNotProvided(List<Card> cardList) {
}
}

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

0 comments on commit 2bbfd59

Please sign in to comment.