Skip to content
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 @@ -19,6 +19,7 @@
import com.recordit.server.dto.comment.CommentRequestDto;
import com.recordit.server.dto.comment.CommentResponseDto;
import com.recordit.server.dto.comment.ModifyCommentRequestDto;
import com.recordit.server.dto.comment.MyCommentRequestDto;
import com.recordit.server.dto.comment.WriteCommentRequestDto;
import com.recordit.server.dto.comment.WriteCommentResponseDto;
import com.recordit.server.exception.ErrorMessage;
Expand Down Expand Up @@ -126,4 +127,23 @@ public ResponseEntity modifyComment(
commentService.modifyComment(commentId, modifyCommentRequestDto, attachment);
return ResponseEntity.ok().build();
}

@ApiOperation(
value = "내가 작성한 댓글 조회",
notes = "내가 작성한 댓글 조회 합니다."
)
@ApiResponses({
@ApiResponse(
code = 200, message = "내가 작성한 댓글 조회 성공"
),
@ApiResponse(
code = 400,
message = "잘못 된 요청",
response = ErrorMessage.class
)
})
@GetMapping("/my")
public ResponseEntity getMyComments(@ModelAttribute @Valid MyCommentRequestDto myCommentRequestDto) {
return ResponseEntity.ok().body(commentService.getMyComments(myCommentRequestDto));
}
}
75 changes: 75 additions & 0 deletions src/main/java/com/recordit/server/dto/comment/MyCommentDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.recordit.server.dto.comment;

import java.time.LocalDateTime;

