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} 오류 수정 #55

Merged
merged 2 commits into from
Aug 12, 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
66 changes: 64 additions & 2 deletions src/main/java/umc/haruchi/converter/DayBudgetConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
import umc.haruchi.domain.MonthBudget;
import umc.haruchi.domain.enums.ClosingStatus;
import umc.haruchi.domain.enums.DayBudgetStatus;
import umc.haruchi.domain.enums.ExpenditureCategory;
import umc.haruchi.domain.enums.IncomeCategory;
import umc.haruchi.web.dto.DayBudgetRequestDTO;
import umc.haruchi.web.dto.DayBudgetResponseDTO;
import umc.haruchi.web.dto.MonthBudgetResponseDTO;

import java.time.LocalDate;
import java.util.List;
import java.util.Locale;

public class DayBudgetConverter {

Expand All @@ -38,10 +41,35 @@ public static DayBudgetResponseDTO.getBudget toGetBudget(List<Integer> allBudget
}

public static Income toIncome(DayBudgetRequestDTO.createIncomeDTO request, DayBudget dayBudget) {
IncomeCategory category = null;
switch(request.getCategory()) {
case "미분류":
category = IncomeCategory.NONE;
break;
case "용돈":
category = IncomeCategory.ALLOWANCE;
break;
case "월급":
category = IncomeCategory.SALARY;
break;
case "부수입":
category = IncomeCategory.SIDELINE;
break;
case "상여":
category = IncomeCategory.BONUS;
break;
case "금융소득":
category = IncomeCategory.INTEREST;
break;
case "기타":
category = IncomeCategory.OTHER;
break;

}
return Income.builder()
.dayBudget(dayBudget)
.incomeAmount(request.getIncomeAmount())
.incomeCategory(request.getCategory())
.incomeCategory(category)
.build();
}

Expand All @@ -61,10 +89,44 @@ public static DayBudgetResponseDTO.expenditureReg toCreateExpenditure(Expenditur
}

public static Expenditure toExpenditure(DayBudgetRequestDTO.createExpenditureDTO request, DayBudget dayBudget) {
ExpenditureCategory category = null;
switch (request.getCategory()){
case "미분류":
category = ExpenditureCategory.NONE;
break;
case "식비":
category = ExpenditureCategory.FOOD;
break;
case "커피":
category = ExpenditureCategory.COFFEE;
break;
case "교통":
category = ExpenditureCategory.TRANSPORT;
break;
case "취미":
category = ExpenditureCategory.HOBBY;
break;
case "패션":
category = ExpenditureCategory.FASHION;
break;
case "교육":
category = ExpenditureCategory.EDUCATION;
break;
case "경조사":
category = ExpenditureCategory.EVENT;
break;
case "구독":
category = ExpenditureCategory.SUBSCRIPTION;
break;
case "기타":
category = ExpenditureCategory.OTHER;
break;
}

return Expenditure.builder()
.dayBudget(dayBudget)
.expenditureAmount(request.getExpenditureAmount())
.expenditureCategory(request.getCategory())
.expenditureCategory(category)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
public enum ExpenditureCategory {
NONE("미분류"),
FOOD("식비"),
CAFE("카페"),
COFFEE("커피"),
TRANSPORT("교통"),
HOBBY("취미"),
FASHION("패션"),
Expand Down
49 changes: 32 additions & 17 deletions src/main/java/umc/haruchi/service/DayBudgetService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package umc.haruchi.service;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -22,35 +23,29 @@

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

@Slf4j
@Service
@RequiredArgsConstructor
public class DayBudgetService {

@Autowired
private MonthBudgetRepository monthBudgetRepository;
private final MonthBudgetRepository monthBudgetRepository;

@Autowired
private DayBudgetRepository dayBudgetRepository;
private final DayBudgetRepository dayBudgetRepository;

@Autowired
private MemberRepository memberRepository;
private final MemberRepository memberRepository;

@Autowired
private IncomeRepository incomeRepository;
private final IncomeRepository incomeRepository;

@Autowired
private ExpenditureRepository expenditureRepository;
private final ExpenditureRepository expenditureRepository;

private final MonthBudgetService monthBudgetService;

@Autowired
private MonthBudgetService monthBudgetService;

LocalDate now = LocalDate.now();
int year = now.getYear();
int month = now.getMonthValue();
int day = now.getDayOfMonth();
int lastDay = now.lengthOfMonth();

public MonthBudget check(Long memberId){
LocalDate now = LocalDate.now();
int year = now.getYear();
int month = now.getMonthValue();

if(memberRepository.findById(memberId).isEmpty()){
throw new MemberHandler(ErrorStatus.NO_MEMBER_EXIST);
Expand All @@ -65,6 +60,9 @@ public MonthBudget check(Long memberId){
}

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

MonthBudget monthBudget = check(memberId);

DayBudget dayBudget = dayBudgetRepository.findByMonthBudgetAndDay(monthBudget, day)
Expand All @@ -74,6 +72,11 @@ public Integer findDayBudget(Long memberId) {
}

public List<Integer> findAllBudget(Long memberId) {
LocalDate now = LocalDate.now();
int year = now.getYear();
int month = now.getMonthValue();
int day = now.getDayOfMonth();
int lastDay = now.lengthOfMonth();

MonthBudget monthBudget = monthBudgetRepository.findByMemberIdAndYearAndMonth(memberId, year, month)
.orElseThrow(() -> new MonthBudgetHandler(ErrorStatus.NOT_DAY_BUDGET));
Expand All @@ -87,6 +90,7 @@ public List<Integer> findAllBudget(Long memberId) {
DayBudget dayBudget = dayBudgetRepository.findByMonthBudgetAndDay(monthBudget, i)
.orElseThrow(() -> new DayBudgetHandler(NOT_SOME_DAY_BUDGET));
allBudget.add(dayBudget.getDayBudget());

}

return allBudget;
Expand All @@ -95,6 +99,9 @@ public List<Integer> findAllBudget(Long memberId) {

@Transactional
public void deleteIncome(Long memberId, Long incomeId) {
LocalDate now = LocalDate.now();
int day = now.getDayOfMonth();

MonthBudget monthBudget = check(memberId);

DayBudget dayBudget = dayBudgetRepository.findByMonthBudgetAndDay(monthBudget, day)
Expand All @@ -117,6 +124,8 @@ public void deleteIncome(Long memberId, Long incomeId) {

@Transactional
public Income joinIncome(DayBudgetRequestDTO.createIncomeDTO request, Long memberId) {
LocalDate now = LocalDate.now();
int day = now.getDayOfMonth();

MonthBudget monthBudget = check(memberId);

Expand All @@ -134,6 +143,9 @@ public Income joinIncome(DayBudgetRequestDTO.createIncomeDTO request, Long membe

@Transactional
public Expenditure joinExpenditure(DayBudgetRequestDTO.createExpenditureDTO request, Long memberId) {
LocalDate now = LocalDate.now();
int day = now.getDayOfMonth();

MonthBudget monthBudget = check(memberId);

DayBudget dayBudget = dayBudgetRepository.findByMonthBudgetAndDay(monthBudget, day)
Expand Down Expand Up @@ -162,6 +174,9 @@ public Expenditure joinExpenditure(DayBudgetRequestDTO.createExpenditureDTO requ

@Transactional
public void deleteExpenditure(Long memberId, Long expenditureId) {
LocalDate now = LocalDate.now();
int day = now.getDayOfMonth();

MonthBudget monthBudget = check(memberId);

DayBudget dayBudget = dayBudgetRepository.findByMonthBudgetAndDay(monthBudget, day)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ public class DayBudgetController {
private DayBudgetService dayBudgetService;


@Operation(summary = "하루 예산을 조회하는 API", description = "회원의 하루 예산을 조회하는 API 입니다.")
@Operation(summary = "하루 예산을 조회하는 API.", description = "회원의 하루 예산을 조회하는 API 입니다.")
@GetMapping("")
public ApiResponse<DayBudgetResponseDTO.getDayBudget> getDailyBudget(@AuthenticationPrincipal MemberDetail memberDetail){
Integer todayBudget = dayBudgetService.findDayBudget(memberDetail.getMember().getId());
return ApiResponse.onSuccess(DayBudgetConverter.toGetDayBudget(todayBudget));
}

@Operation(summary = "날짜별 예산 금액 조회하는 API", description = "오늘부터 말일까지의 예산을 조회하는 API 입니다.")
@Operation(summary = "날짜별 예산 금액 조회하는 API.", description = "오늘부터 말일까지의 예산을 조회하는 API 입니다.")
@GetMapping("/list")
public ApiResponse<DayBudgetResponseDTO.getBudget> getAllBudget(@AuthenticationPrincipal MemberDetail memberDetail){
List<Integer> allBudget = dayBudgetService.findAllBudget(memberDetail.getMember().getId());
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/umc/haruchi/web/dto/DayBudgetRequestDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static class createIncomeDTO{
Long incomeAmount;

@NotNull(message = "수입 카테고리는 필수 입력 값입니다.")
IncomeCategory category;
String category;

}

Expand All @@ -29,6 +29,6 @@ public static class createExpenditureDTO{
Long expenditureAmount;

@NotNull(message = "지출 카테고리은 필수 입력 값입니다.")
ExpenditureCategory category;
String category;
}
}
Loading