Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #37

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
@RestController
@RequestMapping("api/v1/members")
@RequiredArgsConstructor
@CrossOrigin(origins = "http://localhost:5173", allowedHeaders = "*")
@CrossOrigin(origins = "http://localhost", allowedHeaders = "*")
public class MemberController {

private final MemberService service;
Expand Down
Loading