import com.recordit.server.domain.Comment;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Getter
@ToString
@ApiModel
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class MyCommentDto {
@ApiModelProperty(notes = "레코드의 카테고리 이름")
private String categoryName;

@ApiModelProperty(notes = "레코드 아이디")
private Long recordId;

@ApiModelProperty(notes = "레코드의 제목")
private String title;

@ApiModelProperty(notes = "레코드의 아이콘 이름")
private String iconName;

@ApiModelProperty(notes = "레코드의 색상 이름")
private String colorName;

@ApiModelProperty(notes = "레코드 작성 시각")
private LocalDateTime recordCreatedAt;

@ApiModelProperty(notes = "레코드에 달린 댓글 내용")
private String commentContent;

@ApiModelProperty(notes = "레코드에 달린 댓글 작성 시간")
private LocalDateTime commentCreatedAt;

private MyCommentDto(
String categoryName,
Long recordId,
String title,
String iconName,
String colorName,
LocalDateTime recordCreatedAt,
String commentContent,
LocalDateTime commentCreatedAt
) {
this.categoryName = categoryName;
this.recordId = recordId;
this.title = title;
this.iconName = iconName;
this.colorName = colorName;
this.recordCreatedAt = recordCreatedAt;
this.commentContent = commentContent;
this.commentCreatedAt = commentCreatedAt;
}

public static MyCommentDto of(Comment comment) {
return new MyCommentDto(
comment.getRecord().getRecordCategory().getName(),
comment.getRecord().getId(),
comment.getRecord().getTitle(),
comment.getRecord().getRecordIcon().getName(),
comment.getRecord().getRecordColor().getName(),
comment.getRecord().getCreatedAt(),
comment.getContent(),
comment.getCreatedAt()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.recordit.server.dto.comment;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

import io.swagger.annotations.ApiParam;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.ToString;

@Getter
@ToString
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@Builder
public class MyCommentRequestDto {
@ApiParam(value = "조회할 페이지 번호 !주의: 0이상이어야 합니다.", required = true, example = "0")
@NotNull
@Min(0)
private Integer page;

@ApiParam(value = "조회할 레코드 갯수 !주의: 1이상이어야 합니다.", required = true, example = "1")
@NotNull
@Min(1)
private Integer size;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.recordit.server.dto.comment;

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

import org.springframework.data.domain.Page;

import com.recordit.server.domain.Comment;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Getter
@ToString
@ApiModel
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class MyCommentResponseDto {
@ApiModelProperty(notes = "내가 작성한 댓글의 전체 페이지 개수", required = true)
private Integer totalPage;

@ApiModelProperty(notes = "내가 작성한 댓글의 전체 개수", required = true)
private Long totalCount;

@ApiModelProperty(notes = "내가 작성한 댓글 리스트", required = true)
private List<MyCommentDto> myCommentDtos;

private MyCommentResponseDto(
Integer totalPage,
Long totalCount,
List<MyCommentDto> myCommentDtos
) {
this.totalPage = totalPage;
this.totalCount = totalCount;
this.myCommentDtos = myCommentDtos;
}

public static MyCommentResponseDto of(Page<Comment> comments) {
return new MyCommentResponseDto(
comments.getTotalPages(),
comments.getTotalElements(),
comments.getContent().stream()
.map(
comment -> MyCommentDto.of(comment)
).collect(Collectors.toList())
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.data.repository.query.Param;

import com.recordit.server.domain.Comment;
import com.recordit.server.domain.Member;
import com.recordit.server.domain.Record;

public interface CommentRepository extends JpaRepository<Comment, Long> {
Expand All @@ -31,4 +32,7 @@ public interface CommentRepository extends JpaRepository<Comment, Long> {

@EntityGraph(attributePaths = {"record", "record.recordColor", "record.recordIcon"})
List<Comment> findByRecord(Record fixRecord);

@EntityGraph(attributePaths = {"record", "record.recordCategory", "record.recordColor", "record.recordIcon"})
Page<Comment> findByWriter(Member writer, Pageable pageable);
}
21 changes: 21 additions & 0 deletions src/main/java/com/recordit/server/service/CommentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import com.recordit.server.dto.comment.CommentRequestDto;
import com.recordit.server.dto.comment.CommentResponseDto;
import com.recordit.server.dto.comment.ModifyCommentRequestDto;
import com.recordit.server.dto.comment.MyCommentRequestDto;
import com.recordit.server.dto.comment.MyCommentResponseDto;
import com.recordit.server.dto.comment.WriteCommentRequestDto;
import com.recordit.server.dto.comment.WriteCommentResponseDto;
import com.recordit.server.exception.comment.CommentNotFoundException;
Expand Down Expand Up @@ -214,4 +216,23 @@ public void modifyComment(

comment.modify(modifyCommentRequestDto);
}

@Transactional(readOnly = true)
public MyCommentResponseDto getMyComments(MyCommentRequestDto myCommentRequestDto) {
Long userIdBySession = sessionUtil.findUserIdBySession();
log.info("세션에서 찾은 사용자 ID : {}", userIdBySession);

Member member = memberRepository.findById(userIdBySession)
.orElseThrow(() -> new MemberNotFoundException("회원 정보를 찾을 수 없습니다."));

PageRequest pageRequest = PageRequest.of(
myCommentRequestDto.getPage(),
myCommentRequestDto.getSize(),
Sort.by(Sort.Direction.DESC, "createdAt")
);

Page<Comment> findComments = commentRepository.findByWriter(member, pageRequest);

return MyCommentResponseDto.of(findComments);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.assertj.core.api.Assertions.*;
import static org.mockito.BDDMockito.*;

import java.util.List;
import java.util.Optional;

import org.junit.jupiter.api.BeforeEach;
Expand All @@ -13,14 +14,22 @@
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;

import com.recordit.server.domain.Comment;
import com.recordit.server.domain.Member;
import com.recordit.server.domain.Record;
import com.recordit.server.domain.RecordCategory;
import com.recordit.server.domain.RecordColor;
import com.recordit.server.domain.RecordIcon;
import com.recordit.server.dto.comment.CommentRequestDto;
import com.recordit.server.dto.comment.ModifyCommentRequestDto;
import com.recordit.server.dto.comment.MyCommentRequestDto;
import com.recordit.server.dto.comment.WriteCommentRequestDto;
import com.recordit.server.dto.comment.WriteCommentResponseDto;
import com.recordit.server.exception.comment.CommentNotFoundException;
Expand Down Expand Up @@ -508,4 +517,69 @@ class 댓글을_수정_할_때 {
.doesNotThrowAnyException();
}
}

@Nested
@DisplayName("내가작성한 댓글 조회에서")
class 내가작성한_댓글_조회에서 {
private Long memberId = 1L;
private MyCommentRequestDto myCommentRequestDto = MyCommentRequestDto.builder()
.page(1)
.size(10)
.build();
Member member = mock(Member.class);
PageRequest pageRequest = PageRequest.of(
myCommentRequestDto.getPage(),
myCommentRequestDto.getSize(),
Sort.by(Sort.Direction.DESC, "createdAt")
);

List<Comment> commentList = List.of(mock(Comment.class));
Page<Comment> commentPage = new PageImpl<>(commentList, pageRequest, 1);

@Test
@DisplayName("회원_정보를_찾을 수 없다면 예외를 던진다")
void 회원_정보를_찾을_수_없다면_예외를_던진다() {
// given
given(sessionUtil.findUserIdBySession())
.willReturn(memberId);

given(memberRepository.findById(memberId))
.willReturn(Optional.empty());

// when, then
assertThatThrownBy(() -> commentService.getMyComments(myCommentRequestDto))
.isInstanceOf(MemberNotFoundException.class)
.hasMessage("회원 정보를 찾을 수 없습니다.");
}

@Test
@DisplayName("정상적이라면 예외를 던지지 않는다")
void 정상적이라면_예외를_던지지_않는다() {
//given
given(sessionUtil.findUserIdBySession())
.willReturn(memberId);

given(memberRepository.findById(memberId))
.willReturn(Optional.of(member));

given(commentRepository.findByWriter(member, pageRequest))
.willReturn(commentPage);

given(commentPage.getContent().get(0).getRecord())
.willReturn(mock(Record.class));

given(commentPage.getContent().get(0).getRecord().getRecordCategory())
.willReturn(mock(RecordCategory.class));

given(commentPage.getContent().get(0).getRecord().getRecordIcon())
.willReturn(mock(RecordIcon.class));

given(commentPage.getContent().get(0).getRecord().getRecordColor())
.willReturn(mock(RecordColor.class));

//when, then
assertThatCode(() -> commentService.getMyComments(myCommentRequestDto))
.doesNotThrowAnyException();
}
}
}