Skip to content

Commit

Permalink
Merge pull request #29 from 9oormthon-univ/feat/point-2
Browse files Browse the repository at this point in the history
[FEAT] 포인트 이전/이전 조회 API
  • Loading branch information
kduoh99 authored Nov 23, 2024
2 parents d9cfe69 + 2be0e9e commit 5e45226
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -69,7 +72,6 @@ public ResTemplate<Void> registerCard(
return new ResTemplate<>(HttpStatus.CREATED, "카드 등록 성공");
}


@GetMapping("/swipstone")
@Operation(
summary = "스윕스톤 조회",
Expand Down Expand Up @@ -101,8 +103,43 @@ public ResTemplate<SwipstoneResponse> getSwipstone(Principal principal) {
@ApiResponse(responseCode = "500", description = "서버 오류")
}
)
public ResTemplate<SwipstoneSwapResponse> swapSwipstone(@RequestBody SwipstoneSwapRequest resquest, Principal principal) {
public ResTemplate<SwipstoneSwapResponse> 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<PointTransferInfoResponse> 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<PointTransferResponse> pointTransfer(@RequestBody PointTransferRequest request, Principal principal) {
PointTransferResponse data = pointService.pointTransfer(request, principal);
return new ResTemplate<>(HttpStatus.OK, "이전 성공", data);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.groom.swipo.domain.point.dto.Request;

public record PointTransferRequest(
String fromCardId,
String toCardId,
Integer point
){
}
Original file line number Diff line number Diff line change
@@ -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<CardInfo> cards
) {
public static PointTransferInfoResponse of(Integer cardNum, List<CardInfo> cards){
return PointTransferInfoResponse.builder()
.cardNum(cardNum)
.cards(cards)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<Card> cards = cardRepository.findAllByUser(user);
List<CardInfo> 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);
}
}

0 comments on commit 5e45226

Please sign in to comment.