diff --git a/src/main/java/com/hana/api/saving/entity/SavingCategory.java b/src/main/java/com/hana/api/saving/entity/SavingCategory.java index 9f23a50..3445032 100644 --- a/src/main/java/com/hana/api/saving/entity/SavingCategory.java +++ b/src/main/java/com/hana/api/saving/entity/SavingCategory.java @@ -39,5 +39,6 @@ public class SavingCategory extends BaseEntity { private List children = new ArrayList<>(); @OneToMany(mappedBy = "savingCategory", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE) + @ToString.Exclude private List savings = new ArrayList<>(); } diff --git a/src/main/java/com/hana/api/saving/service/SavingService.java b/src/main/java/com/hana/api/saving/service/SavingService.java index a4bdbd6..09d9c2b 100644 --- a/src/main/java/com/hana/api/saving/service/SavingService.java +++ b/src/main/java/com/hana/api/saving/service/SavingService.java @@ -1,17 +1,23 @@ package com.hana.api.saving.service; +import com.hana.api.deposit.entity.Deposit; import com.hana.api.saving.dto.response.SavingResponseDto; import com.hana.api.saving.entity.Saving; import com.hana.api.saving.repository.SavingRepository; import com.hana.api.user.dto.request.UserRequestDto; +import com.hana.api.user.dto.response.UserDepositResponseDto; +import com.hana.api.user.dto.response.UserSavingResponseDto; import com.hana.api.user.entity.User; +import com.hana.api.user.entity.UserDeposit; import com.hana.api.user.entity.UserSaving; +import com.hana.api.user.repository.UserDepositRepository; import com.hana.api.user.repository.UserSavingRepository; import com.hana.api.user.repository.UserRepository; import com.hana.common.dto.Response; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.security.SecureRandom; import java.util.List; @@ -27,6 +33,9 @@ public class SavingService { private final UserRepository userRepository; //사용자의 saving 정보 가져오기 위해서 private final UserSavingRepository userSavingRepository; + //사용자의 deposit 정보 가져오기 위해서 + private final UserDepositRepository userDepositRepository; + private final Response response; public ResponseEntity getAllSavings() { @@ -44,12 +53,19 @@ public ResponseEntity getSavingById(Long savingId) { return response.success(new SavingResponseDto(saving)); } + @Transactional public ResponseEntity joinSaving(Long savingId, UserRequestDto.UserSavingRequestDto dto) { - //1. user의 가입정보 가져오기 > 중복가입 방지 //todo //2. saving 정보 가져오기 Saving saving = savingRepository.findById(savingId) .orElseThrow(); User user = userRepository.findById(dto.getUserId()).orElse(null); + UserDeposit userDeposit = userDepositRepository.getReferenceById(dto.getUserDepositId()); + + // userDeposit의 balance 필드에서 dto.getPerMonth() 만큼 감소시키기 + long newBalance = userDeposit.getBalance() - dto.getPerMonth(); + userDeposit.updateBalance(newBalance); + userDepositRepository.save(userDeposit); + //계좌번호 SecureRandom secureRandom = new SecureRandom(); @@ -61,15 +77,17 @@ public ResponseEntity joinSaving(Long savingId, UserRequestDto.UserSavingRequ .accountNumber(accountNumber) .isHuman(false) .isLoss(false) - .balance(dto.getBalance()) + .balance(dto.getPerMonth()) + .perMonth(dto.getPerMonth()) .period(dto.getPeriod()) .user(user) .password(dto.getPassword()) .bounds(300000L) .saving(saving) + .userDeposit(userDepositRepository.getReferenceById(dto.getUserDepositId())) .build(); userSavingRepository.save(userSaving); - return response.success(userSaving); + return response.success(new UserSavingResponseDto(userSaving)); } public ResponseEntity cancelSaving(Long userSavingId) { @@ -85,14 +103,19 @@ public ResponseEntity setDormancy(Long userSavingId) { } public ResponseEntity getUserSavings(Long userId) { - // Fetch user-specific savings logic here - // This is a placeholder implementation - return response.success("Fetched savings for user with id " + userId); + List userSavings = userSavingRepository.findByUserId(userId); + List responseDtos = userSavings.stream() + .map(UserSavingResponseDto::new) + .collect(Collectors.toList()); + return response.success(responseDtos); } public ResponseEntity getUserSavingById(Long userId, Long savingId) { - // Fetch user-specific saving by id logic here - // This is a placeholder implementation - return response.success("Fetched saving with id " + savingId + " for user with id " + userId); + Saving saving = savingRepository.getDepositById(savingId).orElseThrow(() -> new RuntimeException("UserDeposit not found with id " + savingId)); + List userSavings = userSavingRepository.findByUserIdAndSaving(userId, saving); + List responseDtos = userSavings.stream() + .map(UserSavingResponseDto::new) + .collect(Collectors.toList()); + return response.success(responseDtos); } } diff --git a/src/main/java/com/hana/api/user/dto/request/UserRequestDto.java b/src/main/java/com/hana/api/user/dto/request/UserRequestDto.java index 8930377..a2c377e 100644 --- a/src/main/java/com/hana/api/user/dto/request/UserRequestDto.java +++ b/src/main/java/com/hana/api/user/dto/request/UserRequestDto.java @@ -1,9 +1,6 @@ package com.hana.api.user.dto.request; -import jakarta.validation.constraints.NotBlank; -import jakarta.validation.constraints.NotEmpty; -import jakarta.validation.constraints.NotNull; -import jakarta.validation.constraints.Pattern; +import jakarta.validation.constraints.*; import lombok.*; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.web.multipart.MultipartFile; @@ -39,15 +36,15 @@ public static class UserDepositRequestDto { @Getter @Setter public static class UserSavingRequestDto { - @NotEmpty(message = "예금 상품명은 필수 값입니다.") + @NotNull(message = "유저 ID는 필수 값입니다.") private Long userId; - @NotEmpty(message = "잔액은 필수 값입니다.") - private Long balance; - @NotEmpty(message = "가입 기간은 필수 값입니다.") - private long period; + @Positive(message = "가입 기간은 양수여야 합니다.") + private Long period; @NotEmpty(message = "비밀번호는 필수 값입니다.") private String password; - private Long userDepositId2; + @NotNull(message = "연결 계좌 ID는 필수 값입니다.") + private Long userDepositId; + @NotNull(message = "납입금액은 필수 값입니다.") private Long perMonth; } diff --git a/src/main/java/com/hana/api/user/dto/response/UserSavingResponseDto.java b/src/main/java/com/hana/api/user/dto/response/UserSavingResponseDto.java index 84c2834..bf3f5c7 100644 --- a/src/main/java/com/hana/api/user/dto/response/UserSavingResponseDto.java +++ b/src/main/java/com/hana/api/user/dto/response/UserSavingResponseDto.java @@ -22,7 +22,7 @@ public class UserSavingResponseDto { private Long perMonth; private LocalDateTime createdAt; private LocalDateTime updatedAt; - private Saving saving; + private UserDepositResponseDto parent; public UserSavingResponseDto(UserSaving entity){ this.id = entity.getId(); this.accountNumber = entity.getAccountNumber(); @@ -31,7 +31,9 @@ public UserSavingResponseDto(UserSaving entity){ this.password = entity.getPassword(); this.perMonth = entity.getPerMonth(); this.period = entity.getPeriod(); - this.saving = entity.getSaving(); + if(entity.getUserDeposit() != null) { + this.parent = new UserDepositResponseDto(entity.getUserDeposit()); + } this.createdAt = entity.getSaving().getCreatedDate(); this.updatedAt = entity.getSaving().getModifiedDate(); } diff --git a/src/main/java/com/hana/api/user/entity/User.java b/src/main/java/com/hana/api/user/entity/User.java index 2a1726f..6930bd0 100644 --- a/src/main/java/com/hana/api/user/entity/User.java +++ b/src/main/java/com/hana/api/user/entity/User.java @@ -45,17 +45,22 @@ public class User extends BaseEntity { private String socialNumber; @OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE) + @ToString.Exclude private List consultingInfos = new ArrayList<>(); @OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE) + @ToString.Exclude private List userCards = new ArrayList<>(); @OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE) + @ToString.Exclude private List userDeposits = new ArrayList<>(); @OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE) + @ToString.Exclude private List userSavings = new ArrayList<>(); @OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE) + @ToString.Exclude private List consultantReviews = new ArrayList<>(); } diff --git a/src/main/java/com/hana/api/user/entity/UserDeposit.java b/src/main/java/com/hana/api/user/entity/UserDeposit.java index 37da30b..c942e7c 100644 --- a/src/main/java/com/hana/api/user/entity/UserDeposit.java +++ b/src/main/java/com/hana/api/user/entity/UserDeposit.java @@ -61,6 +61,10 @@ public class UserDeposit extends BaseEntity { @ToString.Exclude // Lombok의 순환 참조 문제를 피하기 위해 private List children = new ArrayList<>(); + @OneToMany(mappedBy = "userDeposit", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE) + @ToString.Exclude // Lombok의 순환 참조 문제를 피하기 위해 + private List userSavings = new ArrayList<>(); + public void updateIsLoss(boolean status){ this.isLoss = status; } diff --git a/src/main/java/com/hana/api/user/entity/UserSaving.java b/src/main/java/com/hana/api/user/entity/UserSaving.java index 3547f53..25bfdb8 100644 --- a/src/main/java/com/hana/api/user/entity/UserSaving.java +++ b/src/main/java/com/hana/api/user/entity/UserSaving.java @@ -48,14 +48,17 @@ public class UserSaving { private Boolean isLoss; @ManyToOne(fetch = FetchType.LAZY) + @ToString.Exclude @JoinColumn(name = "user_id", referencedColumnName = "user_id") private User user; @ManyToOne(fetch = FetchType.LAZY) + @ToString.Exclude @JoinColumn(name = "saving_id", referencedColumnName = "saving_id") private Saving saving; - @OneToOne(fetch = FetchType.LAZY) + @ManyToOne(fetch = FetchType.LAZY) + @ToString.Exclude @JoinColumn(name = "user_deposit_id", referencedColumnName = "user_deposit_id") private UserDeposit userDeposit; } diff --git a/src/main/java/com/hana/api/user/repository/UserSavingRepository.java b/src/main/java/com/hana/api/user/repository/UserSavingRepository.java index 1791dbe..d6ba9f1 100644 --- a/src/main/java/com/hana/api/user/repository/UserSavingRepository.java +++ b/src/main/java/com/hana/api/user/repository/UserSavingRepository.java @@ -1,11 +1,18 @@ package com.hana.api.user.repository; +import com.hana.api.deposit.entity.Deposit; +import com.hana.api.saving.entity.Saving; +import com.hana.api.user.entity.UserDeposit; import com.hana.api.user.entity.UserSaving; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +import java.util.List; + @Repository public interface UserSavingRepository extends JpaRepository { + List findByUserId(long userId); + List findByUserIdAndSaving(long userId, Saving saving); }