From 3c10ea3ebd3dd86084048027735a74c0b864701c Mon Sep 17 00:00:00 2001 From: sangeun Date: Wed, 29 May 2024 23:44:53 +0900 Subject: [PATCH] =?UTF-8?q?feat=20:=20=EC=9C=A0=EC=A0=80=20=EA=B0=9C?= =?UTF-8?q?=EC=9D=B8=20=ED=8F=89=EA=B7=A0=20=ED=96=89=EB=B3=B5=EC=A7=80?= =?UTF-8?q?=EC=88=98=20API=20=EA=B5=AC=EC=B6=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../record/repository/RecordRepository.java | 2 + .../domain/report/api/ReportController.java | 32 ++++++++--- .../application/AverageHappinessService.java | 53 +++++++++++++++++++ .../application/TrendHappinessService.java | 34 ------------ 4 files changed, 79 insertions(+), 42 deletions(-) create mode 100644 src/main/java/com/hobak/happinessql/domain/report/application/AverageHappinessService.java delete mode 100644 src/main/java/com/hobak/happinessql/domain/report/application/TrendHappinessService.java diff --git a/src/main/java/com/hobak/happinessql/domain/record/repository/RecordRepository.java b/src/main/java/com/hobak/happinessql/domain/record/repository/RecordRepository.java index f3e3b16..85ff632 100644 --- a/src/main/java/com/hobak/happinessql/domain/record/repository/RecordRepository.java +++ b/src/main/java/com/hobak/happinessql/domain/record/repository/RecordRepository.java @@ -14,4 +14,6 @@ public interface RecordRepository extends JpaRepository { Page findByRecordIdLessThanAndUserOrderByRecordIdDesc(Long recordId, User user, Pageable pageRequest); List findAllByCreatedAtBetweenAndUser(LocalDateTime startOfMonth, LocalDateTime endOfMonth, User user); List findAllByUser(User user); + Long countAllByUser(User user); + Long countAllByCreatedAtBetweenAndUser(LocalDateTime startDate, LocalDateTime endDate, User user); } diff --git a/src/main/java/com/hobak/happinessql/domain/report/api/ReportController.java b/src/main/java/com/hobak/happinessql/domain/report/api/ReportController.java index e9eb689..517350b 100644 --- a/src/main/java/com/hobak/happinessql/domain/report/api/ReportController.java +++ b/src/main/java/com/hobak/happinessql/domain/report/api/ReportController.java @@ -1,14 +1,8 @@ package com.hobak.happinessql.domain.report.api; -import com.hobak.happinessql.domain.report.application.ReportActivityRankingService; -import com.hobak.happinessql.domain.report.application.ReportGraphService; -import com.hobak.happinessql.domain.report.application.ReportLocationRankingService; -import com.hobak.happinessql.domain.report.application.ReportSummaryService; -import com.hobak.happinessql.domain.report.dto.ActivityHappinessDto; -import com.hobak.happinessql.domain.report.dto.LocationHappinessDto; -import com.hobak.happinessql.domain.report.dto.ReportGraphResponseDto; -import com.hobak.happinessql.domain.report.dto.ReportSummaryResponseDto; +import com.hobak.happinessql.domain.report.application.*; +import com.hobak.happinessql.domain.report.dto.*; import com.hobak.happinessql.domain.user.application.UserFindService; import com.hobak.happinessql.domain.user.domain.User; import com.hobak.happinessql.global.response.DataResponseDto; @@ -34,6 +28,7 @@ public class ReportController { private final ReportActivityRankingService reportActivityRankingService; private final ReportLocationRankingService reportLocationRankingService; private final ReportGraphService reportGraphService; + private final AverageHappinessService averageHappinessService; @Operation(summary = "[전체] 행복 종합 리포트", description = "전체 기간에서 언제, 어디에서, 무엇을 할 때 행복했는지에 대한 종합적인 리포트를 제공합니다.") @GetMapping("/all/summary") public DataResponseDto getAllSummary(@AuthenticationPrincipal UserDetails userDetails) { @@ -167,5 +162,26 @@ public DataResponseDto> getMonthlyLocationRankings(@A List responseDto = reportLocationRankingService.getMonthlyLocationRankings(user); return DataResponseDto.of(responseDto, "월간 위치 행복도 순위를 성공적으로 조회했습니다."); } + @Operation(summary = "[전체] 평균 행복지수", description = "유저 개인의 전체기간 평균 행복지수와 그에 따른 수준을 판단합니다.") + @GetMapping("/all/happiness/") + public DataResponseDto getAllHappiness(@AuthenticationPrincipal UserDetails userDetails) { + User user = userFindService.findByUserDetails(userDetails); + AverageHappinessResponseDto responseDto = averageHappinessService.getAllHappiness(user); + return DataResponseDto.of(responseDto, "유저 개인의 전체기간 평균 행복지수를 성공적으로 조회했습니다."); + } + @Operation(summary = "[연간] 평균 행복지수", description = "유저 개인의 연간 평균 행복지수와 그에 따른 수준을 판단합니다.") + @GetMapping("/year/happiness") + public DataResponseDto getAnnualHappiness(@AuthenticationPrincipal UserDetails userDetails) { + User user = userFindService.findByUserDetails(userDetails); + AverageHappinessResponseDto responseDto = averageHappinessService.getAnnualHappiness(user); + return DataResponseDto.of(responseDto, "유저 개인의 연간 평균 행복지수를 성공적으로 조회했습니다."); + } + @Operation(summary = "[월간] 평균 행복지수", description = "유저 개인의 월간 평균 행복지수와 그에 따른 수준을 판단합니다.") + @GetMapping("/month/happiness") + public DataResponseDto getMonthlyHappiness(@AuthenticationPrincipal UserDetails userDetails) { + User user = userFindService.findByUserDetails(userDetails); + AverageHappinessResponseDto responseDto = averageHappinessService.getMonthlyHappiness(user); + return DataResponseDto.of(responseDto, "유저 개인의 월간 평균 행복지수를 성공적으로 조회했습니다."); + } } diff --git a/src/main/java/com/hobak/happinessql/domain/report/application/AverageHappinessService.java b/src/main/java/com/hobak/happinessql/domain/report/application/AverageHappinessService.java new file mode 100644 index 0000000..22373f0 --- /dev/null +++ b/src/main/java/com/hobak/happinessql/domain/report/application/AverageHappinessService.java @@ -0,0 +1,53 @@ +package com.hobak.happinessql.domain.report.application; + +import com.hobak.happinessql.domain.record.domain.Record; +import com.hobak.happinessql.domain.record.repository.RecordRepository; +import com.hobak.happinessql.domain.report.converter.ReportConverter; +import com.hobak.happinessql.domain.report.domain.HappinessLevel; +import com.hobak.happinessql.domain.report.dto.AverageHappinessResponseDto; +import com.hobak.happinessql.domain.user.domain.User; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.time.LocalDate; +import java.time.LocalDateTime; + +@Service +@RequiredArgsConstructor +public class AverageHappinessService { + private final RecordRepository recordRepository; + LocalDate currentDate = LocalDate.now(); + int currentYear = currentDate.getYear(); + int currentMonth = currentDate.getMonthValue(); + public AverageHappinessResponseDto getTrendHappiness() { + double totalHappiness = recordRepository.findAll().stream().mapToInt(Record::getHappiness).sum(); + double averageHappiness = totalHappiness / recordRepository.count(); + averageHappiness = Math.round(averageHappiness * 100.0) / 100.0; + HappinessLevel level = HappinessLevel.of(averageHappiness); + return ReportConverter.toAverageHappinessResponseDto(averageHappiness,level,level.getEmoji()); + } + public AverageHappinessResponseDto getAllHappiness(User user) { + double totalHappiness = recordRepository.findAllByUser(user).stream().mapToInt(Record::getHappiness).sum(); + double averageHappiness = totalHappiness / recordRepository.countAllByUser(user); + averageHappiness = Math.round(averageHappiness * 100.0) / 100.0; + HappinessLevel level = HappinessLevel.of(averageHappiness); + return ReportConverter.toAverageHappinessResponseDto(averageHappiness,level,level.getEmoji()); + } + public AverageHappinessResponseDto getAnnualHappiness(User user) { + LocalDateTime startOfYear = LocalDateTime.of(currentYear, 1, 1, 0, 0); + LocalDateTime endOfYear = LocalDateTime.of(currentYear, 12, 31, 23, 59, 59); + double totalHappiness = recordRepository.findAllByCreatedAtBetweenAndUser(startOfYear, endOfYear, user).stream().mapToInt(Record::getHappiness).sum(); + double averageHappiness = totalHappiness / recordRepository.countAllByCreatedAtBetweenAndUser(startOfYear, endOfYear, user); + averageHappiness = Math.round(averageHappiness * 100.0) / 100.0; + HappinessLevel level = HappinessLevel.of(averageHappiness); + return ReportConverter.toAverageHappinessResponseDto(averageHappiness,level,level.getEmoji()); } + public AverageHappinessResponseDto getMonthlyHappiness(User user) { + LocalDateTime startOfMonth = LocalDateTime.of(currentYear, currentMonth, 1, 0, 0); + LocalDateTime endOfMonth = LocalDateTime.of(currentYear, currentMonth, 31, 23, 59, 59); + double totalHappiness = recordRepository.findAllByCreatedAtBetweenAndUser(startOfMonth, endOfMonth, user).stream().mapToInt(Record::getHappiness).sum(); + double averageHappiness = totalHappiness / recordRepository.countAllByCreatedAtBetweenAndUser(startOfMonth, endOfMonth, user); + averageHappiness = Math.round(averageHappiness * 100.0) / 100.0; + HappinessLevel level = HappinessLevel.of(averageHappiness); + return ReportConverter.toAverageHappinessResponseDto(averageHappiness,level,level.getEmoji()); + } +} diff --git a/src/main/java/com/hobak/happinessql/domain/report/application/TrendHappinessService.java b/src/main/java/com/hobak/happinessql/domain/report/application/TrendHappinessService.java deleted file mode 100644 index a157f08..0000000 --- a/src/main/java/com/hobak/happinessql/domain/report/application/TrendHappinessService.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.hobak.happinessql.domain.report.application; - -import com.hobak.happinessql.domain.record.domain.Record; -import com.hobak.happinessql.domain.record.repository.RecordRepository; -import com.hobak.happinessql.domain.report.converter.TrendConverter; -import com.hobak.happinessql.domain.report.domain.HappinessLevel; -import com.hobak.happinessql.domain.report.dto.TrendHappinessResponseDto; -import lombok.RequiredArgsConstructor; -import org.springframework.stereotype.Service; - -@Service -@RequiredArgsConstructor -public class TrendHappinessService { - private final RecordRepository recordRepository; - public TrendHappinessResponseDto getTrendHappiness() { - double totalHappiness = recordRepository.findAll().stream().mapToInt(Record::getHappiness).sum(); - double averageHappiness = totalHappiness / recordRepository.count(); - averageHappiness = Math.round(averageHappiness * 100.0) / 100.0; - HappinessLevel level = HappinessLevel.of(averageHappiness); - String emoji; - if (averageHappiness >= 1 && averageHappiness < 2) { - emoji = "😱"; - } else if (averageHappiness >= 2 && averageHappiness < 3) { - emoji = "🙁"; - } else if (averageHappiness >= 3 && averageHappiness < 5) { - emoji = "😐"; - } else if (averageHappiness >= 5 && averageHappiness < 6) { - emoji = "🙂"; - } else { - emoji = "😄"; - } - return TrendConverter.toTrendHappinessResponseDto(averageHappiness, level, emoji); - } -}