Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,27 @@ public class HomeController {
### 제공 정보
1. 주간 답변 상태: 이번 주(월~일) 답변한 날짜 목록
2. 연속 기록(Streak): 현재 연속 답변 일수
3. 총 기록 일수: 첫 답변 이후 경과 일수 (N일째 기록 중)
3. 총 기록 일수: 실제 답변한 날짜의 총 개수

### 계산 기준
- 주 시작: 월요일, 주 종료: 일요일
- Streak: 현재까지 매일 연속 답변한 총 일수
* 오늘 답변 있음 → 오늘까지 포함한 연속 일수
* 오늘 답변 없음 → 어제까지의 연속 일수
* 어제도 답변 없음 → 0
- 총 기록 일수: (오늘 - 첫 답변 날짜) + 1
- 총 기록 일수: 실제 답변을 작성한 날짜의 개수

### 예시
- 첫 답변: 2025-12-27
- 1월 1일~15일 매일 연속 답변
- 12월 27일~31일 매일 답변 (5일)
- 1월 1일~3일 답변 안 함
- 1월 4일~15일 매일 답변 (12일)
- 오늘: 2026-01-15

응답:
- answeredDates: ["2026-01-13", "2026-01-14", "2026-01-15"]
- streakCount: 15 (1월 1일~15일 연속)
- totalRecordDays: 20 (12월 27일부터 오늘까지 경과)
- answeredDates: ["2026-01-12", "2026-01-13", "2026-01-14", "2026-01-15"]
- streakCount: 12 (1월 4일부터 15일까지 연속)
- totalRecordDays: 17 (5+ 12 = 총 17일 답변)
""",
security = @SecurityRequirement(name = "bearerAuth"),
responses = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public record HomeResponse(
List<LocalDate> answeredDates,

@Schema(description = "현재 연속 답변 일수 (오늘 답변 있으면 오늘까지, 없으면 어제까지)", example = "15")
long streakCount,
int streakCount,

@Schema(description = "첫 답변 이후 경과 일수 (N일째 기록 중)", example = "20")
long totalRecordDays
@Schema(description = "실제 답변한 날짜의 총 개수", example = "20")
int totalRecordDays
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand All @@ -28,42 +27,31 @@ public HomeResponse getHomeData(Long userId) {
// 1. 이번 주 범위 계산
WeekRangeDto weekRange = WeekRangeCalculator.weekRangeOf(today);

// 2. 첫 답변 날짜 조회
LocalDate firstAnswerDate = answerEntryQueryRepository
.findFirstAnswerDateByUserId(userId)
.orElse(null);

// 3. 첫 답변 날짜 ~ 오늘까지 전체 답변 날짜 조회
List<LocalDate> allAnswerDates;
if (firstAnswerDate == null) {
allAnswerDates = List.of(); // 신규 사용자
} else {
allAnswerDates = answerEntryQueryRepository
.findAnswerDatesByUserIdAndDateBetween(userId, firstAnswerDate, today);
}
// 2. 전체 답변 날짜 조회
List<LocalDate> allAnswerDates = answerEntryQueryRepository.findAllAnswerDatesByUserId(userId);

// 4. 주간 답변 필터링
// 3. 이번 주 답변 필터링
List<LocalDate> weeklyAnsweredDates = allAnswerDates.stream()
.filter(date -> !date.isBefore(weekRange.weekStartDate())
&& !date.isAfter(weekRange.weekEndDate()))
.sorted()
.toList();

// 5. Streak 계산
long currentStreak = calculateCurrentStreak(today, allAnswerDates);
// 4. Streak 계산
int currentStreak = calculateCurrentStreak(today, allAnswerDates);

// 6. 총 기록 일수 계산
long totalDaysSinceStart = calculateTotalDaysSinceStart(today, firstAnswerDate);
// 5. 총 기록 일수 (실제 답변한 날짜 수)
int totalAnsweredDays = allAnswerDates.size();

// 7. 응답 생성
// 6. 응답 생성
return new HomeResponse(
weeklyAnsweredDates,
currentStreak,
totalDaysSinceStart
totalAnsweredDays
);
}

private long calculateCurrentStreak(LocalDate today, List<LocalDate> answerDates) {
private int calculateCurrentStreak(LocalDate today, List<LocalDate> answerDates) {
if (answerDates == null || answerDates.isEmpty()) {
return 0;
}
Expand All @@ -73,8 +61,13 @@ private long calculateCurrentStreak(LocalDate today, List<LocalDate> answerDates
// 시작 날짜 결정: 오늘 답변 있으면 오늘부터, 없으면 어제부터
LocalDate checkDate = dateSet.contains(today) ? today : today.minusDays(1);

// 어제부터 시작하는데 어제 답변이 없으면 streak 0
if (!dateSet.contains(checkDate)) {
return 0;
}

// 역순 연속 계산
long streak = 0;
int streak = 0;

while (dateSet.contains(checkDate)) {
streak++;
Expand All @@ -83,12 +76,4 @@ private long calculateCurrentStreak(LocalDate today, List<LocalDate> answerDates

return streak;
}

private long calculateTotalDaysSinceStart(LocalDate today, LocalDate firstAnswerDate) {
if (firstAnswerDate == null || firstAnswerDate.isAfter(today)) {
return 0;
}

return ChronoUnit.DAYS.between(firstAnswerDate, today) + 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,29 +119,12 @@ Optional<SearchAnswerEntryDto> findByUserAndDate(
);

/**
* 특정 기간 내 사용자가 답변한 날짜 목록 조회 (오름차순)
* 사용자의 모든 답변 날짜 조회
*/
@Query("""
SELECT ae.date
FROM AnswerEntry ae
WHERE ae.user.id = :userId
AND ae.date >= :startDate
AND ae.date <= :endDate
ORDER BY ae.date ASC
""")
List<LocalDate> findAnswerDatesByUserIdAndDateBetween(
@Param("userId") Long userId,
@Param("startDate") LocalDate startDate,
@Param("endDate") LocalDate endDate
);

/**
* 사용자의 첫 번째 답변 날짜 조회
*/
@Query("""
SELECT MIN(ae.date)
FROM AnswerEntry ae
WHERE ae.user.id = :userId
""")
Optional<LocalDate> findFirstAnswerDateByUserId(@Param("userId") Long userId);
List<LocalDate> findAllAnswerDatesByUserId(@Param("userId") Long userId);
}