diff --git a/src/main/java/com/groom/swipo/domain/point/controller/PointController.java b/src/main/java/com/groom/swipo/domain/point/controller/PointController.java index a72d1b9..a26443b 100644 --- a/src/main/java/com/groom/swipo/domain/point/controller/PointController.java +++ b/src/main/java/com/groom/swipo/domain/point/controller/PointController.java @@ -11,8 +11,11 @@ import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; +import com.groom.swipo.domain.point.dto.Request.PointTransferRequest; import com.groom.swipo.domain.point.dto.Request.SwipstoneSwapRequest; import com.groom.swipo.domain.point.dto.Response.PointHomeResponse; +import com.groom.swipo.domain.point.dto.Response.PointTransferResponse; +import com.groom.swipo.domain.point.dto.Response.PointTransferInfoResponse; import com.groom.swipo.domain.point.dto.Response.SwipstoneResponse; import com.groom.swipo.domain.point.dto.Response.SwipstoneSwapResponse; import com.groom.swipo.domain.point.service.PointService; @@ -69,7 +72,6 @@ public ResTemplate registerCard( return new ResTemplate<>(HttpStatus.CREATED, "카드 등록 성공"); } - @GetMapping("/swipstone") @Operation( summary = "스윕스톤 조회", @@ -101,8 +103,43 @@ public ResTemplate getSwipstone(Principal principal) { @ApiResponse(responseCode = "500", description = "서버 오류") } ) - public ResTemplate swapSwipstone(@RequestBody SwipstoneSwapRequest resquest, Principal principal) { + public ResTemplate swapSwipstone(@RequestBody SwipstoneSwapRequest resquest, + Principal principal) { SwipstoneSwapResponse data = pointService.swapSwipstone(resquest, principal); return new ResTemplate<>(HttpStatus.OK, "교환 성공", data); } + + @GetMapping("/transfer-info") + @Operation( + summary = "포인트 이전 조희", + description = "포인트 이전 페이지에 들어왔을떄 자신이 보유한 포인트 카드들 조회 가능", + security = {}, + responses = { + @ApiResponse(responseCode = "200", description = "교환 성공"), + @ApiResponse(responseCode = "400", description = "잘못된 요청"), + @ApiResponse(responseCode = "401", description = "인증되지 않은 요청"), + @ApiResponse(responseCode = "500", description = "서버 오류") + } + ) + public ResTemplate getPointTransferInfo(Principal principal) { + PointTransferInfoResponse data= pointService.getPointTransferInfo(principal); + return new ResTemplate<>(HttpStatus.OK, "내 카드 조회 성공", data); + } + + @PostMapping("/transefer") + @Operation( + summary = "포인트 이전", + description = "포인트 이전 페이지에서 포인트 이전을 클릭 시 ", + security = {}, + responses = { + @ApiResponse(responseCode = "200", description = "이전 성공"), + @ApiResponse(responseCode = "400", description = "잘못된 요청"), + @ApiResponse(responseCode = "401", description = "인증되지 않은 요청"), + @ApiResponse(responseCode = "500", description = "서버 오류") + } + ) + public ResTemplate pointTransfer(@RequestBody PointTransferRequest request, Principal principal) { + PointTransferResponse data = pointService.pointTransfer(request, principal); + return new ResTemplate<>(HttpStatus.OK, "이전 성공", data); + } } diff --git a/src/main/java/com/groom/swipo/domain/point/dto/Request/PointTransferRequest.java b/src/main/java/com/groom/swipo/domain/point/dto/Request/PointTransferRequest.java new file mode 100644 index 0000000..0a2ee02 --- /dev/null +++ b/src/main/java/com/groom/swipo/domain/point/dto/Request/PointTransferRequest.java @@ -0,0 +1,8 @@ +package com.groom.swipo.domain.point.dto.Request; + +public record PointTransferRequest( + String fromCardId, + String toCardId, + Integer point +){ +} diff --git a/src/main/java/com/groom/swipo/domain/point/dto/Response/PointTransferInfoResponse.java b/src/main/java/com/groom/swipo/domain/point/dto/Response/PointTransferInfoResponse.java new file mode 100644 index 0000000..9e541c2 --- /dev/null +++ b/src/main/java/com/groom/swipo/domain/point/dto/Response/PointTransferInfoResponse.java @@ -0,0 +1,20 @@ +package com.groom.swipo.domain.point.dto.Response; + +import java.util.List; + +import com.groom.swipo.domain.point.dto.CardInfo; + +import lombok.Builder; + +@Builder +public record PointTransferInfoResponse( + Integer cardNum, + List cards +) { + public static PointTransferInfoResponse of(Integer cardNum, List cards){ + return PointTransferInfoResponse.builder() + .cardNum(cardNum) + .cards(cards) + .build(); + } +} diff --git a/src/main/java/com/groom/swipo/domain/point/dto/Response/PointTransferResponse.java b/src/main/java/com/groom/swipo/domain/point/dto/Response/PointTransferResponse.java new file mode 100644 index 0000000..6968c01 --- /dev/null +++ b/src/main/java/com/groom/swipo/domain/point/dto/Response/PointTransferResponse.java @@ -0,0 +1,22 @@ +package com.groom.swipo.domain.point.dto.Response; + +import com.groom.swipo.domain.point.entity.Card; + +import lombok.Builder; + +@Builder +public record PointTransferResponse( + String fromCardId, + String toCardId, + Integer fromPoint, + Integer toPoint +) { + public static PointTransferResponse of(Card fromCard, Card toCard) { + return PointTransferResponse.builder() + .fromCardId(String.valueOf(fromCard.getId())) + .toCardId(String.valueOf(toCard.getId())) + .fromPoint(fromCard.getTotalPoint()) + .toPoint(toCard.getTotalPoint()) + .build(); + } +} diff --git a/src/main/java/com/groom/swipo/domain/point/service/PointService.java b/src/main/java/com/groom/swipo/domain/point/service/PointService.java index 502f8d2..1d26626 100644 --- a/src/main/java/com/groom/swipo/domain/point/service/PointService.java +++ b/src/main/java/com/groom/swipo/domain/point/service/PointService.java @@ -2,6 +2,7 @@ import java.security.Principal; import java.util.List; +import java.util.Objects; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -14,13 +15,18 @@ import com.groom.swipo.domain.payment.repository.PayRepository; import com.groom.swipo.domain.payment.repository.PaylistRepository; import com.groom.swipo.domain.point.dto.PieceInfo; +import com.groom.swipo.domain.point.dto.Request.PointTransferRequest; import com.groom.swipo.domain.point.dto.Request.SwipstoneSwapRequest; import com.groom.swipo.domain.point.dto.Response.PointHomeResponse; +import com.groom.swipo.domain.point.dto.Response.PointTransferResponse; +import com.groom.swipo.domain.point.dto.Response.PointTransferInfoResponse; import com.groom.swipo.domain.point.dto.Response.SwipstoneResponse; import com.groom.swipo.domain.point.dto.Response.SwipstoneSwapResponse; import com.groom.swipo.domain.point.entity.Card; import com.groom.swipo.domain.point.entity.MyPiece; +import com.groom.swipo.domain.point.exception.CardNotFoundException; import com.groom.swipo.domain.point.exception.DuplicateCardException; +import com.groom.swipo.domain.point.exception.InsufficientPointsException; import com.groom.swipo.domain.point.exception.PiecesNotFoundException; import com.groom.swipo.domain.point.repository.CardRepository; import com.groom.swipo.domain.point.repository.MyPieceRepository; @@ -136,4 +142,46 @@ public SwipstoneSwapResponse swapSwipstone(SwipstoneSwapRequest request, Princip return new SwipstoneSwapResponse(pay.getTotalPay()); } + + // 포인트 카드 조회 + public PointTransferInfoResponse getPointTransferInfo(Principal principal) { + Long userId = Long.parseLong(principal.getName()); + User user = userRepository.findById(userId).orElseThrow(UserNotFoundException::new); + + // 사용자 카드 정보 조회 + List cards = cardRepository.findAllByUser(user); + List cardInfos = cards.stream() + .map(CardInfo::from) + .toList(); + + return PointTransferInfoResponse.of(cards.size(),cardInfos); + } + + // 포인트 이전 + @Transactional + public PointTransferResponse pointTransfer(PointTransferRequest request, Principal principal) { + Long userId = Long.parseLong(principal.getName()); + User user = userRepository.findById(userId).orElseThrow(UserNotFoundException::new); + + Card fromCard = cardRepository.findById(Long.valueOf(request.fromCardId())) + .orElseThrow(CardNotFoundException::new); + Card toCard = cardRepository.findById(Long.valueOf(request.toCardId())) + .orElseThrow(CardNotFoundException::new); + + //해당 유저가 가진 카드가 맞는지 검증 + if (!Objects.equals(user.getId(), toCard.getUser().getId()) & !Objects.equals(user.getId(), + toCard.getUser().getId())){ + throw new CardNotFoundException("해당 유저가 보유한 카드가 아닙니다"); + } + + // 포인트 업데이트(이전) + if ((fromCard.getTotalPoint() - request.point()) < 0) { + throw new InsufficientPointsException("이전할 포인트가 부족합니다."); + } + + fromCard.updatePoint(-request.point()); + toCard.updatePoint(request.point()); + + return PointTransferResponse.of(fromCard, toCard); + } }