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: 자신의 댓글인지 확인할 수 있는 반환 데이터 추가 및 flyway 스크립트 추가 #96

Merged
merged 2 commits into from
May 6, 2024
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 @@ -16,7 +16,7 @@ public class CommentQueryService {
private final CommentRepository commentRepository;

@Transactional(readOnly = true)
public List<CommentSimpleResponse> findAllCommentsByBoardId(final Long boardId, final Long commentId, final int pageSize) {
return commentRepository.findAllCommentsByBoardId(boardId, commentId, pageSize);
public List<CommentSimpleResponse> findAllCommentsByBoardId(final Long boardId, final Long memberId, final Long commentId, final int pageSize) {
return commentRepository.findAllCommentsByBoardId(boardId, memberId, commentId, pageSize);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Entity
@Table(name = "comment", indexes = {@Index(name = "idx_comment_paging", columnList = "board_id")})
@Table(name = "comment", indexes = {@Index(name = "idx_comment_paging", columnList = "board_id, writer_id")})
public class Comment extends BaseEntity {

@Id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public interface CommentRepository {

Optional<Comment> findById(Long id);

List<CommentSimpleResponse> findAllCommentsByBoardId(Long boardId, Long commentId, int pageSize);
List<CommentSimpleResponse> findAllCommentsByBoardId(Long boardId, Long memberId, Long commentId, int pageSize);

void deleteById(Long id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public record CommentSimpleResponse(
Long id,
String content,
Long writerId,
Boolean isMine,
String writerNickname,
LocalDateTime createDate
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
package com.server.community.infrastructure.comment;

import com.server.community.domain.comment.dto.CommentSimpleResponse;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.server.community.domain.comment.dto.CommentSimpleResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;

import static com.querydsl.core.types.Projections.constructor;
import static com.server.community.domain.comment.QComment.comment;
import static com.server.member.domain.member.QMember.member;
import static com.querydsl.core.types.Projections.constructor;

@RequiredArgsConstructor
@Repository
public class CommentQueryRepository {

private final JPAQueryFactory jpaQueryFactory;

public List<CommentSimpleResponse> findAllWithPaging(final Long boardId, final Long commentId, final int pageSize) {
public List<CommentSimpleResponse> findAllWithPaging(final Long boardId, final Long memberId, final Long commentId, final int pageSize) {
return jpaQueryFactory.select(constructor(CommentSimpleResponse.class,
comment.id,
comment.content,
member.id,
member.id.eq(memberId),
member.nickname,
comment.createdAt
)).from(comment)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public Optional<Comment> findById(final Long id) {
return commentJpaRepository.findById(id);
}

public List<CommentSimpleResponse> findAllCommentsByBoardId(final Long boardId, final Long commentId, final int pageSize) {
return commentQueryRepository.findAllWithPaging(boardId, commentId, pageSize);
public List<CommentSimpleResponse> findAllCommentsByBoardId(final Long boardId, final Long memberId, final Long commentId, final int pageSize) {
return commentQueryRepository.findAllWithPaging(boardId, memberId, commentId, pageSize);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ public ResponseEntity<Void> createComment(@AuthMember final Long memberId,
@GetMapping("/{boardId}/comments")
public ResponseEntity<List<CommentSimpleResponse>> findAllCommentsByBoardId(@PathVariable("boardId") final Long boardId,
@RequestParam(name = "commentId", required = false) final Long commentId,
@AuthMember final Long memberId,
@RequestParam(name = "pageSize") final Integer pageSize) {
List<CommentSimpleResponse> comments = commentQueryService.findAllCommentsByBoardId(boardId, commentId, pageSize);
List<CommentSimpleResponse> comments = commentQueryService.findAllCommentsByBoardId(boardId, memberId, commentId, pageSize);
return ResponseEntity.ok(comments);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ALTER TABLE comment DROP INDEX idx_comment_paging;

ALTER TABLE comment
ADD INDEX idx_comment_paging (board_id, writer_id);
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void setup() {
Long boardId = saved.getBoardId();

// when
List<CommentSimpleResponse> result = commentQueryService.findAllCommentsByBoardId(boardId, null, 10);
List<CommentSimpleResponse> result = commentQueryService.findAllCommentsByBoardId(boardId, 1L, null, 10);

// then
assertSoftly(softly -> {
Expand Down Expand Up @@ -133,7 +133,7 @@ void setup() {
assertDoesNotThrow(() -> commentService.deleteAllCommentsByBoardId(saved.getBoardId()));

// then
List<CommentSimpleResponse> result = commentQueryService.findAllCommentsByBoardId(saved.getBoardId(), null, 10);
List<CommentSimpleResponse> result = commentQueryService.findAllCommentsByBoardId(saved.getBoardId(), 1L, null, 10);
assertThat(result).isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public Optional<Comment> findById(final Long id) {
}

@Override
public List<CommentSimpleResponse> findAllCommentsByBoardId(final Long boardId, final Long commentId, final int pageSize) {
public List<CommentSimpleResponse> findAllCommentsByBoardId(final Long boardId, final Long memberId, final Long commentId, final int pageSize) {
if (commentId == null) {
return map.values().stream()
.sorted(Comparator.comparing(Comment::getId).reversed())
Expand Down Expand Up @@ -81,6 +81,7 @@ private static CommentSimpleResponse parse(final Comment comment) {
comment.getId(),
comment.getContent(),
comment.getWriterId(),
true,
"writer",
LocalDateTime.now()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void setup() {
}

// when
List<CommentSimpleResponse> result = commentQueryRepository.findAllWithPaging(1L, null, 10);
List<CommentSimpleResponse> result = commentQueryRepository.findAllWithPaging(1L, member.getId(), null, 10);

// then
assertSoftly(softly -> {
Expand All @@ -83,7 +83,7 @@ void setup() {
}

// when
List<CommentSimpleResponse> result = commentQueryRepository.findAllWithPaging(1L, 11L, 10);
List<CommentSimpleResponse> result = commentQueryRepository.findAllWithPaging(1L, member.getId(), 11L, 10);

// then
assertSoftly(softly -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import static com.server.helper.RestDocsHelper.customDocument;
import static org.apache.http.HttpHeaders.AUTHORIZATION;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doNothing;
Expand Down Expand Up @@ -80,8 +82,8 @@ class CommentControllerWebMvcTest extends MockBeanInjection {
void 게시글의_댓글을_모두_조회한다() throws Exception {
// given
Long boardId = 1L;
CommentSimpleResponse response = new CommentSimpleResponse(1L, "댓글 내용", 1L, "꿈꾸는돼지_123", LocalDateTime.now());
when(commentQueryService.findAllCommentsByBoardId(1L, 2L, 10)).thenReturn(List.of(response));
CommentSimpleResponse response = new CommentSimpleResponse(1L, "댓글 내용", 1L, true, "꿈꾸는돼지_123", LocalDateTime.now());
when(commentQueryService.findAllCommentsByBoardId(anyLong(), anyLong(), anyLong(), anyInt())).thenReturn(List.of(response));

// when & then
mockMvc.perform(get("/api/boards/{boardId}/comments", boardId)
Expand All @@ -105,6 +107,7 @@ class CommentControllerWebMvcTest extends MockBeanInjection {
fieldWithPath("[].id").description("댓글 id"),
fieldWithPath("[].content").description("댓글 내용"),
fieldWithPath("[].writerId").description("작성자 id"),
fieldWithPath("[].isMine").description("자신이 작성한 댓글인지"),
fieldWithPath("[].writerNickname").description("작성자 닉네임"),
fieldWithPath("[].createDate").description("댓글 생성일자")
)
Expand Down
Loading