Skip to content

Commit

Permalink
Merge pull request #82 from Happy-HOBAK/feat/69
Browse files Browse the repository at this point in the history
[#69] 행복 달력 상세 조회 API 구축
  • Loading branch information
KkomSang authored May 14, 2024
2 parents d77fc91 + d418827 commit 227344e
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -66,6 +68,13 @@ public DataResponseDto<List<RecordCalendarResponseDto>> getRecordCalenderList(@R
return DataResponseDto.of(responseDtos, "행복 달력을 성공적으로 조회했습니다.");
}

@Operation(summary = "행복 달력 상세 조회", description = "패스 파라미터로 받은 날짜에 해당하는 모든 행복 기록을 조회합니다.")
@GetMapping("/calendar/{date}")
public DataResponseDto<List<RecordResponseDto>> getRecordCalenderList(@PathVariable String date, @RequestParam Long userId) {
List<RecordResponseDto> responseDtos = recordCalendarDetailService.getRecords(date, userId);
return DataResponseDto.of(responseDtos, "행복 달력을 성공적으로 조회했습니다.");
}




Expand Down
Original file line number Diff line number Diff line change
@@ -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<RecordResponseDto> getRecords(String date, Long userId) {
LocalDate dt = TimeConverter.stringToLocalDate(date);
User user = userFindService.findUserById(userId);
List<Record> records = findRecordsBy(dt, user);
return RecordConverter.toRecordResponseDtos(records);
}

private List<Record> findRecordsBy(LocalDate date, User user) {
LocalDateTime startOfDay = date.atStartOfDay();
LocalDateTime endOfDay = date.atTime(23, 59);
return recordRepository.findAllByCreatedAtBetweenAndUser(startOfDay, endOfDay, user);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,35 +29,40 @@ public class RecordCalendarListService {

public List<RecordCalendarResponseDto> 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<Record> records = findRecordsBy(month, year, user);

if(month == null) {
month = LocalDate.now().getMonthValue();
}
Map<LocalDate, List<Integer>> happinessIndexesByDate = calculateHappinessIndexes(records);
List<RecordCalendarResponseDto> responseDtos = createResponseDtos(happinessIndexesByDate);

return responseDtos.stream()
.sorted(Comparator.comparing(RecordCalendarResponseDto::getDate))
.collect(Collectors.toList());
}

private List<Record> 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<Record> records = recordRepository.findAllByCreatedAtBetweenAndUser(startOfMonth, endOfMonth, user);

Map<LocalDate, List<Integer>> happinessIndexesByDate = records.stream()
private Map<LocalDate, List<Integer>> calculateHappinessIndexes(List<Record> records) {
return records.stream()
.collect(Collectors.groupingBy(
record -> record.getCreatedAt().toLocalDate(),
Collectors.mapping(Record::getHappiness, Collectors.toList())
));
}

private List<RecordCalendarResponseDto> createResponseDtos(Map<LocalDate, List<Integer>> happinessIndexesByDate) {
List<RecordCalendarResponseDto> 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;
}
}
19 changes: 19 additions & 0 deletions src/main/java/com/hobak/happinessql/global/util/TimeConverter.java
Original file line number Diff line number Diff line change
@@ -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.");
}
}
}

0 comments on commit 227344e

Please sign in to comment.