Skip to content

Commit

Permalink
Merge pull request #17 from HanaView/feat/saving
Browse files Browse the repository at this point in the history
Feat/saving
  • Loading branch information
yunred authored Jun 5, 2024
2 parents 6306920 + 46848c9 commit c588809
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ public class SavingCategory extends BaseEntity {
private List<SavingCategory> children = new ArrayList<>();

@OneToMany(mappedBy = "savingCategory", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
@ToString.Exclude
private List<Saving> savings = new ArrayList<>();
}
41 changes: 32 additions & 9 deletions src/main/java/com/hana/api/saving/service/SavingService.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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() {
Expand All @@ -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();
Expand All @@ -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) {
Expand All @@ -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<UserSaving> userSavings = userSavingRepository.findByUserId(userId);
List<UserSavingResponseDto> 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<UserSaving> userSavings = userSavingRepository.findByUserIdAndSaving(userId, saving);
List<UserSavingResponseDto> responseDtos = userSavings.stream()
.map(UserSavingResponseDto::new)
.collect(Collectors.toList());
return response.success(responseDtos);
}
}
17 changes: 7 additions & 10 deletions src/main/java/com/hana/api/user/dto/request/UserRequestDto.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/hana/api/user/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<ConsultingInfo> consultingInfos = new ArrayList<>();

@OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
@ToString.Exclude
private List<UserCard> userCards = new ArrayList<>();

@OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
@ToString.Exclude
private List<UserDeposit> userDeposits = new ArrayList<>();

@OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
@ToString.Exclude
private List<UserSaving> userSavings = new ArrayList<>();

@OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
@ToString.Exclude
private List<ConsultantReview> consultantReviews = new ArrayList<>();
}
4 changes: 4 additions & 0 deletions src/main/java/com/hana/api/user/entity/UserDeposit.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ public class UserDeposit extends BaseEntity {
@ToString.Exclude // Lombok의 순환 참조 문제를 피하기 위해
private List<UserDeposit> children = new ArrayList<>();

@OneToMany(mappedBy = "userDeposit", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
@ToString.Exclude // Lombok의 순환 참조 문제를 피하기 위해
private List<UserSaving> userSavings = new ArrayList<>();

public void updateIsLoss(boolean status){
this.isLoss = status;
}
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/hana/api/user/entity/UserSaving.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
@@ -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<UserSaving, Long> {
List<UserSaving> findByUserId(long userId);
List<UserSaving> findByUserIdAndSaving(long userId, Saving saving);

}

0 comments on commit c588809

Please sign in to comment.