From 277745af3aaada99294b839ca6cf50a8a2974e5b Mon Sep 17 00:00:00 2001 From: 5jisoo <56earls@gmail.com> Date: Mon, 12 Feb 2024 22:47:12 +0900 Subject: [PATCH] =?UTF-8?q?#8=20Feat:=20=EC=84=A0=EC=96=B8=20=ED=8E=98?= =?UTF-8?q?=EC=9D=B4=EC=A7=80=20=EC=A1=B0=ED=9A=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/DeclarationController.java | 6 ++++ .../repository/DeclarationRepository.java | 16 +++++++++ .../response/PrivateDeclarationElements.java | 28 ++++++++++++++++ .../service/DeclarationService.java | 33 +++++++++++++++++++ 4 files changed, 83 insertions(+) create mode 100644 src/main/java/com/gdsc/remine/declaration/dto/response/PrivateDeclarationElements.java diff --git a/src/main/java/com/gdsc/remine/declaration/controller/DeclarationController.java b/src/main/java/com/gdsc/remine/declaration/controller/DeclarationController.java index dada258..85f5574 100644 --- a/src/main/java/com/gdsc/remine/declaration/controller/DeclarationController.java +++ b/src/main/java/com/gdsc/remine/declaration/controller/DeclarationController.java @@ -1,6 +1,7 @@ package com.gdsc.remine.declaration.controller; import com.gdsc.remine.declaration.dto.response.PrivateDeclarationElement; +import com.gdsc.remine.declaration.dto.response.PrivateDeclarationElements; import com.gdsc.remine.declaration.service.DeclarationService; import com.gdsc.remine.global.api_payload.ApiResponse; import lombok.RequiredArgsConstructor; @@ -20,4 +21,9 @@ public ApiResponse uploadDeclaration( ) { return ApiResponse.onSuccess(declarationService.uploadDeclaration(audioFile, content)); } + + @GetMapping("") + public ApiResponse getDeclarationInWeek() { + return ApiResponse.onSuccess(declarationService.getDeclarationInWeek()); + } } diff --git a/src/main/java/com/gdsc/remine/declaration/domain/repository/DeclarationRepository.java b/src/main/java/com/gdsc/remine/declaration/domain/repository/DeclarationRepository.java index 32856a4..98fa067 100644 --- a/src/main/java/com/gdsc/remine/declaration/domain/repository/DeclarationRepository.java +++ b/src/main/java/com/gdsc/remine/declaration/domain/repository/DeclarationRepository.java @@ -3,7 +3,9 @@ import com.gdsc.remine.declaration.domain.Declaration; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +import java.time.LocalDateTime; import java.util.List; public interface DeclarationRepository extends JpaRepository { @@ -13,4 +15,18 @@ public interface DeclarationRepository extends JpaRepository "order by d.createdDate desc " + "limit 9 ") List findRecentDeclarations(); + + @Query("select d from Declaration d " + + "where d.createdDate >= :oneWeekAgo and d.member.id = :memberId " + + "order by d.createdDate desc ") + List findInRecent7Day( + @Param("oneWeekAgo") final LocalDateTime oneWeekAgo, + @Param("memberId") final Long loginMemberId + ); + + @Query("select count(d) from Declaration d " + + "where d.createdDate >= :oneDayAgo ") + Long countInRecent1Day( + @Param("oneDayAgo") final LocalDateTime oneDayAgo + ); } diff --git a/src/main/java/com/gdsc/remine/declaration/dto/response/PrivateDeclarationElements.java b/src/main/java/com/gdsc/remine/declaration/dto/response/PrivateDeclarationElements.java new file mode 100644 index 0000000..96b8e6f --- /dev/null +++ b/src/main/java/com/gdsc/remine/declaration/dto/response/PrivateDeclarationElements.java @@ -0,0 +1,28 @@ +package com.gdsc.remine.declaration.dto.response; + +import com.gdsc.remine.declaration.domain.Declaration; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; +import java.util.stream.Collectors; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class PrivateDeclarationElements { + private Long todayParticipantsCount; + private List declarationList; + + public static PrivateDeclarationElements from( + Long todayParticipantsCount, + List declarationList + ) { + final List declarationElementList = declarationList.stream().map( + PrivateDeclarationElement::new + ).collect(Collectors.toList()); + + return new PrivateDeclarationElements(todayParticipantsCount, declarationElementList); + } +} diff --git a/src/main/java/com/gdsc/remine/declaration/service/DeclarationService.java b/src/main/java/com/gdsc/remine/declaration/service/DeclarationService.java index 753398c..2a5a75a 100644 --- a/src/main/java/com/gdsc/remine/declaration/service/DeclarationService.java +++ b/src/main/java/com/gdsc/remine/declaration/service/DeclarationService.java @@ -3,6 +3,7 @@ import com.gdsc.remine.declaration.domain.Declaration; import com.gdsc.remine.declaration.domain.repository.DeclarationRepository; import com.gdsc.remine.declaration.dto.response.PrivateDeclarationElement; +import com.gdsc.remine.declaration.dto.response.PrivateDeclarationElements; import com.gdsc.remine.global.gcs.FilePath; import com.gdsc.remine.global.gcs.service.GoogleCloudStorageService; import com.gdsc.remine.login.jwt.util.AuthTokensGenerator; @@ -12,6 +13,9 @@ import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; +import java.time.LocalDateTime; +import java.util.List; + @Service @RequiredArgsConstructor public class DeclarationService { @@ -36,4 +40,33 @@ public PrivateDeclarationElement uploadDeclaration( .declaration(saved) .build(); } + + public PrivateDeclarationElements getDeclarationInWeek() { + final Long loginMemberId = authTokensGenerator.getLoginMemberId(); + + final LocalDateTime oneWeekAgo = getOneWeekAgoDate(); + final List declarationList = declarationRepository.findInRecent7Day( + oneWeekAgo, + loginMemberId + ); + + final LocalDateTime oneDayAgo = getOneDayAgoDate(); + final Long count = declarationRepository.countInRecent1Day(oneDayAgo); + + return PrivateDeclarationElements.from( + count, + declarationList + ); + } + + private LocalDateTime getOneWeekAgoDate() { + LocalDateTime oneWeekAgo = LocalDateTime.now().minusDays(6); + return oneWeekAgo + .minusHours(oneWeekAgo.getHour()) + .minusMinutes(oneWeekAgo.getMinute()); + } + + private LocalDateTime getOneDayAgoDate() { + return LocalDateTime.now().minusDays(1); + } }