Skip to content

Commit

Permalink
[FIX] MonthBudget 관련 에러처리 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
ohu-star committed Jul 30, 2024
1 parent f4bb09d commit 126881a
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public enum ErrorStatus implements BaseErrorCode {
WITHDRAWAL_MEMBER(HttpStatus.NOT_FOUND, "MEMBER4007", "탈퇴한 회원입니다."),

// MonthBudget 관련 에러
NOT_MONTH_BUDGET(HttpStatus.NOT_FOUND, "MONTHBUDGET4001", "한 달 예산이 존재하지 않습니다."),
MONTH_BUDGET_NOT_FOUND(HttpStatus.NOT_FOUND, "MONTHBUDGET4001", "한 달 예산이 존재하지 않습니다."),
NOT_MONTH_BUDGET(HttpStatus.BAD_REQUEST, "MONTHBUDGET4002", "입력된 한 달 예산이 없거나 유효하지 않습니다."),

// DayBudget 관련 에러
NOT_DAY_BUDGET(HttpStatus.NOT_FOUND, "DAYBUDGET4001", "하루 예산이 존재하지 않습니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@

public interface MonthBudgetRepository extends JpaRepository<MonthBudget, Long> {

MonthBudget findByMemberIdAndYearAndMonth(Long memberId, int year, int month);
Optional<MonthBudget> findByMemberIdAndYearAndMonth(Long memberId, int year, int month);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import umc.haruchi.apiPayload.exception.handler.BudgetRedistributionHandler;
import umc.haruchi.apiPayload.exception.handler.DayBudgetHandler;
import umc.haruchi.apiPayload.exception.handler.MemberHandler;
import umc.haruchi.apiPayload.exception.handler.MonthBudgetHandler;
import umc.haruchi.converter.BudgetRedistributionConverter;
import umc.haruchi.domain.*;
import umc.haruchi.repository.*;
Expand Down Expand Up @@ -38,7 +39,8 @@ public class BudgetRedistributionService {
public PushPlusClosing push(BudgetRedistributionRequestDTO.createPushDTO request, Long memberId) {
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new MemberHandler(NO_MEMBER_EXIST)); //영속화
MonthBudget monthBudget = monthBudgetRepository.findByMemberIdAndYearAndMonth(member.getId(), year, month);
MonthBudget monthBudget = monthBudgetRepository.findByMemberIdAndYearAndMonth(member.getId(), year, month)
.orElseThrow(() -> new MonthBudgetHandler(MONTH_BUDGET_NOT_FOUND));
DayBudget sourceBudget = dayBudgetRepository.findByMonthBudgetAndDay(monthBudget, request.getSourceDay());

long totalAmount = request.getAmount();
Expand Down Expand Up @@ -131,7 +133,9 @@ public PullMinusClosing pull(BudgetRedistributionRequestDTO.createPullDTO reques
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new MemberHandler(NO_MEMBER_EXIST)); //영속화

MonthBudget monthBudget = monthBudgetRepository.findByMemberIdAndYearAndMonth(member.getId(), year, month);
MonthBudget monthBudget = monthBudgetRepository.findByMemberIdAndYearAndMonth(member.getId(), year, month)
.orElseThrow(() -> new MonthBudgetHandler(MONTH_BUDGET_NOT_FOUND));

DayBudget targetBudget = dayBudgetRepository.findByMonthBudgetAndDay(monthBudget, request.getTargetDay());
long totalAmount = request.getAmount();
int tossAmount = (int) (roundDownToNearestHundred(totalAmount));
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/umc/haruchi/service/DayBudgetService.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ public MonthBudget check(Long memberId){
throw new MemberHandler(ErrorStatus.NO_MEMBER_EXIST);
}

MonthBudget monthBudget = monthBudgetRepository.findByMemberIdAndYearAndMonth(memberId, year, month);
MonthBudget monthBudget = monthBudgetRepository.findByMemberIdAndYearAndMonth(memberId, year, month)
.orElseThrow(() -> new MonthBudgetHandler(ErrorStatus.MONTH_BUDGET_NOT_FOUND));
if(monthBudget == null){
throw new MonthBudgetHandler(ErrorStatus.NOT_MONTH_BUDGET);
throw new MonthBudgetHandler(ErrorStatus.MONTH_BUDGET_NOT_FOUND);
}
return monthBudget;
}
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/umc/haruchi/service/MonthBudgetService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package umc.haruchi.service;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import umc.haruchi.apiPayload.ApiResponse;
import umc.haruchi.apiPayload.code.status.ErrorStatus;
import umc.haruchi.apiPayload.exception.handler.DayBudgetHandler;
import umc.haruchi.apiPayload.exception.handler.MonthBudgetHandler;
import umc.haruchi.domain.DayBudget;
import umc.haruchi.domain.Member;
import umc.haruchi.domain.MonthBudget;
import umc.haruchi.repository.MemberRepository;
import umc.haruchi.repository.MonthBudgetRepository;
import umc.haruchi.web.dto.MonthBudgetRequestDTO;
import umc.haruchi.web.dto.MonthBudgetResponseDTO;

import java.time.LocalDate;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class MonthBudgetService {
private final MonthBudgetRepository monthBudgetRepository;
private final MemberRepository memberRepository;

@Transactional
public MonthBudget updateMonthBudget(Long memberId, MonthBudgetRequestDTO.UpdateMonthDTO request) {
LocalDate today = LocalDate.now();

//member가 존재하는 지 확인
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new MonthBudgetHandler(ErrorStatus.NO_MEMBER_EXIST));

//member와 year, month 기반으로 해당하는 monthBudget 찾기
MonthBudget monthBudget = monthBudgetRepository.findByMemberIdAndYearAndMonth(memberId, today.getYear(), today.getMonthValue())
.orElseThrow(() -> new MonthBudgetHandler(ErrorStatus.MONTH_BUDGET_NOT_FOUND));

//monthBudget이 request body에 전달되었는지 확인
if(request.getMonthBudget() == null)
throw new MonthBudgetHandler(ErrorStatus.NOT_MONTH_BUDGET);

if(!(request.getMonthBudget() > 0))
throw new MonthBudgetHandler(ErrorStatus.NOT_MONTH_BUDGET);
monthBudget.updateMonthBudget(request.getMonthBudget());
return monthBudget;
}
}
21 changes: 21 additions & 0 deletions src/main/java/umc/haruchi/web/dto/MonthBudgetRequestDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package umc.haruchi.web.dto;

import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import lombok.Getter;
import org.hibernate.validator.constraints.Range;

public class MonthBudgetRequestDTO {

@Getter
public static class CreateMonthDTO {
@Positive(message = "한달 예산은 0 이상이어야 합니다.")
@NotNull(message = "한달 예산은 필수 입력 값입니다.")
private Long monthBudget;
}

@Getter
public static class UpdateMonthDTO {
private Long monthBudget;
}
}

0 comments on commit 126881a

Please sign in to comment.