From 2e273e0bd79c7d58b0a03539d1f248dc79fb0a39 Mon Sep 17 00:00:00 2001 From: jungeun-e Date: Sun, 25 Aug 2024 14:31:50 +0900 Subject: [PATCH 1/5] =?UTF-8?q?=F0=9F=8E=A8=20Breathing=20=EC=97=94?= =?UTF-8?q?=ED=8B=B0=ED=8B=B0=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shimpyoo/domain/breathing/entity/Breathing.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/hsu/shimpyoo/domain/breathing/entity/Breathing.java b/src/main/java/com/hsu/shimpyoo/domain/breathing/entity/Breathing.java index 05c7381..0a09cf5 100644 --- a/src/main/java/com/hsu/shimpyoo/domain/breathing/entity/Breathing.java +++ b/src/main/java/com/hsu/shimpyoo/domain/breathing/entity/Breathing.java @@ -21,7 +21,16 @@ public class Breathing extends BaseEntity { @JoinColumn(name = "user_id", nullable = false) private User userId; // 사용자 기본키 - @Column(name = "breathing_rate", nullable = false) + @Column(name = "breathing_rate") private Float breathingRate; // 호흡 수치 + + @Column(name = "first") + private Float first; // 1회차 + + @Column(name = "second") + private Float second; // 2회차 + + @Column(name = "third") + private Float third; // 3회차 } From e30cfc090b14722897cf84dbc258b89bd99a4c6b Mon Sep 17 00:00:00 2001 From: jungeun-e Date: Sun, 25 Aug 2024 14:54:16 +0900 Subject: [PATCH 2/5] =?UTF-8?q?=E2=9C=A8=20BreatingService=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../breathing/service/BreathingService.java | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/main/java/com/hsu/shimpyoo/domain/breathing/service/BreathingService.java 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 new file mode 100644 index 0000000..6c30cd7 --- /dev/null +++ b/src/main/java/com/hsu/shimpyoo/domain/breathing/service/BreathingService.java @@ -0,0 +1,50 @@ +package com.hsu.shimpyoo.domain.breathing.service; +import com.hsu.shimpyoo.domain.breathing.entity.Breathing; +import com.hsu.shimpyoo.domain.breathing.repository.BreathingRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +public class BreathingService { + private final BreathingRepository breathingRepository; + + public String evaluateBreathingRate(Long breathingId) { + // Breathing 엔티티를 ID로 조회 + Breathing oldBreathing = breathingRepository.findById(breathingId) + .orElseThrow(() -> new RuntimeException("호흡 기록을 찾을 수 없습니다.")); + + // first, second, third 중에서 최대값 계산 + Float maxBreathingRate = Math.max(oldBreathing.getFirst(), Math.max(oldBreathing.getSecond(), oldBreathing.getThird())); + + // 이전의 breathing_rate 가져오기 + Float previousBreathingRate = oldBreathing.getBreathingRate(); + + // 새로운 Breathing 객체 생성 및 데이터 복사 + Breathing newBreathing = Breathing.builder() + .userId(oldBreathing.getUserId()) + .first(oldBreathing.getFirst()) + .second(oldBreathing.getSecond()) + .third(oldBreathing.getThird()) + .breathingRate(maxBreathingRate) + .build(); + + // 새로운 기록을 데이터베이스에 저장 + breathingRepository.save(newBreathing); + + // 비교를 기반으로 상태 결정 + if (previousBreathingRate == null) { + return "이전 데이터 없음"; + } else { + float percentage = (maxBreathingRate / previousBreathingRate) * 100; + if (percentage >= 80) { + return "안정"; + } else if (percentage >= 60) { + return "주의"; + } else { + return "위험"; + } + } + } + +} From fc36152cc78ecbb3037920963c738102bd8fd04e Mon Sep 17 00:00:00 2001 From: jungeun-e Date: Sun, 25 Aug 2024 18:02:06 +0900 Subject: [PATCH 3/5] =?UTF-8?q?=E2=9C=A8=20=EC=98=A4=EB=8A=98=EC=9D=98=20?= =?UTF-8?q?=EC=89=BC=20=EA=B2=B0=EA=B3=BC=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 | 34 ++++++++ .../breathing/dto/BreathingRequestDto.java | 16 ++++ .../repository/BreathingRepository.java | 3 + .../breathing/service/BreathingService.java | 80 ++++++++++++------- 4 files changed, 105 insertions(+), 28 deletions(-) create mode 100644 src/main/java/com/hsu/shimpyoo/domain/breathing/controller/BreathingController.java create mode 100644 src/main/java/com/hsu/shimpyoo/domain/breathing/dto/BreathingRequestDto.java 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 new file mode 100644 index 0000000..2254c70 --- /dev/null +++ b/src/main/java/com/hsu/shimpyoo/domain/breathing/controller/BreathingController.java @@ -0,0 +1,34 @@ +package com.hsu.shimpyoo.domain.breathing.controller; + +import com.hsu.shimpyoo.domain.breathing.dto.BreathingRequestDto; +import com.hsu.shimpyoo.domain.breathing.service.BreathingService; +import com.hsu.shimpyoo.domain.user.entity.User; +import com.hsu.shimpyoo.domain.user.repository.UserRepository; +import com.hsu.shimpyoo.global.response.CustomAPIResponse; +import com.hsu.shimpyoo.global.security.jwt.util.AuthenticationUserUtils; +import lombok.RequiredArgsConstructor; +import org.springframework.security.core.userdetails.UsernameNotFoundException; +import org.springframework.web.bind.annotation.*; + +import java.util.Map; + +@RestController +@RequestMapping("/api/breathing") +@RequiredArgsConstructor +public class BreathingController { + private final BreathingService breathingService; + private final AuthenticationUserUtils authenticationUserUtils; + private final UserRepository userRepository; + + // 오늘의 쉼 결과 + @PostMapping("/today/result") + public CustomAPIResponse> getTodayBreathingResult( + @RequestBody BreathingRequestDto dto) { + // 현재 로그인된 사용자 정보 가져오기 + String loginId = authenticationUserUtils.getCurrentUserId(); + User user = userRepository.findByLoginId(loginId) + .orElseThrow(() -> new UsernameNotFoundException("해당 사용자가 존재하지 않습니다.")); + + return breathingService.calculateBreathingResult(dto, user); + } +} diff --git a/src/main/java/com/hsu/shimpyoo/domain/breathing/dto/BreathingRequestDto.java b/src/main/java/com/hsu/shimpyoo/domain/breathing/dto/BreathingRequestDto.java new file mode 100644 index 0000000..5041986 --- /dev/null +++ b/src/main/java/com/hsu/shimpyoo/domain/breathing/dto/BreathingRequestDto.java @@ -0,0 +1,16 @@ +package com.hsu.shimpyoo.domain.breathing.dto; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +public class BreathingRequestDto { + private Float first; + private Float second; + private Float third; +} 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 eb8dfb1..f8dbea5 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 @@ -1,7 +1,10 @@ package com.hsu.shimpyoo.domain.breathing.repository; import com.hsu.shimpyoo.domain.breathing.entity.Breathing; +import com.hsu.shimpyoo.domain.user.entity.User; import org.springframework.data.jpa.repository.JpaRepository; public interface BreathingRepository extends JpaRepository { + // UserId로 가장 최근의 Breathing 데이터를 가져오는 메서드 + Breathing findTopByUserIdOrderByCreatedAtDesc(User userId); } 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 6c30cd7..e213f0d 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 @@ -1,50 +1,74 @@ package com.hsu.shimpyoo.domain.breathing.service; +import com.hsu.shimpyoo.domain.breathing.dto.BreathingRequestDto; import com.hsu.shimpyoo.domain.breathing.entity.Breathing; import com.hsu.shimpyoo.domain.breathing.repository.BreathingRepository; +import com.hsu.shimpyoo.domain.user.entity.User; +import com.hsu.shimpyoo.global.response.CustomAPIResponse; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; + @Service @RequiredArgsConstructor public class BreathingService { private final BreathingRepository breathingRepository; - public String evaluateBreathingRate(Long breathingId) { - // Breathing 엔티티를 ID로 조회 - Breathing oldBreathing = breathingRepository.findById(breathingId) - .orElseThrow(() -> new RuntimeException("호흡 기록을 찾을 수 없습니다.")); - - // first, second, third 중에서 최대값 계산 - Float maxBreathingRate = Math.max(oldBreathing.getFirst(), Math.max(oldBreathing.getSecond(), oldBreathing.getThird())); + public CustomAPIResponse> calculateBreathingResult(BreathingRequestDto dto, User user) { + // 가장 최근의 Breathing 데이터를 userId로 조회 + Breathing recentBreathing = breathingRepository.findTopByUserIdOrderByCreatedAtDesc(user); - // 이전의 breathing_rate 가져오기 - Float previousBreathingRate = oldBreathing.getBreathingRate(); + // 최대호기량 설정 + Float maxBreathingRate = Math.max(dto.getFirst(), Math.max(dto.getSecond(), dto.getThird())); - // 새로운 Breathing 객체 생성 및 데이터 복사 - Breathing newBreathing = Breathing.builder() - .userId(oldBreathing.getUserId()) - .first(oldBreathing.getFirst()) - .second(oldBreathing.getSecond()) - .third(oldBreathing.getThird()) + // 새로운 Breathing 데이터 저장 + Breathing breathing = Breathing.builder() + .userId(user) .breathingRate(maxBreathingRate) + .first(dto.getFirst()) + .second(dto.getSecond()) + .third(dto.getThird()) .build(); + breathingRepository.save(breathing); - // 새로운 기록을 데이터베이스에 저장 - breathingRepository.save(newBreathing); + // 이전 breathingRate 값과 비교하여 상태 결정 + String status; + String rateChangeDirection = ""; // 증가 또는 감소 방향 + int rateDifferencePercent = 0; - // 비교를 기반으로 상태 결정 - if (previousBreathingRate == null) { - return "이전 데이터 없음"; - } else { - float percentage = (maxBreathingRate / previousBreathingRate) * 100; - if (percentage >= 80) { - return "안정"; - } else if (percentage >= 60) { - return "주의"; + if (recentBreathing != null) { + Float previousBreathingRate = recentBreathing.getBreathingRate(); + rateDifferencePercent = Math.round(((maxBreathingRate - previousBreathingRate) / previousBreathingRate) * 100); + + if (rateDifferencePercent >= 0) { + rateChangeDirection = "증가"; } else { - return "위험"; + rateChangeDirection = "감소"; + rateDifferencePercent = Math.abs(rateDifferencePercent); // 절대값으로 변환 } + + // 상태 결정 로직 수정 + float rateChange = ((float) maxBreathingRate / previousBreathingRate) * 100; + if (rateChange >= 80) { + status = "안정"; + } else if (rateChange >= 60) { + status = "주의"; + } else { + status = "위험"; + } + } else { + // 이전 데이터가 없을 때 + return CustomAPIResponse.createFailWithout(404, "이전 데이터를 찾을 수 없습니다."); } - } + // 반환 데이터 + Map responseData = new LinkedHashMap<>(); + responseData.put("status", status); + responseData.put("breathingRate", maxBreathingRate); + responseData.put("rateDifference", rateDifferencePercent + "% " + rateChangeDirection); + + return CustomAPIResponse.createSuccess(200, responseData, "오늘의 쉼 결과 조회에 성공했습니다."); + } } From f7681b795b1f16de60638929fddc4b6d5a199d6d Mon Sep 17 00:00:00 2001 From: jungeun-e Date: Mon, 26 Aug 2024 14:57:28 +0900 Subject: [PATCH 4/5] =?UTF-8?q?=E2=9C=A8=20=EC=98=A4=EB=8A=98=EC=9D=98=20?= =?UTF-8?q?=EC=89=BC=20=EA=B2=B0=EA=B3=BC=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 --- .../shimpyoo/domain/breathing/service/BreathingService.java | 3 +++ 1 file changed, 3 insertions(+) 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 e213f0d..c65b1fc 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 @@ -68,6 +68,9 @@ public CustomAPIResponse> calculateBreathingResult(Breathing responseData.put("status", status); responseData.put("breathingRate", maxBreathingRate); responseData.put("rateDifference", rateDifferencePercent + "% " + rateChangeDirection); + responseData.put("first", breathing.getFirst()); + responseData.put("second", breathing.getSecond()); + responseData.put("third", breathing.getThird()); return CustomAPIResponse.createSuccess(200, responseData, "오늘의 쉼 결과 조회에 성공했습니다."); } From 4137f621c07342653180349d7f161ca93b30b725 Mon Sep 17 00:00:00 2001 From: jungeun-e Date: Mon, 26 Aug 2024 15:01:38 +0900 Subject: [PATCH 5/5] =?UTF-8?q?=F0=9F=8E=A8=20=EC=9A=94=EC=B2=AD=20?= =?UTF-8?q?=EA=B2=BD=EB=A1=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/breathing/controller/BreathingController.java | 2 +- .../hsu/shimpyoo/domain/breathing/service/BreathingService.java | 2 -- 2 files changed, 1 insertion(+), 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 2254c70..7d47a64 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 @@ -21,7 +21,7 @@ public class BreathingController { private final UserRepository userRepository; // 오늘의 쉼 결과 - @PostMapping("/today/result") + @PostMapping("/todayResult") public CustomAPIResponse> getTodayBreathingResult( @RequestBody BreathingRequestDto dto) { // 현재 로그인된 사용자 정보 가져오기 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 c65b1fc..6b09a25 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,6 @@ import com.hsu.shimpyoo.global.response.CustomAPIResponse; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; - -import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map;