@Component
public class StockDataLoader {
private final StockInfoService stockInfoService;
private final StockPriceService stockPriceService;
private final DataPortalApi dataPortalApi;
public StockDataLoader(StockInfoService stockInfoService, StockPriceService stockPriceService, DataPortalApi dataPortalApi) {
this.stockInfoService = stockInfoService;
this.stockPriceService = stockPriceService;
this.dataPortalApi = dataPortalApi;
}
public void loadStockInfoByBaseDate(String baseDate) {
JsonArray stockJson = dataPortalApi.getStock(baseDate);
if(stockJson == null){
return;
}
for(int i = 0 ; i < stockJson.size() ; i++){
StockInfo stockInfo = new StockInfo();
stockInfo.setItmsNm(stockJson.get(i).getAsJsonObject().get("itmsNm").getAsString());
stockInfo.setMrktCtg(stockJson.get(i).getAsJsonObject().get("mrktCtg").getAsString());
stockInfo.setSrtnCd(stockJson.get(i).getAsJsonObject().get("srtnCd").getAsString());
stockInfo.setIsinCd(stockJson.get(i).getAsJsonObject().get("isinCd").getAsString());
stockInfoService.save(stockInfo);
}
}
public void loadStockPriceByBaseDate(String baseDate) {
JsonArray stockJson = dataPortalApi.getStock(baseDate);
if(stockJson == null){
return;
}
for(int i = 0 ; i < stockJson.size() ; i++){
StockPrice stockPrice = new StockPrice();
stockPrice.setItmsNm(stockJson.get(i).getAsJsonObject().get("itmsNm").getAsString());
stockPrice.setMkp(Long.parseLong(stockJson.get(i).getAsJsonObject().get("mkp").getAsString()));
stockPrice.setClpr(Long.parseLong(stockJson.get(i).getAsJsonObject().get("clpr").getAsString()));
stockPrice.setHipr(Long.parseLong(stockJson.get(i).getAsJsonObject().get("hipr").getAsString()));
stockPrice.setLopr(Long.parseLong(stockJson.get(i).getAsJsonObject().get("lopr").getAsString()));
stockPrice.setTrqu(Long.parseLong(stockJson.get(i).getAsJsonObject().get("trqu").getAsString()));
stockPrice.setTrPrc(Long.parseLong(stockJson.get(i).getAsJsonObject().get("trPrc").getAsString()));
stockPrice.setMrktTotAmt(Long.parseLong(stockJson.get(i).getAsJsonObject().get("mrktTotAmt").getAsString()));
String basDt = stockJson.get(i).getAsJsonObject().get("basDt").getAsString();
LocalDate date = LocalDate.parse(basDt, DateTimeFormatter.ofPattern("yyyyMMdd"));
stockPrice.setBasDt(date);
stockPriceService.save(stockPrice);
}
}
public void loadStockPriceByTerm(String startDate, String endDate){
LocalDate start = LocalDate.parse(startDate, DateTimeFormatter.ofPattern("yyyyMMdd"));
LocalDate end = LocalDate.parse(endDate, DateTimeFormatter.ofPattern("yyyyMMdd")).plusDays(1);
LocalDate date = start;
while(date.isBefore(end)){
loadStockPriceByBaseDate(date.format(DateTimeFormatter.ofPattern("yyyyMMdd")));
date = date.plusDays(1);
}
}
}
날짜를 입력받아 StockInfo, StockPrice를 저장하는 함수와 시작날짜와 끝날짜를 받아 StockPrice를 저장하는 함수를 구현하였다.
public String getStockJson(String baseDate) {
String path = "json/stock/"+baseDate.substring(0, 4)+"/"+baseDate+".json";
if(JsonUtility.getJsonFromFile(path) != null){
return JsonUtility.getJsonFromFile(path);
}
String apiUrl = dataPortalUrl+"/getStockPriceInfo";
String queryParams = "?serviceKey=" + dataPortalKey
+ "&numOfRows=10000&resultType=json"
+ "&basDt=" + baseDate;
String json = JsonUtility.getJsonFromUrl(apiUrl + queryParams);
return json;
}
resources/json/stock/(year)/basedate에 json 파일이 있을 경우 파일에서 데이터를 가져오도록 수정하였다.
또한, 다른 파일에서도 데이터가 없는 경우 null을 반환하도록 하였다.
다음번에는 DB를 관리하는 관리자 페이지와 Controller를 만들어보자!