Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev' into NABI-184--yejin--feat-…
Browse files Browse the repository at this point in the history
…-complete-request

# Conflicts:
#	src/main/java/org/prgrms/nabimarketbe/domain/card/entity/Card.java
#	src/main/java/org/prgrms/nabimarketbe/domain/card/service/CardService.java
  • Loading branch information
born-A committed Nov 11, 2023
2 parents 008e422 + e1f956b commit cea49a0
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.RequiredArgsConstructor;

import org.prgrms.nabimarketbe.domain.card.dto.request.CardCreateRequestDTO;
import org.prgrms.nabimarketbe.domain.card.dto.request.CardStatusUpdateRequestDTO;
import org.prgrms.nabimarketbe.domain.card.dto.response.CardCreateResponseDTO;
import org.prgrms.nabimarketbe.domain.card.dto.response.CardListReadPagingResponseDTO;
import org.prgrms.nabimarketbe.domain.card.dto.response.CardListResponseDTO;
Expand All @@ -13,6 +14,7 @@
import org.prgrms.nabimarketbe.domain.category.entity.CategoryEnum;
import org.prgrms.nabimarketbe.domain.item.entity.PriceRange;
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.MediaType;
Expand Down Expand Up @@ -87,4 +89,45 @@ public ResponseEntity<SingleResult<CardListResponseDTO<SuggestionAvailableCardRe

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

@PutMapping("/status/{cardId}")
public ResponseEntity<CommonResult> updateCardStatusById(
@RequestHeader(name = "authorization") String token,
@PathVariable Long cardId,
@RequestBody CardStatusUpdateRequestDTO cardStatusUpdateRequestDTO
) {
cardService.updateCardStatusById(
token,
cardId,
cardStatusUpdateRequestDTO
);

return ResponseEntity.ok(ResponseFactory.getSuccessResult());

@GetMapping("/{status}/my-cards")
public ResponseEntity<SingleResult<CardListReadPagingResponseDTO>> getMyCardsByStatus(
@RequestHeader(name = "authorization") String token,
@PathVariable CardStatus status,
@RequestParam(required = false) String cursorId,
@RequestParam Integer size
) {
CardListReadPagingResponseDTO cardListReadPagingResponseDTO = cardService.getMyCardsByStatus(
token,
status,
cursorId,
size
);

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

@DeleteMapping("/{cardId}")
public ResponseEntity<CommonResult> deleteCardById(
@RequestHeader(name = "authorization") String token,
@PathVariable Long cardId
) {
cardService.deleteCardById(token, cardId);

return ResponseEntity.ok(ResponseFactory.getSuccessResult());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.prgrms.nabimarketbe.domain.card.dto.request;

import org.prgrms.nabimarketbe.domain.card.entity.CardStatus;
import org.prgrms.nabimarketbe.global.annotation.ValidEnum;

public record CardStatusUpdateRequestDTO(
@ValidEnum(enumClass = CardStatus.class, message = "유효하지 않은 상태입니다.")
CardStatus cardStatus
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,15 @@ public void decreaseDibCount() {
this.dibCount -= 1;
}

public void reserved() {
public void updateCardStatusToTradeAvailable() {
this.status = CardStatus.TRADE_AVAILABLE;
}

public void updateCardStatusToReserved() {
this.status = CardStatus.RESERVED;
}

public void tradeCompleted() {
public void updateCardStatusToTradeComplete() {
this.status = CardStatus.TRADE_COMPLETE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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.user.entity.User;

import java.util.List;

Expand All @@ -18,6 +19,13 @@ CardListReadPagingResponseDTO getCardsByCondition(
Integer size
);

CardListReadPagingResponseDTO getMyCardsByStatus(
User user,
CardStatus status,
String cursorId,
Integer size
);

List<SuggestionAvailableCardResponseDTO> getSuggestionAvailableCards(
Long userId,
PriceRange priceRange,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
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 java.util.List;

Expand Down Expand Up @@ -65,6 +66,41 @@ public CardListReadPagingResponseDTO getCardsByCondition(
return new CardListReadPagingResponseDTO(cardList, nextCursor);
}

@Override
public CardListReadPagingResponseDTO getMyCardsByStatus(
User user,
CardStatus status,
String cursorId,
Integer size
) {
List<CardListReadResponseDTO> cardList = jpaQueryFactory.select(
Projections.fields(
CardListReadResponseDTO.class,
card.cardId,
card.cardTitle,
item.itemName,
item.priceRange,
card.thumbNailImage.as("thumbNail"),
card.status,
card.createdDate.as("createdAt"),
card.modifiedDate.as("modifiedAt")
)
)
.from(card)
.leftJoin(item).on(card.item.itemId.eq(item.itemId))
.where(
cursorId(cursorId),
card.status.eq(status)
)
.orderBy(card.createdDate.desc()) // 디폴트는 생성일자 최신순 정렬
.limit(size)
.fetch();

String nextCursor = cardList.size() < size ? null : generateCursor(cardList.get(cardList.size() - 1));

return new CardListReadPagingResponseDTO(cardList, nextCursor);
}

@Override
public List<SuggestionAvailableCardResponseDTO> getSuggestionAvailableCards(
Long userId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
import java.util.List;

import org.prgrms.nabimarketbe.domain.card.dto.request.CardCreateRequestDTO;
import org.prgrms.nabimarketbe.domain.card.dto.response.*;
import org.prgrms.nabimarketbe.domain.card.dto.request.CardStatusUpdateRequestDTO;
import org.prgrms.nabimarketbe.domain.card.dto.response.CardCreateResponseDTO;
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.CardSingleReadResponseDTO;
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;
Expand Down Expand Up @@ -183,4 +188,57 @@ public CardListResponseDTO<SuggestionAvailableCardResponseDTO> getSuggestionAvai

return new CardListResponseDTO<>(cardListResponse);
}

@Transactional(readOnly = true)
public CardListReadPagingResponseDTO getMyCardsByStatus(
String token,
CardStatus status,
String cursorId,
Integer size
) {
User user = userRepository.findById(checkService.parseToken(token))
.orElseThrow(() -> new BaseException(ErrorCode.USER_NOT_FOUND));

return cardRepository.getMyCardsByStatus(
user,
status,
cursorId,
size
);
}

@Transactional
public void updateCardStatusById(
String token,
Long cardId,
CardStatusUpdateRequestDTO cardStatusUpdateRequestDTO
) {
Card card = cardRepository.findById(cardId)
.orElseThrow(() -> new BaseException(ErrorCode.CARD_NOT_FOUND));

if (!checkService.isEqual(token, card.getUser().getUserId())) {
throw new BaseException(ErrorCode.USER_NOT_MATCHED);
}

switch (cardStatusUpdateRequestDTO.cardStatus()) {
case TRADE_AVAILABLE -> card.updateCardStatusToTradeAvailable();
case RESERVED -> card.updateCardStatusToReserved();
case TRADE_COMPLETE -> card.updateCardStatusToTradeComplete();
}
}

@Transactional
public void deleteCardById(
String token,
Long cardId
) {
Card card = cardRepository.findById(cardId)
.orElseThrow(() -> new BaseException(ErrorCode.CARD_NOT_FOUND));

if (!checkService.isEqual(token, card.getUser().getUserId())) {
throw new BaseException(ErrorCode.USER_NOT_MATCHED);
}

cardRepository.delete(card);
}
}

0 comments on commit cea49a0

Please sign in to comment.