Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/saving #17

Merged
merged 2 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);

}