From 0a02801faffe7509a8c9d7bab84732e97a2b592f Mon Sep 17 00:00:00 2001 From: rabbit-22 Date: Wed, 6 Sep 2023 01:34:27 +0900 Subject: [PATCH] fix: report page error --- .../domain/health/dao/HealthRepository.java | 18 ++++-- .../domain/health/service/GraphService.java | 62 +++++++++++++------ 2 files changed, 55 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/tukorea/turtleneck/backend/domain/health/dao/HealthRepository.java b/src/main/java/com/tukorea/turtleneck/backend/domain/health/dao/HealthRepository.java index 7f0a350..ff165a6 100644 --- a/src/main/java/com/tukorea/turtleneck/backend/domain/health/dao/HealthRepository.java +++ b/src/main/java/com/tukorea/turtleneck/backend/domain/health/dao/HealthRepository.java @@ -7,11 +7,12 @@ import org.springframework.data.repository.query.Param; import java.time.LocalDate; +import java.time.LocalDateTime; import java.util.List; public interface HealthRepository extends JpaRepository { @Query("SELECT info FROM HealthInfo info WHERE info.memberEntity = :memberEntity " + - "AND DAYOFWEEK(info.createdAt) = :date " + + "AND DAYOFWEEK(info.createdAt) = DAYOFWEEK(:date) " + "AND info.isActive = true") List findByDay( @Param("memberEntity") MemberEntity memberEntity, @@ -19,17 +20,24 @@ List findByDay( ); @Query("SELECT info FROM HealthInfo info WHERE info.memberEntity = :memberEntity " + - "AND DAYOFWEEK(info.createdAt) >= :startOfWeek " + - "AND DAYOFWEEK(info.createdAt) <= :endOfWeek " + + "AND info.createdAt >= :startOfWeek " + + "AND info.createdAt <= :endOfWeek " + "AND info.isActive = true") List findByWeek( @Param("memberEntity") MemberEntity memberEntity, - @Param("startOfWeek") LocalDate startOfWeek, - @Param("endOfWeek") LocalDate endOfWeek + @Param("startOfWeek") LocalDateTime startOfWeek, + @Param("endOfWeek") LocalDateTime endOfWeek ); @Query("SELECT info FROM HealthInfo info WHERE info.memberEntity = :member " + "AND YEAR(info.createdAt) = :year " + "AND info.isActive = true") List findByMemberEntityAndYear(@Param("member") MemberEntity member, @Param("year") int year); + + @Query("SELECT h FROM HealthInfo h WHERE h.memberEntity = :memberEntity AND h.createdAt BETWEEN :startOfDay AND :endOfDay") + List findByDateTimeRange( + @Param("memberEntity") MemberEntity memberEntity, + @Param("startOfDay") LocalDateTime startOfDay, + @Param("endOfDay") LocalDateTime endOfDay + ); } diff --git a/src/main/java/com/tukorea/turtleneck/backend/domain/health/service/GraphService.java b/src/main/java/com/tukorea/turtleneck/backend/domain/health/service/GraphService.java index 33ffdd8..388f8e5 100644 --- a/src/main/java/com/tukorea/turtleneck/backend/domain/health/service/GraphService.java +++ b/src/main/java/com/tukorea/turtleneck/backend/domain/health/service/GraphService.java @@ -15,6 +15,7 @@ import java.time.DayOfWeek; import java.time.LocalDate; +import java.time.LocalDateTime; import java.time.Month; import java.time.temporal.TemporalAdjusters; import java.util.*; @@ -27,51 +28,72 @@ public class GraphService { private final MemberRepository memberRepository; public DayGraphInfo getDayGraphInfo(LocalDate date, String nickname){ + try{ MemberEntity memberEntity = memberRepository.findMemberEntityByNickname(nickname) .orElseThrow(NotFoundMemberException::new); List infoList = healthRepository.findByDay(memberEntity, date); long portion = GraphTool.calculatePortion(infoList); return DayGraphInfo.builder().portion(portion).build(); + } catch (Exception e) { + e.printStackTrace(); + } + return null; } public WeekGraphInfo getWeekGraphInfo(LocalDate date, String nickname) { + try { MemberEntity memberEntity = memberRepository.findMemberEntityByNickname(nickname) .orElseThrow(NotFoundMemberException::new); - LocalDate startOfWeek = date.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)); - LocalDate endOfWeek = date.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)); + + LocalDateTime startOfWeek = date.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)).atStartOfDay(); + LocalDateTime endOfWeek = date.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)).atTime(23, 59, 59); + List infoList = healthRepository.findByWeek(memberEntity, startOfWeek, endOfWeek); + if (infoList == null) { infoList = new ArrayList<>(); } + Map> map = new EnumMap<>(WeekOfDay.class); for (WeekOfDay day : WeekOfDay.values()) { map.put(day, new ArrayList<>()); } - for(HealthInfo info : infoList) { - switch (info.getCreatedAt().getDayOfWeek()) { - case MONDAY -> GraphTool.addHealthInfo(map, WeekOfDay.MONDAY, info); - case TUESDAY -> GraphTool.addHealthInfo(map, WeekOfDay.TUESDAY, info); - case WEDNESDAY -> GraphTool.addHealthInfo(map, WeekOfDay.WEDNESDAY, info); - case THURSDAY -> GraphTool.addHealthInfo(map, WeekOfDay.THURSDAY, info); - case FRIDAY -> GraphTool.addHealthInfo(map, WeekOfDay.FRIDAY, info); - case SATURDAY -> GraphTool.addHealthInfo(map, WeekOfDay.SATURDAY, info); - case SUNDAY -> GraphTool.addHealthInfo(map, WeekOfDay.SUNDAY, info); - } + + for (HealthInfo info : infoList) { + WeekOfDay day = WeekOfDay.valueOf(info.getCreatedAt().getDayOfWeek().name()); + GraphTool.addHealthInfo(map, day, info); } + return new WeekGraphInfo(map); + + } catch (Exception e) { + e.printStackTrace(); } + return null; +} - public MonthGraphInfo getMonthGraphInfo(LocalDate date, String nickname){ - MemberEntity memberEntity = memberRepository.findMemberEntityByNickname(nickname) - .orElseThrow(NotFoundMemberException::new); - List infoList = new ArrayList<>(); - for(int i = 1; i <= date.lengthOfMonth(); i++){ - LocalDate nowDate = date.withDayOfMonth(i); - double average = GraphTool.calculateAverage(healthRepository.findByDay(memberEntity, nowDate)); +public MonthGraphInfo getMonthGraphInfo(LocalDate date, String nickname) { + MemberEntity memberEntity = memberRepository.findMemberEntityByNickname(nickname) + .orElseThrow(NotFoundMemberException::new); + List infoList = new ArrayList<>(); + for (int i = 1; i <= date.lengthOfMonth(); i++) { + LocalDate nowDate = date.withDayOfMonth(i); + LocalDateTime startOfDay = nowDate.atStartOfDay(); + LocalDateTime endOfDay = nowDate.atTime(23, 59, 59); + + // Use the new findByDateTimeRange method + List healthInfos = healthRepository.findByDateTimeRange(memberEntity, startOfDay, endOfDay); + + if (healthInfos == null || healthInfos.isEmpty()) { + infoList.add(DayInfo.builder().x(i).y(0L).build()); + } else { + double average = GraphTool.calculateAverage(healthInfos); infoList.add(DayInfo.builder().x(i).y((long) average).build()); } - return MonthGraphInfo.builder().infoList(infoList).build(); } + return MonthGraphInfo.builder().infoList(infoList).build(); +} + public YearGraphInfo getYearGraphInfo(LocalDate date, String nickname){ MemberEntity memberEntity = memberRepository.findMemberEntityByNickname(nickname)