From 37b6ab73f7f4f373de1b55ef8e08a869cea9368c Mon Sep 17 00:00:00 2001 From: ghdeo Date: Thu, 19 Oct 2023 12:21:04 +0900 Subject: [PATCH 1/5] =?UTF-8?q?[FEAT]=20Parser=20=EC=88=98=EC=A0=95=20(#78?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...Response.java => ParseStockJsonResponse.java} | 14 +++++++------- .../kkanbustock/stock/service/StockService.java | 16 ++++++++-------- 2 files changed, 15 insertions(+), 15 deletions(-) rename src/main/java/com/bull4jo/kkanbustock/common/{ParseJsonResponse.java => ParseStockJsonResponse.java} (90%) diff --git a/src/main/java/com/bull4jo/kkanbustock/common/ParseJsonResponse.java b/src/main/java/com/bull4jo/kkanbustock/common/ParseStockJsonResponse.java similarity index 90% rename from src/main/java/com/bull4jo/kkanbustock/common/ParseJsonResponse.java rename to src/main/java/com/bull4jo/kkanbustock/common/ParseStockJsonResponse.java index 2a674df..379632e 100644 --- a/src/main/java/com/bull4jo/kkanbustock/common/ParseJsonResponse.java +++ b/src/main/java/com/bull4jo/kkanbustock/common/ParseStockJsonResponse.java @@ -5,12 +5,11 @@ import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.google.gson.JsonParser; -import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.List; -public class ParseJsonResponse { +public class ParseStockJsonResponse { public static Stock parseSingleResponse(String jsonResponse) { // Parse the JSON response @@ -26,9 +25,11 @@ public static Stock parseSingleResponse(String jsonResponse) { .mrktCtg(MarketType.valueOf(item.get("mrktCtg").getAsString())) .clpr(item.get("clpr").getAsInt()) .mkp(item.get("mkp").getAsInt()) + .basDt(item.get("basDt").getAsInt()) .vs(item.get("vs").getAsInt()) + .fltRt((item.get("fltRt")).getAsFloat()) + .trPrc(item.get("trPrc").getAsLong()) .mrktTotAmt(item.get("mrktTotAmt").getAsNumber().longValue()) - .basDt(item.get("basDt").getAsInt()) .build(); return stock; } @@ -52,13 +53,12 @@ public static List parseResponses(String jsonResponse) { .mrktCtg(MarketType.valueOf(asJsonObject.get("mrktCtg").getAsString())) .clpr(asJsonObject.get("clpr").getAsInt()) .mkp(asJsonObject.get("mkp").getAsInt()) + .basDt(asJsonObject.get("basDt").getAsInt()) .vs(asJsonObject.get("vs").getAsInt()) + .fltRt(asJsonObject.get("fltRt").getAsFloat()) + .trPrc(asJsonObject.get("trPrc").getAsLong()) .mrktTotAmt(asJsonObject.get("mrktTotAmt").getAsNumber().longValue()) - .basDt(asJsonObject.get("basDt").getAsInt()) .build(); - - list.add(stock); - } return list; diff --git a/src/main/java/com/bull4jo/kkanbustock/stock/service/StockService.java b/src/main/java/com/bull4jo/kkanbustock/stock/service/StockService.java index 284c357..835285b 100644 --- a/src/main/java/com/bull4jo/kkanbustock/stock/service/StockService.java +++ b/src/main/java/com/bull4jo/kkanbustock/stock/service/StockService.java @@ -1,6 +1,6 @@ package com.bull4jo.kkanbustock.stock.service; -import com.bull4jo.kkanbustock.common.ParseJsonResponse; +import com.bull4jo.kkanbustock.common.ParseStockJsonResponse; import com.bull4jo.kkanbustock.common.RandomNumberGenerator; import com.bull4jo.kkanbustock.exception.CustomException; import com.bull4jo.kkanbustock.exception.ErrorCode; @@ -19,7 +19,6 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.util.ArrayList; import java.util.List; import java.util.Optional; import java.util.concurrent.TimeUnit; @@ -79,7 +78,7 @@ public String save(final Stock stock) { public void setStockRepository() { OkHttpClient client = new OkHttpClient() .newBuilder() - .readTimeout(10000, TimeUnit.MILLISECONDS) // 10 초 타임아웃 + .readTimeout(30000, TimeUnit.MILLISECONDS) // 10 초 타임아웃 .build(); HttpUrl.Builder urlBuilder = HttpUrl.parse(dartUrl).newBuilder(); @@ -105,7 +104,7 @@ public void setStockRepository() { if (response.isSuccessful()) { // 응답 처리 로직 String responseBody = response.body().string(); - List stocks = ParseJsonResponse.parseResponses(responseBody); + List stocks = ParseStockJsonResponse.parseResponses(responseBody); stockRepository.saveAll(stocks); } else { // 응답이 실패인 경우 @@ -146,7 +145,7 @@ public Stock findByItmsNm(final String itmsNm) { private Optional getFromDartByStrnCd(final String srtnCd) { OkHttpClient client = new OkHttpClient() .newBuilder() - .readTimeout(10000, TimeUnit.MILLISECONDS) // 10 초 타임아웃 + .readTimeout(30000, TimeUnit.MILLISECONDS) // 10 초 타임아웃 .build(); HttpUrl.Builder urlBuilder = HttpUrl.parse(dartUrl).newBuilder(); @@ -171,7 +170,7 @@ private Optional getFromDartByStrnCd(final String srtnCd) { // 응답 처리 로직 String responseBody = response.body().string(); // System.out.println("API 응답: " + responseBody); - stock = ParseJsonResponse.parseSingleResponse(responseBody); + stock = ParseStockJsonResponse.parseSingleResponse(responseBody); } else { // 응답이 실패인 경우 throw new RuntimeException("API 요청 실패. 응답 코드: " + response.code()); @@ -190,7 +189,7 @@ private Optional getFromDartByStrnCd(final String srtnCd) { private Optional getFromDartByItmsNm(final String itmsNm) { OkHttpClient client = new OkHttpClient() .newBuilder() - .readTimeout(10000, TimeUnit.MILLISECONDS) // 10 초 타임아웃 + .readTimeout(30000, TimeUnit.MILLISECONDS) // 10 초 타임아웃 .build(); HttpUrl.Builder urlBuilder = HttpUrl.parse(dartUrl).newBuilder(); @@ -214,7 +213,7 @@ private Optional getFromDartByItmsNm(final String itmsNm) { if (response.isSuccessful()) { // 응답 처리 로직 String responseBody = response.body().string(); - stock = ParseJsonResponse.parseSingleResponse(responseBody); + stock = ParseStockJsonResponse.parseSingleResponse(responseBody); } else { // 응답이 실패인 경우 throw new RuntimeException("API 요청 실패. 응답 코드: " + response.code()); @@ -286,6 +285,7 @@ public void setRecommendConservativeStock() { recommendStockRepository.save(recommendStock); } } + @Transactional public Page getRecommendedStocks(Pageable pageable) { Page page = recommendStockRepository.findAll(pageable); From 3087efa98d68f40356455cefef6d1f1607c585e7 Mon Sep 17 00:00:00 2001 From: ghdeo Date: Thu, 19 Oct 2023 15:40:42 +0900 Subject: [PATCH 2/5] =?UTF-8?q?[FEAT]=20Parser=20=EC=88=98=EC=A0=95=20(#78?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kkanbustock/common/CustomParser.java | 86 +++++++++++++++++++ .../common/ParseStockJsonResponse.java | 66 -------------- 2 files changed, 86 insertions(+), 66 deletions(-) create mode 100644 src/main/java/com/bull4jo/kkanbustock/common/CustomParser.java delete mode 100644 src/main/java/com/bull4jo/kkanbustock/common/ParseStockJsonResponse.java diff --git a/src/main/java/com/bull4jo/kkanbustock/common/CustomParser.java b/src/main/java/com/bull4jo/kkanbustock/common/CustomParser.java new file mode 100644 index 0000000..8c3e94b --- /dev/null +++ b/src/main/java/com/bull4jo/kkanbustock/common/CustomParser.java @@ -0,0 +1,86 @@ +package com.bull4jo.kkanbustock.common; + +import com.bull4jo.kkanbustock.stock.domain.MarketType; +import com.bull4jo.kkanbustock.stock.domain.Stock; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class CustomParser { + + public static Stock parseSingleJsonResponse(String jsonResponse) throws IOException { + ObjectMapper objectMapper = new ObjectMapper(); + JsonNode responseJson = objectMapper.readTree(jsonResponse); + + JsonNode item = responseJson + .path("response") + .path("body") + .path("items") + .path("item") + .get(0); + + float fltRt = CustomParser.formatFltRt(item.get("fltRt").asText()); + Stock stock = Stock.builder() + .srtnCd(item.get("srtnCd").asText()) + .itmsNm(item.get("itmsNm").asText()) + .mrktCtg(MarketType.valueOf(item.get("mrktCtg").asText())) + .clpr(item.get("clpr").asInt()) + .mkp(item.get("mkp").asInt()) + .basDt(item.get("basDt").asInt()) + .vs(item.get("vs").asInt()) + .fltRt(fltRt) + .trPrc(item.get("trPrc").asLong()) + .mrktTotAmt(item.get("mrktTotAmt").asLong()) + .build(); + + return stock; + } + + public static List parseJsonResponses(String jsonResponse) throws IOException { + ObjectMapper objectMapper = new ObjectMapper(); + JsonNode responseJson = objectMapper.readTree(jsonResponse); + JsonNode items = responseJson.path("response").path("body").path("items").path("item"); + + List list = new ArrayList<>(); + + + for (JsonNode itemNode : items) { + float fltRt = CustomParser.formatFltRt(itemNode.get("fltRt").asText()); + + Stock stock = Stock.builder() + .srtnCd(itemNode.get("srtnCd").asText()) + .itmsNm(itemNode.get("itmsNm").asText()) + .mrktCtg(MarketType.valueOf(itemNode.get("mrktCtg").asText())) + .clpr(itemNode.get("clpr").asInt()) + .mkp(itemNode.get("mkp").asInt()) + .basDt(itemNode.get("basDt").asInt()) + .vs(itemNode.get("vs").asInt()) + .fltRt(fltRt) + .trPrc(itemNode.get("trPrc").asLong()) + .mrktTotAmt(itemNode.get("mrktTotAmt").asLong()) + .build(); + + list.add(stock); + } + + return list; + } + + public static float formatFltRt(String input) { + try { + // 문자열을 부동 소수점 숫자로 파싱 + double parsedValue = Double.parseDouble(input); + + // 소수점 이하 두 자리까지 포맷 + String formattedValue = String.format("%.2f", parsedValue); + // 포맷된 문자열을 float로 파싱하여 반환 + return Float.parseFloat(formattedValue); + } catch (NumberFormatException e) { + return 0.0f; + } + } + +} diff --git a/src/main/java/com/bull4jo/kkanbustock/common/ParseStockJsonResponse.java b/src/main/java/com/bull4jo/kkanbustock/common/ParseStockJsonResponse.java deleted file mode 100644 index 379632e..0000000 --- a/src/main/java/com/bull4jo/kkanbustock/common/ParseStockJsonResponse.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.bull4jo.kkanbustock.common; - -import com.bull4jo.kkanbustock.stock.domain.MarketType; -import com.bull4jo.kkanbustock.stock.domain.Stock; -import com.google.gson.JsonArray; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; - -import java.util.ArrayList; -import java.util.List; - -public class ParseStockJsonResponse { - - public static Stock parseSingleResponse(String jsonResponse) { - // Parse the JSON response - JsonObject responseJson = JsonParser.parseString(jsonResponse).getAsJsonObject(); - JsonObject response = responseJson.getAsJsonObject("response"); - JsonObject body = response.getAsJsonObject("body"); - JsonObject items = body.getAsJsonObject("items"); - JsonObject item = items.getAsJsonArray("item").get(0).getAsJsonObject(); - - Stock stock = Stock.builder() - .srtnCd(item.get("srtnCd").getAsString()) - .itmsNm(item.get("itmsNm").getAsString()) - .mrktCtg(MarketType.valueOf(item.get("mrktCtg").getAsString())) - .clpr(item.get("clpr").getAsInt()) - .mkp(item.get("mkp").getAsInt()) - .basDt(item.get("basDt").getAsInt()) - .vs(item.get("vs").getAsInt()) - .fltRt((item.get("fltRt")).getAsFloat()) - .trPrc(item.get("trPrc").getAsLong()) - .mrktTotAmt(item.get("mrktTotAmt").getAsNumber().longValue()) - .build(); - return stock; - } - - public static List parseResponses(String jsonResponse) { - // Parse the JSON response - JsonObject responseJson = JsonParser.parseString(jsonResponse).getAsJsonObject(); - JsonObject response = responseJson.getAsJsonObject("response"); - JsonObject body = response.getAsJsonObject("body"); - JsonObject items = body.getAsJsonObject("items"); - - JsonArray item = items.getAsJsonArray("item"); - List list = new ArrayList<>(); - - for (int i = 0; i < item.size(); i++) { - JsonObject asJsonObject = item.get(i).getAsJsonObject(); - - Stock stock = Stock.builder() - .srtnCd(asJsonObject.get("srtnCd").getAsString()) - .itmsNm(asJsonObject.get("itmsNm").getAsString()) - .mrktCtg(MarketType.valueOf(asJsonObject.get("mrktCtg").getAsString())) - .clpr(asJsonObject.get("clpr").getAsInt()) - .mkp(asJsonObject.get("mkp").getAsInt()) - .basDt(asJsonObject.get("basDt").getAsInt()) - .vs(asJsonObject.get("vs").getAsInt()) - .fltRt(asJsonObject.get("fltRt").getAsFloat()) - .trPrc(asJsonObject.get("trPrc").getAsLong()) - .mrktTotAmt(asJsonObject.get("mrktTotAmt").getAsNumber().longValue()) - .build(); - } - - return list; - } -} From 1e59c380b4480be0f3696ddd7f4a2670702e65fc Mon Sep 17 00:00:00 2001 From: ghdeo Date: Thu, 19 Oct 2023 15:41:00 +0900 Subject: [PATCH 3/5] =?UTF-8?q?[FEAT]=20=EA=B0=80=EC=9E=A5=20=EA=B0=80?= =?UTF-8?q?=EA=B9=8C=EC=9A=B4=20=ED=8F=89=EC=9D=BC=20=EA=B2=80=EC=83=89=20?= =?UTF-8?q?(#78)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/RecentWeekdayFinder.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/main/java/com/bull4jo/kkanbustock/common/RecentWeekdayFinder.java diff --git a/src/main/java/com/bull4jo/kkanbustock/common/RecentWeekdayFinder.java b/src/main/java/com/bull4jo/kkanbustock/common/RecentWeekdayFinder.java new file mode 100644 index 0000000..e69ade8 --- /dev/null +++ b/src/main/java/com/bull4jo/kkanbustock/common/RecentWeekdayFinder.java @@ -0,0 +1,34 @@ +package com.bull4jo.kkanbustock.common; + +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; + +public class RecentWeekdayFinder { + + public static String findMostRecentWeekday() { + Calendar calendar = Calendar.getInstance(); + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd"); + String today = dateFormat.format(new Date()); + + while (true) { + calendar.add(Calendar.DAY_OF_MONTH, -1); // 하루씩 이전 날짜로 이동 + Date currentDate = calendar.getTime(); + String currentDateStr = dateFormat.format(currentDate); + int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK); + + // 주말(토요일 또는 일요일)이 아니면 반환 + if (dayOfWeek != Calendar.SATURDAY && dayOfWeek != Calendar.SUNDAY) { + return currentDateStr; + } + + // 오늘과 같은 날짜면 루프 종료 + if (currentDateStr.equals(today)) { + break; + } + } + + return null; // 가장 최근 평일을 찾을 수 없을 경우 + } +} + From d38458a1e38cac720e73fab8f71dc008eb6b3248 Mon Sep 17 00:00:00 2001 From: ghdeo Date: Thu, 19 Oct 2023 15:41:53 +0900 Subject: [PATCH 4/5] =?UTF-8?q?[FEAT]=20=ED=88=AC=EC=9E=90=EC=9E=90=20?= =?UTF-8?q?=EC=84=B1=ED=96=A5=EB=B3=84=20=EC=A2=85=EB=AA=A9=20=EC=B6=94?= =?UTF-8?q?=EC=B2=9C=20=EC=84=9C=EB=B9=84=EC=8A=A4(#78)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../stock/service/StockService.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/bull4jo/kkanbustock/stock/service/StockService.java b/src/main/java/com/bull4jo/kkanbustock/stock/service/StockService.java index 835285b..8d7f3e4 100644 --- a/src/main/java/com/bull4jo/kkanbustock/stock/service/StockService.java +++ b/src/main/java/com/bull4jo/kkanbustock/stock/service/StockService.java @@ -1,7 +1,8 @@ package com.bull4jo.kkanbustock.stock.service; -import com.bull4jo.kkanbustock.common.ParseStockJsonResponse; +import com.bull4jo.kkanbustock.common.CustomParser; import com.bull4jo.kkanbustock.common.RandomNumberGenerator; +import com.bull4jo.kkanbustock.common.RecentWeekdayFinder; import com.bull4jo.kkanbustock.exception.CustomException; import com.bull4jo.kkanbustock.exception.ErrorCode; import com.bull4jo.kkanbustock.member.domain.entity.InvestorType; @@ -30,7 +31,7 @@ public class StockService { private final StockRepository stockRepository; private final RecommendStockRepository recommendStockRepository; private final String dartUrl = "https://apis.data.go.kr/1160100/service/GetStockSecuritiesInfoService/getStockPriceInfo"; - private final String authKey = "IDHVrjX6z8KgvQCdBokJywDgdQjpPnlGVoqDPNmuz5RcZthIW+GAQmSYfzT/n+Um2jMkMly1jI2eEJWKqO4C0g=="; + private final String authKey = "GYfL+RKMgNsn5M9GGWuVBwTIKQIPk0DdvBEFWfll2LmhKvI7QN6eUcvkX5x1bUd3RIzIl4HlsMEGfs/JxQOubA=="; private final String shinhanUrl = "https://gapi.shinhansec.com:8443/openapi/v1.0/recommend/portfolio"; private final String apiKey = "l7xxR7Fe0dP3i8KPZaPKpknI2vWrMeJfwDpk"; @@ -74,11 +75,10 @@ public String save(final Stock stock) { } @Scheduled(cron = "0 0 2 * * *") - @Transactional public void setStockRepository() { OkHttpClient client = new OkHttpClient() .newBuilder() - .readTimeout(30000, TimeUnit.MILLISECONDS) // 10 초 타임아웃 + .readTimeout(1000000, TimeUnit.MILLISECONDS) // 10 초 타임아웃 .build(); HttpUrl.Builder urlBuilder = HttpUrl.parse(dartUrl).newBuilder(); @@ -87,9 +87,11 @@ public void setStockRepository() { urlBuilder.addQueryParameter("serviceKey", authKey); urlBuilder.addQueryParameter("resultType", "json"); - urlBuilder.addQueryParameter("numOfRows", "2420021"); + urlBuilder.addQueryParameter("numOfRows", "3000"); + urlBuilder.addQueryParameter("basDt", RecentWeekdayFinder.findMostRecentWeekday()); String urlWithParam = urlBuilder.build().toString(); + System.out.println("urlWithParam = " + urlWithParam); Request request = new Request.Builder() .url(urlWithParam) .method("GET", null) @@ -104,7 +106,7 @@ public void setStockRepository() { if (response.isSuccessful()) { // 응답 처리 로직 String responseBody = response.body().string(); - List stocks = ParseStockJsonResponse.parseResponses(responseBody); + List stocks = CustomParser.parseJsonResponses(responseBody); stockRepository.saveAll(stocks); } else { // 응답이 실패인 경우 @@ -170,7 +172,7 @@ private Optional getFromDartByStrnCd(final String srtnCd) { // 응답 처리 로직 String responseBody = response.body().string(); // System.out.println("API 응답: " + responseBody); - stock = ParseStockJsonResponse.parseSingleResponse(responseBody); + stock = CustomParser.parseSingleJsonResponse(responseBody); } else { // 응답이 실패인 경우 throw new RuntimeException("API 요청 실패. 응답 코드: " + response.code()); @@ -213,7 +215,7 @@ private Optional getFromDartByItmsNm(final String itmsNm) { if (response.isSuccessful()) { // 응답 처리 로직 String responseBody = response.body().string(); - stock = ParseStockJsonResponse.parseSingleResponse(responseBody); + stock = CustomParser.parseSingleJsonResponse(responseBody); } else { // 응답이 실패인 경우 throw new RuntimeException("API 요청 실패. 응답 코드: " + response.code()); From 10aa2f007dd5e5895dc921da1c522d5e28b6fd0d Mon Sep 17 00:00:00 2001 From: ghdeo Date: Thu, 19 Oct 2023 16:14:53 +0900 Subject: [PATCH 5/5] =?UTF-8?q?[FEAT]=20=ED=88=AC=EC=9E=90=EC=9E=90=20?= =?UTF-8?q?=EC=84=B1=ED=96=A5=EB=B3=84=20=EC=A2=85=EB=AA=A9=20=EC=B6=94?= =?UTF-8?q?=EC=B2=9C=20=EC=84=9C=EB=B9=84=EC=8A=A4(#78)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../stock/controller/StockController.java | 16 ++++++++++++++-- .../bull4jo/kkanbustock/stock/domain/Stock.java | 6 ++++++ .../repository/RecommendStockRepository.java | 6 ++++++ .../kkanbustock/stock/service/StockService.java | 13 +++++++++++++ 4 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/bull4jo/kkanbustock/stock/controller/StockController.java b/src/main/java/com/bull4jo/kkanbustock/stock/controller/StockController.java index 6241af9..2e9ec79 100644 --- a/src/main/java/com/bull4jo/kkanbustock/stock/controller/StockController.java +++ b/src/main/java/com/bull4jo/kkanbustock/stock/controller/StockController.java @@ -1,5 +1,6 @@ package com.bull4jo.kkanbustock.stock.controller; +import com.bull4jo.kkanbustock.member.domain.entity.InvestorType; import com.bull4jo.kkanbustock.stock.domain.RecommendStockResponse; import com.bull4jo.kkanbustock.stock.domain.Stock; import com.bull4jo.kkanbustock.stock.service.StockService; @@ -28,11 +29,22 @@ public ResponseEntity searchStockByName(@RequestParam(value = "itmsNm") f } @GetMapping("/v1/recommends") - public ResponseEntity> getRecommendedStocks(@RequestParam(value = "page", defaultValue = "0") int page, - @RequestParam(value = "size", defaultValue = "15") int size) { + public ResponseEntity> getRecommendedStocks( + @RequestParam(value = "page", defaultValue = "0") int page, + @RequestParam(value = "size", defaultValue = "15") int size) { Pageable pageable = PageRequest.of(page, size); return ResponseEntity.ok(stockService.getRecommendedStocks(pageable)); } + + @GetMapping("/v1/recommends/{investorType}") + public ResponseEntity> getRecommendStocksByInvestorType( + @PathVariable InvestorType investorType, + @RequestParam(value = "page", defaultValue = "0") int page, + @RequestParam(value = "size", defaultValue = "15") int size) { + Pageable pageable = PageRequest.of(page, size); + return ResponseEntity.ok(stockService.getRecommendStocksByInvestorType(investorType, pageable)); + } + @GetMapping("/v1/init-stocks") public void setInitStock() { stockService.setStockRepository(); diff --git a/src/main/java/com/bull4jo/kkanbustock/stock/domain/Stock.java b/src/main/java/com/bull4jo/kkanbustock/stock/domain/Stock.java index 885df3b..c235f2a 100644 --- a/src/main/java/com/bull4jo/kkanbustock/stock/domain/Stock.java +++ b/src/main/java/com/bull4jo/kkanbustock/stock/domain/Stock.java @@ -78,6 +78,12 @@ public class Stock { @Column private long mrktTotAmt; + public Stock(RecommendStock recommendStock) { + this.srtnCd = recommendStock.getSrtnCd(); + this.itmsNm = recommendStock.getItmsNm(); + this.clpr = recommendStock.getClpr(); + } + public void update(int clpr, int mkp) { this.clpr = clpr; this.mkp = mkp; diff --git a/src/main/java/com/bull4jo/kkanbustock/stock/repository/RecommendStockRepository.java b/src/main/java/com/bull4jo/kkanbustock/stock/repository/RecommendStockRepository.java index 1a9f9a3..6f90941 100644 --- a/src/main/java/com/bull4jo/kkanbustock/stock/repository/RecommendStockRepository.java +++ b/src/main/java/com/bull4jo/kkanbustock/stock/repository/RecommendStockRepository.java @@ -1,12 +1,18 @@ package com.bull4jo.kkanbustock.stock.repository; +import com.bull4jo.kkanbustock.member.domain.entity.InvestorType; import com.bull4jo.kkanbustock.stock.domain.RecommendStock; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +import java.util.Optional; + @Repository public interface RecommendStockRepository extends JpaRepository { + @Override Page findAll(Pageable pageable); + + Optional> findByInvestorType(InvestorType investorType, Pageable pageable); } diff --git a/src/main/java/com/bull4jo/kkanbustock/stock/service/StockService.java b/src/main/java/com/bull4jo/kkanbustock/stock/service/StockService.java index 8d7f3e4..6dc0eeb 100644 --- a/src/main/java/com/bull4jo/kkanbustock/stock/service/StockService.java +++ b/src/main/java/com/bull4jo/kkanbustock/stock/service/StockService.java @@ -23,6 +23,7 @@ import java.util.List; import java.util.Optional; import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; // 주식 넣고, 빼고, 업데이트, 가져오고, 있는지 확인하고 @Service @@ -301,6 +302,18 @@ public Page getRecommendedStocks(Pageable pageable) { return new PageImpl<>(recommends, pageable, page.getTotalElements()); } + @Transactional + public Page getRecommendStocksByInvestorType(InvestorType investorType, Pageable pageable) { + Page page = recommendStockRepository.findByInvestorType(investorType, pageable).orElseThrow(() -> new CustomException(ErrorCode.RECOMMENDED_STOCK_NOT_FOUND)); + List recommends = page + .getContent() + .stream() + .map(RecommendStockResponse::new) + .collect(Collectors.toList()); + + return new PageImpl<>(recommends, pageable, page.getTotalElements()); + } + @Transactional public String update(final String strnCd, final Stock req) { Stock stock = stockRepository.findById(strnCd).orElseThrow(() -> new CustomException(ErrorCode.STOCK_NOT_FOUND));