Skip to content

Commit

Permalink
NABI-204--refactor : 카드 단건조회 로직 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
BeommoKoo-dev committed Nov 14, 2023
1 parent 84aa5bb commit a11dd71
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.prgrms.nabimarketbe.domain.cardimage.dto.response.CardImageSingleReadResponseDTO;
import org.prgrms.nabimarketbe.domain.cardimage.entity.CardImage;
import org.prgrms.nabimarketbe.domain.category.entity.CategoryEnum;
import org.prgrms.nabimarketbe.domain.item.entity.Item;
import org.prgrms.nabimarketbe.domain.item.entity.PriceRange;

import lombok.Builder;
Expand All @@ -35,25 +34,25 @@ public record CardDetail(
) {
public static CardDetail of(
Card card,
Item item,
List<CardImage> cardImages
List<CardImage> cardImages,
boolean isMyDib
) {
return CardDetail.builder()
.cardId(card.getCardId())
.cardTitle(card.getCardTitle())
.category(item.getCategory().getCategoryName())
.itemName(item.getItemName())
.category(card.getItem().getCategory().getCategoryName())
.itemName(card.getItem().getItemName())
.pokeAvailable(card.getPokeAvailable())
.createdAt(card.getCreatedDate())
.modifiedAt(card.getModifiedDate())
.viewCount(card.getViewCount())
.priceRange(item.getPriceRange())
.priceRange(card.getItem().getPriceRange())
.content(card.getContent())
.cardStatus(card.getStatus())
.tradeType(card.getTradeType())
.tradeArea(card.getTradeArea())
.dibsCount(card.getDibCount())
.isMyDib(true) //TODO
.isMyDib(isMyDib)
.images(cardImages.stream().map(
cardImage -> CardImageSingleReadResponseDTO.from(cardImage.getImageUrl())).toList())
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package org.prgrms.nabimarketbe.domain.card.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import org.prgrms.nabimarketbe.domain.card.dto.request.CardCreateRequestDTO;
import org.prgrms.nabimarketbe.domain.card.dto.request.CardStatusUpdateRequestDTO;
import org.prgrms.nabimarketbe.domain.card.dto.request.CardUpdateRequestDTO;
import org.prgrms.nabimarketbe.domain.card.dto.response.CardCreateResponseDTO;
import org.prgrms.nabimarketbe.domain.card.dto.response.CardDetail;
import org.prgrms.nabimarketbe.domain.card.dto.response.CardListReadPagingResponseDTO;
import org.prgrms.nabimarketbe.domain.card.dto.response.CardListResponseDTO;
import org.prgrms.nabimarketbe.domain.card.dto.response.CardResponseDTO;
Expand All @@ -18,15 +18,17 @@
import org.prgrms.nabimarketbe.domain.card.entity.Card;
import org.prgrms.nabimarketbe.domain.card.entity.CardStatus;
import org.prgrms.nabimarketbe.domain.card.repository.CardRepository;
import org.prgrms.nabimarketbe.domain.cardimage.dto.response.CardImageSingleReadResponseDTO;
import org.prgrms.nabimarketbe.domain.cardimage.entity.CardImage;
import org.prgrms.nabimarketbe.domain.cardimage.repository.CardImageRepository;
import org.prgrms.nabimarketbe.domain.category.entity.Category;
import org.prgrms.nabimarketbe.domain.category.entity.CategoryEnum;
import org.prgrms.nabimarketbe.domain.category.repository.CategoryRepository;
import org.prgrms.nabimarketbe.domain.dibs.repository.DibRepository;
import org.prgrms.nabimarketbe.domain.item.entity.Item;
import org.prgrms.nabimarketbe.domain.item.entity.PriceRange;
import org.prgrms.nabimarketbe.domain.item.repository.ItemRepository;
import org.prgrms.nabimarketbe.domain.user.dto.response.UserResponseDTO;
import org.prgrms.nabimarketbe.domain.user.dto.response.UserSummaryResponseDTO;
import org.prgrms.nabimarketbe.domain.user.entity.User;
import org.prgrms.nabimarketbe.domain.user.repository.UserRepository;
import org.prgrms.nabimarketbe.domain.user.service.CheckService;
Expand All @@ -52,6 +54,8 @@ public class CardService {

private final CheckService checkService;

private final DibRepository dibRepository;

@Transactional
public CardResponseDTO<CardCreateResponseDTO> createCard(
String token,
Expand All @@ -67,16 +71,20 @@ public CardResponseDTO<CardCreateResponseDTO> createCard(

Card card = cardCreateRequestDTO.toCardEntity(item, user);

CardImage thumbNail = new CardImage(cardCreateRequestDTO.thumbNailImage(), card);

// images 비어있을 경우..
List<CardImage> images = Optional.ofNullable(cardCreateRequestDTO.images())
.orElseGet(Collections::emptyList)
.orElse(new ArrayList<>())
.stream()
.map(i -> i.toCardImageEntity(card))
.toList();

List<CardImage> newCardImages = addThumbNail(images, thumbNail);

Item savedItem = itemRepository.save(item);
Card savedCard = cardRepository.save(card);
List<CardImage> savedCardImages = cardImageRepository.saveAll(images); // TODO: images bulk insert로 전환
Card savedCard = cardRepository.save(card);
List<CardImage> savedCardImages = cardImageRepository.saveAll(newCardImages); // TODO: images bulk insert로 전환

CardCreateResponseDTO cardCreateResponseDTO = CardCreateResponseDTO.of(
savedCard,
Expand Down Expand Up @@ -153,18 +161,25 @@ public CardSingleReadResponseDTO getCardById(
card.updateViewCount();
}

Item item = card.getItem();

List<CardImage> cardImages = cardImageRepository.findAllByCard(card);
List<CardImageSingleReadResponseDTO> cardImageSingleReadResponseDTOS = new ArrayList<>();

for (CardImage cardImage : cardImages) {
cardImageSingleReadResponseDTOS.add(CardImageSingleReadResponseDTO.from(cardImage.getImageUrl()));
}
boolean isMyDib = dibRepository.existsDibByCardAndUser(card, user);


CardDetail cardDetail = CardDetail.of(
card,
cardImages,
isMyDib
);

UserSummaryResponseDTO userSummaryResponseDTO = UserSummaryResponseDTO.from(user);

CardResponseDTO<CardDetail> cardInfo = new CardResponseDTO<>(cardDetail);
UserResponseDTO<UserSummaryResponseDTO> userInfo = new UserResponseDTO<>(userSummaryResponseDTO);

return CardSingleReadResponseDTO.of(
cardInfo,
userinfo
userInfo
);
}

Expand Down Expand Up @@ -262,4 +277,11 @@ public void deleteCardById(

cardRepository.delete(card);
}

private List<CardImage> addThumbNail(List<CardImage> cardImages, CardImage thumbnail) {
List<CardImage> newCardImages = new ArrayList<>(cardImages);
newCardImages.add(0, thumbnail);

return newCardImages;
}
}

0 comments on commit a11dd71

Please sign in to comment.