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] 나의 챌린지 상세 인사이트 수 조회 #258

Merged
merged 6 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
@@ -1,6 +1,23 @@
package ccc.keeweapi.component;

import ccc.keeweapi.dto.challenge.*;
import ccc.keeweapi.dto.challenge.ChallengeCreateRequest;
import ccc.keeweapi.dto.challenge.ChallengeCreateResponse;
import ccc.keeweapi.dto.challenge.ChallengeDetailResponse;
import ccc.keeweapi.dto.challenge.ChallengeInsightNumberResponse;
import ccc.keeweapi.dto.challenge.ChallengeParticipateRequest;
import ccc.keeweapi.dto.challenge.ChallengeParticipationResponse;
import ccc.keeweapi.dto.challenge.ChallengeStatisticsResponse;
import ccc.keeweapi.dto.challenge.ChallengerCountResponse;
import ccc.keeweapi.dto.challenge.DayProgressResponse;
import ccc.keeweapi.dto.challenge.FinishedChallengeCountResponse;
import ccc.keeweapi.dto.challenge.FinishedChallengeResponse;
import ccc.keeweapi.dto.challenge.FriendResponse;
import ccc.keeweapi.dto.challenge.MyParticipationProgressResponse;
import ccc.keeweapi.dto.challenge.OpenedChallengeResponse;
import ccc.keeweapi.dto.challenge.ParticipatingChallengeDetailResponse;
import ccc.keeweapi.dto.challenge.ParticipatingChallengeResponse;
import ccc.keeweapi.dto.challenge.ParticipationCheckResponse;
import ccc.keeweapi.dto.challenge.WeekProgressResponse;
import ccc.keeweapi.utils.SecurityUtil;
import ccc.keewedomain.dto.challenge.ChallengeCreateDto;
import ccc.keewedomain.dto.challenge.ChallengeParticipateDto;
Expand Down Expand Up @@ -170,6 +187,10 @@ public ParticipatingChallengeDetailResponse toParticipatingChallengeDetailRespon
);
}

public ChallengeInsightNumberResponse toChallengeInsightNumberResponse(Long insightNumber) {
return ChallengeInsightNumberResponse.of(insightNumber);
}

