From 80eabb75c85d27c1c9a59cbff70ef4e32a1f1dba Mon Sep 17 00:00:00 2001 From: ohu-star Date: Tue, 30 Jul 2024 23:30:53 +0900 Subject: [PATCH] =?UTF-8?q?[FEAT]=20=ED=95=9C=EB=8B=AC=20=EC=98=88?= =?UTF-8?q?=EC=82=B0=20=EC=88=98=EC=A0=95=20API=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/umc/haruchi/HaruchiApplication.java | 2 ++ .../haruchi/converter/MemberConverter.java | 1 - .../converter/MonthBudgetConverter.java | 28 +++++++++++++++++ src/main/java/umc/haruchi/domain/Member.java | 4 +-- .../repository/MonthBudgetRepository.java | 2 ++ .../umc/haruchi/service/MemberService.java | 11 +++++++ .../haruchi/service/MonthBudgetService.java | 9 ++---- .../web/controller/MonthBudgetController.java | 31 +++++++++++++++++++ .../web/dto/MonthBudgetRequestDTO.java | 5 ++- .../web/dto/MonthBudgetResponseDTO.java | 29 +++++++++++++++++ 10 files changed, 111 insertions(+), 11 deletions(-) create mode 100644 src/main/java/umc/haruchi/converter/MonthBudgetConverter.java create mode 100644 src/main/java/umc/haruchi/web/controller/MonthBudgetController.java create mode 100644 src/main/java/umc/haruchi/web/dto/MonthBudgetResponseDTO.java diff --git a/src/main/java/umc/haruchi/HaruchiApplication.java b/src/main/java/umc/haruchi/HaruchiApplication.java index 0926392..8d9bc41 100644 --- a/src/main/java/umc/haruchi/HaruchiApplication.java +++ b/src/main/java/umc/haruchi/HaruchiApplication.java @@ -5,10 +5,12 @@ import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration; import org.springframework.data.jpa.repository.config.EnableJpaAuditing; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication(exclude = SecurityAutoConfiguration.class) @EnableJpaRepositories @EnableJpaAuditing +@EnableScheduling public class HaruchiApplication { public static void main(String[] args) { diff --git a/src/main/java/umc/haruchi/converter/MemberConverter.java b/src/main/java/umc/haruchi/converter/MemberConverter.java index 6369acf..696328b 100644 --- a/src/main/java/umc/haruchi/converter/MemberConverter.java +++ b/src/main/java/umc/haruchi/converter/MemberConverter.java @@ -12,7 +12,6 @@ public class MemberConverter { public static Member toMember(MemberRequestDTO.MemberJoinDTO request) { return Member.builder() - .monthBudget(request.getMonthBudget()) .name(request.getName()) .email(request.getEmail()) .build(); diff --git a/src/main/java/umc/haruchi/converter/MonthBudgetConverter.java b/src/main/java/umc/haruchi/converter/MonthBudgetConverter.java new file mode 100644 index 0000000..3c1bbd0 --- /dev/null +++ b/src/main/java/umc/haruchi/converter/MonthBudgetConverter.java @@ -0,0 +1,28 @@ +package umc.haruchi.converter; + +import umc.haruchi.domain.MonthBudget; +import umc.haruchi.web.dto.MonthBudgetRequestDTO; +import umc.haruchi.web.dto.MonthBudgetResponseDTO; + +import java.time.LocalDate; + +public class MonthBudgetConverter { + public static MonthBudgetResponseDTO.CreateMonthResultDTO toCreateMonthResultDTO(MonthBudget monthBudget) { + return MonthBudgetResponseDTO.CreateMonthResultDTO.builder() + .id(monthBudget.getId()) + .build(); + } + + public static MonthBudgetResponseDTO.UpdateMonthResultDTO toUpdateMonthResultDTO(MonthBudget monthBudget) { + return MonthBudgetResponseDTO.UpdateMonthResultDTO.builder() + .id(monthBudget.getId()) + .updatedAt(monthBudget.getUpdatedAt()) + .build(); + } + + public static MonthBudget toMonthBudget(Long monthBudget) { + return MonthBudget.builder() + .monthBudget(monthBudget) + .build(); + } +} diff --git a/src/main/java/umc/haruchi/domain/Member.java b/src/main/java/umc/haruchi/domain/Member.java index cfdabd3..012a980 100644 --- a/src/main/java/umc/haruchi/domain/Member.java +++ b/src/main/java/umc/haruchi/domain/Member.java @@ -23,8 +23,8 @@ public class Member extends BaseEntity { @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; - @Column(nullable = false, columnDefinition = "bigint default 0") - private Long monthBudget; + //@Column(nullable = false, columnDefinition = "bigint default 0") + //private Long monthBudget; @Column(nullable = false, length = 5) private String name; diff --git a/src/main/java/umc/haruchi/repository/MonthBudgetRepository.java b/src/main/java/umc/haruchi/repository/MonthBudgetRepository.java index 1abf041..a75c3db 100644 --- a/src/main/java/umc/haruchi/repository/MonthBudgetRepository.java +++ b/src/main/java/umc/haruchi/repository/MonthBudgetRepository.java @@ -3,6 +3,8 @@ import org.springframework.data.jpa.repository.JpaRepository; import umc.haruchi.domain.MonthBudget; +import java.util.Optional; + public interface MonthBudgetRepository extends JpaRepository { Optional findByMemberIdAndYearAndMonth(Long memberId, int year, int month); diff --git a/src/main/java/umc/haruchi/service/MemberService.java b/src/main/java/umc/haruchi/service/MemberService.java index 5a78aa2..4ad6e05 100644 --- a/src/main/java/umc/haruchi/service/MemberService.java +++ b/src/main/java/umc/haruchi/service/MemberService.java @@ -14,15 +14,19 @@ import umc.haruchi.apiPayload.exception.handler.MemberHandler; import umc.haruchi.config.login.jwt.JwtUtil; import umc.haruchi.converter.MemberConverter; +import umc.haruchi.converter.MonthBudgetConverter; import umc.haruchi.domain.Member; import umc.haruchi.domain.MemberToken; +import umc.haruchi.domain.MonthBudget; import umc.haruchi.domain.Withdrawer; import umc.haruchi.repository.MemberRepository; import umc.haruchi.repository.MemberTokenRepository; import umc.haruchi.repository.WithdrawerRepository; import umc.haruchi.web.dto.MemberRequestDTO; import umc.haruchi.web.dto.MemberResponseDTO; +import umc.haruchi.web.dto.MonthBudgetRequestDTO; +import java.util.List; import java.util.Optional; import java.util.concurrent.TimeUnit; @@ -40,6 +44,8 @@ public class MemberService { public static int code; + private final MonthBudgetService monthBudgetService; + // 회원가입 @Transactional public Member joinMember(MemberRequestDTO.MemberJoinDTO request) throws Exception { @@ -56,6 +62,11 @@ public Member joinMember(MemberRequestDTO.MemberJoinDTO request) throws Exceptio Member newMember = MemberConverter.toMember(request); newMember.encodePassword(passwordEncoder.encode(request.getPassword())); + + //회원가입 시 monthBudget 생성 + MonthBudget monthBudget = MonthBudgetConverter.toMonthBudget(request.getMonthBudget()); + monthBudget.setMember(newMember); + return memberRepository.save(newMember); } diff --git a/src/main/java/umc/haruchi/service/MonthBudgetService.java b/src/main/java/umc/haruchi/service/MonthBudgetService.java index 132ca3f..8caff76 100644 --- a/src/main/java/umc/haruchi/service/MonthBudgetService.java +++ b/src/main/java/umc/haruchi/service/MonthBudgetService.java @@ -36,13 +36,8 @@ public MonthBudget updateMonthBudget(Long memberId, MonthBudgetRequestDTO.Update 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; + + return monthBudgetRepository.save(monthBudget); } } diff --git a/src/main/java/umc/haruchi/web/controller/MonthBudgetController.java b/src/main/java/umc/haruchi/web/controller/MonthBudgetController.java new file mode 100644 index 0000000..bd340d4 --- /dev/null +++ b/src/main/java/umc/haruchi/web/controller/MonthBudgetController.java @@ -0,0 +1,31 @@ +package umc.haruchi.web.controller; + +import io.swagger.v3.oas.annotations.Operation; +import jakarta.validation.Valid; +import lombok.RequiredArgsConstructor; +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.web.bind.annotation.*; +import umc.haruchi.apiPayload.ApiResponse; +import umc.haruchi.config.login.auth.MemberDetail; +import umc.haruchi.converter.MonthBudgetConverter; +import umc.haruchi.domain.MonthBudget; +import umc.haruchi.service.MonthBudgetService; +import umc.haruchi.web.dto.MonthBudgetRequestDTO; +import umc.haruchi.web.dto.MonthBudgetResponseDTO; + +import java.time.LocalDateTime; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/monthly-budget") +public class MonthBudgetController { + private final MonthBudgetService monthBudgetService; + + //한달 예산 수정 + @Operation(summary = "한달 예산 수정 API", description = "본인의 한달 예산을 수정하는 API 입니다.") + @PatchMapping("/") + public ApiResponse updateMonthBudget(@AuthenticationPrincipal MemberDetail memberDetail, @RequestBody @Valid MonthBudgetRequestDTO.UpdateMonthDTO request) { + MonthBudget monthBudget = monthBudgetService.updateMonthBudget(memberDetail.getMember().getId(), request); + return ApiResponse.onSuccess(MonthBudgetConverter.toUpdateMonthResultDTO(monthBudget)); + } +} diff --git a/src/main/java/umc/haruchi/web/dto/MonthBudgetRequestDTO.java b/src/main/java/umc/haruchi/web/dto/MonthBudgetRequestDTO.java index 7ffa7b0..9f6de09 100644 --- a/src/main/java/umc/haruchi/web/dto/MonthBudgetRequestDTO.java +++ b/src/main/java/umc/haruchi/web/dto/MonthBudgetRequestDTO.java @@ -2,6 +2,7 @@ import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.Positive; +import jakarta.validation.constraints.PositiveOrZero; import lombok.Getter; import org.hibernate.validator.constraints.Range; @@ -9,13 +10,15 @@ public class MonthBudgetRequestDTO { @Getter public static class CreateMonthDTO { - @Positive(message = "한달 예산은 0 이상이어야 합니다.") + @PositiveOrZero(message = "한달 예산은 0 이상이어야 합니다.") @NotNull(message = "한달 예산은 필수 입력 값입니다.") private Long monthBudget; } @Getter public static class UpdateMonthDTO { + @PositiveOrZero(message = "한달 예산은 0 이상이어야 합니다.") + @NotNull(message = "한달 예산은 필수 입력 값입니다.") private Long monthBudget; } } diff --git a/src/main/java/umc/haruchi/web/dto/MonthBudgetResponseDTO.java b/src/main/java/umc/haruchi/web/dto/MonthBudgetResponseDTO.java new file mode 100644 index 0000000..570f890 --- /dev/null +++ b/src/main/java/umc/haruchi/web/dto/MonthBudgetResponseDTO.java @@ -0,0 +1,29 @@ +package umc.haruchi.web.dto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.time.LocalDate; +import java.time.LocalDateTime; + +public class MonthBudgetResponseDTO { + @Builder + @Getter + @NoArgsConstructor + @AllArgsConstructor + public static class CreateMonthResultDTO { + Long id; + LocalDateTime createdAt; + } + + @Builder + @Getter + @NoArgsConstructor + @AllArgsConstructor + public static class UpdateMonthResultDTO { + Long id; + LocalDateTime updatedAt; + } +}