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: 댓글과 사용자를 분리 #460

Merged
merged 8 commits into from
Mar 22, 2024
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.digginroom.digginroom.controller;
package com.digginroom.digginroom.comment.controller;

import static org.springframework.http.HttpStatus.CREATED;
import static org.springframework.http.HttpStatus.OK;

import com.digginroom.digginroom.service.CommentService;
import com.digginroom.digginroom.service.dto.CommentRequest;
import com.digginroom.digginroom.service.dto.CommentResponse;
import com.digginroom.digginroom.service.dto.CommentsResponse;
import com.digginroom.digginroom.controller.Auth;
import com.digginroom.digginroom.comment.service.CommentService;
import com.digginroom.digginroom.comment.service.dto.CommentRequest;
import com.digginroom.digginroom.comment.service.dto.CommentResponse;
import com.digginroom.digginroom.comment.service.dto.CommentsResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.digginroom.digginroom.domain.comment;
package com.digginroom.digginroom.comment.domain;

import com.digginroom.digginroom.domain.BaseEntity;
import com.digginroom.digginroom.domain.member.Member;
import com.digginroom.digginroom.exception.CommentException.NotOwnerException;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.ManyToOne;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
Expand All @@ -20,17 +18,16 @@ public class Comment extends BaseEntity {
private Long roomId;
@Column(length = 500)
private String comment;
@ManyToOne
private Member member;
private Long memberId;

public void updateComment(final String comment, final Member member) {
if (!isOwner(member)) {
public void updateComment(final String comment, final Long memberId) {
if (!isOwner(memberId)) {
throw new NotOwnerException();
}
this.comment = comment;
}

public boolean isOwner(final Member member) {
return this.member.equals(member);
public boolean isOwner(final Long memberId) {
return this.memberId.equals(memberId);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.digginroom.digginroom.repository;
package com.digginroom.digginroom.comment.repository;

import com.digginroom.digginroom.domain.comment.Comment;
import com.digginroom.digginroom.comment.domain.Comment;
import com.digginroom.digginroom.comment.repository.dto.CommentMember;
import com.digginroom.digginroom.exception.CommentException.NotFoundException;
import java.util.Optional;
import org.springframework.data.domain.Pageable;
Expand All @@ -17,14 +18,15 @@ default Comment getCommentById(final Long id) {
return findCommentById(id).orElseThrow(NotFoundException::new);
}

@Query("SELECT c "
+ "FROM Comment c "
+ "JOIN FETCH c.member "
@Query("SELECT new com.digginroom.digginroom.comment.repository.dto.CommentMember("
+ "c.id, c.comment, c.createdAt, c.updatedAt, m.id, m.nickname.nickname"
+ ") "
+ "FROM Comment c JOIN Member m "
+ "ON c.memberId = m.id "
+ "WHERE c.roomId = :roomId "
+ "AND c.id < :lastCommentId "
+ "ORDER BY c.id DESC "
)
Slice<Comment> getCommentsByCursor(
+ "ORDER BY c.id DESC ")
Slice<CommentMember> getCommentsByCursor(
@Param("roomId") final Long roomId,
@Param("lastCommentId") final Long lastCommentId,
final Pageable pageable
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.digginroom.digginroom.comment.repository.dto;

import java.time.LocalDateTime;

public record CommentMember(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

: comment 와 member를 조인했으므로 commentMember

Long id,
String comment,
LocalDateTime createdAt,
LocalDateTime updatedAt,
Long memberId,
String nickname
) {
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package com.digginroom.digginroom.service;
package com.digginroom.digginroom.comment.service;

import static com.digginroom.digginroom.exception.CommentException.InvalidCommentSizeException;
import static com.digginroom.digginroom.exception.CommentException.NotOwnerException;

import com.digginroom.digginroom.domain.comment.Comment;
import com.digginroom.digginroom.domain.member.Member;
import com.digginroom.digginroom.comment.domain.Comment;
import com.digginroom.digginroom.comment.repository.CommentRepository;
import com.digginroom.digginroom.comment.repository.dto.CommentMember;
import com.digginroom.digginroom.comment.service.dto.CommentRequest;
import com.digginroom.digginroom.comment.service.dto.CommentResponse;
import com.digginroom.digginroom.comment.service.dto.CommentsResponse;
import com.digginroom.digginroom.exception.CommentException.InvalidLastCommentIdException;
import com.digginroom.digginroom.exception.RoomException.NotFoundException;
import com.digginroom.digginroom.repository.CommentRepository;
import com.digginroom.digginroom.repository.MemberRepository;
import com.digginroom.digginroom.repository.RoomRepository;
import com.digginroom.digginroom.service.dto.CommentRequest;
import com.digginroom.digginroom.service.dto.CommentResponse;
import com.digginroom.digginroom.service.dto.CommentsResponse;
import com.digginroom.digginroom.repository.dto.MemberNickname;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
Expand Down Expand Up @@ -40,16 +41,13 @@ public CommentsResponse getRoomComments(
Long resolvedLastCommentId = getLastCommentId(lastCommentId);
validateLastCommentId(resolvedLastCommentId);
validateCommentSize(size);

Member member = memberRepository.getMemberById(memberId);
Slice<Comment> comments = commentRepository.getCommentsByCursor(
Slice<CommentMember> commentMembers = commentRepository.getCommentsByCursor(
roomId,
resolvedLastCommentId,
PageRequest.of(DEFAULT_PAGE_SIZE, size)
);

return new CommentsResponse(comments.getContent().stream()
.map(comment -> CommentResponse.of(comment, comment.isOwner(member)))
return new CommentsResponse(commentMembers.getContent().stream()
.map(commentMember -> CommentResponse.of(commentMember, memberId))
.toList());
}

Expand All @@ -74,12 +72,12 @@ private Long getLastCommentId(final Long lastCommentId) {

public CommentResponse comment(final Long roomId, final Long memberId, final CommentRequest request) {
validateExistRoom(roomId);
Member member = memberRepository.getMemberById(memberId);
MemberNickname memberNickname = memberRepository.getMemberNickname(memberId);

Comment comment = new Comment(roomId, request.comment(), member);
Comment comment = new Comment(roomId, request.comment(), memberId);
commentRepository.save(comment);
Comment on lines +75 to +77
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

: 더 재활용하기 쉬운 형태를 찾아보기로 함


return CommentResponse.of(comment, comment.isOwner(member));
return CommentResponse.of(comment, comment.isOwner(memberId), memberNickname.getNickname());
}

public void validateExistRoom(final Long roomId) {
Expand All @@ -89,15 +87,14 @@ public void validateExistRoom(final Long roomId) {
}

public void delete(final Long memberId, final Long commentId) {
Member member = memberRepository.getMemberById(memberId);
Comment comment = commentRepository.getCommentById(commentId);

validateSameOwner(member, comment);
validateSameOwner(memberId, comment);
commentRepository.delete(comment);
}

private void validateSameOwner(final Member member, final Comment comment) {
if (!comment.isOwner(member)) {
private void validateSameOwner(final Long memberId, final Comment comment) {
if (!comment.isOwner(memberId)) {
throw new NotOwnerException();
}
}
Expand All @@ -107,9 +104,9 @@ public CommentResponse update(
final Long commentId,
final CommentRequest request
) {
Member member = memberRepository.getMemberById(memberId);
MemberNickname memberNickname = memberRepository.getMemberNickname(memberId);
Comment comment = commentRepository.getCommentById(commentId);
comment.updateComment(request.comment(), member);
return CommentResponse.of(comment, comment.isOwner(member));
comment.updateComment(request.comment(), memberId);
return CommentResponse.of(comment, comment.isOwner(memberId), memberNickname.getNickname());
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.digginroom.digginroom.service.dto;
package com.digginroom.digginroom.comment.service.dto;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.digginroom.digginroom.comment.service.dto;

import com.digginroom.digginroom.comment.domain.Comment;
import com.digginroom.digginroom.comment.repository.dto.CommentMember;
import java.time.LocalDateTime;
import java.util.Objects;

public record CommentResponse(
Long id,
String writer,
String comment,
LocalDateTime createdAt,
LocalDateTime updatedAt,
boolean isOwner
) {

public static CommentResponse of(final CommentMember commentMember, final Long memberId) {
return new CommentResponse(
commentMember.id(),
commentMember.nickname(),
commentMember.comment(),
commentMember.createdAt(),
commentMember.updatedAt(),
Objects.equals(commentMember.memberId(), memberId)
);
}

public static CommentResponse of(final Comment comment, boolean isOwner, String nickname) {
return new CommentResponse(
comment.getId(),
nickname,
comment.getComment(),
comment.getCreatedAt(),
comment.getUpdatedAt(),
isOwner
);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.digginroom.digginroom.service.dto;
package com.digginroom.digginroom.comment.service.dto;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.digginroom.digginroom.domain.member.Member;
import com.digginroom.digginroom.exception.MemberException.NotFoundException;
import com.digginroom.digginroom.repository.dto.MemberNickname;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;

Expand All @@ -18,4 +19,10 @@ default Member getMemberByUsername(final String username) {
default Member getMemberById(final Long id) {
return findById(id).orElseThrow(NotFoundException::new);
}

Optional<MemberNickname> findMemberNicknameById(final Long id);

default MemberNickname getMemberNickname(final Long id) {
return findMemberNicknameById(id).orElseThrow(NotFoundException::new);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.digginroom.digginroom.repository.dto;

import org.springframework.beans.factory.annotation.Value;

public interface MemberNickname {

@Value("#{target.nickname}")
String getNickname();
}

This file was deleted.

8 changes: 8 additions & 0 deletions backend/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ spring:
enabled: true
baseline-on-migrate: true
baseline-version: 5
datasource:
hikari:
maximum-pool-size: 44
springdoc:
api-docs:
path: /docs
Expand All @@ -38,3 +41,8 @@ management:
base-path: /manage
info:
version: '1.0.0'
server:
tomcat:
threads:
max: 16
min-spare: 16
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE INDEX COMMENT_ROOM_ID_COMMENT_ID_IDX ON comment (room_id, id desc);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

우리가 댓글을 Id 순으로 가져오고 있었군요
일단 OK 입니다 👍

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.digginroom.digginroom.oauth.jwk.KakaoJwkProvider;
import com.digginroom.digginroom.oauth.jwk.ThirdPartyJwkProviders;
import com.digginroom.digginroom.oauth.payload.IdTokenPayload;
import com.digginroom.digginroom.service.dto.CommentRequest;
import com.digginroom.digginroom.comment.service.dto.CommentRequest;
import com.digginroom.digginroom.service.dto.MemberLoginRequest;
import com.digginroom.digginroom.service.dto.MemberSaveRequest;
import com.digginroom.digginroom.util.DigginRoomPasswordEncoder;
Expand Down
Loading
Loading