Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nabi-206--refactor api 제안가능한 카드들 (no.8) #35

Merged
merged 3 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ public ResponseEntity<SingleResult<CardListReadPagingResponseDTO>> getCardsByCon
@GetMapping("/{cardId}/available-cards")
public ResponseEntity<SingleResult<CardListResponseDTO<SuggestionAvailableCardResponseDTO>>> getSuggestionAvailableCards(
@RequestHeader(name = "Authorization") String token,
@PathVariable Long cardId
@PathVariable(name = "cardId") Long targetCardId
) {
CardListResponseDTO<SuggestionAvailableCardResponseDTO> cardListResponseDTO
= cardService.getSuggestionAvailableCards(token, cardId);
= cardService.getSuggestionAvailableCards(token, targetCardId);

return ResponseEntity.ok(ResponseFactory.getSingleResult(cardListResponseDTO));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ CardListReadPagingResponseDTO getMyCardsByStatus(

List<SuggestionAvailableCardResponseDTO> getSuggestionAvailableCards(
Long userId,
PriceRange priceRange,
Boolean pokeAvailable
Long targetCardId
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -106,30 +108,39 @@ public CardListReadPagingResponseDTO getMyCardsByStatus(

@Override
public List<SuggestionAvailableCardResponseDTO> getSuggestionAvailableCards(
Long userId,
PriceRange priceRange,
Boolean pokeAvailable
Long userId,
Long targetCardId
) {
List<SuggestionAvailableCardResponseDTO> cardList = jpaQueryFactory.select(
List<SuggestionAvailableCardResponseDTO> 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),
suggestion.toCard.cardId.eq(targetCardId).or(suggestion.toCard.cardId.isNull()))
.fetch();

return cardList;
}


private BooleanExpression cursorId(String cursorId){
if (cursorId == null) {
return null;
Expand Down Expand Up @@ -192,29 +203,4 @@ private String generateCursor(CardListReadResponseDTO cardListReadResponseDTO) {
.replace(":", "")
+ String.format("%08d", cardListReadResponseDTO.getCardId());
}

private List<SuggestionAvailableCardResponseDTO> getSuggestionResultCardList(
Boolean pokeAvailable,
PriceRange priceRange,
List<SuggestionAvailableCardResponseDTO> 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<SuggestionAvailableCardResponseDTO> offerOnlyCardList = cardList.stream()
.filter(c -> c.getPriceRange().getValue() >= priceRange.getValue())
.toList();
offerOnlyCardList.forEach(o -> o.updateSuggestionType(SuggestionType.OFFER));

return offerOnlyCardList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -173,11 +174,11 @@ public CardListReadPagingResponseDTO getCardsByCondition(
@Transactional(readOnly = true)
public CardListResponseDTO<SuggestionAvailableCardResponseDTO> 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())) {
Expand All @@ -186,11 +187,13 @@ public CardListResponseDTO<SuggestionAvailableCardResponseDTO> getSuggestionAvai

List<SuggestionAvailableCardResponseDTO> cardListResponse = cardRepository.getSuggestionAvailableCards(
requestUser.getUserId(),
suggestionTargetCard.getItem().getPriceRange(),
suggestionTargetCard.getPoke()
suggestionTargetCard.getCardId()
);

return new CardListResponseDTO<>(cardListResponse);
List<SuggestionAvailableCardResponseDTO> suggestionResultCardList =
getSuggestionResultCardList(suggestionTargetCard.getCardId(), cardListResponse);

return new CardListResponseDTO<>(suggestionResultCardList);
}

@Transactional(readOnly = true)
Expand Down Expand Up @@ -245,4 +248,33 @@ public void deleteCardById(

cardRepository.delete(card);
}

@Transactional
public List<SuggestionAvailableCardResponseDTO> getSuggestionResultCardList(
Long targetId,
List<SuggestionAvailableCardResponseDTO> 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<SuggestionAvailableCardResponseDTO> offerOnlyCardList = cardList.stream()
.filter(c -> c.getCardInfo().getPriceRange().getValue() >= priceRange.getValue())
.toList();
offerOnlyCardList.forEach(o -> o.getSuggestionInfo().updateSuggestionType(SuggestionType.OFFER));

return offerOnlyCardList;
}
}
Loading