-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
โจ FEAT. ์ค๋์ ์ผ ๊ฒฐ๊ณผ ์กฐํ ๊ธฐ๋ฅ
- Loading branch information
Showing
5 changed files
with
138 additions
and
1 deletion.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
src/main/java/com/hsu/shimpyoo/domain/breathing/controller/BreathingController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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("/todayResult") | ||
public CustomAPIResponse<Map<String, Object>> getTodayBreathingResult( | ||
@RequestBody BreathingRequestDto dto) { | ||
// ํ์ฌ ๋ก๊ทธ์ธ๋ ์ฌ์ฉ์ ์ ๋ณด ๊ฐ์ ธ์ค๊ธฐ | ||
String loginId = authenticationUserUtils.getCurrentUserId(); | ||
User user = userRepository.findByLoginId(loginId) | ||
.orElseThrow(() -> new UsernameNotFoundException("ํด๋น ์ฌ์ฉ์๊ฐ ์กด์ฌํ์ง ์์ต๋๋ค.")); | ||
|
||
return breathingService.calculateBreathingResult(dto, user); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/hsu/shimpyoo/domain/breathing/dto/BreathingRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
src/main/java/com/hsu/shimpyoo/domain/breathing/repository/BreathingRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Breathing, Long> { | ||
// UserId๋ก ๊ฐ์ฅ ์ต๊ทผ์ Breathing ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๋ ๋ฉ์๋ | ||
Breathing findTopByUserIdOrderByCreatedAtDesc(User userId); | ||
} |
75 changes: 75 additions & 0 deletions
75
src/main/java/com/hsu/shimpyoo/domain/breathing/service/BreathingService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
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.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class BreathingService { | ||
private final BreathingRepository breathingRepository; | ||
|
||
public CustomAPIResponse<Map<String, Object>> calculateBreathingResult(BreathingRequestDto dto, User user) { | ||
// ๊ฐ์ฅ ์ต๊ทผ์ Breathing ๋ฐ์ดํฐ๋ฅผ userId๋ก ์กฐํ | ||
Breathing recentBreathing = breathingRepository.findTopByUserIdOrderByCreatedAtDesc(user); | ||
|
||
// ์ต๋ํธ๊ธฐ๋ ์ค์ | ||
Float maxBreathingRate = Math.max(dto.getFirst(), Math.max(dto.getSecond(), dto.getThird())); | ||
|
||
// ์๋ก์ด Breathing ๋ฐ์ดํฐ ์ ์ฅ | ||
Breathing breathing = Breathing.builder() | ||
.userId(user) | ||
.breathingRate(maxBreathingRate) | ||
.first(dto.getFirst()) | ||
.second(dto.getSecond()) | ||
.third(dto.getThird()) | ||
.build(); | ||
breathingRepository.save(breathing); | ||
|
||
// ์ด์ breathingRate ๊ฐ๊ณผ ๋น๊ตํ์ฌ ์ํ ๊ฒฐ์ | ||
String status; | ||
String rateChangeDirection = ""; // ์ฆ๊ฐ ๋๋ ๊ฐ์ ๋ฐฉํฅ | ||
int rateDifferencePercent = 0; | ||
|
||
if (recentBreathing != null) { | ||
Float previousBreathingRate = recentBreathing.getBreathingRate(); | ||
rateDifferencePercent = Math.round(((maxBreathingRate - previousBreathingRate) / previousBreathingRate) * 100); | ||
|
||
if (rateDifferencePercent >= 0) { | ||
rateChangeDirection = "์ฆ๊ฐ"; | ||
} else { | ||
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<String, Object> responseData = new LinkedHashMap<>(); | ||
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, "์ค๋์ ์ผ ๊ฒฐ๊ณผ ์กฐํ์ ์ฑ๊ณตํ์ต๋๋ค."); | ||
} | ||
} |