오늘 공부한 것
* 중간 발표 대비 자료 정리
* 댓글 및 대댓글 기능 코드 수정
(Response 값을 nickname -> email 로 변경 및 게시글 작성자가 쓴 댓글과 대댓글에 글쓴이 표기)
오늘은 오전에 중간발표 관련한 발제가 있었다
이 발제 이후 팀원과 모여서 발표 대비 자료를 정리했다
1 서비스 아키텍처
틈틈히 어제 실패한 Test 코드를 작성했으나 계속해서 오류가 나와서 결국 고치진 못했다
프론트엔드에서 댓글과 대댓글 코드 변경을 원하셨다
1) Response 로 nickname 이 나오도록 해두었는데 유니크한 값이 아니기 때문에 email로 변경을 원했다.
2) 게시글 작성자가 작성한 댓글과 대댓글에 글쓴이 라는 표기가 나오도록 하길 원하셨다.
1. 댓글
1) Dto
nickname 을 없앴고 게시글 작성한 유저와 댓글 작성한 유저를 비교하기 위해 checkUser를 추가하였다
@Getter
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CommentsResponseDto {
private Long commentId;
private String contents;
private String email;
private String checkUser;
private LocalDateTime createAt;
private LocalDateTime modifiedAt;
public CommentsResponseDto(Comments comments, String users) {
this.commentId = comments.getId();
this.contents = comments.getContents();
this.createAt = comments.getCreatedAt();
this.modifiedAt = comments.getModifiedAt();
this.email = users;
}
public CommentsResponseDto(Comments comments, String users, String checkUser) {
this.commentId = comments.getId();
this.contents = comments.getContents();
this.createAt = comments.getCreatedAt();
this.modifiedAt = comments.getModifiedAt();
this.email = users;
this.checkUser = checkUser;
}
}
2) Entity
불필요해진 nickname을 삭제하였다
@Entity
@Getter
@NoArgsConstructor
@Table(name = "comments")
public class Comments extends TimeStamped {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name ="comments_id")
private Long id;
private String email;
@Column(nullable = false, length = 500)
private String contents;
@ManyToOne
@JoinColumn(name = "posts_id")
private Posts posts;
@OneToMany (mappedBy = "comments", orphanRemoval = true)
private List<Replies> repliesList= new ArrayList<>();
public Comments(CommentsRequestDto requestDto, Users users, Posts posts) {
this.contents = requestDto.getContents();
this.posts = posts;
this.email = users.getEmail();
}
public void update(CommentsRequestDto requestDto, Users users) {
this.contents = requestDto.getContents();
}
}
3) Service
Posts 의 email 과 Comments 의 eamil 을 비교하여 같을 때는 "글쓴이" 를 출력하였다
// 댓글 조회
public Slice<CommentsResponseDto> commentsList(Long postId,
Pageable pageable) {
Posts posts = postsRepository.findById(postId).orElseThrow(
() -> new CustomException(ErrorCode.POST_NOT_EXIST)); // 존재하지 않는 게시글입니다
Slice<Comments> commentsList = commentsRepository.findByPosts_IdOrderByCreatedAtDesc(postId, pageable);
if (commentsList.isEmpty()) {
throw new CustomException(ErrorCode.COMMENTS_NOT_EXIST); // 존재하지 않는 댓글입니다
}
List<CommentsResponseDto> commentsResponseDtoList = new ArrayList<>();
for (Comments comments : commentsList) {
if (posts.getUsers().getEmail().equals(comments.getEmail())) {
commentsResponseDtoList.add(new CommentsResponseDto(comments, comments.getEmail(), "글쓴이"));
} else {
commentsResponseDtoList.add(new CommentsResponseDto(comments, comments.getEmail()));
}
}
return new SliceImpl<>(commentsResponseDtoList, pageable, commentsList.hasNext());
}
2. 대댓글
1) Dto
nickname 을 없앴고 게시글 작성한 유저와 댓글 작성한 유저를 비교하기 위해 checkUser를 추가하였다
@Getter
@JsonInclude(JsonInclude.Include.NON_NULL)
public class RepliesResponseDto {
private Long repliesId;
private String contents;
private String email;
private String checkUser;
private LocalDateTime createAt;
private LocalDateTime modifiedAt;
public RepliesResponseDto(Replies replies, String users) {
this.repliesId = replies.getId();
this.contents = replies.getContents();
this.createAt = replies.getCreatedAt();
this.modifiedAt = replies.getModifiedAt();
this.email = users;
}
public RepliesResponseDto(Replies replies, String users, String checkUser) {
this.repliesId = replies.getId();
this.contents = replies.getContents();
this.createAt = replies.getCreatedAt();
this.modifiedAt = replies.getModifiedAt();
this.email = users;
this.checkUser = checkUser;
}
}
2) Entity
불필요해진 nickname을 삭제하였다
@Entity
@Getter
@NoArgsConstructor
@Table(name = "replies")
public class Replies extends TimeStamped {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name ="replies_id")
private Long id;
private String email;
@Column(nullable = false, length = 500)
private String contents;
@ManyToOne
@JoinColumn(name = "comments_id")
private Comments comments;
public Replies(RepliesRequestDto requestDto, Users users, Comments comments) {
this.contents = requestDto.getContents();
this.comments = comments;
this.email = users.getEmail();
}
public void update(RepliesRequestDto requestDto, Users users) {
this.contents = requestDto.getContents();
}
}
3) Service
Posts 의 email 과 Comments 의 eamil 을 비교하여 같을 때는 "글쓴이" 를 출력하였다
다만, 댓글과 달리 postId 가 없기 때문에 commentId 로 조회하고 거기서 getPosts( ) 를 가지고 오는 방식으로 하였다
// 대댓글 조회
public Slice<RepliesResponseDto> repliesList(Long commentId,
Pageable pageable) {
Comments comments = commentsRepository.findById(commentId).orElseThrow(
() -> new CustomException(ErrorCode.COMMENTS_NOT_EXIST)); // 존재하지 않는 댓글입니다
Posts posts = comments.getPosts();
Slice<Replies> repliesList = repliesRepository.findByComments_IdOrderByCreatedAtDesc(commentId, pageable);
if (repliesList.isEmpty()) {
throw new CustomException(ErrorCode.REPLIES_NOT_EXIST); // 존재하지 않는 대댓글입니다
}
List<RepliesResponseDto> RepliesResponseDtoList = new ArrayList<>();
for (Replies replies : repliesList) {
if (posts.getUsers().getEmail().equals(replies.getEmail())) {
RepliesResponseDtoList.add(new RepliesResponseDto(replies, replies.getEmail(), "글쓴이"));
} else {
RepliesResponseDtoList.add(new RepliesResponseDto(replies, replies.getEmail()));
}
}
return new SliceImpl<>(RepliesResponseDtoList, pageable, repliesList.hasNext());
}
'항해99' 카테고리의 다른 글
23.10.26 항해 99 16기 실전 프로젝트 20일차 (0) | 2023.10.26 |
---|---|
23.10.25 항해 99 16기 실전 프로젝트 19일차 (0) | 2023.10.25 |
23.10.23 항해 99 16기 실전 프로젝트 17일차 (0) | 2023.10.23 |
23.10.16~10.22 항해 99 16기 9주차 회고록 (1) | 2023.10.22 |
23.10.21 항해 99 16기 실전 프로젝트 16일차 (0) | 2023.10.21 |