Skip to content

Commit

Permalink
Merge pull request #67 from HanaPiece/feat/account-auto-split
Browse files Browse the repository at this point in the history
feat/ 통장 쪼개기 비율 추천 API 추가
  • Loading branch information
duddn2012 authored Jun 9, 2024
2 parents e85f52b + 9ba844a commit dc2bc9b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
package com.project.hana_piece.account.controller;

import com.project.hana_piece.account.dto.AccountAutoDebitAdjustGetResponse;
import com.project.hana_piece.account.dto.AccountAutoDebitAdjustUpsertRequest;
import com.project.hana_piece.account.dto.AccountGetResponse;
import com.project.hana_piece.account.dto.AccountMonthTransactionGetResponse;
import com.project.hana_piece.account.dto.AccountSalaryGetResponse;
import com.project.hana_piece.account.dto.AccountSavingGetResponse;
import com.project.hana_piece.account.dto.AccountTransactionGetResponse;
import com.project.hana_piece.account.dto.AccountTypeRegRequest;
import com.project.hana_piece.account.dto.AccountUpsertResponse;
import com.project.hana_piece.account.dto.UserGoalAccountGetResponse;
import com.project.hana_piece.account.dto.*;
import com.project.hana_piece.account.service.AccountService;
import java.util.List;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -90,6 +81,12 @@ public ResponseEntity<List<AccountAutoDebitAdjustGetResponse>> findAccountAutoDe
return ResponseEntity.ok(response);
}

@GetMapping("/auto-debit/suggestions/{type}")
public ResponseEntity<AccountAutoDebitSuggestGetResponse> findAccountAutoDebitSuggest(@AuthenticationPrincipal Long userId, @PathVariable String type){
AccountAutoDebitSuggestGetResponse response = accountService.findAccountAutoDebitSuggest(userId, type);
return ResponseEntity.ok(response);
}

@PostMapping("/auto-debit/adjust")
public ResponseEntity<Void> updateAccountAutoDebitAdjust(@RequestBody AccountAutoDebitAdjustUpsertRequest request) {
accountService.updateAccountAutoDebitAdjust(request);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.project.hana_piece.account.dto;


public record AccountAutoDebitSuggestGetResponse (int life, int reserve, int saving){
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,8 @@
package com.project.hana_piece.account.service;

import static com.project.hana_piece.account.domain.AccountType.*;
import static com.project.hana_piece.account.domain.AccountType.isParkingAccountType;
import static com.project.hana_piece.account.util.AccountNumberGenerator.generateAccountNumber;

import com.project.hana_piece.account.domain.Account;
import com.project.hana_piece.account.domain.AccountAutoDebit;
import com.project.hana_piece.account.domain.AccountPaymentType;
import com.project.hana_piece.account.domain.AccountTransaction;
import com.project.hana_piece.account.domain.AccountTransactionType;
import com.project.hana_piece.account.domain.AccountType;
import com.project.hana_piece.account.dto.AccountAutoDebitAdjustGetResponse;
import com.project.hana_piece.account.dto.AccountAutoDebitAdjustUpsertRequest;
import com.project.hana_piece.account.dto.AccountDailyTransactionGetResponse;
import com.project.hana_piece.account.dto.AccountGetResponse;
import com.project.hana_piece.account.dto.AccountMonthTransactionGetResponse;
import com.project.hana_piece.account.dto.AccountSalaryGetResponse;
import com.project.hana_piece.account.dto.AccountSavingGetResponse;
import com.project.hana_piece.account.dto.AccountTransactionGetResponse;
import com.project.hana_piece.account.dto.AccountTypeRegRequest;
import com.project.hana_piece.account.dto.AccountUpsertResponse;
import com.project.hana_piece.account.dto.UserGoalAccountGetResponse;
import com.google.gson.JsonObject;
import com.project.hana_piece.account.domain.*;
import com.project.hana_piece.account.dto.*;
import com.project.hana_piece.account.exception.AccountAutoDebitNotFoundException;
import com.project.hana_piece.account.exception.AccountInvalidException;
import com.project.hana_piece.account.exception.AccountNotFoundException;
Expand All @@ -31,23 +13,29 @@
import com.project.hana_piece.account.repository.AccountTransactionRepository;
import com.project.hana_piece.account.repository.AccountTransactionRepositoryCustom;
import com.project.hana_piece.common.exception.ValueInvalidException;
import com.project.hana_piece.common.util.JsonUtil;
import com.project.hana_piece.goal.domain.UserGoal;
import com.project.hana_piece.goal.exception.UserGoalNotFoundException;
import com.project.hana_piece.goal.repository.UserGoalRepository;
import com.project.hana_piece.user.domain.User;
import com.project.hana_piece.user.exception.UserInvalidException;
import com.project.hana_piece.user.exception.UserNotFoundException;
import com.project.hana_piece.user.repository.UserRepository;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.reactive.function.client.WebClient;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.project.hana_piece.account.domain.AccountType.*;
import static com.project.hana_piece.account.util.AccountNumberGenerator.generateAccountNumber;

@Service
@RequiredArgsConstructor
Expand All @@ -62,6 +50,8 @@ public class AccountService {
private final UserRepository userRepository;
private final UserGoalRepository userGoalRepository;

private final JsonUtil jsonUtil;

public AccountUpsertResponse saveAccount(Long userId) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new UserNotFoundException(userId));
Expand Down Expand Up @@ -227,6 +217,35 @@ public List<AccountAutoDebitAdjustGetResponse> findAccountAutoDebitAdjust(Long u
return autoDebitAccountList.stream().map(AccountAutoDebitAdjustGetResponse::fromProjection).toList();
}

public AccountAutoDebitSuggestGetResponse findAccountAutoDebitSuggest(Long userId, String type){
WebClient webClient = WebClient.builder()
.baseUrl("http://54.180.220.88:5000")
.build();

String endpoint = "";
if(type.equals("init")){
endpoint = "/first_calc/"+userId;
} else if(type.equals("lux")){
endpoint = "/second_calc/"+userId+"/lux";
} else if(type.equals("save")){
endpoint = "/second_calc/"+userId+"/save";
}

String responseBody = webClient.get()
.uri(endpoint)
.retrieve()
.bodyToMono(String.class)
.block();

JsonObject jsonObject = jsonUtil.toJson(responseBody);
JsonObject resultData = jsonUtil.extractProperty(jsonObject, "result_data", JsonObject.class);
int life = jsonUtil.extractProperty(resultData, "life", Integer.class);
int reserve = jsonUtil.extractProperty(resultData, "reserve", Integer.class);
int saving = jsonUtil.extractProperty(resultData, "saving", Integer.class);

return new AccountAutoDebitSuggestGetResponse(life, reserve, saving);
}

public void updateAccountAutoDebitAdjust(AccountAutoDebitAdjustUpsertRequest request) {
Long savingId = request.savingAccountAutoDebitId();
Long lifeId = request.lifeAccountAutoDebitId();
Expand Down

0 comments on commit dc2bc9b

Please sign in to comment.