From 4e2c61227d4c3e99ccb737ecf9bbff15c66220de Mon Sep 17 00:00:00 2001 From: born-A <93516595+born-A@users.noreply.github.com> Date: Tue, 14 Nov 2023 19:31:38 +0900 Subject: [PATCH 1/3] =?UTF-8?q?NABI-205--refactor=20:=208=EB=B2=88=20api?= =?UTF-8?q?=20=EB=AA=85=EC=84=B8=EC=97=90=20=EB=A7=9E=EC=B6=B0=20=EB=A6=AC?= =?UTF-8?q?=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/card/api/CardController.java | 17 ++++- .../domain/card/dto/response/CardInfo.java | 13 ++++ .../SuggestionAvailableCardResponseDTO.java | 17 +---- .../card/dto/response/SuggestionInfo.java | 15 ++++ .../card/repository/CardRepositoryCustom.java | 3 +- .../card/repository/CardRepositoryImpl.java | 76 ++++++++----------- .../domain/card/service/CardService.java | 33 +++++++- 7 files changed, 106 insertions(+), 68 deletions(-) create mode 100644 src/main/java/org/prgrms/nabimarketbe/domain/card/dto/response/CardInfo.java create mode 100644 src/main/java/org/prgrms/nabimarketbe/domain/card/dto/response/SuggestionInfo.java 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..71e33ac4 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 @@ -77,15 +77,24 @@ public ResponseEntity> getCardsByCon return ResponseEntity.ok(ResponseFactory.getSingleResult(cardListReadPagingResponseDTO)); } - @GetMapping("/{cardId}/available-cards") + @GetMapping("/{targetCardId}/available-cards") public ResponseEntity>> getSuggestionAvailableCards( @RequestHeader(name = "Authorization") String token, - @PathVariable Long cardId + @PathVariable Long targetCardId ) { CardListResponseDTO cardListResponseDTO - = cardService.getSuggestionAvailableCards(token, cardId); + = cardService.getSuggestionAvailableCards(token, targetCardId); + + + List suggestionResultCardList = cardService.getSuggestionResultCardList( + targetCardId, + cardListResponseDTO.cardList() + ); + + CardListResponseDTO listResponseDTO = + new CardListResponseDTO<>(suggestionResultCardList); - return ResponseEntity.ok(ResponseFactory.getSingleResult(cardListResponseDTO)); + return ResponseEntity.ok(ResponseFactory.getSingleResult(listResponseDTO)); } @PutMapping("/{cardId}") diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/card/dto/response/CardInfo.java b/src/main/java/org/prgrms/nabimarketbe/domain/card/dto/response/CardInfo.java new file mode 100644 index 00000000..d6fb59b2 --- /dev/null +++ b/src/main/java/org/prgrms/nabimarketbe/domain/card/dto/response/CardInfo.java @@ -0,0 +1,13 @@ +package org.prgrms.nabimarketbe.domain.card.dto.response; + +import lombok.Getter; +import org.prgrms.nabimarketbe.domain.item.entity.PriceRange; + +@Getter +public class CardInfo { + private Long cardId; + private String thumbNail; + private String cardTitle; + private String itemName; + private PriceRange priceRange; +} \ No newline at end of file diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/card/dto/response/SuggestionAvailableCardResponseDTO.java b/src/main/java/org/prgrms/nabimarketbe/domain/card/dto/response/SuggestionAvailableCardResponseDTO.java index 2606695d..8ae76c50 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/card/dto/response/SuggestionAvailableCardResponseDTO.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/card/dto/response/SuggestionAvailableCardResponseDTO.java @@ -1,25 +1,12 @@ package org.prgrms.nabimarketbe.domain.card.dto.response; -import org.prgrms.nabimarketbe.domain.item.entity.PriceRange; -import org.prgrms.nabimarketbe.domain.suggestion.entity.SuggestionType; - import lombok.Getter; import lombok.NoArgsConstructor; @Getter @NoArgsConstructor public class SuggestionAvailableCardResponseDTO { - private Long cardId; - - private String thumbNail; - - private String itemName; - - private PriceRange priceRange; - - private SuggestionType suggestionType; + private CardInfo cardInfo; - public void updateSuggestionType(SuggestionType suggestionType) { - this.suggestionType = suggestionType; - } + private SuggestionInfo suggestionInfo; } diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/card/dto/response/SuggestionInfo.java b/src/main/java/org/prgrms/nabimarketbe/domain/card/dto/response/SuggestionInfo.java new file mode 100644 index 00000000..1fd4e509 --- /dev/null +++ b/src/main/java/org/prgrms/nabimarketbe/domain/card/dto/response/SuggestionInfo.java @@ -0,0 +1,15 @@ +package org.prgrms.nabimarketbe.domain.card.dto.response; + +import lombok.Getter; +import org.prgrms.nabimarketbe.domain.suggestion.entity.SuggestionStatus; +import org.prgrms.nabimarketbe.domain.suggestion.entity.SuggestionType; + +@Getter +public class SuggestionInfo { + private SuggestionType suggestionType; + private SuggestionStatus suggestionStatus; + + public void updateSuggestionType(SuggestionType suggestionType) { + this.suggestionType = suggestionType; + } +} diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/card/repository/CardRepositoryCustom.java b/src/main/java/org/prgrms/nabimarketbe/domain/card/repository/CardRepositoryCustom.java index 3027a050..832deedd 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/card/repository/CardRepositoryCustom.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/card/repository/CardRepositoryCustom.java @@ -28,7 +28,6 @@ CardListReadPagingResponseDTO getMyCardsByStatus( List getSuggestionAvailableCards( Long userId, - PriceRange priceRange, - Boolean pokeAvailable + Long targetCardId ); } diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/card/repository/CardRepositoryImpl.java b/src/main/java/org/prgrms/nabimarketbe/domain/card/repository/CardRepositoryImpl.java index 04624b1a..4e8b0fa2 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/card/repository/CardRepositoryImpl.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/card/repository/CardRepositoryImpl.java @@ -2,16 +2,18 @@ import static org.prgrms.nabimarketbe.domain.card.entity.QCard.card; import static org.prgrms.nabimarketbe.domain.item.entity.QItem.item; +import static org.prgrms.nabimarketbe.domain.suggestion.entity.QSuggestion.suggestion; import java.util.List; +import org.prgrms.nabimarketbe.domain.card.dto.response.CardInfo; import org.prgrms.nabimarketbe.domain.card.dto.response.CardListReadPagingResponseDTO; import org.prgrms.nabimarketbe.domain.card.dto.response.CardListReadResponseDTO; import org.prgrms.nabimarketbe.domain.card.dto.response.SuggestionAvailableCardResponseDTO; +import org.prgrms.nabimarketbe.domain.card.dto.response.SuggestionInfo; import org.prgrms.nabimarketbe.domain.card.entity.CardStatus; import org.prgrms.nabimarketbe.domain.category.entity.CategoryEnum; import org.prgrms.nabimarketbe.domain.item.entity.PriceRange; -import org.prgrms.nabimarketbe.domain.suggestion.entity.SuggestionType; import org.prgrms.nabimarketbe.domain.user.entity.User; import com.querydsl.core.types.ConstantImpl; @@ -106,30 +108,39 @@ public CardListReadPagingResponseDTO getMyCardsByStatus( @Override public List getSuggestionAvailableCards( - Long userId, - PriceRange priceRange, - Boolean pokeAvailable + Long userId, + Long targetCardId ) { - List cardList = jpaQueryFactory.select( + List cardList = jpaQueryFactory + .select( Projections.fields( - SuggestionAvailableCardResponseDTO.class, + SuggestionAvailableCardResponseDTO.class, + Projections.fields( + CardInfo.class, card.cardId, - card.thumbNailImage.as("thumbNail"), - item.itemName, - item.priceRange - )) - .from(card) - .leftJoin(item).on(card.item.itemId.eq(item.itemId)) - .where(card.user.userId.eq(userId)) - .fetch(); - - return getSuggestionResultCardList( - pokeAvailable, - priceRange, - cardList - ); + card.thumbNailImage.as("thumbnail"), + card.cardTitle, + card.item.itemName, + card.item.priceRange + ).as("cardInfo"), + Projections.fields( + SuggestionInfo.class, + suggestion.suggestionType, + suggestion.suggestionStatus + ).as("suggestionInfo") + ) + ) + .from(card) + .leftJoin(suggestion).on(suggestion.fromCard.cardId.eq(card.cardId)) + .leftJoin(suggestion).on(suggestion.toCard.cardId.eq(targetCardId)) + .where(card.user.userId.eq(userId)) + .distinct() + .fetch(); + + return cardList; } + private BooleanExpression cursorId(String cursorId){ if (cursorId == null) { return null; @@ -192,29 +203,4 @@ private String generateCursor(CardListReadResponseDTO cardListReadResponseDTO) { .replace(":", "") + String.format("%08d", cardListReadResponseDTO.getCardId()); } - - private List getSuggestionResultCardList( - Boolean pokeAvailable, - PriceRange priceRange, - List cardList - ) { - if (pokeAvailable) { - return cardList.stream() - .peek(c -> { - if (c.getPriceRange().getValue() < priceRange.getValue()) { - c.updateSuggestionType(SuggestionType.POKE); - } else { - c.updateSuggestionType(SuggestionType.OFFER); - } - }) - .toList(); - } - - List offerOnlyCardList = cardList.stream() - .filter(c -> c.getPriceRange().getValue() >= priceRange.getValue()) - .toList(); - offerOnlyCardList.forEach(o -> o.updateSuggestionType(SuggestionType.OFFER)); - - return offerOnlyCardList; - } } 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 00211ad5..766c104e 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 @@ -22,6 +22,7 @@ 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.suggestion.entity.SuggestionType; import org.prgrms.nabimarketbe.domain.user.entity.User; import org.prgrms.nabimarketbe.domain.user.repository.UserRepository; import org.prgrms.nabimarketbe.domain.user.service.CheckService; @@ -186,8 +187,7 @@ public CardListResponseDTO getSuggestionAvai List cardListResponse = cardRepository.getSuggestionAvailableCards( requestUser.getUserId(), - suggestionTargetCard.getItem().getPriceRange(), - suggestionTargetCard.getPoke() + suggestionTargetCard.getCardId() ); return new CardListResponseDTO<>(cardListResponse); @@ -245,4 +245,33 @@ public void deleteCardById( cardRepository.delete(card); } + + @Transactional + public List getSuggestionResultCardList( + Long targetId, + List cardList + ) { + Card targetCard = cardRepository.findById(targetId).orElseThrow(); + + Boolean pokeAvailable = targetCard.getPoke(); + PriceRange priceRange = targetCard.getItem().getPriceRange();; + + if (pokeAvailable) { + return cardList.stream() + .peek(c -> { + if (c.getCardInfo().getPriceRange().getValue() < priceRange.getValue()) { + c.getSuggestionInfo().updateSuggestionType(SuggestionType.POKE); + } else { + c.getSuggestionInfo().updateSuggestionType(SuggestionType.OFFER); + } + }).toList(); + } + + List offerOnlyCardList = cardList.stream() + .filter(c -> c.getCardInfo().getPriceRange().getValue() >= priceRange.getValue()) + .toList(); + offerOnlyCardList.forEach(o -> o.getSuggestionInfo().updateSuggestionType(SuggestionType.OFFER)); + + return offerOnlyCardList; + } } From 03542d1b32475a65e3bdb507ed30ad8fd1db1296 Mon Sep 17 00:00:00 2001 From: born-A <93516595+born-A@users.noreply.github.com> Date: Wed, 15 Nov 2023 01:18:32 +0900 Subject: [PATCH 2/3] =?UTF-8?q?NABI-205--refactor=20:=20=EC=84=9C=EB=B9=84?= =?UTF-8?q?=EC=8A=A4=201=EB=B2=88=20=ED=98=B8=EC=B6=9C=ED=95=98=EA=B2=8C?= =?UTF-8?q?=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nabimarketbe/domain/card/api/CardController.java | 11 +---------- .../domain/card/dto/response/CardDetail.java | 2 +- .../domain/card/repository/CardRepositoryImpl.java | 4 ++-- .../nabimarketbe/domain/card/service/CardService.java | 9 ++++++--- 4 files changed, 10 insertions(+), 16 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 71e33ac4..3a59f663 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 @@ -85,16 +85,7 @@ public ResponseEntity cardListResponseDTO = cardService.getSuggestionAvailableCards(token, targetCardId); - - List suggestionResultCardList = cardService.getSuggestionResultCardList( - targetCardId, - cardListResponseDTO.cardList() - ); - - CardListResponseDTO listResponseDTO = - new CardListResponseDTO<>(suggestionResultCardList); - - return ResponseEntity.ok(ResponseFactory.getSingleResult(listResponseDTO)); + return ResponseEntity.ok(ResponseFactory.getSingleResult(cardListResponseDTO)); } @PutMapping("/{cardId}") 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 9b63dfd7..99a07bc6 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 @@ -52,7 +52,7 @@ public static CardDetail of( .tradeType(card.getTradeType()) .tradeArea(card.getTradeArea()) .dibsCount(card.getDibCount()) - .isMyDib() //TODO + .isMyDib(true) //TODO .images(cardImages) .build(); } diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/card/repository/CardRepositoryImpl.java b/src/main/java/org/prgrms/nabimarketbe/domain/card/repository/CardRepositoryImpl.java index 4e8b0fa2..842d090a 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/card/repository/CardRepositoryImpl.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/card/repository/CardRepositoryImpl.java @@ -133,8 +133,8 @@ public List getSuggestionAvailableCards( .from(card) .leftJoin(suggestion).on(suggestion.fromCard.cardId.eq(card.cardId)) .leftJoin(suggestion).on(suggestion.toCard.cardId.eq(targetCardId)) - .where(card.user.userId.eq(userId)) - .distinct() + .where(card.user.userId.eq(userId), + suggestion.toCard.cardId.eq(targetCardId).or(suggestion.toCard.cardId.isNull())) .fetch(); return cardList; 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 766c104e..2d008036 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 @@ -174,11 +174,11 @@ public CardListReadPagingResponseDTO getCardsByCondition( @Transactional(readOnly = true) public CardListResponseDTO getSuggestionAvailableCards( String token, - Long cardId + Long targetCardId ) { User requestUser = userRepository.findById(checkService.parseToken(token)) .orElseThrow(() -> new BaseException(ErrorCode.USER_NOT_FOUND)); - Card suggestionTargetCard = cardRepository.findById(cardId) + Card suggestionTargetCard = cardRepository.findById(targetCardId) .orElseThrow(() -> new BaseException(ErrorCode.CARD_NOT_FOUND)); if (requestUser.getUserId().equals(suggestionTargetCard.getUser().getUserId())) { @@ -190,7 +190,10 @@ public CardListResponseDTO getSuggestionAvai suggestionTargetCard.getCardId() ); - return new CardListResponseDTO<>(cardListResponse); + List suggestionResultCardList = + getSuggestionResultCardList(suggestionTargetCard.getCardId(), cardListResponse); + + return new CardListResponseDTO<>(suggestionResultCardList); } @Transactional(readOnly = true) From 7e4b217f5b650b6609958031d9ca754d45795baf Mon Sep 17 00:00:00 2001 From: born-A <93516595+born-A@users.noreply.github.com> Date: Wed, 15 Nov 2023 01:20:18 +0900 Subject: [PATCH 3/3] =?UTF-8?q?NABI-205--refactor=20:=20uri=20=EC=95=88?= =?UTF-8?q?=EB=B0=94=EB=80=8C=EA=B2=8C=20pathvariable=20name=20=EB=B6=80?= =?UTF-8?q?=EC=97=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../prgrms/nabimarketbe/domain/card/api/CardController.java | 4 ++-- 1 file changed, 2 insertions(+), 2 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 3a59f663..4d0fe35b 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 @@ -77,10 +77,10 @@ public ResponseEntity> getCardsByCon return ResponseEntity.ok(ResponseFactory.getSingleResult(cardListReadPagingResponseDTO)); } - @GetMapping("/{targetCardId}/available-cards") + @GetMapping("/{cardId}/available-cards") public ResponseEntity>> getSuggestionAvailableCards( @RequestHeader(name = "Authorization") String token, - @PathVariable Long targetCardId + @PathVariable(name = "cardId") Long targetCardId ) { CardListResponseDTO cardListResponseDTO = cardService.getSuggestionAvailableCards(token, targetCardId);