diff --git a/src/main/java/com/mookive/mookive_backend/record/application/dto/response/RecordResponse.java b/src/main/java/com/mookive/mookive_backend/record/application/dto/response/RecordResponse.java index d492e32..a7713a3 100644 --- a/src/main/java/com/mookive/mookive_backend/record/application/dto/response/RecordResponse.java +++ b/src/main/java/com/mookive/mookive_backend/record/application/dto/response/RecordResponse.java @@ -96,4 +96,15 @@ public RecordWeekResponse(String tmdbId, String title, String poster) { this.poster = poster; } } + + @Getter + @NoArgsConstructor + public static class RecordTotalResponse { + private int total; + + @Builder + public RecordTotalResponse(int total) { + this.total = total; + } + } } diff --git a/src/main/java/com/mookive/mookive_backend/record/application/service/RecordTotalGetService.java b/src/main/java/com/mookive/mookive_backend/record/application/service/RecordTotalGetService.java new file mode 100644 index 0000000..5c264c3 --- /dev/null +++ b/src/main/java/com/mookive/mookive_backend/record/application/service/RecordTotalGetService.java @@ -0,0 +1,52 @@ +package com.mookive.mookive_backend.record.application.service; + +import com.mookive.mookive_backend.record.application.dto.response.RecordResponse; +import com.mookive.mookive_backend.record.domain.service.RecordQueryService; +import jakarta.transaction.Transactional; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; + +@Service +@RequiredArgsConstructor +@Transactional +public class RecordTotalGetService { + + private final RecordQueryService recordQueryService; + + public RecordResponse.RecordTotalResponse getRecordTotalOfMonth(Long userId) { + LocalDate now = LocalDate.now(); + LocalDate startDate = LocalDate.of(now.getYear(), now.getMonthValue(), 1); + LocalTime startTime = LocalTime.of(0,0,0,0); + LocalDateTime startLocalDateTime = LocalDateTime.of(startDate, startTime); + + LocalDate endDate = LocalDate.of(now.getYear(), now.getMonthValue(), now.lengthOfMonth()); + LocalTime endTime = LocalTime.of(23,59,9,999999999); + LocalDateTime endLocalDateTime = LocalDateTime.of(endDate, endTime); + + int total = recordQueryService.countByUserIdAndCreatedAtBetween(userId, startLocalDateTime, endLocalDateTime); + return RecordResponse.RecordTotalResponse.builder() + .total(total) + .build(); + } + + public RecordResponse.RecordTotalResponse getRecordTotalOfYear(Long userId) { + LocalDate now = LocalDate.now(); + LocalDate startDate = LocalDate.of(now.getYear(), 1, 1); + LocalTime startTime = LocalTime.of(0,0,0,0); + LocalDateTime startLocalDateTime = LocalDateTime.of(startDate, startTime); + + LocalDate endDate = LocalDate.of(now.getYear(), 12, 31); + LocalTime endTime = LocalTime.of(23,59,9,999999999); + LocalDateTime endLocalDateTime = LocalDateTime.of(endDate, endTime); + + int total = recordQueryService.countByUserIdAndCreatedAtBetween(userId, startLocalDateTime, endLocalDateTime); + return RecordResponse.RecordTotalResponse.builder() + .total(total) + .build(); + } + +} diff --git a/src/main/java/com/mookive/mookive_backend/record/domain/repository/RecordRepository.java b/src/main/java/com/mookive/mookive_backend/record/domain/repository/RecordRepository.java index 45fd2bb..cc123d3 100644 --- a/src/main/java/com/mookive/mookive_backend/record/domain/repository/RecordRepository.java +++ b/src/main/java/com/mookive/mookive_backend/record/domain/repository/RecordRepository.java @@ -21,4 +21,6 @@ public interface RecordRepository extends JpaRepository { List findTop20ByOrderByUpdatedAtDesc(); List findAllByUserId(Long userId); + + int countByUserIdAndCreatedAtBetween(Long userId, LocalDateTime startOfLocalDateTime, LocalDateTime endOfLocalDateTime); } diff --git a/src/main/java/com/mookive/mookive_backend/record/domain/service/RecordQueryService.java b/src/main/java/com/mookive/mookive_backend/record/domain/service/RecordQueryService.java index 644860b..808a14c 100644 --- a/src/main/java/com/mookive/mookive_backend/record/domain/service/RecordQueryService.java +++ b/src/main/java/com/mookive/mookive_backend/record/domain/service/RecordQueryService.java @@ -36,6 +36,10 @@ public List findByUserIdAndUpdatedAtBetween(Long userId, LocalDateTime s return recordRepository.findByUserIdAndUpdatedAtBetween(userId, startDate, endDate); } + public int countByUserIdAndCreatedAtBetween(Long userId, LocalDateTime startOfLocalDateTime, LocalDateTime endOfLocalDateTime) { + return recordRepository.countByUserIdAndCreatedAtBetween(userId, startOfLocalDateTime, endOfLocalDateTime); + } + public List findTop5ByUserIdOrderByUpdatedAtDesc(Long userId) { return recordRepository.findTop5ByUserIdOrderByUpdatedAtDesc(userId); } diff --git a/src/main/java/com/mookive/mookive_backend/record/presentation/RecordController.java b/src/main/java/com/mookive/mookive_backend/record/presentation/RecordController.java index 11bc672..a32c5c6 100644 --- a/src/main/java/com/mookive/mookive_backend/record/presentation/RecordController.java +++ b/src/main/java/com/mookive/mookive_backend/record/presentation/RecordController.java @@ -5,6 +5,7 @@ import com.mookive.mookive_backend.record.application.service.RecordCreateService; import com.mookive.mookive_backend.record.application.service.RecordDeleteService; import com.mookive.mookive_backend.record.application.service.RecordGetService; +import com.mookive.mookive_backend.record.application.service.RecordTotalGetService; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.*; @@ -17,6 +18,7 @@ public class RecordController { private final RecordCreateService recordCreateService; private final RecordGetService recordGetService; private final RecordDeleteService recordDeleteService; + private final RecordTotalGetService recordTotalGetService; @PostMapping("/record") public void createRecord(@RequestBody RecordRequest.RecordCreateRequest recordCreateRequest) { @@ -53,4 +55,14 @@ public List getRecordWithinWeek(@PathVariable return recordGetService.getRecordWithinWeek(userId); } + @GetMapping("/record/total/month/{userId}") + public RecordResponse.RecordTotalResponse getRecordTotalOfMonth(@PathVariable Long userId) { + return recordTotalGetService.getRecordTotalOfMonth(userId); + } + + @GetMapping("/record/total/year/{userId}") + public RecordResponse.RecordTotalResponse getRecordTotalOfYear(@PathVariable Long userId) { + return recordTotalGetService.getRecordTotalOfYear(userId); + } + }