Skip to content

Commit 0bf9a40

Browse files
authored
Merge pull request #10 from Driving-Capstone/feat/6-api-구현
feat: 이번 주 통계에 리턴값 추가
2 parents 5dd3c28 + ba2c88f commit 0bf9a40

3 files changed

Lines changed: 43 additions & 54 deletions

File tree

src/main/java/com/drivingcoach/backend/domain/driving/repository/DrivingRecordRepository.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,4 +187,24 @@ List<DrivingRecord> findAllByDateOrderByTotalTimeDesc(@Param("userId") Long user
187187
@Param("year") int year,
188188
@Param("month") int month,
189189
@Param("day") int day);
190+
191+
/**
192+
* (추가) 특정 기간 내 주행 횟수 카운트
193+
*/
194+
long countByUserIdAndStartTimeBetween(Long userId, LocalDateTime from, LocalDateTime to);
195+
/**
196+
* (추가) 특정 기간 내 발생한 총 이벤트(DrivingEvent) 개수 카운트
197+
* - DrivingEvent와 Join하여 개수를 셉니다.
198+
*/
199+
@Query("""
200+
select count(e)
201+
from DrivingEvent e
202+
join e.drivingRecord dr
203+
where dr.user.id = :userId
204+
and dr.startTime >= :from
205+
and dr.startTime < :to
206+
""")
207+
long countEventsByUserIdAndPeriod(@Param("userId") Long userId,
208+
@Param("from") LocalDateTime from,
209+
@Param("to") LocalDateTime to);
190210
}

src/main/java/com/drivingcoach/backend/domain/home/domain/dto/response/WeeklyStatusResponse.java

Lines changed: 13 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,29 @@
77
import java.time.LocalDateTime;
88
import java.util.List;
99

10-
/**
11-
* ✅ WeeklyStatusResponse
12-
*
13-
* 용도
14-
* - 홈 화면 "주간 요약" API 응답 모델.
15-
* - 총 주행 시간, 평균 점수, 일자별 합계(차트용), 최근 주행 요약을 포함합니다.
16-
*
17-
* 기간 규칙
18-
* - [from, to) 반개구간을 사용합니다. (from 이상, to 미만)
19-
* - 컨트롤러/서비스에서 타임존 경계(자정)를 맞춘 뒤 값을 채워 넣습니다.
20-
*
21-
* 구성
22-
* - from / to : 조회 기간 경계
23-
* - totalSeconds : 기간 내 총 주행 시간(초)
24-
* - averageScore : 기간 내 평균 점수 (null 가능)
25-
* - dailySeconds : 일자별 총 주행 시간 버킷(차트/스파크라인 등 UI에 사용)
26-
* - lastDriving : 최근 주행 1건 요약(선택 정보)
27-
*/
2810
@Getter
2911
@Setter
3012
@NoArgsConstructor
3113
@AllArgsConstructor
3214
@Builder
3315
public class WeeklyStatusResponse {
3416

35-
@Schema(description = "기간 시작 (포함)", example = "2025-09-22T00:00:00")
17+
@Schema(description = "기간 시작 (포함)")
3618
private LocalDateTime from;
3719

38-
@Schema(description = "기간 끝 (미포함)", example = "2025-09-29T00:00:00")
20+
@Schema(description = "기간 끝 (미포함)")
3921
private LocalDateTime to;
4022

41-
@Schema(description = "총 주행 시간(초)", example = "7380")
23+
@Schema(description = "기간 내 총 주행 시간(초)")
4224
private int totalSeconds;
4325

44-
@Schema(description = "평균 점수(null 가능)", example = "83.4")
26+
@Schema(description = "기간 내 총 주행 횟수") // (추가)
27+
private long totalDrivingCount;
28+
29+
@Schema(description = "기간 내 총 이벤트 발생 횟수") // (추가)
30+
private long totalEventCount;
31+
32+
@Schema(description = "기간 내 평균 점수 (null 가능)")
4533
private Double averageScore;
4634

4735
@Schema(description = "일자별 총 주행(초) 버킷")
@@ -50,46 +38,19 @@ public class WeeklyStatusResponse {
5038
@Schema(description = "가장 최근 주행 요약(선택)")
5139
private LastDriving lastDriving;
5240

53-
/* ---------- 하위 타입 ---------- */
41+
// ... (DayBucket, LastDriving 내부 클래스는 기존 코드 그대로 유지) ...
42+
public static record DayBucket(LocalDate date, int seconds) {}
5443

55-
/**
56-
* 📅 DayBucket
57-
* - 특정 날짜(LocalDate)와 그 날짜의 총 주행 시간(초)
58-
* - Java 16+ record 로 간결하게 정의 (불변)
59-
* - 사용 예: new DayBucket(LocalDate.of(2025,9,28), 1800)
60-
*/
61-
public static record DayBucket(
62-
@Schema(description = "날짜", example = "2025-09-28")
63-
LocalDate date,
64-
@Schema(description = "해당 날짜의 총 주행 시간(초)", example = "1800")
65-
int seconds
66-
) {}
67-
68-
/**
69-
* 🚘 LastDriving
70-
* - 최근 주행 1건에 대한 요약 정보
71-
* - 목록에서 상단 카드나 홈 위젯으로 노출할 때 사용
72-
*/
7344
@Getter
7445
@Setter
7546
@NoArgsConstructor
7647
@AllArgsConstructor
7748
@Builder
7849
public static class LastDriving {
79-
80-
@Schema(description = "주행 기록 ID", example = "321")
8150
private Long recordId;
82-
83-
@Schema(description = "시작 시각", example = "2025-09-28T10:00:00")
8451
private LocalDateTime startTime;
85-
86-
@Schema(description = "종료 시각", example = "2025-09-28T10:45:12")
8752
private LocalDateTime endTime;
88-
89-
@Schema(description = "총 주행 시간(초)", example = "2712")
9053
private Integer totalSeconds;
91-
92-
@Schema(description = "운전 점수(0~100, null 가능)", example = "87.5")
9354
private Float score;
9455
}
95-
}
56+
}

