From 98fe21d6b0382f09b63142cd1b41bc3f1f4fa212 Mon Sep 17 00:00:00 2001 From: Kwon770 Date: Tue, 7 May 2024 23:28:13 +0900 Subject: [PATCH 1/7] feat: Add getLatestPost API to implement new policy of family member ranking --- .../repository/PostRepositoryCustomImpl.java | 10 ++++++++++ .../com/oing/controller/PostController.java | 7 +++++++ .../oing/repository/PostRepositoryCustom.java | 2 ++ .../src/main/java/com/oing/restapi/PostApi.java | 17 +++++++++++++++++ .../main/java/com/oing/service/PostService.java | 4 ++++ 5 files changed, 40 insertions(+) diff --git a/gateway/src/main/java/com/oing/repository/PostRepositoryCustomImpl.java b/gateway/src/main/java/com/oing/repository/PostRepositoryCustomImpl.java index f47101ae..ab310fed 100644 --- a/gateway/src/main/java/com/oing/repository/PostRepositoryCustomImpl.java +++ b/gateway/src/main/java/com/oing/repository/PostRepositoryCustomImpl.java @@ -51,7 +51,17 @@ public List findLatestPostOfEveryday(LocalDateTime startDate, LocalDateTim )) .orderBy(post.createdAt.asc()) .fetch(); + } + @Override + public Post findLatestPost(LocalDateTime startDate, LocalDateTime endDate, String familyId) { + return queryFactory + .selectFrom(post) + .where(post.familyId.eq(familyId) + .and(post.createdAt.between(startDate, endDate)) + ) + .orderBy(post.createdAt.desc()) + .fetchFirst(); } @Override diff --git a/post/src/main/java/com/oing/controller/PostController.java b/post/src/main/java/com/oing/controller/PostController.java index 8081b3ee..7d6799b3 100644 --- a/post/src/main/java/com/oing/controller/PostController.java +++ b/post/src/main/java/com/oing/controller/PostController.java @@ -97,6 +97,13 @@ public PostResponse getPost(String postId, String loginMemberId) { return PostResponse.from(memberPostProjection); } + @Override + public PostResponse getLatestPost(LocalDate inclusiveStartDate, LocalDate exclusiveEndDate, String loginFamilyId) { + Post latestPost = postService.findLatestPost(inclusiveStartDate, exclusiveEndDate, loginFamilyId); + + return PostResponse.from(latestPost); + } + @Override public SurvivalUploadStatusResponse getSurvivalUploadStatus(String memberId, String loginMemberId, String loginFamilyId) { validateMemberId(loginMemberId, memberId); diff --git a/post/src/main/java/com/oing/repository/PostRepositoryCustom.java b/post/src/main/java/com/oing/repository/PostRepositoryCustom.java index 64038258..eab73466 100644 --- a/post/src/main/java/com/oing/repository/PostRepositoryCustom.java +++ b/post/src/main/java/com/oing/repository/PostRepositoryCustom.java @@ -13,6 +13,8 @@ public interface PostRepositoryCustom { List findLatestPostOfEveryday(LocalDateTime startDate, LocalDateTime endDate, String familyId); + Post findLatestPost(LocalDateTime startDate, LocalDateTime endDate, String familyId); + QueryResults searchPosts(int page, int size, LocalDate date, String memberId, String requesterMemberId, String familyId, boolean asc, PostType type); diff --git a/post/src/main/java/com/oing/restapi/PostApi.java b/post/src/main/java/com/oing/restapi/PostApi.java index a951e737..bf71afb5 100644 --- a/post/src/main/java/com/oing/restapi/PostApi.java +++ b/post/src/main/java/com/oing/restapi/PostApi.java @@ -115,6 +115,23 @@ PostResponse getPost( String loginMemberId ); + @Operation(summary = "가장 최근 게시물 조회", description = "특정 기간 안에서 가장 최근에 업로드된 게시물을 조회합니다.") + PostResponse getLatestPost( + @RequestParam + @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) + @Parameter(description = "조회 시작 날짜", example = "2023-12-05") + LocalDate inclusiveStartDate, + + @RequestParam + @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) + @Parameter(description = "조회 종료 날짜", example = "2023-12-05") + LocalDate exclusiveEndDate, + + @Parameter(hidden = true) + @LoginFamilyId + String loginFamilyId + ); + @Operation(summary = "회원 생존신고 게시글 업로드 여부 응답 조회", description = "회원 생존신고 게시글 업로드 여부를 조회합니다.") @GetMapping("/{memberId}/survival-uploaded") SurvivalUploadStatusResponse getSurvivalUploadStatus( diff --git a/post/src/main/java/com/oing/service/PostService.java b/post/src/main/java/com/oing/service/PostService.java index 2d7888c4..e487ffcd 100644 --- a/post/src/main/java/com/oing/service/PostService.java +++ b/post/src/main/java/com/oing/service/PostService.java @@ -116,6 +116,10 @@ public Post getMemberPostById(String postId) { return postRepository.findById(postId).orElseThrow(PostNotFoundException::new); } + public Post findLatestPost(LocalDate inclusiveStartDate, LocalDate exclusiveEndDate, String loginFamilyId) { + return postRepository.findLatestPost(inclusiveStartDate.atStartOfDay(), exclusiveEndDate.atStartOfDay(), loginFamilyId); + } + public PaginationDTO searchMemberPost(int page, int size, LocalDate date, String memberId, String requesterMemberId, String familyId, boolean asc, PostType type) { QueryResults results = null; From 4f1a16726b4ede71941d839af617c625b8128464 Mon Sep 17 00:00:00 2001 From: Kwon770 Date: Tue, 7 May 2024 23:36:27 +0900 Subject: [PATCH 2/7] fix: Add null handling in getLatestPostAPI --- post/src/main/java/com/oing/controller/PostController.java | 3 ++- post/src/main/java/com/oing/restapi/PostApi.java | 5 ++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/post/src/main/java/com/oing/controller/PostController.java b/post/src/main/java/com/oing/controller/PostController.java index 7d6799b3..a2c97f07 100644 --- a/post/src/main/java/com/oing/controller/PostController.java +++ b/post/src/main/java/com/oing/controller/PostController.java @@ -98,8 +98,9 @@ public PostResponse getPost(String postId, String loginMemberId) { } @Override - public PostResponse getLatestPost(LocalDate inclusiveStartDate, LocalDate exclusiveEndDate, String loginFamilyId) { + public PostResponse findLatestPost(LocalDate inclusiveStartDate, LocalDate exclusiveEndDate, String loginFamilyId) { Post latestPost = postService.findLatestPost(inclusiveStartDate, exclusiveEndDate, loginFamilyId); + if (latestPost == null) return null; return PostResponse.from(latestPost); } diff --git a/post/src/main/java/com/oing/restapi/PostApi.java b/post/src/main/java/com/oing/restapi/PostApi.java index bf71afb5..01044e87 100644 --- a/post/src/main/java/com/oing/restapi/PostApi.java +++ b/post/src/main/java/com/oing/restapi/PostApi.java @@ -8,7 +8,6 @@ import com.oing.util.security.LoginMemberId; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; -import io.swagger.v3.oas.annotations.Parameters; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.validation.Valid; import jakarta.validation.constraints.Min; @@ -115,8 +114,8 @@ PostResponse getPost( String loginMemberId ); - @Operation(summary = "가장 최근 게시물 조회", description = "특정 기간 안에서 가장 최근에 업로드된 게시물을 조회합니다.") - PostResponse getLatestPost( + @Operation(summary = "가장 최근 게시물 조회", description = "(결과가 없을시, null 반환)\n 특정 기간 안에서 가장 최근에 업로드된 게시물을 조회합니다.") + PostResponse findLatestPost( @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) @Parameter(description = "조회 시작 날짜", example = "2023-12-05") From 8b7dc4ddbff4a91363fb251e53c88008abfdd40f Mon Sep 17 00:00:00 2001 From: Kwon770 Date: Tue, 7 May 2024 23:45:00 +0900 Subject: [PATCH 3/7] feat: Make mostRecentSurvivalPostDate return most recent surcicial post date in this month else then return null --- .../main/java/com/oing/controller/MainViewController.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/gateway/src/main/java/com/oing/controller/MainViewController.java b/gateway/src/main/java/com/oing/controller/MainViewController.java index ff8c998f..7e681f40 100644 --- a/gateway/src/main/java/com/oing/controller/MainViewController.java +++ b/gateway/src/main/java/com/oing/controller/MainViewController.java @@ -183,9 +183,11 @@ public FamilyMemberMonthlyRankingResponse getFamilyMemberMonthlyRanking(String l } LocalDate mostRecentSurvivalPostDate = null; - List mostRecentPosts = postController.fetchDailyFeeds(1, 1, null, null, "desc", PostType.SURVIVAL, loginMemberId, false).results().stream().toList(); - if (!mostRecentPosts.isEmpty()) { - mostRecentSurvivalPostDate = mostRecentPosts.get(0).createdAt().toLocalDate(); + LocalDate startOfMonth = ZonedDateTime.now().withDayOfMonth(1).toLocalDate(); + LocalDate tomorrow = ZonedDateTime.now().plusDays(1).toLocalDate(); + PostResponse mostRecentPost = postController.findLatestPost(startOfMonth, tomorrow, loginFamilyId); + if (mostRecentPost != null) { + mostRecentSurvivalPostDate = mostRecentPost.createdAt().toLocalDate(); } return new FamilyMemberMonthlyRankingResponse( From d9767a7406d689d3e3dbbb97c23b844287c134be Mon Sep 17 00:00:00 2001 From: Kwon770 Date: Tue, 7 May 2024 23:46:12 +0900 Subject: [PATCH 4/7] refactor: Refactor nested test class in MianviewApiTest to follow naming convention --- gateway/src/test/java/com/oing/restapi/MainViewApiTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gateway/src/test/java/com/oing/restapi/MainViewApiTest.java b/gateway/src/test/java/com/oing/restapi/MainViewApiTest.java index 67be7526..0c462102 100644 --- a/gateway/src/test/java/com/oing/restapi/MainViewApiTest.java +++ b/gateway/src/test/java/com/oing/restapi/MainViewApiTest.java @@ -104,7 +104,7 @@ void setUp() { @Nested - class 금월의_가족구성원_월간_랭킹_조회 { + class getFamilyMemberMonthlyRanking { @Test void 정상_조회() throws Exception { // given From c2e7ff5db94b65d51623ad410fd92854ffecf2d0 Mon Sep 17 00:00:00 2001 From: Kwon770 Date: Tue, 7 May 2024 23:54:23 +0900 Subject: [PATCH 5/7] feat: Add test code for findLatestPost in PostRepositoryCustomImpl --- .../repository/PostRepositoryCustomTest.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/gateway/src/test/java/com/oing/repository/PostRepositoryCustomTest.java b/gateway/src/test/java/com/oing/repository/PostRepositoryCustomTest.java index 923d98f9..dd222b7d 100644 --- a/gateway/src/test/java/com/oing/repository/PostRepositoryCustomTest.java +++ b/gateway/src/test/java/com/oing/repository/PostRepositoryCustomTest.java @@ -6,6 +6,7 @@ import com.oing.domain.Post; import com.oing.domain.PostType; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; @@ -112,6 +113,51 @@ void setup() { .containsExactly("2", "4"); } + @Nested + class findLatestPost { + @Test + void 정상_조회_테스트() { + // given + String familyId = testMember1.getFamilyId(); + LocalDateTime inclusiveStart = LocalDate.of(2023, 11, 1).atStartOfDay(); + LocalDateTime exclusiveEnd = LocalDate.of(2023, 11, 2).atStartOfDay(); + + // when + Post post = postRepositoryCustomImpl.findLatestPost(inclusiveStart, exclusiveEnd, familyId); + + // then + assertThat(post.getId()).isEqualTo("2"); + } + + @Test + void 시작날짜는_포함하고_종료날짜는_포함하지_않는다() { + // given + String familyId = testMember1.getFamilyId(); + LocalDateTime inclusiveStart = LocalDate.of(2023, 11, 1).atStartOfDay(); + LocalDateTime exclusiveEnd = LocalDate.of(2023, 11, 1).atStartOfDay(); + + // when + Post post = postRepositoryCustomImpl.findLatestPost(inclusiveStart, exclusiveEnd, familyId); + + // then + assertThat(post).isNull(); + } + + @Test + void 해당_날짜에_게시글이_없는_경우_null을_반환한다() { + // given + String familyId = testMember1.getFamilyId(); + LocalDateTime inclusiveStart = LocalDate.of(9999, 11, 3).atStartOfDay(); + LocalDateTime exclusiveEnd = LocalDate.of(9999, 11, 4).atStartOfDay(); + + // when + Post post = postRepositoryCustomImpl.findLatestPost(inclusiveStart, exclusiveEnd, familyId); + + // then + assertThat(post).isNull(); + } + } + @Test void 특정_날짜에_게시글이_존재하는지_확인한다() { // given From fe28b0821597c8a68f49a3cbb33e9bd633db978a Mon Sep 17 00:00:00 2001 From: Kwon770 Date: Wed, 8 May 2024 00:05:33 +0900 Subject: [PATCH 6/7] refactor: Add PostType paramter at getRecentPost API --- .../com/oing/controller/MainViewController.java | 2 +- .../oing/repository/PostRepositoryCustomImpl.java | 8 +++++--- .../oing/repository/PostRepositoryCustomTest.java | 13 +++++++------ .../java/com/oing/controller/PostController.java | 4 ++-- .../com/oing/repository/PostRepositoryCustom.java | 2 +- post/src/main/java/com/oing/restapi/PostApi.java | 4 ++++ .../src/main/java/com/oing/service/PostService.java | 4 ++-- 7 files changed, 22 insertions(+), 15 deletions(-) diff --git a/gateway/src/main/java/com/oing/controller/MainViewController.java b/gateway/src/main/java/com/oing/controller/MainViewController.java index 7e681f40..9d43bc59 100644 --- a/gateway/src/main/java/com/oing/controller/MainViewController.java +++ b/gateway/src/main/java/com/oing/controller/MainViewController.java @@ -185,7 +185,7 @@ public FamilyMemberMonthlyRankingResponse getFamilyMemberMonthlyRanking(String l LocalDate mostRecentSurvivalPostDate = null; LocalDate startOfMonth = ZonedDateTime.now().withDayOfMonth(1).toLocalDate(); LocalDate tomorrow = ZonedDateTime.now().plusDays(1).toLocalDate(); - PostResponse mostRecentPost = postController.findLatestPost(startOfMonth, tomorrow, loginFamilyId); + PostResponse mostRecentPost = postController.findLatestPost(startOfMonth, tomorrow, PostType.SURVIVAL, loginFamilyId); if (mostRecentPost != null) { mostRecentSurvivalPostDate = mostRecentPost.createdAt().toLocalDate(); } diff --git a/gateway/src/main/java/com/oing/repository/PostRepositoryCustomImpl.java b/gateway/src/main/java/com/oing/repository/PostRepositoryCustomImpl.java index ab310fed..9b2a5ba4 100644 --- a/gateway/src/main/java/com/oing/repository/PostRepositoryCustomImpl.java +++ b/gateway/src/main/java/com/oing/repository/PostRepositoryCustomImpl.java @@ -54,11 +54,13 @@ public List findLatestPostOfEveryday(LocalDateTime startDate, LocalDateTim } @Override - public Post findLatestPost(LocalDateTime startDate, LocalDateTime endDate, String familyId) { + public Post findLatestPost(LocalDateTime startDate, LocalDateTime endDate, PostType postType, String familyId) { return queryFactory .selectFrom(post) - .where(post.familyId.eq(familyId) - .and(post.createdAt.between(startDate, endDate)) + .where( + post.type.eq(postType), + post.familyId.eq(familyId), + post.createdAt.between(startDate, endDate) ) .orderBy(post.createdAt.desc()) .fetchFirst(); diff --git a/gateway/src/test/java/com/oing/repository/PostRepositoryCustomTest.java b/gateway/src/test/java/com/oing/repository/PostRepositoryCustomTest.java index dd222b7d..30802c72 100644 --- a/gateway/src/test/java/com/oing/repository/PostRepositoryCustomTest.java +++ b/gateway/src/test/java/com/oing/repository/PostRepositoryCustomTest.java @@ -19,6 +19,7 @@ import java.time.LocalDateTime; import java.util.List; +import static com.oing.domain.PostType.*; import static org.assertj.core.api.Assertions.assertThat; @DataJpaTest @@ -116,14 +117,14 @@ void setup() { @Nested class findLatestPost { @Test - void 정상_조회_테스트() { + void 생존신고_게시물_정상_조회_테스트() { // given String familyId = testMember1.getFamilyId(); LocalDateTime inclusiveStart = LocalDate.of(2023, 11, 1).atStartOfDay(); LocalDateTime exclusiveEnd = LocalDate.of(2023, 11, 2).atStartOfDay(); // when - Post post = postRepositoryCustomImpl.findLatestPost(inclusiveStart, exclusiveEnd, familyId); + Post post = postRepositoryCustomImpl.findLatestPost(inclusiveStart, exclusiveEnd, SURVIVAL, familyId); // then assertThat(post.getId()).isEqualTo("2"); @@ -137,7 +138,7 @@ class findLatestPost { LocalDateTime exclusiveEnd = LocalDate.of(2023, 11, 1).atStartOfDay(); // when - Post post = postRepositoryCustomImpl.findLatestPost(inclusiveStart, exclusiveEnd, familyId); + Post post = postRepositoryCustomImpl.findLatestPost(inclusiveStart, exclusiveEnd, SURVIVAL, familyId); // then assertThat(post).isNull(); @@ -151,7 +152,7 @@ class findLatestPost { LocalDateTime exclusiveEnd = LocalDate.of(9999, 11, 4).atStartOfDay(); // when - Post post = postRepositoryCustomImpl.findLatestPost(inclusiveStart, exclusiveEnd, familyId); + Post post = postRepositoryCustomImpl.findLatestPost(inclusiveStart, exclusiveEnd, SURVIVAL, familyId); // then assertThat(post).isNull(); @@ -165,7 +166,7 @@ class findLatestPost { // when boolean exists = postRepositoryCustomImpl.existsByMemberIdAndFamilyIdAndTypeAndCreatedAt(testMember1.getId(), - testMember1.getFamilyId(), PostType.SURVIVAL, postDate); + testMember1.getFamilyId(), SURVIVAL, postDate); // then assertThat(exists).isTrue(); @@ -178,7 +179,7 @@ class findLatestPost { // when boolean exists = postRepositoryCustomImpl.existsByMemberIdAndFamilyIdAndTypeAndCreatedAt(testMember1.getId(), - testMember1.getFamilyId(), PostType.SURVIVAL, postDate); + testMember1.getFamilyId(), SURVIVAL, postDate); // then assertThat(exists).isFalse(); diff --git a/post/src/main/java/com/oing/controller/PostController.java b/post/src/main/java/com/oing/controller/PostController.java index a2c97f07..500f759f 100644 --- a/post/src/main/java/com/oing/controller/PostController.java +++ b/post/src/main/java/com/oing/controller/PostController.java @@ -98,8 +98,8 @@ public PostResponse getPost(String postId, String loginMemberId) { } @Override - public PostResponse findLatestPost(LocalDate inclusiveStartDate, LocalDate exclusiveEndDate, String loginFamilyId) { - Post latestPost = postService.findLatestPost(inclusiveStartDate, exclusiveEndDate, loginFamilyId); + public PostResponse findLatestPost(LocalDate inclusiveStartDate, LocalDate exclusiveEndDate, PostType postType, String loginFamilyId) { + Post latestPost = postService.findLatestPost(inclusiveStartDate, exclusiveEndDate, postType, loginFamilyId); if (latestPost == null) return null; return PostResponse.from(latestPost); diff --git a/post/src/main/java/com/oing/repository/PostRepositoryCustom.java b/post/src/main/java/com/oing/repository/PostRepositoryCustom.java index eab73466..8a912308 100644 --- a/post/src/main/java/com/oing/repository/PostRepositoryCustom.java +++ b/post/src/main/java/com/oing/repository/PostRepositoryCustom.java @@ -13,7 +13,7 @@ public interface PostRepositoryCustom { List findLatestPostOfEveryday(LocalDateTime startDate, LocalDateTime endDate, String familyId); - Post findLatestPost(LocalDateTime startDate, LocalDateTime endDate, String familyId); + Post findLatestPost(LocalDateTime startDate, LocalDateTime endDate, PostType postType, String familyId); QueryResults searchPosts(int page, int size, LocalDate date, String memberId, String requesterMemberId, String familyId, boolean asc, PostType type); diff --git a/post/src/main/java/com/oing/restapi/PostApi.java b/post/src/main/java/com/oing/restapi/PostApi.java index 01044e87..feb2afc9 100644 --- a/post/src/main/java/com/oing/restapi/PostApi.java +++ b/post/src/main/java/com/oing/restapi/PostApi.java @@ -126,6 +126,10 @@ PostResponse findLatestPost( @Parameter(description = "조회 종료 날짜", example = "2023-12-05") LocalDate exclusiveEndDate, + @RequestParam + @Parameter(description = "게시물 타입", example = "SURVIVAL") + PostType type, + @Parameter(hidden = true) @LoginFamilyId String loginFamilyId diff --git a/post/src/main/java/com/oing/service/PostService.java b/post/src/main/java/com/oing/service/PostService.java index e487ffcd..9b72330d 100644 --- a/post/src/main/java/com/oing/service/PostService.java +++ b/post/src/main/java/com/oing/service/PostService.java @@ -116,8 +116,8 @@ public Post getMemberPostById(String postId) { return postRepository.findById(postId).orElseThrow(PostNotFoundException::new); } - public Post findLatestPost(LocalDate inclusiveStartDate, LocalDate exclusiveEndDate, String loginFamilyId) { - return postRepository.findLatestPost(inclusiveStartDate.atStartOfDay(), exclusiveEndDate.atStartOfDay(), loginFamilyId); + public Post findLatestPost(LocalDate inclusiveStartDate, LocalDate exclusiveEndDate, PostType postType, String loginFamilyId) { + return postRepository.findLatestPost(inclusiveStartDate.atStartOfDay(), exclusiveEndDate.atStartOfDay(), postType, loginFamilyId); } public PaginationDTO searchMemberPost(int page, int size, LocalDate date, String memberId, String requesterMemberId, From ebfaba11430b3e72dbd197073b170d4fe2e86339 Mon Sep 17 00:00:00 2001 From: Kwon770 Date: Thu, 9 May 2024 23:23:35 +0900 Subject: [PATCH 7/7] feat: Add createdAt index at post table --- .../migration/V202405092310__add_createdAt_idx_in_post_tbl.sql | 1 + 1 file changed, 1 insertion(+) create mode 100644 gateway/src/main/resources/db/migration/V202405092310__add_createdAt_idx_in_post_tbl.sql diff --git a/gateway/src/main/resources/db/migration/V202405092310__add_createdAt_idx_in_post_tbl.sql b/gateway/src/main/resources/db/migration/V202405092310__add_createdAt_idx_in_post_tbl.sql new file mode 100644 index 00000000..247a99c2 --- /dev/null +++ b/gateway/src/main/resources/db/migration/V202405092310__add_createdAt_idx_in_post_tbl.sql @@ -0,0 +1 @@ +ALTER TABLE post ADD INDEX post_idx2 (created_at); \ No newline at end of file