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

refactor(user): 좋아요 취소 로직 변경 및 user cascade 옵션 변경 #697

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
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 @@ -155,8 +155,7 @@ public void deleteComment(User adminUser, Long commentId) {
if (findComment.isParentComment()) {
applicationEventPublisher.publishEvent(new NotificationCommentDeleteEvent(findComment));
}
User author = findComment.getAuthor();
author.deleteComment(findComment);
commentRepository.delete(findComment);
}

private Comment getComment(Long commentId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@ public void addCommentLike(Long commentId, User user) {
throw new BadRequestException(ErrorType.ALREADY_LIKED_COMMENT);
}
CommentLike commentLike = new CommentLike(user, findComment);
user.addCommentLike(commentLike);
commentLikeRepository.save(commentLike);
}

public void deleteCommentLike(Long commentId, User user) {
Comment findComment = commentService.findEntityById(commentId);
CommentLike findCommentLike = findComment.findLikeBy(user)
.orElseThrow(() -> new BadRequestException(ErrorType.NOT_LIKED_COMMENT));
user.delete(findCommentLike);
commentLikeRepository.delete(findCommentLike);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void deleteComment(User user, Long commentId) {
if (findComment.isParentComment()) {
applicationEventPublisher.publishEvent(new NotificationCommentDeleteEvent(findComment));
}
user.deleteComment(findComment);
commentRepository.delete(findComment);
}

public CommentResponse createReply(User user, Long feedId, Long commentId, CommentRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@ public void addLike(User user, Long feedId) {
if (user.isLiked(findFeed)) {
throw new BadRequestException(ErrorType.ALREADY_LIKED);
}
user.addLike(new Like(user, findFeed));
likeRepository.save(new Like(user, findFeed));
applicationEventPublisher.publishEvent(NotificationEvent.likeOf(findFeed, user));
}

public void deleteLike(User user, Long feedId) {
Feed findFeed = feedService.findEntityById(feedId);
Like findLike = findFeed.findLikeBy(user)
.orElseThrow(() -> new BadRequestException(ErrorType.NOT_LIKED));
user.delete(findLike);
likeRepository.delete(findLike);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ public class Comment extends BaseEntity {
@JoinColumn(name = "parent_id")
private Comment parentComment;

@OneToMany(mappedBy = "comment", cascade = CascadeType.REMOVE, orphanRemoval = true)
@OneToMany(mappedBy = "comment", cascade = CascadeType.REMOVE)
Copy link
Collaborator

Choose a reason for hiding this comment

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

CascadeType.REMOVE만 가져가고 orphanRemoval 없애주는 거 좋네요!

private List<CommentLike> likes = new ArrayList<>();

@OneToMany(mappedBy = "parentComment", cascade = CascadeType.REMOVE, orphanRemoval = true)
@OneToMany(mappedBy = "parentComment", cascade = CascadeType.REMOVE)
private List<Comment> replies = new ArrayList<>();

public Comment(String content, boolean helper) {
Expand Down
8 changes: 4 additions & 4 deletions backend/src/main/java/com/wooteco/nolto/user/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,16 @@ public class User extends BaseEntity {

private String bio = "";

@OneToMany(mappedBy = "author", cascade = CascadeType.ALL)
@OneToMany(mappedBy = "author", cascade = CascadeType.REMOVE)
Copy link
Collaborator

Choose a reason for hiding this comment

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

ALL을 쓰는건 생각과 다르게 동작할 여지가 있는 것 같아요!
REMOVE 전환 찬성입니다~!

private List<Feed> feeds = new ArrayList<>();

@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
@OneToMany(mappedBy = "user", cascade = CascadeType.REMOVE)
private List<Like> likes = new ArrayList<>();

@OneToMany(mappedBy = "author", cascade = CascadeType.ALL, orphanRemoval = true)
@OneToMany(mappedBy = "author", cascade = CascadeType.REMOVE)
private List<Comment> comments = new ArrayList<>();

@OneToMany(mappedBy = "user", cascade = CascadeType.REMOVE, orphanRemoval = true)
@OneToMany(mappedBy = "user", cascade = CascadeType.REMOVE)
private List<CommentLike> commentLikes = new ArrayList<>();

public User(String socialId, SocialType socialType, String nickName, String imageUrl) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,23 +249,25 @@ void findAllComments() {
void deleteComment() {
//given
Feed 피드1 = 진행중_단계의_피드_생성("피드1", "피드1").writtenBy(조엘);
feedRepository.saveAndFlush(피드1);
feedRepository.save(피드1);
Feed 피드2 = 진행중_단계의_피드_생성("피드2", "피드2").writtenBy(조엘);
feedRepository.saveAndFlush(피드2);
feedRepository.save(피드2);
Feed 피드3 = 진행중_단계의_피드_생성("피드3", "피드3").writtenBy(조엘);
feedRepository.saveAndFlush(피드3);
feedRepository.save(피드3);

final Comment 피드1_댓글1 = new Comment("피드1_댓글1", false).writtenBy(찰리, 피드1);
commentRepository.saveAndFlush(피드1_댓글1);
commentRepository.save(피드1_댓글1);
final Comment 피드1_댓글2 = new Comment("피드1_댓글2", false).writtenBy(찰리, 피드1);
commentRepository.saveAndFlush(피드1_댓글2);
commentRepository.save(피드1_댓글2);
final Comment 피드1_댓글3 = new Comment("피드1_댓글3", false).writtenBy(찰리, 피드1);
commentRepository.saveAndFlush(피드1_댓글3);
commentRepository.save(피드1_댓글3);

final Comment 피드2_댓글1 = new Comment("피드2_댓글1", false).writtenBy(찰리, 피드2);
commentRepository.saveAndFlush(피드2_댓글1);
commentRepository.save(피드2_댓글1);
final Comment 피드2_댓글2 = new Comment("피드2_댓글2", false).writtenBy(찰리, 피드2);
commentRepository.saveAndFlush(피드2_댓글2);
commentRepository.save(피드2_댓글2);
em.flush();
em.clear();

//when
adminService.deleteComment(User.ADMIN_USER, 피드1_댓글1.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ void addCommentLike() {
em.clear();

// then
찰리가_쓴_피드에_찰리가_쓴_댓글 = commentRepository.getById(찰리가_쓴_피드에_찰리가_쓴_댓글.getId());
assertThat(찰리가_쓴_피드에_찰리가_쓴_댓글.getLikes()).hasSize(1);
}
Comment on lines 33 to 38
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

이전에는 db에 반영된 찰리가_쓴_피드에_찰리가_쓴_댓글을 검증한 것이 아닌 현재 테스트 메서드에 있는
찰리가_쓴_피드에_찰리가_쓴_댓글 객체를 검증하고 있었어요.
해당 서비스 테스트에서는 addCommentLike후 db에 반영된 내용을 검증하니
새롭게 getById()로 찰리가_쓴_피드에_찰리가_쓴_댓글을 찾아 검증해야할 것 같숩니다 !


Expand All @@ -46,6 +47,7 @@ void addCommentLikeTwice() {
em.clear();

// when then
찰리 = userRepository.getById(찰리.getId());
assertThatThrownBy(() -> commentLikeService.addCommentLike(찰리가_쓴_피드에_찰리가_쓴_댓글.getId(), 찰리))
.isInstanceOf(BadRequestException.class);
}
Expand Down Expand Up @@ -76,7 +78,6 @@ void deleteCommentLikeNotLiked() {
commentLikeService.addCommentLike(찰리가_쓴_피드에_찰리가_쓴_댓글.getId(), 찰리);
em.flush();
em.clear();
assertThat(찰리가_쓴_피드에_찰리가_쓴_댓글.getLikes()).hasSize(1);

// when
Comment findComment1 = commentService.findEntityById(찰리가_쓴_피드에_찰리가_쓴_댓글.getId());
Expand All @@ -102,6 +103,7 @@ void addReplyLike() {
em.clear();

// then
찰리가_쓴_피드에_찰리가_쓴_댓글에_포모가_쓴_대댓글 = commentRepository.getById(찰리가_쓴_피드에_찰리가_쓴_댓글에_포모가_쓴_대댓글.getId());
assertThat(찰리가_쓴_피드에_찰리가_쓴_댓글에_포모가_쓴_대댓글.getLikes()).hasSize(1);
}

Expand All @@ -115,6 +117,7 @@ void addReplyLikeTwice() {
em.clear();

// when then
찰리 = userRepository.getById(찰리.getId());
assertThatThrownBy(() -> commentLikeService.addCommentLike(찰리가_쓴_피드에_찰리가_쓴_댓글에_포모가_쓴_대댓글.getId(), 찰리))
.isInstanceOf(BadRequestException.class);
}
Expand Down Expand Up @@ -145,7 +148,6 @@ void deleteReplyLikeNotLiked() {
commentLikeService.addCommentLike(찰리가_쓴_피드에_찰리가_쓴_댓글에_포모가_쓴_대댓글.getId(), 찰리);
em.flush();
em.clear();
assertThat(찰리가_쓴_피드에_찰리가_쓴_댓글에_포모가_쓴_대댓글.getLikes()).hasSize(1);

// when
Comment findComment1 = commentService.findEntityById(찰리가_쓴_피드에_찰리가_쓴_댓글에_포모가_쓴_대댓글.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ void deleteReply() {
assertThat(구글_유저.getComments().size()).isOne();
assertThat(아마찌.getComments().size()).isOne();
em.flush();
em.clear();

// when
Comment 아마찌_대댓글 = commentService.findEntityById(아마찌_대댓글_생성_응답.getId());
Expand Down Expand Up @@ -333,10 +334,13 @@ void deleteCommentExistReply() {
commentLikeService.addCommentLike(포모_댓글_생성_응답.getId(), 구글_유저);
commentLikeService.addCommentLike(아마찌_대댓글_생성_응답.getId(), 아마찌);
em.flush();
em.clear();

// when
Comment 포모_댓글 = commentService.findEntityById(포모_댓글_생성_응답.getId());
Comment 아마찌_대댓글 = commentService.findEntityById(아마찌_대댓글_생성_응답.getId());
구글_유저 = userRepository.getById(구글_유저.getId());
아마찌 = userRepository.getById(아마찌.getId());
assertThat(구글_유저.getComments().size()).isOne();
assertThat(아마찌.getComments().size()).isOne();
assertThat(구글_유저.getCommentLikes().size()).isOne();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.wooteco.nolto.feed.application;

import com.wooteco.nolto.admin.ui.dto.CommentsByFeedResponse;
import com.wooteco.nolto.exception.ErrorType;
import com.wooteco.nolto.exception.NotFoundException;
import com.wooteco.nolto.exception.UnauthorizedException;
import com.wooteco.nolto.feed.domain.Comment;
import com.wooteco.nolto.feed.domain.Feed;
import com.wooteco.nolto.feed.domain.repository.CommentRepository;
import com.wooteco.nolto.feed.domain.repository.FeedRepository;
import com.wooteco.nolto.feed.ui.dto.*;
import com.wooteco.nolto.feed.ui.dto.FeedCardPaginationResponse;
import com.wooteco.nolto.feed.ui.dto.FeedCardResponse;
import com.wooteco.nolto.feed.ui.dto.FeedRequest;
import com.wooteco.nolto.feed.ui.dto.FeedResponse;
import com.wooteco.nolto.image.application.ImageService;
import com.wooteco.nolto.tech.domain.Tech;
import com.wooteco.nolto.tech.domain.TechRepository;
Expand All @@ -27,10 +26,12 @@

import javax.persistence.EntityManager;
import javax.transaction.Transactional;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import static com.wooteco.nolto.FeedFixture.진행중_단계의_피드_생성;
import static com.wooteco.nolto.TechFixture.*;
import static com.wooteco.nolto.UserFixture.조엘_생성;
import static com.wooteco.nolto.UserFixture.찰리_생성;
Expand Down Expand Up @@ -280,6 +281,7 @@ void checkLikeWhenFindFeed() {
likeService.addLike(조엘, feedId1);
em.flush();
em.clear();
조엘 = userRepository.getById(조엘.getId());

// when
FeedResponse feedResponse = feedService.viewFeed(조엘, feedId1, true);
Expand All @@ -295,6 +297,7 @@ void checkNotLikeWhenFindFeed() {
Long feedId1 = feedService.create(찰리, EMPTY_TECH_FEED_REQUEST);
em.flush();
em.clear();
조엘 = userRepository.getById(조엘.getId());

// when
FeedResponse feedResponse = feedService.viewFeed(조엘, feedId1, true);
Expand All @@ -316,6 +319,7 @@ void cancelLike() {
likeService.deleteLike(조엘, feedId1);
em.flush();
em.clear();
조엘 = userRepository.getById(조엘.getId());
FeedResponse feedResponse = feedService.viewFeed(조엘, feedId1, true);
Comment on lines 321 to 323
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

해당 viewFeed 로직 안에서 user.isLiked(feed);로 좋아요 여부를 반환하는데,
이 때도 실제 db에 반영된 조엘 을 넘겨주지 않고 있었어요 !


// then
Expand All @@ -332,12 +336,15 @@ void cancelLikeWhenDeleteUser() {
em.clear();

// when
조엘 = userRepository.getById(조엘.getId());
userRepository.delete(조엘);
em.flush();
em.clear();
Feed findFeed = feedService.findEntityById(feedId1);

// then
조엘 = userRepository.getById(조엘.getId());

assertThat(findFeed.findLikeBy(조엘)).isEmpty();
}

Expand Down