Skip to content

Commit

Permalink
✨ 지난 7일간 내역 조회 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
eunxn committed Aug 26, 2024
1 parent 3d22fff commit 586190e
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 586190e

Please sign in to comment.