Skip to content

Commit

Permalink
feat : 유저 개인 평균 행복지수 API 구축
Browse files Browse the repository at this point in the history
  • Loading branch information
KkomSang committed May 29, 2024
1 parent 8bc4c64 commit 3c10ea3
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ public interface RecordRepository extends JpaRepository<Record, Long> {
Page<Record> findByRecordIdLessThanAndUserOrderByRecordIdDesc(Long recordId, User user, Pageable pageRequest);
List<Record> findAllByCreatedAtBetweenAndUser(LocalDateTime startOfMonth, LocalDateTime endOfMonth, User user);
List<Record> findAllByUser(User user);
Long countAllByUser(User user);
Long countAllByCreatedAtBetweenAndUser(LocalDateTime startDate, LocalDateTime endDate, User user);
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<ReportSummaryResponseDto> getAllSummary(@AuthenticationPrincipal UserDetails userDetails) {
Expand Down Expand Up @@ -167,5 +162,26 @@ public DataResponseDto<List<LocationHappinessDto>> getMonthlyLocationRankings(@A
List<LocationHappinessDto> responseDto = reportLocationRankingService.getMonthlyLocationRankings(user);
return DataResponseDto.of(responseDto, "월간 위치 행복도 순위를 성공적으로 조회했습니다.");
}
@Operation(summary = "[전체] 평균 행복지수", description = "유저 개인의 전체기간 평균 행복지수와 그에 따른 수준을 판단합니다.")
@GetMapping("/all/happiness/")
public DataResponseDto<AverageHappinessResponseDto> 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<AverageHappinessResponseDto> 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<AverageHappinessResponseDto> getMonthlyHappiness(@AuthenticationPrincipal UserDetails userDetails) {
User user = userFindService.findByUserDetails(userDetails);
AverageHappinessResponseDto responseDto = averageHappinessService.getMonthlyHappiness(user);
return DataResponseDto.of(responseDto, "유저 개인의 월간 평균 행복지수를 성공적으로 조회했습니다.");
}

}
Original file line number Diff line number Diff line change
@@ -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());
}
}

This file was deleted.

0 comments on commit 3c10ea3

Please sign in to comment.