Skip to content

Commit

Permalink
Merge pull request #30 from shim-pyoo/feat/#28-todayWeekly
Browse files Browse the repository at this point in the history
โœจ FEAT. ์˜ค๋Š˜์˜ ์‰ผ ์ง€๋‚œ 7์ผ๊ฐ„ ์ธก์ • ๋‚ด์—ญ
  • Loading branch information
eunxn authored Aug 26, 2024
2 parents 3d22fff + 586190e commit bdb836e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -31,4 +33,18 @@ public CustomAPIResponse<Map<String, Object>> getTodayBreathingResult(

return breathingService.calculateBreathingResult(dto, user);
}

// ์˜ค๋Š˜์„ ๊ธฐ์ค€์œผ๋กœ ์ง€๋‚œ 7์ผ๊ฐ„์˜ ์‰ผ ๊ฒฐ๊ณผ ์กฐํšŒ
@GetMapping("/todayWeekly")
public ResponseEntity<CustomAPIResponse<?>> getTodayBreathingWeekly() {
// ํ˜„์žฌ ๋กœ๊ทธ์ธ๋œ ์‚ฌ์šฉ์ž ์ •๋ณด ๊ฐ€์ ธ์˜ค๊ธฐ
String loginId = authenticationUserUtils.getCurrentUserId();
User user = userRepository.findByLoginId(loginId)
.orElseThrow(() -> new UsernameNotFoundException("ํ•ด๋‹น ์‚ฌ์šฉ์ž๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."));

// ์ง€๋‚œ 7์ผ๊ฐ„์˜ breathingRate ๊ฐ€์ ธ์˜ค๊ธฐ
List<Map<String, Object>> weeklyBreathingRates = breathingService.getWeeklyBreathingRates(user);

return ResponseEntity.ok(CustomAPIResponse.createSuccess(200, weeklyBreathingRates, "์ง€๋‚œ 7์ผ๊ฐ„์˜ ์ตœ๋Œ€ํ˜ธ๊ธฐ๋Ÿ‰ ์กฐํšŒ์— ์„ฑ๊ณตํ–ˆ์Šต๋‹ˆ๋‹ค."));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Breathing, Long> {
// UserId๋กœ ๊ฐ€์žฅ ์ตœ๊ทผ์˜ Breathing ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๋ฉ”์„œ๋“œ
Breathing findTopByUserIdOrderByCreatedAtDesc(User userId);
Breathing findTopByUserIdOrderByCreatedAtDesc(User user);
// ์ง€๋‚œ 7์ผ๊ฐ„ ๋‚ด์—ญ ์กฐํšŒ
Optional<Breathing> findTopByUserIdAndCreatedAtBetweenOrderByCreatedAtDesc(User user, LocalDateTime startOfDay, LocalDateTime endOfDay);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -72,4 +73,24 @@ public CustomAPIResponse<Map<String, Object>> calculateBreathingResult(Breathing

return CustomAPIResponse.createSuccess(200, responseData, "์˜ค๋Š˜์˜ ์‰ผ ๊ฒฐ๊ณผ ์กฐํšŒ์— ์„ฑ๊ณตํ–ˆ์Šต๋‹ˆ๋‹ค.");
}

public List<Map<String, Object>> getWeeklyBreathingRates(User user) {
LocalDateTime today = LocalDateTime.now();
List<Map<String, Object>> 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<Breathing> breathingOptional = breathingRepository.findTopByUserIdAndCreatedAtBetweenOrderByCreatedAtDesc(user, startOfDay, endOfDay);

Map<String, Object> data = new HashMap<>();
data.put("date", targetDate.toLocalDate());
data.put("breathingRate", breathingOptional.map(Breathing::getBreathingRate).orElse(null));
weeklyBreathingRates.add(data);
}

return weeklyBreathingRates;
}
}

0 comments on commit bdb836e

Please sign in to comment.