Skip to content

Commit

Permalink
NABI-204--refactor : 자신의 카드 조회시 조회수 증가 x
Browse files Browse the repository at this point in the history
  • Loading branch information
BeommoKoo-dev committed Nov 14, 2023
1 parent 7d65bbe commit 84aa5bb
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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")
Expand All @@ -49,12 +58,14 @@ public ResponseEntity<SingleResult<CardResponseDTO<CardCreateResponseDTO>>> crea
return ResponseEntity.ok(ResponseFactory.getSingleResult(card));
}

// @GetMapping("/{cardId}")
// public ResponseEntity<SingleResult<CardSingleReadResponseDTO>> getCardById(@PathVariable Long cardId) {
// CardSingleReadResponseDTO cardSingleReadResponseDTO = cardService.getCardById(cardId);
//
// return ResponseEntity.ok(ResponseFactory.getSingleResult(cardSingleReadResponseDTO));
// }
@GetMapping("/{cardId}")
public ResponseEntity<SingleResult<CardSingleReadResponseDTO>> getCardById(
@RequestHeader("Authorization") String token,
@PathVariable Long cardId) {
CardSingleReadResponseDTO cardSingleReadResponseDTO = cardService.getCardById(token, cardId);

return ResponseEntity.ok(ResponseFactory.getSingleResult(cardSingleReadResponseDTO));
}

@GetMapping
public ResponseEntity<SingleResult<CardListReadPagingResponseDTO>> getCardsByCondition(
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -134,27 +137,36 @@ public CardResponseDTO<CardUpdateResponseDTO> 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<CardImage> cardImages = cardImageRepository.findAllByCard(card);
// List<CardImageSingleReadResponseDTO> 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<CardImage> cardImages = cardImageRepository.findAllByCard(card);
List<CardImageSingleReadResponseDTO> cardImageSingleReadResponseDTOS = new ArrayList<>();

for (CardImage cardImage : cardImages) {
cardImageSingleReadResponseDTOS.add(CardImageSingleReadResponseDTO.from(cardImage.getImageUrl()));
}

return CardSingleReadResponseDTO.of(
cardInfo,
userinfo
);
}

@Transactional(readOnly = true)
public CardListReadPagingResponseDTO getCardsByCondition(
Expand Down

0 comments on commit 84aa5bb

Please sign in to comment.