public ChallengeStatisticsResponse toChallengeStatisticsResponse(
Long viewCount,
Long reactionCount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import ccc.keeweapi.dto.challenge.ChallengeCreateRequest;
import ccc.keeweapi.dto.challenge.ChallengeCreateResponse;
import ccc.keeweapi.dto.challenge.ChallengeDetailResponse;
import ccc.keeweapi.dto.challenge.ChallengeInsightNumberResponse;
import ccc.keeweapi.dto.challenge.ChallengeStatisticsResponse;
import ccc.keeweapi.dto.challenge.OpenedChallengeResponse;
import ccc.keeweapi.dto.challenge.ParticipatingChallengeDetailResponse;
Expand All @@ -12,8 +13,6 @@
import ccc.keewecore.aop.annotations.FLogging;
import ccc.keewecore.consts.KeeweConsts;
import ccc.keewedomain.persistence.repository.utils.CursorPageable;
import java.util.List;
import javax.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -24,6 +23,9 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/v1/challenge")
@RequiredArgsConstructor
Expand Down Expand Up @@ -65,4 +67,11 @@ public ApiResponse<ParticipatingChallengeDetailResponse> getMyChallengeDetail()
public ApiResponse<ChallengeStatisticsResponse> aggregateChallengeStatistics() {
return ApiResponse.ok(challengeQueryApiService.aggregateChallengeStatistics());
}

@GetMapping("/my/insight/count")
public ApiResponse<ChallengeInsightNumberResponse> countInsightOfChallenge(
@RequestParam(required = false) Long writerId
) {
return ApiResponse.ok(challengeApiService.countInsightOfChallenge(writerId));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ccc.keeweapi.dto.challenge;

import lombok.AllArgsConstructor;
import lombok.Getter;

@AllArgsConstructor(staticName = "of")
@Getter
public class ChallengeInsightNumberResponse {
private long insightNumber;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import ccc.keeweapi.dto.challenge.ChallengeCreateRequest;
import ccc.keeweapi.dto.challenge.ChallengeCreateResponse;
import ccc.keeweapi.dto.challenge.ChallengeDetailResponse;
import ccc.keeweapi.dto.challenge.ChallengeInsightNumberResponse;
import ccc.keeweapi.dto.challenge.ChallengeParticipateRequest;
import ccc.keeweapi.dto.challenge.ChallengeParticipationResponse;
import ccc.keeweapi.dto.challenge.ChallengerCountResponse;
Expand All @@ -24,17 +25,18 @@
import ccc.keewedomain.service.challenge.query.ChallengeQueryDomainService;
import ccc.keewedomain.service.insight.query.InsightQueryDomainService;
import ccc.keewedomain.service.user.ProfileDomainService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
Expand Down Expand Up @@ -108,7 +110,7 @@ public ParticipatingChallengeResponse getParticipatingChallenege() {

public ChallengeDetailResponse getChallengeDetail(Long challengeId) {
Challenge challenge = challengeQueryDomainService.getByIdOrElseThrow(challengeId);
Long insightCount = insightQueryDomainService.getInsightCountByChallenge(challenge);
Long insightCount = insightQueryDomainService.getInsightCountByChallenge(challenge, null);
return challengeAssembler.toChallengeDetailResponse(challenge, insightCount);
}

Expand Down Expand Up @@ -155,4 +157,11 @@ public ParticipatingChallengeDetailResponse getMyChallengeDetail() {
.orElseThrow(() -> new KeeweException(KeeweRtnConsts.ERR432));
return challengeAssembler.toParticipatingChallengeDetailResponse(challenge);
}

public ChallengeInsightNumberResponse countInsightOfChallenge(Long writerId) {
Long insightNumber = challengeParticipateQueryDomainService.findCurrentParticipationByUserId(SecurityUtil.getUserId())
.map(participation -> insightQueryDomainService.getInsightCountByChallenge(participation.getChallenge(), writerId))
.orElseThrow(() -> new KeeweException(KeeweRtnConsts.ERR432));
return challengeAssembler.toChallengeInsightNumberResponse(insightNumber);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import ccc.keeweapi.document.utils.ApiDocumentationTest;
import ccc.keeweapi.dto.challenge.ChallengeCreateResponse;
import ccc.keeweapi.dto.challenge.ChallengeDetailResponse;
import ccc.keeweapi.dto.challenge.ChallengeInsightNumberResponse;
import ccc.keeweapi.dto.challenge.ChallengeStatisticsResponse;
import ccc.keeweapi.dto.challenge.OpenedChallengeResponse;
import ccc.keeweapi.dto.challenge.ParticipatingChallengeDetailResponse;
Expand Down Expand Up @@ -248,4 +249,36 @@ void aggregation_reaction_of_challenge() throws Exception {
.build()
)));
}

@Test
@DisplayName("나의 챌린지 인사이트 수 조회 API")
void get_challenge_insight_count() throws Exception {
ChallengeInsightNumberResponse response = ChallengeInsightNumberResponse.of(100);

when(challengeApiService.countInsightOfChallenge(any())).thenReturn(response);

ResultActions resultActions = mockMvc.perform(get("/api/v1/challenge/my/insight/count")
.header(HttpHeaders.AUTHORIZATION, "Bearer " + JWT)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());

resultActions.andDo(restDocs.document(resource(
ResourceSnippetParameters.builder()
.description("나의 챌린지 인사이트 수 조회 API 입니다.")
.summary("나의 챌린지 인사이트 수 조회 API")
.requestParameters(
parameterWithName("writerId").optional().description("필터링할 작성자 ID, 없을 시 전체 조회")
)
.requestHeaders(
headerWithName("Authorization").description("유저의 JWT")
)
.responseFields(
fieldWithPath("message").description("요청 결과 메세지"),
fieldWithPath("code").description("결과 코드"),
fieldWithPath("data.insightNumber").description("챌린지의 전체 인사이트 수")
)
.tag("Challenge")
.build()
)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ public Map<Long, Long> countValidPerParticipation(List<ChallengeParticipation> p
.transform(GroupBy.groupBy(challengeParticipation.id).as(insight.count()));
}


public List<Insight> findAllForHome(User user, CursorPageable<Long> cPage, Boolean follow) {
BooleanExpression followFilter = Expressions.asBoolean(true).isTrue();
if (follow != null && follow)
Expand Down Expand Up @@ -154,13 +153,15 @@ public Map<Long, Long> countPerChallenge(List<Challenge> challenges) {
.transform(GroupBy.groupBy(challenge.id).as(insight.count()));
}

public Long countByChallenge(Challenge target) {
public Long countByChallenge(Challenge target, Long writerId) {
return queryFactory
.select(insight.count())
.from(insight)
.innerJoin(insight.challengeParticipation, challengeParticipation)
.innerJoin(challengeParticipation.challenge, challenge)
.where(challenge.eq(target))
.where(challenge.eq(target)
.and(writerIdEq(writerId))
)
.fetchFirst();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ public Map<Long, Long> getInsightCountPerChallenge(List<Challenge> challenges) {
return insightQueryRepository.countPerChallenge(challenges);
}

public Long getInsightCountByChallenge(Challenge challenge) {
return insightQueryRepository.countByChallenge(challenge);
public Long getInsightCountByChallenge(Challenge challenge, Long writerId) {
return insightQueryRepository.countByChallenge(challenge, writerId);
}

public Map<Long, Long> getInsightCountPerParticipation(List<ChallengeParticipation> participations) {
Expand Down