Skip to content

Commit

Permalink
[FEAT] 한달 예산 수정 API 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
ohu-star committed Jul 31, 2024
1 parent 126881a commit 80eabb7
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/main/java/umc/haruchi/HaruchiApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 0 additions & 1 deletion src/main/java/umc/haruchi/converter/MemberConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/umc/haruchi/converter/MonthBudgetConverter.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
4 changes: 2 additions & 2 deletions src/main/java/umc/haruchi/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<MonthBudget, Long> {

Optional<MonthBudget> findByMemberIdAndYearAndMonth(Long memberId, int year, int month);
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/umc/haruchi/service/MemberService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -40,6 +44,8 @@ public class MemberService {

public static int code;

private final MonthBudgetService monthBudgetService;

// 회원가입
@Transactional
public Member joinMember(MemberRequestDTO.MemberJoinDTO request) throws Exception {
Expand All @@ -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);
}

Expand Down
9 changes: 2 additions & 7 deletions src/main/java/umc/haruchi/service/MonthBudgetService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<MonthBudgetResponseDTO.UpdateMonthResultDTO> updateMonthBudget(@AuthenticationPrincipal MemberDetail memberDetail, @RequestBody @Valid MonthBudgetRequestDTO.UpdateMonthDTO request) {
MonthBudget monthBudget = monthBudgetService.updateMonthBudget(memberDetail.getMember().getId(), request);
return ApiResponse.onSuccess(MonthBudgetConverter.toUpdateMonthResultDTO(monthBudget));
}
}
5 changes: 4 additions & 1 deletion src/main/java/umc/haruchi/web/dto/MonthBudgetRequestDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@

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

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;
}
}
29 changes: 29 additions & 0 deletions src/main/java/umc/haruchi/web/dto/MonthBudgetResponseDTO.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 80eabb7

Please sign in to comment.