diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/card/dto/response/CardDetail.java b/src/main/java/org/prgrms/nabimarketbe/domain/card/dto/response/CardDetail.java index 85873412..4b7158e5 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/card/dto/response/CardDetail.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/card/dto/response/CardDetail.java @@ -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; @@ -35,25 +34,25 @@ public record CardDetail( ) { public static CardDetail of( Card card, - Item item, - List cardImages + List 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(); diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/card/service/CardService.java b/src/main/java/org/prgrms/nabimarketbe/domain/card/service/CardService.java index da85485e..05586824 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/card/service/CardService.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/card/service/CardService.java @@ -1,7 +1,6 @@ package org.prgrms.nabimarketbe.domain.card.service; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.Optional; @@ -9,6 +8,7 @@ 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; @@ -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; @@ -52,6 +54,8 @@ public class CardService { private final CheckService checkService; + private final DibRepository dibRepository; + @Transactional public CardResponseDTO createCard( String token, @@ -67,16 +71,20 @@ public CardResponseDTO createCard( Card card = cardCreateRequestDTO.toCardEntity(item, user); + CardImage thumbNail = new CardImage(cardCreateRequestDTO.thumbNailImage(), card); + // images 비어있을 경우.. List images = Optional.ofNullable(cardCreateRequestDTO.images()) - .orElseGet(Collections::emptyList) + .orElse(new ArrayList<>()) .stream() .map(i -> i.toCardImageEntity(card)) .toList(); + List newCardImages = addThumbNail(images, thumbNail); + Item savedItem = itemRepository.save(item); - Card savedCard = cardRepository.save(card); - List savedCardImages = cardImageRepository.saveAll(images); // TODO: images bulk insert로 전환 + Card savedCard = cardRepository.save(card); + List savedCardImages = cardImageRepository.saveAll(newCardImages); // TODO: images bulk insert로 전환 CardCreateResponseDTO cardCreateResponseDTO = CardCreateResponseDTO.of( savedCard, @@ -153,18 +161,25 @@ public CardSingleReadResponseDTO getCardById( card.updateViewCount(); } - Item item = card.getItem(); - List cardImages = cardImageRepository.findAllByCard(card); - List 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 cardInfo = new CardResponseDTO<>(cardDetail); + UserResponseDTO userInfo = new UserResponseDTO<>(userSummaryResponseDTO); return CardSingleReadResponseDTO.of( cardInfo, - userinfo + userInfo ); } @@ -262,4 +277,11 @@ public void deleteCardById( cardRepository.delete(card); } + + private List addThumbNail(List cardImages, CardImage thumbnail) { + List newCardImages = new ArrayList<>(cardImages); + newCardImages.add(0, thumbnail); + + return newCardImages; + } }