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 #123

Merged
merged 3 commits into from
Oct 9, 2022
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,7 @@
package ccc.keeweapi.api.insight;

import ccc.keeweapi.dto.ApiResponse;
import ccc.keeweapi.dto.insight.InsightAuthorAreaResponse;
import ccc.keeweapi.dto.insight.InsightCreateRequest;
import ccc.keeweapi.dto.insight.InsightCreateResponse;
import ccc.keeweapi.service.insight.InsightApiService;
Expand All @@ -26,4 +27,9 @@ public ApiResponse incrementViewCount(@PathVariable Long insightId) {
return ApiResponse.ok(insightApiService.incrementViewCount(insightId));
}

@GetMapping("/author/{insightId}")
public ApiResponse<InsightAuthorAreaResponse> getInsightAuthorAreaInfo(@PathVariable Long insightId) {
return ApiResponse.ok(insightApiService.getInsightAuthorAreaInfo(insightId));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import ccc.keeweapi.dto.insight.*;
import ccc.keeweapi.utils.SecurityUtil;
import ccc.keewedomain.persistence.domain.insight.Drawer;
import ccc.keewedomain.persistence.domain.insight.Insight;
import ccc.keewedomain.persistence.domain.insight.Comment;
import ccc.keewedomain.dto.insight.CommentCreateDto;
import ccc.keewedomain.dto.insight.DrawerCreateDto;
import ccc.keewedomain.dto.insight.InsightCreateDto;
import ccc.keewedomain.dto.insight.InsightViewIncrementDto;
import ccc.keewedomain.persistence.domain.insight.Comment;
import ccc.keewedomain.persistence.domain.insight.Drawer;
import ccc.keewedomain.persistence.domain.insight.Insight;
import ccc.keewedomain.persistence.domain.user.User;
import org.springframework.stereotype.Component;

@Component
Expand Down Expand Up @@ -54,4 +55,13 @@ public CommentCreateDto toCommentCreateDto(CommentCreateRequest request) {
public CommentCreateResponse toCommentCreateResponse(Comment comment) {
return CommentCreateResponse.of(comment.getId());
}

public InsightAuthorAreaResponse toInsightAuthorAreaResponse(Insight insight) {
User writer = insight.getWriter();
return InsightAuthorAreaResponse.of(insight.getId(), writer.getNickname(), "title", writer.getInterests(), "www.api-keewe.com/images" , isAuthor(insight), insight.getCreatedAt().toString());
}

private boolean isAuthor(Insight insight) {
return insight.getWriter().getId() == SecurityUtil.getUserId();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ccc.keeweapi.dto.insight;


import ccc.keewedomain.persistence.domain.common.Interest;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.List;

@AllArgsConstructor(staticName = "of")
@Getter
public class InsightAuthorAreaResponse {
private Long authorId;
private String nickname;
private String title;
private List<Interest> interests;
private String image;
private boolean isAuthor;
private String createdAt;
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package ccc.keeweapi.service.insight;

import ccc.keeweapi.component.InsightAssembler;
import ccc.keeweapi.dto.insight.InsightAuthorAreaResponse;
import ccc.keeweapi.dto.insight.InsightCreateRequest;
import ccc.keeweapi.dto.insight.InsightCreateResponse;
import ccc.keeweapi.dto.insight.InsightViewIncrementResponse;
import ccc.keewedomain.persistence.domain.insight.Insight;
import ccc.keewedomain.service.insight.InsightDomainService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
Expand All @@ -25,4 +27,10 @@ public InsightViewIncrementResponse incrementViewCount(Long insightId) {
Long viewCount = insightDomainService.incrementViewCount(insightAssembler.toInsightViewIncrementDto(insightId));
return insightAssembler.toInsightViewIncrementResponse(viewCount);
}

@Transactional(readOnly = true)
public InsightAuthorAreaResponse getInsightAuthorAreaInfo(Long insightId) {
Insight insight = insightDomainService.getByIdWithWriter(insightId);
return insightAssembler.toInsightAuthorAreaResponse(insight);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package ccc.keeweapi.api.insight;

import ccc.keeweapi.document.utils.ApiDocumentationTest;
import ccc.keeweapi.dto.insight.InsightAuthorAreaResponse;
import ccc.keeweapi.dto.insight.InsightCreateResponse;
import ccc.keeweapi.dto.insight.InsightViewIncrementResponse;
import ccc.keeweapi.service.insight.InsightApiService;
import ccc.keewedomain.persistence.domain.common.Interest;
import com.epages.restdocs.apispec.ResourceSnippetParameters;
import org.json.JSONObject;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -16,11 +18,15 @@
import org.springframework.restdocs.RestDocumentationContextProvider;
import org.springframework.test.web.servlet.ResultActions;

import java.time.LocalDateTime;
import java.util.List;

import static com.epages.restdocs.apispec.ResourceDocumentation.headerWithName;
import static com.epages.restdocs.apispec.ResourceDocumentation.resource;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.when;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
Expand Down Expand Up @@ -107,4 +113,48 @@ void increment_insight_views() throws Exception {
.build()
)));
}

@Test
@DisplayName("인사이트 작성자 프로필 조회 API")
void insight_author_get() throws Exception {

when(insightApiService.getInsightAuthorAreaInfo(anyLong())).thenReturn(
InsightAuthorAreaResponse.of(1L,
"nickname",
"고급 기록가",
List.of(Interest.of("운동"), Interest.of("영화")),
"www.api-keewe.com/images/128398681",
true,
LocalDateTime.now().toString()
)
);

ResultActions resultActions = mockMvc.perform(get("/api/v1/insight/author/{insightId}", 1L)
.header(HttpHeaders.AUTHORIZATION, "Bearer " + JWT)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());

resultActions.andDo(restDocs.document(resource(
ResourceSnippetParameters.builder()
.description("인사이트 상세 조회 작성자 프로필 조회 API 입니다.")
.summary("인사이트 작성자 프로필 조회 API")
.requestHeaders(
headerWithName("Authorization").description("유저의 JWT"))
.responseFields(
fieldWithPath("message").description("요청 결과 메세지"),
fieldWithPath("code").description("결과 코드"),
fieldWithPath("data.authorId").description("작성자 ID (유저 ID)"),
fieldWithPath("data.nickname").description("작성자 닉네임"),
fieldWithPath("data.title").description("작성자 대표 타이틀 (업적)"),
fieldWithPath("data.interests[].name").description("작성자 관심사 목록"),
fieldWithPath("data.image").description("작성자 프로필 이미지 링크"),
fieldWithPath("data.author").description("인사이트 조회자와 작성자 일치 여부"),
fieldWithPath("data.createdAt").description("작성자 닉네임"))
.tag("Insight")
.build()
)));

}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package ccc.keewedomain.persistence.repository.insight;

import ccc.keewedomain.persistence.domain.insight.Insight;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import static ccc.keewedomain.persistence.domain.common.QInterest.interest;
import static ccc.keewedomain.persistence.domain.insight.QInsight.insight;
import static ccc.keewedomain.persistence.domain.user.QUser.user;

@Repository
@RequiredArgsConstructor
public class InsightQueryRepository {
private final JPAQueryFactory queryFactory;

public Insight findByIdWithWriter(Long insightId) {
return queryFactory.select(insight)
.from(insight)
.where(insight.id.eq(insightId))
.leftJoin(insight.writer, user)
.fetchJoin()
.leftJoin(user.interests, interest)
.fetchJoin()
.fetchOne();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public interface InsightRepository extends JpaRepository<Insight, Long> {
@Query(value = "select i from Insight i where i.id = :id")
Optional<Insight> findByIdWithReadWriteLock(@Param("id") Long id);

default Insight findByIdOrElseThrow(Long id) {
default Insight findByIdWithLockOrElseThrow(Long id) {
return findByIdWithReadWriteLock(id).orElseThrow(() -> new KeeweException(KeeweRtnConsts.ERR445));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import ccc.keewedomain.persistence.domain.insight.Insight;
import ccc.keewedomain.persistence.domain.insight.enums.ReactionType;
import ccc.keewedomain.persistence.domain.user.User;
import ccc.keewedomain.persistence.repository.insight.InsightQueryRepository;
import ccc.keewedomain.persistence.repository.insight.InsightRepository;
import ccc.keewedomain.persistence.repository.insight.ReactionAggregationRepository;
import ccc.keewedomain.service.challenge.ChallengeDomainService;
Expand All @@ -37,7 +38,7 @@ public class InsightDomainService {
private final UserDomainService userDomainService;
private final ChallengeDomainService challengeDomainService;
private final DrawerDomainService drawerDomainService;

private final InsightQueryRepository insightQueryRepository;
private final CInsightViewRepository cInsightViewRepository;

//TODO 참가한 챌린지에 기록하기
Expand Down Expand Up @@ -71,11 +72,15 @@ public Long incrementViewCount(InsightViewIncrementDto dto) {

@Transactional
public Long incrementViewCount(Long insightId) {
Insight insight = insightRepository.findByIdOrElseThrow(insightId);
Insight insight = insightRepository.findByIdWithLockOrElseThrow(insightId);
log.info("[IDS::incrementViewCount] DB Curr view {}, Next view {}", insight.getView(), insight.getView() + 1);
return incrementViewCount(insight);
}

public Insight getByIdWithWriter(Long insightId) {
return insightQueryRepository.findByIdWithWriter(insightId);
}

private Long incrementViewCount(Insight insight) {
Long viewCount = insight.incrementView();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class MQPublishService {
private final RabbitTemplate rabbitTemplate;

//TODO: retry handler 도입
public void publish(String exchange, Object body) {
public <T> void publish(String exchange, T body) {
rabbitTemplate.convertAndSend(exchange
, KeeweConsts.DEFAULT_ROUTING_KEY
, body);
Expand Down