Skip to content

Commit

Permalink
fix: report page error
Browse files Browse the repository at this point in the history
  • Loading branch information
rabbit-22 committed Sep 5, 2023
1 parent 748aa60 commit 0a02801
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,37 @@
import org.springframework.data.repository.query.Param;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

public interface HealthRepository extends JpaRepository<HealthInfo, Long> {
@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<HealthInfo> findByDay(
@Param("memberEntity") MemberEntity memberEntity,
@Param("date") LocalDate date
);

@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<HealthInfo> 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<HealthInfo> 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<HealthInfo> findByDateTimeRange(
@Param("memberEntity") MemberEntity memberEntity,
@Param("startOfDay") LocalDateTime startOfDay,
@Param("endOfDay") LocalDateTime endOfDay
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -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<HealthInfo> 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<HealthInfo> infoList = healthRepository.findByWeek(memberEntity, startOfWeek, endOfWeek);

if (infoList == null) {
infoList = new ArrayList<>();
}

Map<WeekOfDay, List<HealthInfo>> 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<DayInfo> 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<DayInfo> 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<HealthInfo> 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)
Expand Down

0 comments on commit 0a02801

Please sign in to comment.