Skip to content

Commit

Permalink
feat: 투자성향 테스트 이후 Flask 서버로 요청을 보내서, 추천 좀목 3개를 받아서 db에 update하는 로직 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
megymj committed Nov 12, 2023
1 parent 7dd8647 commit f95b703
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.ai.roboadvisor.domain.tendency.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.ai.roboadvisor.domain.tendency.entity.Tendency;

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class TendencyDto {
private Tendency tendency;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.ai.roboadvisor.domain.tendency.dto.response;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.ai.roboadvisor.domain.tendency.entity.Tendency;

import java.util.List;

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class TendencyFlaskResponse {

@JsonProperty("recommended_stocks")
private List<String> recommended_stocks;

@JsonProperty("tendency")
private Tendency tendency;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.ai.roboadvisor.domain.tendency.entity;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.persistence.*;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED) // 인자 없는 기본 생성자 필요
@Entity
@Table(name = "stock_list_kr")
public class StockKr {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(nullable = false, name = "stock_code")
private String stockCode;

@Column(nullable = false, name = "stock_name")
private String stockName;

@Column(nullable = false)
private String market;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.ai.roboadvisor.domain.tendency.repository;


import org.ai.roboadvisor.domain.tendency.entity.StockKr;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface StockKrRepository extends JpaRepository<StockKr, Long> {

Optional<StockKr> findByStockName(String stockName);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.ai.roboadvisor.domain.tendency.service;

import lombok.extern.slf4j.Slf4j;
import org.ai.roboadvisor.domain.tendency.dto.TendencyDto;
import org.ai.roboadvisor.domain.tendency.dto.response.TendencyFlaskResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import java.time.Duration;

@Slf4j
@Service
public class FlaskRecommendClient {

private final WebClient flaskClient;

@Autowired
public FlaskRecommendClient(@Qualifier("FlaskClient") WebClient flaskClient) {
this.flaskClient = flaskClient;
}

public TendencyFlaskResponse getStockData(TendencyDto dto) {
log.info(">> run here");
try {
return flaskClient.post()
.uri("/recommend_stock")
.bodyValue(dto)
.retrieve()
.bodyToMono(TendencyFlaskResponse.class)
.timeout(Duration.ofSeconds(600))
.onErrorResume(e -> {
log.error("]] Error: Get Recommend stock from Flask Server", e);
return Mono.empty();
})
.block();
} catch (Exception e) {
log.error(">> Exception: Get Recommend stock from Flask Server", e);
return null;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,28 @@

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.ai.roboadvisor.domain.tendency.dto.TendencyDto;
import org.ai.roboadvisor.domain.tendency.dto.TendencyUpdateDto;
import org.ai.roboadvisor.domain.tendency.entity.StockKr;
import org.ai.roboadvisor.domain.tendency.entity.Tendency;
import org.ai.roboadvisor.domain.tendency.repository.StockKrRepository;
import org.ai.roboadvisor.domain.user.entity.User;
import org.ai.roboadvisor.domain.user.repository.UserRepository;
import org.ai.roboadvisor.global.exception.CustomException;
import org.ai.roboadvisor.global.exception.ErrorCode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@RequiredArgsConstructor
@Service
public class TendencyService {

private final UserRepository userRepository;
private final FlaskRecommendClient flaskRecommendClient;
private final StockKrRepository stockKrRepository;

@Transactional
public TendencyUpdateDto updateTendency(TendencyUpdateDto tendencyUpdateDto) {
Expand All @@ -28,6 +35,26 @@ public TendencyUpdateDto updateTendency(TendencyUpdateDto tendencyUpdateDto) {
checkTendencyIsValid(updateTendency);

updateTendencyOfUser(user, updateTendency);

// Flask 서버에서 추천 주식 종목 3개를 받아온다.
List<String> getRecommendStocks = flaskRecommendClient
.getStockData(new TendencyDto(updateTendency)).getRecommended_stocks();

// 받아온 추천 종목 3개를, 주식 코드로 변환한다.
StringBuilder updateStocks = new StringBuilder();
for (int i = 0; i < getRecommendStocks.size(); i++) {
StockKr stockKr = stockKrRepository.findByStockName(getRecommendStocks.get(i))
.orElse(null);
if (stockKr != null && stockKr.getStockCode() != null) {
if (i < getRecommendStocks.size() - 1) {
updateStocks.append(stockKr.getStockCode()).append(",");
} else {
updateStocks.append(stockKr.getStockCode());
}
}
}
updateRecommendStocksOfUser(user, updateStocks.toString());

return TendencyUpdateDto.of(userNickname, updateTendency);
}

Expand All @@ -40,4 +67,8 @@ private void checkTendencyIsValid(Tendency tendency) {
private void updateTendencyOfUser(User user, Tendency tendency) {
user.setTendency(tendency);
}

private void updateRecommendStocksOfUser(User user, String stocks) {
user.setRecommendStocks(stocks);
}
}
7 changes: 7 additions & 0 deletions src/main/java/org/ai/roboadvisor/domain/user/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public class User {
@Enumerated(EnumType.STRING)
private Tendency tendency;

@Column(nullable = true, length = 20, name = "recommend_stocks")
private String recommendStocks;

@Builder
private User(String email, String password,
String nickname, LocalDate birth, String gender, String career, Tendency tendency) {
Expand All @@ -63,4 +66,8 @@ private User(String email, String password,
public void setTendency(Tendency tendency) {
this.tendency = tendency;
}

public void setRecommendStocks(String recommendStocks) {
this.recommendStocks = recommendStocks;
}
}

0 comments on commit f95b703

Please sign in to comment.