-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
81 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
21
src/main/java/umc/haruchi/web/dto/MonthBudgetRequestDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |