Skip to content

Commit

Permalink
Merge pull request #75 from jisung-in/refactor/71-comment-refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
AHNYUNKI authored Apr 12, 2024
2 parents 3f44742 + c0ef411 commit 1c9c692
Show file tree
Hide file tree
Showing 12 changed files with 248 additions and 118 deletions.
22 changes: 10 additions & 12 deletions src/main/java/com/jisungin/api/comment/CommentController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
import com.jisungin.api.comment.request.CommentCreateRequest;
import com.jisungin.api.comment.request.CommentEditRequest;
import com.jisungin.api.oauth.Auth;
import com.jisungin.api.oauth.AuthContext;
import com.jisungin.application.PageResponse;
import com.jisungin.application.comment.CommentService;
import com.jisungin.application.comment.response.CommentQueryResponse;
import com.jisungin.application.comment.response.CommentPageResponse;
import com.jisungin.application.comment.response.CommentResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
Expand All @@ -31,27 +29,27 @@ public class CommentController {
@PostMapping("{talkRoomId}/comments")
public ApiResponse<CommentResponse> writeComment(@PathVariable Long talkRoomId,
@Valid @RequestBody CommentCreateRequest request,
@Auth AuthContext authContext) {
return ApiResponse.ok(commentService.writeComment(request.toService(), talkRoomId, authContext));
@Auth Long userId) {
return ApiResponse.ok(commentService.writeComment(request.toService(), talkRoomId, userId));
}

@GetMapping("{talkRoomId}/comments")
public ApiResponse<PageResponse<CommentQueryResponse>> findAllComments(@PathVariable Long talkRoomId,
@Auth AuthContext authContext) {
return ApiResponse.ok(commentService.findAllComments(talkRoomId, authContext));
public ApiResponse<CommentPageResponse> findAllComments(@PathVariable Long talkRoomId,
@Auth Long userId) {
return ApiResponse.ok(commentService.findAllComments(talkRoomId, userId));
}

@PatchMapping("/comments/{commentId}")
public ApiResponse<CommentResponse> editComment(@PathVariable Long commentId,
@Valid @RequestBody CommentEditRequest request,
@Auth AuthContext authContext) {
return ApiResponse.ok(commentService.editComment(commentId, request.toService(), authContext));
@Auth Long userId) {
return ApiResponse.ok(commentService.editComment(commentId, request.toService(), userId));
}

@DeleteMapping("/comments/{commentId}")
public ApiResponse<Void> deleteComment(@PathVariable Long commentId,
@Auth AuthContext authContext) {
commentService.deleteComment(commentId, authContext);
@Auth Long userId) {
commentService.deleteComment(commentId, userId);

return ApiResponse.<Void>builder()
.message("OK")
Expand Down
45 changes: 32 additions & 13 deletions src/main/java/com/jisungin/application/comment/CommentService.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
package com.jisungin.application.comment;

import com.jisungin.api.oauth.AuthContext;
import com.jisungin.application.PageResponse;
import com.jisungin.application.comment.request.CommentCreateServiceRequest;
import com.jisungin.application.comment.request.CommentEditServiceRequest;
import com.jisungin.application.comment.response.CommentPageResponse;
import com.jisungin.application.comment.response.CommentQueryResponse;
import com.jisungin.application.comment.response.CommentResponse;
import com.jisungin.domain.ReadingStatus;
import com.jisungin.domain.comment.Comment;
import com.jisungin.domain.comment.repository.CommentRepository;
import com.jisungin.domain.commentlike.repository.CommentLikeRepository;
import com.jisungin.domain.mylibrary.repository.UserLibraryRepository;
import com.jisungin.domain.talkroom.TalkRoom;
import com.jisungin.domain.talkroom.repository.TalkRoomRepository;
import com.jisungin.domain.talkroom.repository.TalkRoomRoleRepository;
import com.jisungin.domain.user.User;
import com.jisungin.domain.user.repository.UserRepository;
import com.jisungin.exception.BusinessException;
import com.jisungin.exception.ErrorCode;
import java.util.Collections;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -29,38 +33,53 @@ public class CommentService {
private final TalkRoomRepository talkRoomRepository;
private final UserRepository userRepository;
private final CommentLikeRepository commentLikeRepository;
private final UserLibraryRepository userLibraryRepository;
private final TalkRoomRoleRepository talkRoomRoleRepository;

@Transactional
public CommentResponse writeComment(CommentCreateServiceRequest request, Long talkRoomId, AuthContext authContext) {
User user = userRepository.findById(authContext.getUserId())
public CommentResponse writeComment(CommentCreateServiceRequest request, Long talkRoomId, Long userId) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new BusinessException(ErrorCode.USER_NOT_FOUND));

TalkRoom talkRoom = talkRoomRepository.findById(talkRoomId)
.orElseThrow(() -> new BusinessException(ErrorCode.TALK_ROOM_NOT_FOUND));

ReadingStatus userReadingStatus = userLibraryRepository.findByUserId(user.getId());

List<ReadingStatus> talkRoomReadingStatus = talkRoomRoleRepository.findTalkRoomRoleByTalkRoomId(
talkRoom.getId());

talkRoomReadingStatus.stream().filter(i -> i.equals(userReadingStatus)).findFirst()
.orElseThrow(() -> new BusinessException(ErrorCode.UNABLE_WRITE_COMMENT));

Comment comment = Comment.create(request, user, talkRoom);

commentRepository.save(comment);

return CommentResponse.of(comment.getContent(), user.getName());
}

public PageResponse<CommentQueryResponse> findAllComments(Long talkRoomId, AuthContext authContext) {
PageResponse<CommentQueryResponse> response = commentRepository.findAllComments(talkRoomId);
public CommentPageResponse findAllComments(Long talkRoomId, Long userId) {
TalkRoom talkRoom = talkRoomRepository.findById(talkRoomId)
.orElseThrow(() -> new BusinessException(ErrorCode.TALK_ROOM_NOT_FOUND));

User user = userRepository.findById(userId).orElseThrow(() -> new BusinessException(ErrorCode.USER_NOT_FOUND));
List<CommentQueryResponse> findComment = commentRepository.findAllComments(talkRoom.getId());

if (authContext.getUserId() != null) {
List<Long> likeComments = commentLikeRepository.userLikeComments(authContext.getUserId());
}
Long totalCount = commentRepository.commentTotalCount(talkRoom.getId());

List<Long> userLikeCommentIds =
(user.getId() != null) ? commentLikeRepository.userLikeComments(user.getId()) : Collections.emptyList();

return response;
return CommentPageResponse.of(PageResponse.of(50, totalCount, findComment), userLikeCommentIds);
}

@Transactional
public CommentResponse editComment(Long commentId, CommentEditServiceRequest request, AuthContext authContext) {
public CommentResponse editComment(Long commentId, CommentEditServiceRequest request, Long userId) {
Comment comment = commentRepository.findById(commentId)
.orElseThrow(() -> new BusinessException(ErrorCode.COMMENT_NOT_FOUND));

User user = userRepository.findById(authContext.getUserId())
User user = userRepository.findById(userId)
.orElseThrow(() -> new BusinessException(ErrorCode.USER_NOT_FOUND));

if (!comment.isCommentOwner(user.getId())) {
Expand All @@ -73,11 +92,11 @@ public CommentResponse editComment(Long commentId, CommentEditServiceRequest req
}

@Transactional
public void deleteComment(Long commentId, AuthContext authContext) {
public void deleteComment(Long commentId, Long userId) {
Comment comment = commentRepository.findById(commentId)
.orElseThrow(() -> new BusinessException(ErrorCode.COMMENT_NOT_FOUND));

User user = userRepository.findById(authContext.getUserId())
User user = userRepository.findById(userId)
.orElseThrow(() -> new BusinessException(ErrorCode.USER_NOT_FOUND));

if (!comment.isCommentOwner(user.getId())) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.jisungin.application.comment.response;

import com.jisungin.application.PageResponse;
import java.util.ArrayList;
import java.util.List;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class CommentPageResponse {

private PageResponse<CommentQueryResponse> response;

private List<Long> userLikeCommentIds = new ArrayList<>();

@Builder
private CommentPageResponse(PageResponse<CommentQueryResponse> response, List<Long> userLikeCommentIds) {
this.response = response;
this.userLikeCommentIds = userLikeCommentIds;
}

public static CommentPageResponse of(PageResponse<CommentQueryResponse> response,
List<Long> userLikeCommentIds) {
return CommentPageResponse.builder()
.response(response)
.userLikeCommentIds(userLikeCommentIds)
.build();
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.jisungin.application.comment.response;

import com.querydsl.core.annotations.QueryProjection;
import java.time.LocalDateTime;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -13,14 +14,17 @@ public class CommentQueryResponse {
private String userName;
private String content;
private Long commentLikeCount;
private LocalDateTime createTime;

@Builder
@QueryProjection
public CommentQueryResponse(Long commentId, String userName, String content, Long commentLikeCount) {
public CommentQueryResponse(Long commentId, String userName, String content, Long commentLikeCount,
LocalDateTime createTime) {
this.commentId = commentId;
this.userName = userName;
this.content = content;
this.commentLikeCount = commentLikeCount;
this.createTime = createTime;
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.jisungin.domain.comment.repository;

import com.jisungin.application.PageResponse;
import com.jisungin.application.comment.response.CommentQueryResponse;
import java.util.List;

public interface CommentRepositoryCustom {

PageResponse<CommentQueryResponse> findAllComments(Long talkRoomId);
List<CommentQueryResponse> findAllComments(Long talkRoomId);

Long commentTotalCount(Long talkRoomId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import static com.jisungin.domain.talkroom.QTalkRoom.talkRoom;
import static com.jisungin.domain.user.QUser.user;

import com.jisungin.application.PageResponse;
import com.jisungin.application.comment.response.CommentQueryResponse;
import com.jisungin.application.comment.response.QCommentQueryResponse;
import com.querydsl.jpa.impl.JPAQueryFactory;
Expand All @@ -18,35 +17,13 @@ public class CommentRepositoryImpl implements CommentRepositoryCustom {
private final JPAQueryFactory queryFactory;

@Override
public PageResponse<CommentQueryResponse> findAllComments(Long talkRoomId) {
List<CommentQueryResponse> findAllComment = getComments(talkRoomId);

Long commentCount = totalCount(talkRoomId);

return PageResponse.<CommentQueryResponse>builder()
.queryResponse(findAllComment)
.size(50)
.totalCount(commentCount)
.build();
}

private Long totalCount(Long talkRoomId) {
Long commentCount = queryFactory
.select(comment.count())
.from(comment)
.join(comment.talkRoom, talkRoom)
.join(comment.user, user)
.where(comment.talkRoom.id.eq(talkRoomId))
.fetchOne();
return commentCount;
}

private List<CommentQueryResponse> getComments(Long talkRoomId) {
List<CommentQueryResponse> findAllComment = queryFactory.select(new QCommentQueryResponse(
public List<CommentQueryResponse> findAllComments(Long talkRoomId) {
return queryFactory.select(new QCommentQueryResponse(
comment.id.as("commentId"),
user.name.as("userName"),
comment.content,
commentLike.count().as("commentLiKeCount")
commentLike.count().as("commentLiKeCount"),
comment.createDateTime
))
.from(comment)
.join(comment.talkRoom, talkRoom)
Expand All @@ -56,7 +33,17 @@ private List<CommentQueryResponse> getComments(Long talkRoomId) {
.where(comment.talkRoom.id.eq(talkRoomId))
.orderBy(comment.createDateTime.desc())
.fetch();
return findAllComment;
}

@Override
public Long commentTotalCount(Long talkRoomId) {
return queryFactory
.select(comment.count())
.from(comment)
.join(comment.talkRoom, talkRoom)
.join(comment.user, user)
.where(comment.talkRoom.id.eq(talkRoomId))
.fetchOne();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.jisungin.domain.mylibrary.repository;

import com.jisungin.domain.ReadingStatus;
import com.jisungin.domain.mylibrary.UserLibrary;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface UserLibraryRepository extends JpaRepository<UserLibrary, Long> {

@Query(
"SELECT ul.status FROM UserLibrary ul JOIN ul.user u WHERE u.id = :id"
)
ReadingStatus findByUserId(@Param("id") Long userId);
}
4 changes: 2 additions & 2 deletions src/main/java/com/jisungin/exception/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public enum ErrorCode {
REVIEW_LIKE_NOT_FOUND(404, "리뷰 좋아요를 찾을 수 없습니다."),
IMAGE_NOT_FOUND(400, "파일이 없습니다."),
S3_UPLOAD_FAIL(400, "이미지 업로드가 실패되었습니다."),
NOT_IMAGE(400, "이미지 파일이 아닙니다.");

NOT_IMAGE(400, "이미지 파일이 아닙니다."),
UNABLE_WRITE_COMMENT(400, "의견을 쓸 권한이 없습니다.");

private final int code;
private final String message;
Expand Down
Loading

0 comments on commit 1c9c692

Please sign in to comment.