Skip to content

Commit

Permalink
Merge pull request #29 from shim-pyoo/feat/#27-todayShim-result
Browse files Browse the repository at this point in the history
โœจ FEAT. ์˜ค๋Š˜์˜ ์‰ผ ๊ฒฐ๊ณผ ์กฐํšŒ ๊ธฐ๋Šฅ
  • Loading branch information
eunxn authored Aug 26, 2024
2 parents 639d2aa + 4137f62 commit 037106b
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 1 deletion.
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);
}
}
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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ํšŒ์ฐจ
}

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);
}
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, "์˜ค๋Š˜์˜ ์‰ผ ๊ฒฐ๊ณผ ์กฐํšŒ์— ์„ฑ๊ณตํ–ˆ์Šต๋‹ˆ๋‹ค.");
}
}

0 comments on commit 037106b

Please sign in to comment.