포트폴리오 서비스(포트폴리오 폼 개선하기)

·2024년 1월 24일
0

Portfolio Backtest

목록 보기
19/31

기존의 폼

  • 기존의 폼은 내가 주식을 직접 입력해야함
  • 따라서, 주식 이름을 틀리면 오류가 발생
  • 주식명을 입력할 때, 모달창을 사용해서 처리해 보자

StockInfo 수정

Repository와 Service에 문자열을 포함하는 List를 반환하는 JPA를 추가 했다.

    List<StockInfo> findByItmsNmContaining(String keyword);
    List<StockInfo> findBySrtnCdContaining(String keyword);

Containing을 통해 keyword를 포함하는 문자열을 받아온다.

    public List<String> findAllByQuery(String query) {
        List<StockInfo> stockInfos;
        if(query.charAt(0) <= '9' && query.charAt(0) >= '0') {
            stockInfos = stockInfoRepository.findBySrtnCdContaining(query);
        }else{
            stockInfos = stockInfoRepository.findByItmsNmContaining(query);
        }

        List<String> result = new ArrayList<>();
        for(StockInfo stockInfo : stockInfos) {
            result.add(stockInfo.getItmsNm() +" (" + stockInfo.getSrtnCd() + ")");
        }

        return result;
    }
    

문자열의 첫 번째 문자를 기준으로 종목명, 종목코드로 검색을 하고 문자열로 변환하여 "종목명 (종목코드)"의 List를 반환하였다.

SearchController

모달에서 데이터 통신을 위해 Controller를 만들었다.

@Controller
public class SearchController {
    private final StockInfoService stockInfoService;

    public SearchController(StockInfoService stockInfoService) {
        this.stockInfoService = stockInfoService;
    }
    
    @GetMapping("/searchStock")
    public ResponseEntity<List<String>> searchStock(@RequestParam String searchTerm) {
        List<String> searchResult = stockInfoService.findAllByQuery(searchTerm);
        return ResponseEntity.ok(searchResult);
    }
}

html


다음과 같이 구성하였고 클릭시 해당 종목을 선택하고 모달창이 닫힌다.


기존 Controller 수정

    for(int i = 1 ; i <= Integer.parseInt(params.get("count")) ; i++){
        String stockName = params.get("stock" + i);
        int indexOfParenthesis = stockName.indexOf('(');
        stockNames.add(stockName.substring(0, indexOfParenthesis - 1).trim());
        weights.add(Double.parseDouble(params.get("weight" + i)));
    }

다음과 같이 종목명만 가져오도록 변경하였다.

profile
백엔드 개발자가 꿈인 컴공과

0개의 댓글

관련 채용 정보