Skip to content

Commit

Permalink
NABI-205--refactor : 8번 api 명세에 맞춰 리팩토링
Browse files Browse the repository at this point in the history
  • Loading branch information
born-A committed Nov 14, 2023
1 parent 0c7ac54 commit 4e2c612
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,24 @@ public ResponseEntity<SingleResult<CardListReadPagingResponseDTO>> getCardsByCon
return ResponseEntity.ok(ResponseFactory.getSingleResult(cardListReadPagingResponseDTO));
}

@GetMapping("/{cardId}/available-cards")
@GetMapping("/{targetCardId}/available-cards")
public ResponseEntity<SingleResult<CardListResponseDTO<SuggestionAvailableCardResponseDTO>>> getSuggestionAvailableCards(
@RequestHeader(name = "Authorization") String token,
@PathVariable Long cardId
@PathVariable Long targetCardId
) {
CardListResponseDTO<SuggestionAvailableCardResponseDTO> cardListResponseDTO
= cardService.getSuggestionAvailableCards(token, cardId);
= cardService.getSuggestionAvailableCards(token, targetCardId);


List<SuggestionAvailableCardResponseDTO> suggestionResultCardList = cardService.getSuggestionResultCardList(
targetCardId,
cardListResponseDTO.cardList()
);

CardListResponseDTO<SuggestionAvailableCardResponseDTO> listResponseDTO =
new CardListResponseDTO<>(suggestionResultCardList);

return ResponseEntity.ok(ResponseFactory.getSingleResult(cardListResponseDTO));
return ResponseEntity.ok(ResponseFactory.getSingleResult(listResponseDTO));
}

@PutMapping("/{cardId}")
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))
.distinct()
.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 @@ -186,8 +187,7 @@ public CardListResponseDTO<SuggestionAvailableCardResponseDTO> getSuggestionAvai

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

return new CardListResponseDTO<>(cardListResponse);
Expand Down Expand Up @@ -245,4 +245,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;
}
}

0 comments on commit 4e2c612

Please sign in to comment.