From eabdc8fd287e010c980c121823bb6f7c835107c0 Mon Sep 17 00:00:00 2001 From: 13wjdgk <13wjdgk@naver.com> Date: Sat, 25 May 2024 23:03:17 +0900 Subject: [PATCH] =?UTF-8?q?refactor=20:=20findHealthMetricChart=20?= =?UTF-8?q?=EB=A1=9C=EC=A7=81=20=EC=88=98=EC=A0=95=20(=EC=9D=BC=EC=A0=95?= =?UTF-8?q?=20=EA=B8=B0=EA=B0=84=20=EB=82=B4=20=EC=82=AC=EC=9A=A9=EC=9E=90?= =?UTF-8?q?=EC=9D=98=20=EB=AA=A8=EB=93=A0=20=EA=B1=B4=EA=B0=95=EC=A7=80?= =?UTF-8?q?=ED=91=9C=20=EC=A1=B0=ED=9A=8C=20=ED=9B=84,=20=EB=82=B4?= =?UTF-8?q?=EB=B6=80=20=EB=A9=94=EC=84=9C=EB=93=9C=EB=A1=9C=20=ED=98=88?= =?UTF-8?q?=EB=8B=B9,=EC=B2=B4=EC=A4=91,=EA=B1=B8=EC=9D=8C=EC=88=98,?= =?UTF-8?q?=EC=86=8C=EB=AA=A8=20=EC=B9=BC=EB=A1=9C=EB=A6=AC=20=EA=B5=AC?= =?UTF-8?q?=EB=B6=84=ED=95=B4=EC=84=9C=20Response=20=EB=8D=B0=EC=9D=B4?= =?UTF-8?q?=ED=84=B0=EB=A1=9C=20=EA=B0=80=EA=B3=B5)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/HealthMetricRepository.java | 13 ++++ .../HealthMetricChartSearchService.java | 59 ++++++++----------- .../service/HealthMetricSearchService.java | 12 ++++ 3 files changed, 50 insertions(+), 34 deletions(-) diff --git a/src/main/java/com/coniverse/dangjang/domain/healthmetric/repository/HealthMetricRepository.java b/src/main/java/com/coniverse/dangjang/domain/healthmetric/repository/HealthMetricRepository.java index 986285ba..92b814b9 100644 --- a/src/main/java/com/coniverse/dangjang/domain/healthmetric/repository/HealthMetricRepository.java +++ b/src/main/java/com/coniverse/dangjang/domain/healthmetric/repository/HealthMetricRepository.java @@ -89,4 +89,17 @@ public interface HealthMetricRepository extends JpaRepository findAllHealthMetricByDate(String oauthId, LocalDate startDate, LocalDate endDate); + } diff --git a/src/main/java/com/coniverse/dangjang/domain/healthmetric/service/HealthMetricChartSearchService.java b/src/main/java/com/coniverse/dangjang/domain/healthmetric/service/HealthMetricChartSearchService.java index 0aca392d..4393e9f5 100644 --- a/src/main/java/com/coniverse/dangjang/domain/healthmetric/service/HealthMetricChartSearchService.java +++ b/src/main/java/com/coniverse/dangjang/domain/healthmetric/service/HealthMetricChartSearchService.java @@ -46,33 +46,30 @@ public class HealthMetricChartSearchService { */ public HealthMetricChartResponse findHealthMetricChart(String oauthId, LocalDate startDate, LocalDate endDate) { - List bloodSugarMinMaxes = findBloodSugarMinMaxes(oauthId, startDate, endDate); - List weights = findWeights(oauthId, startDate, endDate); - List stepCounts = findStepCounts(oauthId, startDate, endDate); + List healthMetrics = healthMetricSearchService.findAllHealthMetricByDate(oauthId, startDate, endDate); + List bloodSugarMinMaxes = findBloodSugarMinMaxes(healthMetrics); + List weights = findWeights(healthMetrics); + List stepCounts = findStepCounts(healthMetrics); List exerciseCalories = findExerciseCalories(oauthId, startDate, endDate); - return new HealthMetricChartResponse(startDate, endDate, bloodSugarMinMaxes, weights, stepCounts, exerciseCalories); } /** - * 기한 내의 혈당 최저,최고 값을 조회한다. + * 일정 기간 내의 혈당 최저,최고 값을 조회한다. * - * @param startDate 조회 시작 날짜 - * @param endDate 조회 종료 날짜 - * @param oauthId 유저 아이디 + * @param healthMetrics 일정 기간 내, 유저의 모든 건강지표 * @return List 혈당 최저,최고 값 리스트 * @since 1.0.0 */ - private List findBloodSugarMinMaxes(String oauthId, LocalDate startDate, LocalDate endDate) { + private List findBloodSugarMinMaxes(List healthMetrics) { List bloodSugarMinMaxes = new ArrayList<>(); - List bloodSugarHealthMetric = healthMetricSearchService.findWeeklyHealthMetricByGroupCode(oauthId, GroupCode.BLOOD_SUGAR, - startDate, endDate); - Map> bloodSugarMapByDate = bloodSugarHealthMetric.stream().collect(Collectors.groupingBy(HealthMetric::getCreatedAt)); - bloodSugarMapByDate.forEach((date, healthMetrics) -> { - int minBloodSugar = healthMetrics.stream().mapToInt(healthMetric -> Integer.parseInt(healthMetric.getUnit())).min().orElseThrow( + healthMetrics = healthMetrics.stream().filter(healthMetric -> GroupCode.BLOOD_SUGAR.equals(healthMetric.getGroupCode())).toList(); + Map> bloodSugarMapByDate = healthMetrics.stream().collect(Collectors.groupingBy(HealthMetric::getCreatedAt)); + bloodSugarMapByDate.forEach((date, dailyHealthMetrics) -> { + int minBloodSugar = dailyHealthMetrics.stream().mapToInt(healthMetric -> Integer.parseInt(healthMetric.getUnit())).min().orElseThrow( HealthMetricNotFoundException::new); - int maxBloodSugar = healthMetrics.stream().mapToInt(healthMetric -> Integer.parseInt(healthMetric.getUnit())).max().orElseThrow( + int maxBloodSugar = dailyHealthMetrics.stream().mapToInt(healthMetric -> Integer.parseInt(healthMetric.getUnit())).max().orElseThrow( HealthMetricNotFoundException::new); bloodSugarMinMaxes.add(new BloodSugarMinMax(date, minBloodSugar, maxBloodSugar)); @@ -81,39 +78,33 @@ private List findBloodSugarMinMaxes(String oauthId, LocalDate } /** - * 기한 내의 체중 값을 조회한다. + * 일정 기간 내의 체중 값을 조회한다. * - * @param startDate 조회 시작 날짜 - * @param endDate 조회 종료 날짜 - * @param oauthId 유저 아이디 + * @param healthMetrics 일정 기간 내, 유저의 모든 건강지표 * @return List 체중 값 리스트 * @since 1.0.0 */ - private List findWeights(String oauthId, LocalDate startDate, LocalDate endDate) { + private List findWeights(List healthMetrics) { List weights = new ArrayList<>(); - List healthMetrics = healthMetricSearchService.findWeeklyHealthMetricByGroupCode(oauthId, GroupCode.WEIGHT, startDate, endDate); - healthMetrics.forEach(healthMetric -> - weights.add(new HealthMetricChartData(healthMetric.getCreatedAt(), Integer.parseInt(healthMetric.getUnit()))) - ); + healthMetrics.stream().filter(healthMetric -> CommonCode.MEASUREMENT.equals(healthMetric.getType())) + .forEach(healthMetric -> weights.add(new HealthMetricChartData(healthMetric.getCreatedAt(), Integer.parseInt(healthMetric.getUnit())))); + return weights; } /** - * 기한 내의 걸음수를 조회한다. + * 일정 기간 내의 걸음수를 조회한다. * - * @param startDate 조회 시작 날짜 - * @param endDate 조회 종료 날짜 - * @param oauthId 유저 아이디 + * @param healthMetrics 일정 기간 내, 유저의 모든 건강지표 * @return List 걸음수 리스트 * @since 1.0.0 */ - private List findStepCounts(String oauthId, LocalDate startDate, LocalDate endDate) { + private List findStepCounts(List healthMetrics) { List stepCounts = new ArrayList<>(); - - List healthMetrics = healthMetricSearchService.findWeeklyHealthMetricById(oauthId, CommonCode.STEP_COUNT, startDate, endDate); - healthMetrics.forEach(healthMetric -> - stepCounts.add(new HealthMetricChartData(healthMetric.getCreatedAt(), Integer.parseInt(healthMetric.getUnit()))) - ); + healthMetrics.stream().filter(healthMetric -> CommonCode.STEP_COUNT.equals(healthMetric.getType())) + .forEach(healthMetric -> + stepCounts.add(new HealthMetricChartData(healthMetric.getCreatedAt(), Integer.parseInt(healthMetric.getUnit()))) + ); return stepCounts; } diff --git a/src/main/java/com/coniverse/dangjang/domain/healthmetric/service/HealthMetricSearchService.java b/src/main/java/com/coniverse/dangjang/domain/healthmetric/service/HealthMetricSearchService.java index d3a39b84..4fc266d5 100644 --- a/src/main/java/com/coniverse/dangjang/domain/healthmetric/service/HealthMetricSearchService.java +++ b/src/main/java/com/coniverse/dangjang/domain/healthmetric/service/HealthMetricSearchService.java @@ -103,4 +103,16 @@ public HealthMetricLastDateResponse findHealthMetricLastDate(String oauthId) { public int findByGroupCode(String oauthId, GroupCode groupCode, LocalDate createdAt) { return healthMetricRepository.findHealthMetricCountByGroupCode(oauthId, groupCode, createdAt); } + + /** + * 일정 기간 내 유저의 모든 건강 지표를 조회한다. + * + * @param oauthId 유저 PK + * @return HealthMetric 건강 지표 + * @since 1.0.0 + */ + public List findAllHealthMetricByDate(String oauthId, LocalDate startDate, LocalDate endDate) { + return healthMetricRepository.findAllHealthMetricByDate(oauthId, startDate, endDate); + } + }