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

[Feat] 챌린지 상세 전체기록 페이지네이션 조회 API #265

Merged
merged 3 commits into from
Mar 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,13 @@ public ApiResponse<List<InsightGetForHomeResponse>> getBookmarkedInsight(
) {
return ApiResponse.ok(insightQueryApiService.getInsightForBookmark(CursorPageable.of(cursor, limit)));
}

@GetMapping("/challenge/my")
public ApiResponse<List<InsightGetForHomeResponse>> paginateInsightsOfChallenge(
@RequestParam(required = false, defaultValue = KeeweConsts.LONG_MAX_STRING) Long cursor,
@RequestParam Long limit,
@RequestParam(required = false) Long writerId
) {
return ApiResponse.ok(insightQueryApiService.paginateInsightsOfChallenge(CursorPageable.of(cursor, limit), writerId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,25 @@
import ccc.keeweapi.dto.insight.InsightGetResponse;
import ccc.keeweapi.dto.insight.InsightMyPageResponse;
import ccc.keeweapi.utils.SecurityUtil;
import ccc.keewecore.consts.KeeweRtnConsts;
import ccc.keewecore.exception.KeeweException;
import ccc.keewedomain.dto.insight.InsightGetDto;
import ccc.keewedomain.persistence.domain.challenge.Challenge;
import ccc.keewedomain.persistence.domain.challenge.ChallengeParticipation;
import ccc.keewedomain.persistence.domain.insight.Insight;
import ccc.keewedomain.persistence.domain.user.User;
import ccc.keewedomain.persistence.repository.utils.CursorPageable;
import ccc.keewedomain.service.insight.command.InsightCommandDomainService;
import ccc.keewedomain.service.challenge.query.ChallengeParticipateQueryDomainService;
import ccc.keewedomain.service.insight.query.InsightQueryDomainService;
import ccc.keewedomain.service.user.ProfileDomainService;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class InsightQueryApiService {
Expand All @@ -30,6 +35,7 @@ public class InsightQueryApiService {
private final ProfileDomainService profileDomainService;
private final InsightAssembler insightAssembler;
private final ProfileAssembler profileAssembler;
private final ChallengeParticipateQueryDomainService challengeParticipateQueryDomainService;

@Transactional(readOnly = true)
public InsightGetResponse getInsight(Long insightId) {
Expand All @@ -52,6 +58,19 @@ public List<InsightGetForHomeResponse> getInsightsForHome(CursorPageable<Long> c
.collect(Collectors.toList());
}

// FIXME DTO 수정할 때 같이 네이밍 수정 필요
@Transactional(readOnly = true)
public List<InsightGetForHomeResponse> paginateInsightsOfChallenge(CursorPageable<Long> cPage, Long writerId) {
User user = SecurityUtil.getUser();
Challenge challenge = challengeParticipateQueryDomainService.findCurrentParticipationByUserId(user.getId())
.map(ChallengeParticipation::getChallenge)
.orElseThrow(() -> new KeeweException(KeeweRtnConsts.ERR432));

return insightQueryDomainService.getByChallenge(challenge, user, cPage, writerId).stream()
.map(insightAssembler::toInsightGetForHomeResponse)
.collect(Collectors.toList());
}

@Transactional(readOnly = true)
public List<InsightGetForHomeResponse> getInsightForBookmark(CursorPageable<Long> cPage) {
return insightQueryDomainService.getInsightForBookmark(SecurityUtil.getUser(), cPage).stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,4 +453,60 @@ void get_insight_for_bookmark() throws Exception {
)));
}


@Test
@DisplayName("나의 챌린지 전체 기록(인사이트) 조회 API")
void get_insights_of_my_challenge() throws Exception {
long cursor = Long.MAX_VALUE;
long limit = 10L;

when(insightQueryApiService.paginateInsightsOfChallenge(any(), any())).thenReturn(List.of(InsightGetForHomeResponse.of(
1L,
"인사이트 내용입니다. 즐거운 개발 되세요!",
true,
Link.of("www.keewe.com"),
ReactionAggregationResponse.of(1L, 2L, 3L, 4L, 5L, 6L),
LocalDateTime.now().toString(),
InsightWriterDto.of(1L, "nickname", "title", "image")
)));

ResultActions resultActions = mockMvc.perform(get("/api/v1/insight/challenge/my")
.header(HttpHeaders.AUTHORIZATION, "Bearer " + JWT)
.param("cursor", Long.toString(cursor))
.param("limit", Long.toString(limit))
.param("writerId", Long.toString(1L)))
.andExpect(status().isOk());

resultActions.andDo(restDocs.document(resource(
ResourceSnippetParameters.builder()
.description("나의 챌린지 전체 기록(인사이트) 조회 API 입니다.")
.summary("나의 챌린지 전체 기록(인사이트) 조회 API")
.requestHeaders(
headerWithName("Authorization").description("유저의 JWT"))
.requestParameters(
parameterWithName("cursor").description("마지막으로 받은 인사이트 ID"),
parameterWithName("limit").description("가져올 인사이트 개수"),
parameterWithName("writerId").optional().description("인사이트 작성자 필터링. 미포함 시 전체 조회"))
.responseFields(
fieldWithPath("message").description("요청 결과 메세지"),
fieldWithPath("code").description("결과 코드"),
fieldWithPath("data[].id").description("인사이트 ID"),
fieldWithPath("data[].contents").description("인사이트 내용"),
fieldWithPath("data[].bookmark").description("인사이트 북마크 여부"),
fieldWithPath("data[].link.url").description("인사이트 링크"),
fieldWithPath("data[].reaction.clap").description("인사이트 박수 반응 수"),
fieldWithPath("data[].reaction.heart").description("인사이트 하트 반응 수"),
fieldWithPath("data[].reaction.sad").description("인사이트 슬픔 반응 수"),
fieldWithPath("data[].reaction.surprise").description("인사이트 놀람 반응 수"),
fieldWithPath("data[].reaction.fire").description("인사이트 불 반응 수"),
fieldWithPath("data[].reaction.eyes").description("인사이트 눈 반응 수"),
fieldWithPath("data[].createdAt").description("인사이트 생성 시간"),
fieldWithPath("data[].writer.writerId").description("인사이트 저자 ID"),
fieldWithPath("data[].writer.nickname").description("인사이트 저자 닉네임"),
fieldWithPath("data[].writer.title").description("인사이트 저자 타이틀"),
fieldWithPath("data[].writer.image").description("인사이트 저자 사진"))
.tag("Insight")
.build()
)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import static ccc.keewedomain.persistence.domain.challenge.QChallenge.challenge;
import static ccc.keewedomain.persistence.domain.challenge.QChallengeParticipation.challengeParticipation;
import static ccc.keewedomain.persistence.domain.common.QInterest.interest;
import static ccc.keewedomain.persistence.domain.insight.QBookmark.bookmark;
import static ccc.keewedomain.persistence.domain.insight.QInsight.insight;
import static ccc.keewedomain.persistence.domain.user.QFollow.follow;
import static ccc.keewedomain.persistence.domain.user.QProfilePhoto.profilePhoto;
import static ccc.keewedomain.persistence.domain.user.QUser.user;

import ccc.keewedomain.persistence.domain.challenge.Challenge;
Expand All @@ -26,8 +28,6 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import static ccc.keewedomain.persistence.domain.insight.QBookmark.*;

@Repository
@RequiredArgsConstructor
public class InsightQueryRepository {
Expand Down Expand Up @@ -194,7 +194,28 @@ private JPQLQuery<Insight> findBookmarkedInsightId(User user) {
.where(bookmark.user.id.eq(user.getId()));
}

public List<Insight> findByChallenge(Challenge challenge, CursorPageable<Long> cPage, Long writerId) {
return queryFactory
.select(insight)
.from(insight)
.innerJoin(insight.challengeParticipation, challengeParticipation)
.innerJoin(insight.writer, user)
.fetchJoin()
.innerJoin(user.profilePhoto, profilePhoto)
.fetchJoin()
.where(insight.challengeParticipation.challenge.eq(challenge)
.and(insight.id.lt(cPage.getCursor()))
.and(writerIdEq(writerId))
)
.orderBy(insight.id.desc())
.fetch();
}

private BooleanExpression drawerIdEq(Long drawerId) {
return drawerId != null ? insight.drawer.id.eq(drawerId) : null;
}

private BooleanExpression writerIdEq(Long writerId) {
return writerId != null ? insight.writer.id.eq(writerId) : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,30 @@ public List<InsightGetForHomeDto> getInsightForBookmark(User user, CursorPageabl
).collect(Collectors.toList());
}

// FIXME dto 개선 필요
@Transactional(readOnly = true)
public List<InsightGetForHomeDto> getByChallenge(Challenge challenge, User user, CursorPageable<Long> cPage, Long writerId) {
List<Insight> insights = insightQueryRepository.findByChallenge(challenge, cPage, writerId);
Map<Long, Boolean> bookmarkPresence = bookmarkQueryDomainService.getBookmarkPresenceMap(user, insights);

return insights.parallelStream().map(i ->
InsightGetForHomeDto.of(
i.getId(),
i.getContents(),
bookmarkPresence.getOrDefault(i.getId(), false),
i.getLink(),
this.getCurrentReactionAggregation(i.getId()),
i.getCreatedAt(),
InsightWriterDto.of(
i.getWriter().getId(),
i.getWriter().getNickname(),
i.getWriter().getRepTitleName(),
i.getWriter().getProfilePhotoURL()
)
)
).collect(Collectors.toList());
}

public Map<Long, Long> getInsightCountPerChallenge(List<Challenge> challenges) {
return insightQueryRepository.countPerChallenge(challenges);
}
Expand Down Expand Up @@ -192,3 +216,4 @@ private ReactionAggregationGetDto getCurrentReactionAggregation(Long insightId)
));
}
}