From 9d8932567675d7617bbf244c67dc9805c0fc18f8 Mon Sep 17 00:00:00 2001 From: syyling Date: Wed, 29 May 2024 18:23:41 +0900 Subject: [PATCH] =?UTF-8?q?[feat]=20=EC=9B=94=EB=B3=84=20=EC=B4=9D=20?= =?UTF-8?q?=EB=A6=AC=EB=B7=B0=20=EA=B0=AF=EC=88=98=20=EC=A1=B0=ED=9A=8C=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5,=20=EC=97=B0=EB=8F=84=EB=B3=84=20=EC=B4=9D?= =?UTF-8?q?=20=EB=A6=AC=EB=B7=B0=20=EA=B0=AF=EC=88=98=20=EC=A1=B0=ED=9A=8C?= =?UTF-8?q?=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dto/response/RecordResponse.java | 11 ++++ .../service/RecordTotalGetService.java | 52 +++++++++++++++++++ .../domain/repository/RecordRepository.java | 2 + .../domain/service/RecordQueryService.java | 4 ++ .../record/presentation/RecordController.java | 12 +++++ 5 files changed, 81 insertions(+) create mode 100644 src/main/java/com/mookive/mookive_backend/record/application/service/RecordTotalGetService.java 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); + } + }