Skip to content

Commit

Permalink
✨ :: 후원 API 개발
Browse files Browse the repository at this point in the history
  • Loading branch information
lgwk42 committed Jul 17, 2024
1 parent 8658b54 commit f0db2b3
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.project.pieceserver.domain.donate.client.api;

import com.project.pieceserver.domain.donate.client.dto.request.DonateMoneyRequest;
import com.project.pieceserver.domain.donate.client.dto.request.DonatePointRequest;
import com.project.pieceserver.domain.donate.client.dto.response.DonateResponse;
import com.project.pieceserver.domain.donate.usecase.DonateUseCase;
import com.project.pieceserver.global.common.dto.response.BaseResponse;
import com.project.pieceserver.global.common.dto.response.BaseResponseData;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/donate")
@RequiredArgsConstructor
@Tag(name = "후원 API")
public class DonateController {

private final DonateUseCase donateUseCase;

@PostMapping("/money")
@Operation(summary = "계좌 후원")
public BaseResponseData<DonateResponse> donateMoney(@Valid @RequestBody DonateMoneyRequest donateMoneyRequest) {
return BaseResponseData.ok(
"후원 성공",
donateUseCase.saveBankLog(donateMoneyRequest));
}

@PostMapping("/point")
@Operation(summary = "포인트 후원")
public BaseResponseData<DonateResponse> donatePoint(@Valid @RequestBody DonatePointRequest donatePointRequest) {
return BaseResponseData.ok(
"후원 성공",
donateUseCase.savePointLog(donatePointRequest));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.project.pieceserver.domain.donate.client.dto.request;

public record DonateMoneyRequest(
int money
){}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.project.pieceserver.domain.donate.client.dto.request;

public record DonatePointRequest(
int point
){}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.project.pieceserver.domain.donate.client.dto.response;

import lombok.Builder;

@Builder
public record DonateResponse(
int balance
){}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.project.pieceserver.domain.donate.client.dto.PointLog;
import com.project.pieceserver.domain.donate.client.dto.request.DonateMoneyRequest;
import com.project.pieceserver.domain.donate.client.dto.request.DonatePointRequest;
import com.project.pieceserver.domain.donate.client.dto.response.DonateResponse;
import com.project.pieceserver.domain.donate.service.BankLogService;
import com.project.pieceserver.domain.donate.service.PointLogService;
import com.project.pieceserver.domain.user.application.service.UserService;
Expand All @@ -18,18 +19,26 @@ public class DonateUseCase {
private final PointLogService pointLogService;
private final UserService userService;

public void saveBankLog(DonateMoneyRequest donateMoneyRequest) {
public DonateResponse saveBankLog(DonateMoneyRequest donateMoneyRequest) {
int currentMoney = userService.donateMoney(donateMoneyRequest.money());
bankLogService.saveBankLog(BankLog.builder()
.email(userService.getUser().getEmail())
.money(donateMoneyRequest.money())
.build());
return DonateResponse.builder()
.balance(currentMoney)
.build();
}

public void savePointLog(DonatePointRequest donatePointRequest) {
public DonateResponse savePointLog(DonatePointRequest donatePointRequest) {
int currentPoint = userService.donatePoint(donatePointRequest.point());
pointLogService.savePointLog(PointLog.builder()
.email(userService.getUser().getEmail())
.point(donatePointRequest.point())
.build());
return DonateResponse.builder()
.balance(currentPoint)
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.project.pieceserver.domain.user.client.dto.User;
import com.project.pieceserver.domain.user.client.dto.request.NameEditRequest;
import com.project.pieceserver.domain.user.domain.repository.jpa.UserRepository;
import com.project.pieceserver.domain.user.exception.NotEnoughMoneyException;
import com.project.pieceserver.domain.user.exception.NotEnoughPointException;
import com.project.pieceserver.domain.user.exception.PasswordWrongException;
import com.project.pieceserver.domain.user.exception.UserExistException;
import com.project.pieceserver.domain.user.exception.UserNotFoundException;
Expand Down Expand Up @@ -56,5 +58,24 @@ public User getUser(){
.orElseThrow(()-> UserNotFoundException.EXCEPTION);
}

public int donateMoney(int money){
User user = getUser();
int oldBalance = user.getBalance();
int currentBalance = oldBalance - money;
if(currentBalance < 0)
throw NotEnoughMoneyException.EXCEPTION;
user.setBalance(currentBalance);
return currentBalance;
}

public int donatePoint(int point){
User user = getUser();
int oldPoint = user.getPoint();
int currentPoint = oldPoint - point;
if(currentPoint < 0)
throw NotEnoughPointException.EXCEPTION;
user.setPoint(currentPoint);
return currentPoint;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.project.pieceserver.domain.user.exception;

import com.project.pieceserver.domain.user.exception.error.UserError;
import com.project.pieceserver.global.exception.BusinessException;

public class NotEnoughMoneyException extends BusinessException {

public static final NotEnoughMoneyException EXCEPTION = new NotEnoughMoneyException();

private NotEnoughMoneyException() {
super(UserError.NOT_ENOUGH_MONEY);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.project.pieceserver.domain.user.exception;

import com.project.pieceserver.domain.user.exception.error.UserError;
import com.project.pieceserver.global.exception.BusinessException;

public class NotEnoughPointException extends BusinessException {

public static final NotEnoughPointException EXCEPTION = new NotEnoughPointException();

private NotEnoughPointException() {
super(UserError.NOT_ENOUGH_POINT);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ public enum UserError implements ErrorProperty {

USER_NOT_FOUND(HttpStatus.NOT_FOUND, "유저를 찾을 수 없습니다."),
PASSWORD_WRONG(HttpStatus.BAD_REQUEST, "비밀번호가 맞지 않습니다."),
USER_EXIST(HttpStatus.CONFLICT, "이미 존재하는 유저입니다.");
USER_EXIST(HttpStatus.CONFLICT, "이미 존재하는 유저입니다."),
NOT_ENOUGH_MONEY(HttpStatus.BAD_REQUEST,"잔액이 부족합니다."),
NOT_ENOUGH_POINT(HttpStatus.BAD_REQUEST,"포인트가 부족합니다.");

private final HttpStatus status;
private final String message;
Expand Down

0 comments on commit f0db2b3

Please sign in to comment.