From 586190ec7446f84f8d2f8bc016bbdb2509c76f7e Mon Sep 17 00:00:00 2001 From: jungeun-e Date: Mon, 26 Aug 2024 16:29:40 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=EC=A7=80=EB=82=9C=207=EC=9D=BC?= =?UTF-8?q?=EA=B0=84=20=EB=82=B4=EC=97=AD=20=EC=A1=B0=ED=9A=8C=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/BreathingController.java | 16 ++++++++++++ .../repository/BreathingRepository.java | 7 +++++- .../breathing/service/BreathingService.java | 25 +++++++++++++++++-- 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/hsu/shimpyoo/domain/breathing/controller/BreathingController.java b/src/main/java/com/hsu/shimpyoo/domain/breathing/controller/BreathingController.java index 7d47a64..1ef58c5 100644 --- a/src/main/java/com/hsu/shimpyoo/domain/breathing/controller/BreathingController.java +++ b/src/main/java/com/hsu/shimpyoo/domain/breathing/controller/BreathingController.java @@ -7,9 +7,11 @@ import com.hsu.shimpyoo.global.response.CustomAPIResponse; import com.hsu.shimpyoo.global.security.jwt.util.AuthenticationUserUtils; import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.web.bind.annotation.*; +import java.util.List; import java.util.Map; @RestController @@ -31,4 +33,18 @@ public CustomAPIResponse> getTodayBreathingResult( return breathingService.calculateBreathingResult(dto, user); } + + // 오늘을 기준으로 지난 7일간의 쉼 결과 조회 + @GetMapping("/todayWeekly") + public ResponseEntity> getTodayBreathingWeekly() { + // 현재 로그인된 사용자 정보 가져오기 + String loginId = authenticationUserUtils.getCurrentUserId(); + User user = userRepository.findByLoginId(loginId) + .orElseThrow(() -> new UsernameNotFoundException("해당 사용자가 존재하지 않습니다.")); + + // 지난 7일간의 breathingRate 가져오기 + List> weeklyBreathingRates = breathingService.getWeeklyBreathingRates(user); + + return ResponseEntity.ok(CustomAPIResponse.createSuccess(200, weeklyBreathingRates, "지난 7일간의 최대호기량 조회에 성공했습니다.")); + } } diff --git a/src/main/java/com/hsu/shimpyoo/domain/breathing/repository/BreathingRepository.java b/src/main/java/com/hsu/shimpyoo/domain/breathing/repository/BreathingRepository.java index f8dbea5..2f9d215 100644 --- a/src/main/java/com/hsu/shimpyoo/domain/breathing/repository/BreathingRepository.java +++ b/src/main/java/com/hsu/shimpyoo/domain/breathing/repository/BreathingRepository.java @@ -4,7 +4,12 @@ import com.hsu.shimpyoo.domain.user.entity.User; import org.springframework.data.jpa.repository.JpaRepository; +import java.time.LocalDateTime; +import java.util.Optional; + public interface BreathingRepository extends JpaRepository { // UserId로 가장 최근의 Breathing 데이터를 가져오는 메서드 - Breathing findTopByUserIdOrderByCreatedAtDesc(User userId); + Breathing findTopByUserIdOrderByCreatedAtDesc(User user); + // 지난 7일간 내역 조회 + Optional findTopByUserIdAndCreatedAtBetweenOrderByCreatedAtDesc(User user, LocalDateTime startOfDay, LocalDateTime endOfDay); } diff --git a/src/main/java/com/hsu/shimpyoo/domain/breathing/service/BreathingService.java b/src/main/java/com/hsu/shimpyoo/domain/breathing/service/BreathingService.java index 6b09a25..aa6cffd 100644 --- a/src/main/java/com/hsu/shimpyoo/domain/breathing/service/BreathingService.java +++ b/src/main/java/com/hsu/shimpyoo/domain/breathing/service/BreathingService.java @@ -6,8 +6,9 @@ import com.hsu.shimpyoo.global.response.CustomAPIResponse; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; -import java.util.LinkedHashMap; -import java.util.Map; + +import java.time.LocalDateTime; +import java.util.*; @Service @RequiredArgsConstructor @@ -72,4 +73,24 @@ public CustomAPIResponse> calculateBreathingResult(Breathing return CustomAPIResponse.createSuccess(200, responseData, "오늘의 쉼 결과 조회에 성공했습니다."); } + + public List> getWeeklyBreathingRates(User user) { + LocalDateTime today = LocalDateTime.now(); + List> weeklyBreathingRates = new ArrayList<>(); + + for (int i = 6; i >= 0; i--) { + LocalDateTime targetDate = today.minusDays(i); + LocalDateTime startOfDay = targetDate.withHour(0).withMinute(0).withSecond(0).withNano(0); + LocalDateTime endOfDay = targetDate.withHour(23).withMinute(59).withSecond(59).withNano(999999999); + + Optional breathingOptional = breathingRepository.findTopByUserIdAndCreatedAtBetweenOrderByCreatedAtDesc(user, startOfDay, endOfDay); + + Map data = new HashMap<>(); + data.put("date", targetDate.toLocalDate()); + data.put("breathingRate", breathingOptional.map(Breathing::getBreathingRate).orElse(null)); + weeklyBreathingRates.add(data); + } + + return weeklyBreathingRates; + } }