From 84aa5bb8044db6a5efc993051a30057e8ef814af Mon Sep 17 00:00:00 2001 From: BeommoKoo-dev <95630007+BeommoKoo-dev@users.noreply.github.com> Date: Tue, 14 Nov 2023 17:32:12 +0900 Subject: [PATCH] =?UTF-8?q?NABI-204--refactor=20:=20=EC=9E=90=EC=8B=A0?= =?UTF-8?q?=EC=9D=98=20=EC=B9=B4=EB=93=9C=20=EC=A1=B0=ED=9A=8C=EC=8B=9C=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=EC=88=98=20=EC=A6=9D=EA=B0=80=20x?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/card/api/CardController.java | 37 ++++++++----- .../domain/card/service/CardService.java | 54 +++++++++++-------- 2 files changed, 57 insertions(+), 34 deletions(-) diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/card/api/CardController.java b/src/main/java/org/prgrms/nabimarketbe/domain/card/api/CardController.java index 944f6b79..d98d5212 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/card/api/CardController.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/card/api/CardController.java @@ -1,9 +1,6 @@ package org.prgrms.nabimarketbe.domain.card.api; -import io.swagger.v3.oas.annotations.Parameter; -import io.swagger.v3.oas.annotations.enums.ParameterIn; -import io.swagger.v3.oas.annotations.media.Schema; -import lombok.RequiredArgsConstructor; +import java.util.List; import org.prgrms.nabimarketbe.domain.card.dto.request.CardCreateRequestDTO; import org.prgrms.nabimarketbe.domain.card.dto.request.CardStatusUpdateRequestDTO; @@ -12,6 +9,7 @@ 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; +import org.prgrms.nabimarketbe.domain.card.dto.response.CardSingleReadResponseDTO; import org.prgrms.nabimarketbe.domain.card.dto.response.CardUpdateResponseDTO; import org.prgrms.nabimarketbe.domain.card.dto.response.SuggestionAvailableCardResponseDTO; import org.prgrms.nabimarketbe.domain.card.entity.CardStatus; @@ -21,11 +19,22 @@ import org.prgrms.nabimarketbe.global.util.ResponseFactory; import org.prgrms.nabimarketbe.global.util.model.CommonResult; import org.prgrms.nabimarketbe.global.util.model.SingleResult; - import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; -import java.util.List; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.enums.ParameterIn; +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.RequiredArgsConstructor; @RestController @RequestMapping("/api/v1/cards") @@ -49,12 +58,14 @@ public ResponseEntity>> crea return ResponseEntity.ok(ResponseFactory.getSingleResult(card)); } -// @GetMapping("/{cardId}") -// public ResponseEntity> getCardById(@PathVariable Long cardId) { -// CardSingleReadResponseDTO cardSingleReadResponseDTO = cardService.getCardById(cardId); -// -// return ResponseEntity.ok(ResponseFactory.getSingleResult(cardSingleReadResponseDTO)); -// } + @GetMapping("/{cardId}") + public ResponseEntity> getCardById( + @RequestHeader("Authorization") String token, + @PathVariable Long cardId) { + CardSingleReadResponseDTO cardSingleReadResponseDTO = cardService.getCardById(token, cardId); + + return ResponseEntity.ok(ResponseFactory.getSingleResult(cardSingleReadResponseDTO)); + } @GetMapping public ResponseEntity> getCardsByCondition( 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 8cda3c16..da85485e 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,5 +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; @@ -11,11 +12,13 @@ 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; +import org.prgrms.nabimarketbe.domain.card.dto.response.CardSingleReadResponseDTO; import org.prgrms.nabimarketbe.domain.card.dto.response.CardUpdateResponseDTO; import org.prgrms.nabimarketbe.domain.card.dto.response.SuggestionAvailableCardResponseDTO; 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; @@ -134,27 +137,36 @@ public CardResponseDTO updateCardById( return new CardResponseDTO<>(cardUpdateResponseDTO); } -// @Transactional -// public CardSingleReadResponseDTO getCardById(Long cardId) { -// Card card = cardRepository.findById(cardId) -// .orElseThrow(() -> new BaseException(ErrorCode.CARD_NOT_FOUND)); -// -// 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())); -// } -// -// return CardSingleReadResponseDTO.of( -// cardInfo, -// userinfo -// ); -// } + @Transactional(readOnly = true) + public CardSingleReadResponseDTO getCardById( + String token, + Long cardId + ) { + Long userId = checkService.parseToken(token); + User user = userRepository.findById(userId) + .orElseThrow(() -> new BaseException(ErrorCode.USER_NOT_FOUND)); + + Card card = cardRepository.findById(cardId) + .orElseThrow(() -> new BaseException(ErrorCode.CARD_NOT_FOUND)); + + if (!user.getUserId().equals(card.getUser().getUserId())) { + 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())); + } + + return CardSingleReadResponseDTO.of( + cardInfo, + userinfo + ); + } @Transactional(readOnly = true) public CardListReadPagingResponseDTO getCardsByCondition(