Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] 다음 달로 넘어갈 때 예산 재분배 로직 수정 #65

Merged
merged 2 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/main/java/umc/haruchi/converter/MonthBudgetConverter.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package umc.haruchi.converter;

import umc.haruchi.domain.DayBudget;
import umc.haruchi.domain.Member;
import umc.haruchi.domain.MonthBudget;
import umc.haruchi.domain.enums.DayBudgetStatus;
import umc.haruchi.web.dto.MonthBudgetRequestDTO;
Expand Down Expand Up @@ -81,4 +82,13 @@ public static MonthBudgetResponseDTO.GetMonthLeftNowResultDTO toGetMonthLeftNowR
.leftBudget(leftBudget)
.build();
}

public static MonthBudget toMonthBudget(int year, int month, long amount, Member member){
return MonthBudget.builder()
.year(year)
.month(month)
.monthBudget(amount)
.member(member)
.build();
}
}
46 changes: 37 additions & 9 deletions src/main/java/umc/haruchi/service/DayBudgetService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
import umc.haruchi.apiPayload.code.status.ErrorStatus;
import umc.haruchi.apiPayload.exception.handler.*;
import umc.haruchi.converter.DayBudgetConverter;
import umc.haruchi.domain.DayBudget;
import umc.haruchi.domain.Expenditure;
import umc.haruchi.domain.Income;
import umc.haruchi.domain.MonthBudget;
import umc.haruchi.converter.MonthBudgetConverter;
import umc.haruchi.domain.*;
import umc.haruchi.domain.enums.DayBudgetStatus;
import umc.haruchi.repository.*;
import umc.haruchi.web.dto.DayBudgetRequestDTO;
Expand All @@ -20,6 +18,7 @@
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static umc.haruchi.apiPayload.code.status.ErrorStatus.*;

Expand All @@ -41,7 +40,6 @@ public class DayBudgetService {
private final MonthBudgetService monthBudgetService;



public MonthBudget check(Long memberId){
LocalDate now = LocalDate.now();
int year = now.getYear();
Expand All @@ -59,8 +57,26 @@ public MonthBudget check(Long memberId){

public Integer findDayBudget(Long memberId) {
LocalDate now = LocalDate.now();
int year = now.getYear();
int month = now.getMonthValue();
int day = now.getDayOfMonth();


MonthBudget nmonthBudget = monthBudgetRepository.findByMemberIdAndYearAndMonth(memberId, year, month).orElse(null);
if(nmonthBudget == null){
// 컨버터에서 빌드하기
Member member = memberRepository.findById(memberId).orElseThrow(() -> new MemberHandler(ErrorStatus.NO_MEMBER_EXIST));
MonthBudget last = monthBudgetRepository.findByMemberIdAndYearAndMonth(memberId, year, month-1)
.orElseThrow(() -> new MonthBudgetHandler(ErrorStatus.NOT_MONTH_BUDGET));
MonthBudget newMonthBudget = MonthBudgetConverter.toMonthBudget(year, month, last.getMonthBudget(), member);
// 한달 예산 저장
monthBudgetRepository.save(newMonthBudget);
// 하루 예산 분배
List<DayBudget> dayBudgets = monthBudgetService.distributeDayBudgets(memberId);
dayBudgetRepository.saveAll(dayBudgets);
}


MonthBudget monthBudget = check(memberId);

DayBudget dayBudget = dayBudgetRepository.findByMonthBudgetAndDay(monthBudget, day)
Expand All @@ -76,13 +92,25 @@ public List<DayBudget> findAllBudget(Long memberId) {
int day = now.getDayOfMonth();
int lastDay = now.lengthOfMonth();

MonthBudget monthBudget = monthBudgetRepository.findByMemberIdAndYearAndMonth(memberId, year, month)
.orElseThrow(() -> new MonthBudgetHandler(ErrorStatus.NOT_DAY_BUDGET));

List<DayBudget> dayBudgets = monthBudgetService.distributeDayBudgets(memberId);
dayBudgetRepository.saveAll(dayBudgets);
MonthBudget nmonthBudget = monthBudgetRepository.findByMemberIdAndYearAndMonth(memberId, year, month).orElse(null);
if(nmonthBudget == null){
// 컨버터에서 빌드하기
Member member = memberRepository.findById(memberId).orElseThrow(() -> new MemberHandler(ErrorStatus.NO_MEMBER_EXIST));
MonthBudget last = monthBudgetRepository.findByMemberIdAndYearAndMonth(memberId, year, month-1)
.orElseThrow(() -> new MonthBudgetHandler(ErrorStatus.NOT_MONTH_BUDGET));
MonthBudget newMonthBudget = MonthBudgetConverter.toMonthBudget(year, month, last.getMonthBudget(), member);
// 한달 예산 저장
monthBudgetRepository.save(newMonthBudget);
// 하루 예산 분배
List<DayBudget> dayBudgets = monthBudgetService.distributeDayBudgets(memberId);
dayBudgetRepository.saveAll(dayBudgets);
}


MonthBudget monthBudget = monthBudgetRepository.findByMemberIdAndYearAndMonth(memberId, year, month)
.orElseThrow(() -> new MonthBudgetHandler(ErrorStatus.NOT_MONTH_BUDGET));

List<DayBudget> allBudget = new ArrayList<>();
for(int i=day; i<=lastDay; i++){
DayBudget dayBudget = dayBudgetRepository.findByMonthBudgetAndDay(monthBudget, i)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class DayBudgetController {
private DayBudgetService dayBudgetService;


@Operation(summary = "하루 예산을 조회하는 API.", description = "회원의 하루 예산을 조회하는 API 입니다.")
@Operation(summary = "하루 예산을 조회하는 API", description = "회원의 하루 예산을 조회하는 API 입니다.")
@ApiResponses({
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200",description = "OK, 성공"),
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "MEMBER4005", description = "존재하지 않는 회원입니다.",content = @Content(schema = @Schema(implementation = ApiResponse.class))),
Expand All @@ -41,7 +41,7 @@ public ApiResponse<DayBudgetResponseDTO.getDayBudget> getDailyBudget(@Authentica
return ApiResponse.onSuccess(DayBudgetConverter.toGetDayBudget(todayBudget));
}

@Operation(summary = "날짜별 예산 금액 조회하는 API.", description = "오늘부터 말일까지의 예산을 조회하는 API 입니다.")
@Operation(summary = "날짜별 예산 금액 조회하는 API", description = "오늘부터 말일까지의 예산을 조회하는 API 입니다.")
@ApiResponses({
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200",description = "OK, 성공"),
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "MEMBER4005", description = "존재하지 않는 회원입니다.",content = @Content(schema = @Schema(implementation = ApiResponse.class))),
Expand Down
Loading