Skip to content

Commit

Permalink
[OING-304] feature: 회원 생존신고 게시글 업로드 여부 / 가족 미션 참여 가능 여부 확인 API 구현 (#240)
Browse files Browse the repository at this point in the history
* feature: add getSurvivalUploadStatus API logic

* feature: add getMissionAvailableStatus API

* test: add getMissionAvailable Repository Test

* test: add getMissionAvailable and getSurvivalUploadStatus Api Test

* style: change response dto field name

* feature: add getMissionAvailableStatus and getSurvivalUploadStatus logic to getDaytimePage

* refactor: correct anti code

* refactor: add member.deletedAt.isNull condition to isCreatedSurvivalPostByMajority
  • Loading branch information
Ji-soo708 authored Apr 24, 2024
1 parent df906d4 commit 7f36605
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ public class MainViewController implements MainViewApi {

@Override
public DaytimePageResponse getDaytimePage(
boolean isMissionUnlocked,
boolean isMeUploadedToday,
String loginMemberId
) {
String familyId = memberBridge.getFamilyIdByMemberId(loginMemberId);
Expand Down Expand Up @@ -68,6 +66,10 @@ public DaytimePageResponse getDaytimePage(

String todayMissionId = missionBridge.getTodayMissionId();
String dailyMissionContent = missionBridge.getContentByMissionId(todayMissionId);
boolean isMissionUnlocked = postController.getMissionAvailableStatus(loginMemberId, loginMemberId, familyId)
.isMissionUnlocked();
boolean isMeUploadedToday = postController.getSurvivalUploadStatus(loginMemberId, loginMemberId, familyId)
.isMeUploadedToday();
int leftUploadCountUntilMissionUnlock = postController.getRemainingSurvivalPostCount(loginMemberId, loginMemberId, familyId)
.leftUploadCountUntilMissionUnlock();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,27 @@ public boolean existsByMemberIdAndFamilyIdAndTypeAndCreatedAt(String memberId, S
}

@Override
public boolean isCreatedSurvivalPostByMajority(LocalDate date, String familyId) {
long totalFamilyMembers = queryFactory
.select(member.count())
.from(member)
.where(member.familyId.eq(familyId)
.and(member.deletedAt.isNull()))
.fetchFirst();

long survivalPostCount = queryFactory
.select(post.count())
.from(post)
.where(
post.familyId.eq(familyId),
post.type.eq(PostType.SURVIVAL),
dateExpr(post.createdAt).eq(date)
)
.fetchFirst();

return survivalPostCount >= totalFamilyMembers / 2;
}

public int calculateRemainingSurvivalPostCount(String familyId) {
Long familyMemberCount = queryFactory
.select(member.id.count())
Expand Down
10 changes: 0 additions & 10 deletions gateway/src/main/java/com/oing/restapi/MainViewApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
Expand All @@ -27,15 +26,6 @@ public interface MainViewApi {
@Operation(summary = "주간의 메인 페이지 조회")
@GetMapping("/daytime-page")
DaytimePageResponse getDaytimePage(

@RequestParam(required = false, defaultValue = "true")
@Parameter(description = "(디버그용) 미션 해금 여부 조작 필드", example = "true")
boolean isMissionUnlocked,

@RequestParam(required = false, defaultValue = "true")
@Parameter(description = "(디버그용) 오늘 나 업로드 여부 조작 필드", example = "true")
boolean isMeUploadedToday,

@Parameter(hidden = true)
@LoginMemberId
String loginMemberId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,31 @@ void setup() {
}

@Test
void 미션_키_획득한_날짜에_가족의_미션_키_획득_여부를_조회한다() {
// given
String familyId = testMember1.getFamilyId();
LocalDate today = LocalDate.of(2023, 11, 1);

// when
boolean exists = postRepositoryCustomImpl.isCreatedSurvivalPostByMajority(today, familyId);

// then
assertThat(exists).isTrue();
}

@Test
void 미션_키_획득하지_못한_날짜에_가족의_미션_키_획득_여부를_조회한다() {
// given
String familyId = testMember1.getFamilyId();
LocalDate today = LocalDate.of(2024, 4, 1);

// when
boolean exists = postRepositoryCustomImpl.isCreatedSurvivalPostByMajority(today, familyId);

// then
assertThat(exists).isFalse();
}

void 가족_구성원_수가_짝수인_경우_남은_생존게시글_업로드_수를_확인한다() {
// given
String familyId = testMember1.getFamilyId();
Expand Down
38 changes: 38 additions & 0 deletions gateway/src/test/java/com/oing/restapi/PostApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,42 @@ void setUp() {
.andExpect(status().isOk())
.andExpect(jsonPath("$.leftUploadCountUntilMissionUnlock").value(0));
}

@Test
void 회원_생존신고_게시글_업로드_여부_조회_테스트() throws Exception {
//given
postRepository.save(new Post(TEST_POST_ID, TEST_MEMBER1_ID, TEST_FAMILY_ID, PostType.SURVIVAL, "img", "img",
"content"));

//when
ResultActions resultActions = mockMvc.perform(
get("/v1/posts/{memberId}/survival-uploaded", TEST_MEMBER1_ID)
.header("X-AUTH-TOKEN", TEST_MEMBER1_TOKEN)
.contentType(MediaType.APPLICATION_JSON)
);

//then
resultActions
.andExpect(status().isOk())
.andExpect(jsonPath("$.isMeUploadedToday").value(true));
}

@Test
void 해당_가족의_미션_키_획득_여부_조회_테스트() throws Exception {
//given
postRepository.save(new Post(TEST_POST_ID, TEST_MEMBER1_ID, TEST_FAMILY_ID, PostType.SURVIVAL, "img", "img",
"content"));

//when
ResultActions resultActions = mockMvc.perform(
get("/v1/posts/{memberId}/mission-available", TEST_MEMBER1_ID)
.header("X-AUTH-TOKEN", TEST_MEMBER1_TOKEN)
.contentType(MediaType.APPLICATION_JSON)
);

//then
resultActions
.andExpect(status().isOk())
.andExpect(jsonPath("$.isMissionUnlocked").value(true));
}
}
16 changes: 8 additions & 8 deletions post/src/main/java/com/oing/controller/PostController.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.springframework.stereotype.Controller;

import java.time.LocalDate;
import java.time.ZonedDateTime;

/**
* no5ing-server
Expand Down Expand Up @@ -91,22 +92,21 @@ public PostResponse getPost(String postId, String loginMemberId) {
}

@Override
public SurvivalUploadStatusResponse getSurvivalUploadStatus(String memberId, String loginMemberId, boolean valid) {
public SurvivalUploadStatusResponse getSurvivalUploadStatus(String memberId, String loginMemberId, String loginFamilyId) {
validateMemberId(loginMemberId, memberId);
// TODO: 추후 valid 파라미터 삭제
// TODO: 해당 회원이 생존신고 글을 올렸는지 검증 로직 추가
if (valid) {

if (postService.existsByMemberIdAndFamilyIdAndTypeAndCreatedAt(memberId, loginFamilyId, PostType.SURVIVAL, LocalDate.now())) {
return new SurvivalUploadStatusResponse(true);
}
return new SurvivalUploadStatusResponse(false);
}

@Override
public MissionAvailableStatusResponse getMissionAvailableStatus(String memberId, String loginMemberId, boolean valid) {
public MissionAvailableStatusResponse getMissionAvailableStatus(String memberId, String loginMemberId, String loginFamilyId) {
validateMemberId(loginMemberId, memberId);
// TODO: 추후 valid 파라미터 삭제
// TODO: 해당 회원이 미션에 참여 가능한 회원인지 검증 로직 추가
if (valid) {
LocalDate today = ZonedDateTime.now().toLocalDate();

if (postService.isCreatedSurvivalPostByMajority(today, loginFamilyId)) {
return new MissionAvailableStatusResponse(true);
}
return new MissionAvailableStatusResponse(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import io.swagger.v3.oas.annotations.media.Schema;

@Schema(description = "회원 미션 참여 가능 여부 응답")
@Schema(description = "가족 미션 참여 가능 (미션 키) 여부 응답")
public record MissionAvailableStatusResponse(
@Schema(description = "회원 미션 참여 가능 여부", example = "true")
boolean isAvailable
@Schema(description = "가족 미션 참여 가능 (미션 키) 여부", example = "true")
boolean isMissionUnlocked
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
@Schema(description = "회원 생존신고 게시글 업로드 여부 응답")
public record SurvivalUploadStatusResponse(
@Schema(description = "회원 생존신고 게시글 업로드 여부", example = "true")
boolean isValid
boolean isMeUploadedToday
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ QueryResults<Post> searchPosts(int page, int size, LocalDate date, String member

boolean existsByMemberIdAndFamilyIdAndTypeAndCreatedAt(String memberId, String familyId, PostType type, LocalDate postDate);

boolean isCreatedSurvivalPostByMajority(LocalDate date, String familyId);
int calculateRemainingSurvivalPostCount(String familyId);
}
12 changes: 6 additions & 6 deletions post/src/main/java/com/oing/restapi/PostApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ SurvivalUploadStatusResponse getSurvivalUploadStatus(
@LoginMemberId
String loginMemberId,

@RequestParam
@Parameter(description = "응답 값 조작 필드", example = "true")
boolean valid
@Parameter(hidden = true)
@LoginFamilyId
String loginFamilyId
);

@Operation(summary = "회원 미션 참여 가능 여부 응답 조회", description = "회원 미션 참여 가능 여부를 조회합니다.")
Expand All @@ -141,9 +141,9 @@ MissionAvailableStatusResponse getMissionAvailableStatus(
@LoginMemberId
String loginMemberId,

@RequestParam
@Parameter(description = "응답 값 조작 필드", example = "true")
boolean valid
@Parameter(hidden = true)
@LoginFamilyId
String loginFamilyId
);

@Operation(summary = "미션 키 획득까지 남은 생존신고 업로드 수", description = "미션 키 획득까지 남은 생존신고 업로드 수를 조회합니다.")
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 @@ -161,6 +161,10 @@ public boolean existsByMemberIdAndFamilyIdAndTypeAndCreatedAt(String memberId, S
return postRepository.existsByMemberIdAndFamilyIdAndTypeAndCreatedAt(memberId, familyId, type, postDate);
}

public boolean isCreatedSurvivalPostByMajority(LocalDate date, String familyId) {
return postRepository.isCreatedSurvivalPostByMajority(date, familyId);
}

public int calculateRemainingSurvivalPostCount(String familyId) {
return postRepository.calculateRemainingSurvivalPostCount(familyId);
}
Expand Down

0 comments on commit 7f36605

Please sign in to comment.