diff --git a/src/main/java/com/hobak/happinessql/domain/record/api/RecordController.java b/src/main/java/com/hobak/happinessql/domain/record/api/RecordController.java index df87ff6..d378edd 100644 --- a/src/main/java/com/hobak/happinessql/domain/record/api/RecordController.java +++ b/src/main/java/com/hobak/happinessql/domain/record/api/RecordController.java @@ -3,6 +3,7 @@ import com.hobak.happinessql.domain.record.application.RecordCalendarListService; +import com.hobak.happinessql.domain.record.application.RecordCalendarDetailService; import com.hobak.happinessql.domain.record.application.RecordCreateService; import com.hobak.happinessql.domain.record.application.RecordPagingService; import com.hobak.happinessql.domain.record.converter.RecordConverter; @@ -31,6 +32,7 @@ public class RecordController { private final RecordCreateService recordCreateService; private final RecordPagingService recordPagingService; private final RecordCalendarListService recordCalendarListService; + private final RecordCalendarDetailService recordCalendarDetailService; @Operation(summary = "행복 기록 추가", description = "행복 기록을 생성합니다.") @PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE) @@ -66,6 +68,13 @@ public DataResponseDto> getRecordCalenderList(@R return DataResponseDto.of(responseDtos, "행복 달력을 성공적으로 조회했습니다."); } + @Operation(summary = "행복 달력 상세 조회", description = "패스 파라미터로 받은 날짜에 해당하는 모든 행복 기록을 조회합니다.") + @GetMapping("/calendar/{date}") + public DataResponseDto> getRecordCalenderList(@PathVariable String date, @RequestParam Long userId) { + List responseDtos = recordCalendarDetailService.getRecords(date, userId); + return DataResponseDto.of(responseDtos, "행복 달력을 성공적으로 조회했습니다."); + } + diff --git a/src/main/java/com/hobak/happinessql/domain/record/application/RecordCalendarDetailService.java b/src/main/java/com/hobak/happinessql/domain/record/application/RecordCalendarDetailService.java new file mode 100644 index 0000000..f43c0bd --- /dev/null +++ b/src/main/java/com/hobak/happinessql/domain/record/application/RecordCalendarDetailService.java @@ -0,0 +1,37 @@ +package com.hobak.happinessql.domain.record.application; + +import com.hobak.happinessql.domain.record.converter.RecordConverter; +import com.hobak.happinessql.domain.record.domain.Record; +import com.hobak.happinessql.domain.record.dto.RecordResponseDto; +import com.hobak.happinessql.domain.record.repository.RecordRepository; +import com.hobak.happinessql.domain.user.application.UserFindService; +import com.hobak.happinessql.domain.user.domain.User; +import com.hobak.happinessql.global.util.TimeConverter; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.util.List; + + +@Service +@RequiredArgsConstructor +public class RecordCalendarDetailService { + + private final RecordRepository recordRepository; + private final UserFindService userFindService; + + public List getRecords(String date, Long userId) { + LocalDate dt = TimeConverter.stringToLocalDate(date); + User user = userFindService.findUserById(userId); + List records = findRecordsBy(dt, user); + return RecordConverter.toRecordResponseDtos(records); + } + + private List findRecordsBy(LocalDate date, User user) { + LocalDateTime startOfDay = date.atStartOfDay(); + LocalDateTime endOfDay = date.atTime(23, 59); + return recordRepository.findAllByCreatedAtBetweenAndUser(startOfDay, endOfDay, user); + } +} diff --git a/src/main/java/com/hobak/happinessql/domain/record/application/RecordCalendarListService.java b/src/main/java/com/hobak/happinessql/domain/record/application/RecordCalendarListService.java index d04abe2..843286d 100644 --- a/src/main/java/com/hobak/happinessql/domain/record/application/RecordCalendarListService.java +++ b/src/main/java/com/hobak/happinessql/domain/record/application/RecordCalendarListService.java @@ -29,35 +29,40 @@ public class RecordCalendarListService { public List getHappinessAverages(Integer month, Integer year, Long userId) { User user = userFindService.findUserById(userId); + year = (year == null) ? LocalDate.now().getYear() : year; + month = (month == null) ? LocalDate.now().getMonthValue() : month; - if(year == null) { - year = LocalDate.now().getYear(); - } + List records = findRecordsBy(month, year, user); - if(month == null) { - month = LocalDate.now().getMonthValue(); - } + Map> happinessIndexesByDate = calculateHappinessIndexes(records); + List responseDtos = createResponseDtos(happinessIndexesByDate); + return responseDtos.stream() + .sorted(Comparator.comparing(RecordCalendarResponseDto::getDate)) + .collect(Collectors.toList()); + } + + private List findRecordsBy(Integer month, Integer year, User user) { LocalDateTime startOfMonth = YearMonth.of(year, month).atDay(1).atStartOfDay(); LocalDateTime endOfMonth = YearMonth.of(year, month).atEndOfMonth().atTime(23, 59); + return recordRepository.findAllByCreatedAtBetweenAndUser(startOfMonth, endOfMonth, user); + } - List records = recordRepository.findAllByCreatedAtBetweenAndUser(startOfMonth, endOfMonth, user); - - Map> happinessIndexesByDate = records.stream() + private Map> calculateHappinessIndexes(List records) { + return records.stream() .collect(Collectors.groupingBy( record -> record.getCreatedAt().toLocalDate(), Collectors.mapping(Record::getHappiness, Collectors.toList()) )); + } + private List createResponseDtos(Map> happinessIndexesByDate) { List responseDtos = new ArrayList<>(); happinessIndexesByDate.forEach((date, happinessIndexes) -> { Integer averageHappiness = Calculator.getAverage(happinessIndexes); RecordCalendarResponseDto dto = RecordConverter.toRecordCalendarResponseDto(date, averageHappiness); responseDtos.add(dto); }); - - return responseDtos.stream() - .sorted(Comparator.comparing(RecordCalendarResponseDto::getDate)) - .collect(Collectors.toList()); + return responseDtos; } } diff --git a/src/main/java/com/hobak/happinessql/global/util/TimeConverter.java b/src/main/java/com/hobak/happinessql/global/util/TimeConverter.java new file mode 100644 index 0000000..96ab9a2 --- /dev/null +++ b/src/main/java/com/hobak/happinessql/global/util/TimeConverter.java @@ -0,0 +1,19 @@ +package com.hobak.happinessql.global.util; + +import com.hobak.happinessql.global.exception.GeneralException; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; + +public class TimeConverter { + public static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + + public static LocalDate stringToLocalDate(String dateString) { + try { + return LocalDate.parse(dateString, formatter); + } catch (DateTimeParseException e) { + throw new GeneralException("Invalid date format. Please use yyyy-MM-dd."); + } + } +}