Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OING-330] feat: 가족 구성원 랭킹에서 '지난 날 생존신고 보기'에 대한 보충 정책 반영 #250

Merged
merged 7 commits into from
May 9, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,11 @@ public FamilyMemberMonthlyRankingResponse getFamilyMemberMonthlyRanking(String l
}

LocalDate mostRecentSurvivalPostDate = null;
List<PostResponse> 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, PostType.SURVIVAL, loginFamilyId);
if (mostRecentPost != null) {
mostRecentSurvivalPostDate = mostRecentPost.createdAt().toLocalDate();
}

return new FamilyMemberMonthlyRankingResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,19 @@ public List<Post> findLatestPostOfEveryday(LocalDateTime startDate, LocalDateTim
))
.orderBy(post.createdAt.asc())
.fetch();
}

@Override
public Post findLatestPost(LocalDateTime startDate, LocalDateTime endDate, PostType postType, String familyId) {
return queryFactory
.selectFrom(post)
.where(
post.type.eq(postType),
post.familyId.eq(familyId),
post.createdAt.between(startDate, endDate)
)
.orderBy(post.createdAt.desc())
Kwon770 marked this conversation as resolved.
Show resolved Hide resolved
.fetchFirst();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,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
Expand Down Expand Up @@ -112,14 +114,59 @@ 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, SURVIVAL, 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, SURVIVAL, 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, SURVIVAL, familyId);

// then
assertThat(post).isNull();
}
}

@Test
void 특정_날짜에_게시글이_존재하는지_확인한다() {
// given
LocalDate postDate = LocalDate.of(2023, 11, 1);

// when
boolean exists = postRepositoryCustomImpl.existsByMemberIdAndFamilyIdAndTypeAndCreatedAt(testMember1.getId(),
testMember1.getFamilyId(), PostType.SURVIVAL, postDate);
testMember1.getFamilyId(), SURVIVAL, postDate);

// then
assertThat(exists).isTrue();
Expand All @@ -132,7 +179,7 @@ void setup() {

// when
boolean exists = postRepositoryCustomImpl.existsByMemberIdAndFamilyIdAndTypeAndCreatedAt(testMember1.getId(),
testMember1.getFamilyId(), PostType.SURVIVAL, postDate);
testMember1.getFamilyId(), SURVIVAL, postDate);

// then
assertThat(exists).isFalse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ void setUp() {


@Nested
class 금월의_가족구성원_월간_랭킹_조회 {
class getFamilyMemberMonthlyRanking {
@Test
void 정상_조회() throws Exception {
// given
Expand Down
8 changes: 8 additions & 0 deletions post/src/main/java/com/oing/controller/PostController.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ public PostResponse getPost(String postId, String loginMemberId) {
return PostResponse.from(memberPostProjection);
}

@Override
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);
}

@Override
public SurvivalUploadStatusResponse getSurvivalUploadStatus(String memberId, String loginMemberId, String loginFamilyId) {
validateMemberId(loginMemberId, memberId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public interface PostRepositoryCustom {

List<Post> findLatestPostOfEveryday(LocalDateTime startDate, LocalDateTime endDate, String familyId);

Post findLatestPost(LocalDateTime startDate, LocalDateTime endDate, PostType postType, String familyId);

QueryResults<Post> searchPosts(int page, int size, LocalDate date, String memberId, String requesterMemberId,
String familyId, boolean asc, PostType type);

Expand Down
22 changes: 21 additions & 1 deletion post/src/main/java/com/oing/restapi/PostApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -115,6 +114,27 @@ PostResponse getPost(
String loginMemberId
);

@Operation(summary = "가장 최근 게시물 조회", description = "(결과가 없을시, null 반환)\n 특정 기간 안에서 가장 최근에 업로드된 게시물을 조회합니다.")
PostResponse findLatestPost(
@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,

@RequestParam
@Parameter(description = "게시물 타입", example = "SURVIVAL")
PostType type,

@Parameter(hidden = true)
@LoginFamilyId
String loginFamilyId
);

@Operation(summary = "회원 생존신고 게시글 업로드 여부 응답 조회", description = "회원 생존신고 게시글 업로드 여부를 조회합니다.")
@GetMapping("/{memberId}/survival-uploaded")
SurvivalUploadStatusResponse getSurvivalUploadStatus(
Expand Down
4 changes: 4 additions & 0 deletions post/src/main/java/com/oing/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ public Post getMemberPostById(String postId) {
return postRepository.findById(postId).orElseThrow(PostNotFoundException::new);
}

public Post findLatestPost(LocalDate inclusiveStartDate, LocalDate exclusiveEndDate, PostType postType, String loginFamilyId) {
return postRepository.findLatestPost(inclusiveStartDate.atStartOfDay(), exclusiveEndDate.atStartOfDay(), postType, loginFamilyId);
}

public PaginationDTO<Post> searchMemberPost(int page, int size, LocalDate date, String memberId, String requesterMemberId,
String familyId, boolean asc, PostType type) {
QueryResults<Post> results = null;
Expand Down
Loading