Skip to content

Commit

Permalink
feat: 댓글과 사용자를 분리 (#460)
Browse files Browse the repository at this point in the history
* refactor: comment 내에서 member의 객체 참조 -> id 참조로 변경

* test: 커버링 인덱스 성능 테스트를 위한 쿼리 케이스 생성
- 2번: 댓글에 커버링 인덱스 적용
- 3번: 서브쿼리를 활용해 댓글에 커버링 인덱스 적용
- 4번: 댓글에 커버링 인덱스 적용 후 쓰레드 풀을 사용해 댓글, 멤버에 조회

* refactor: 사용하지 않는 쿼리 및 기능 삭제

* chore: 인덱스 추가

* test: 사용하지 않는 테스트 삭제

* refactor: comment 패키지 생성 및 의존성 분리

* test: comment 필드 변경으로 불필요한 테스트 로직 제거

* refactor: dto를 interface로 프로젝션하도록 변경
  • Loading branch information
kong-hana01 authored Mar 22, 2024
1 parent 08dfcc4 commit 2f33705
Show file tree
Hide file tree
Showing 19 changed files with 157 additions and 134 deletions.
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(
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);

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);
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

0 comments on commit 2f33705

Please sign in to comment.