src/main/java/com/drivingcoach/backend/domain/home/service/HomeService.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,16 @@ public WeeklyStatusResponse buildWeeklyStatus(Long userId, LocalDateTime from, L
5252
// 2) 평균 점수 (null 가능)
5353
Double avgScore = drivingRecordRepository.averageScoreByUserIdAndPeriod(userId, from, to);
5454

55-
// 3) 일자별 버킷 합계
55+
// 3) (추가) 총 주행 횟수
56+
long drivingCount = drivingRecordRepository.countByUserIdAndStartTimeBetween(userId, from, to);
57+
58+
// 4) (추가) 총 이벤트 발생 횟수
59+
long eventCount = drivingRecordRepository.countEventsByUserIdAndPeriod(userId, from, to);
60+
61+
// 5) 일자별 버킷 합계
5662
List<WeeklyStatusResponse.DayBucket> buckets = aggregateDailySeconds(userId, from, to);
5763

58-
// 4) 마지막 주행(최근 1건) 요약 (선택: 홈 화면 카드용)
64+
// 6) 마지막 주행(최근 1건) 요약 (선택: 홈 화면 카드용)
5965
DrivingRecord last = drivingRecordRepository
6066
.findTop1ByUserIdOrderByStartTimeDesc(userId, PageRequest.of(0, 1))
6167
.stream().findFirst().orElse(null);
@@ -75,6 +81,8 @@ public WeeklyStatusResponse buildWeeklyStatus(Long userId, LocalDateTime from, L
7581
.from(from)
7682
.to(to)
7783
.totalSeconds(totalSec)
84+
.totalDrivingCount(drivingCount) // (추가)
85+
.totalEventCount(eventCount) // (추가)
7886
.averageScore(avgScore)
7987
.dailySeconds(buckets)
8088
.lastDriving(lastDriving)

0 commit comments

Comments
 (0)