Skip to content

Commit

Permalink
refactor: Like를 VO로 취급, Likes를 컨테이너로 취급하도록 변경 (#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
bperhaps committed Jul 28, 2021
1 parent 2a6e420 commit c96d3e6
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 40 deletions.
Expand Up @@ -7,6 +7,7 @@
import com.woowacourse.pickgit.post.domain.Post;
import com.woowacourse.pickgit.post.domain.comment.Comment;
import com.woowacourse.pickgit.tag.domain.Tag;
import com.woowacourse.pickgit.user.domain.User;
import java.util.List;
import java.util.function.Function;

Expand All @@ -16,16 +17,16 @@ private PostDtoAssembler() {
}

public static List<PostResponseDto> assembleFrom(
String userName,
User requestUser,
boolean isGuest,
List<Post> posts
) {
return posts.stream()
.map(post -> convertFrom(userName, isGuest, post))
.map(post -> convertFrom(requestUser, isGuest, post))
.collect(toList());
}

private static PostResponseDto convertFrom(String userName, boolean isGuest, Post post) {
private static PostResponseDto convertFrom(User requestUser, boolean isGuest, Post post) {
List<String> tags = createTagsFrom(post);
List<CommentResponseDto> comments = createCommentResponsesFrom(post);

Expand All @@ -41,7 +42,7 @@ private static PostResponseDto convertFrom(String userName, boolean isGuest, Pos
.createdAt(post.getCreatedAt())
.updatedAt(post.getUpdatedAt())
.comments(comments)
.isLiked(isLikedBy(userName, post, isGuest))
.isLiked(isLikedBy(requestUser, post, isGuest))
.build();
}

Expand Down Expand Up @@ -69,11 +70,11 @@ private static List<String> createTagsFrom(Post post) {
.collect(toList());
}

private static Boolean isLikedBy(String userName, Post post, boolean isGuest) {
private static Boolean isLikedBy(User requestUser, Post post, boolean isGuest) {
if (isGuest) {
return null;
}

return post.isLikedBy(userName);
return post.isLikedBy(requestUser);
}
}
Expand Up @@ -60,12 +60,18 @@ private List<PostResponseDto> readFeed(HomeFeedRequestDto homeFeedRequestDto,
.map(target -> postRepository.findAllPostsByUser(target, pageable))
.orElse(postRepository.findAllPosts(pageable));

return PostDtoAssembler.assembleFrom(requestUserName, isGuest, result);
User requestUser = findUserByName(requestUserName);

return PostDtoAssembler.assembleFrom(requestUser, isGuest, result);
}

private User findUserByName(String username) {
private User findUserByName(String userName) {
if(Objects.isNull(userName)) {
return null;
}

return userRepository
.findByBasicProfile_Name(username)
.findByBasicProfile_Name(userName)
.orElseThrow(UserNotFoundException::new);
}
}
Expand Up @@ -7,6 +7,7 @@
import com.woowacourse.pickgit.post.domain.content.Image;
import com.woowacourse.pickgit.post.domain.content.Images;
import com.woowacourse.pickgit.post.domain.content.PostContent;
import com.woowacourse.pickgit.post.domain.like.Like;
import com.woowacourse.pickgit.post.domain.like.Likes;
import com.woowacourse.pickgit.post.domain.tag.PostTags;
import com.woowacourse.pickgit.tag.domain.Tag;
Expand Down Expand Up @@ -104,8 +105,8 @@ public void addTags(List<Tag> tags) {
postTags.addAll(this, tags);
}

public boolean isLikedBy(String userName) {
return likes.contains(userName);
public boolean isLikedBy(User user) {
return likes.contains(new Like(this, user));
}

public Long getId() {
Expand Down
Expand Up @@ -40,8 +40,12 @@ public Like(Long id, Post post, User user) {
this.user = user;
}

public boolean isOwnedBy(String userName) {
return user.getName().equals(userName);
private Post getPost() {
return post;
}

private User getUser() {
return user;
}

@Override
Expand All @@ -53,11 +57,12 @@ public boolean equals(Object o) {
return false;
}
Like like = (Like) o;
return Objects.equals(id, like.id);
return Objects.equals(post, like.getPost()) &&
Objects.equals(user, like.getUser());
}

@Override
public int hashCode() {
return Objects.hash(id);
return Objects.hash(post, user);
}
}
@@ -1,5 +1,6 @@
package com.woowacourse.pickgit.post.domain.like;

import com.woowacourse.pickgit.user.domain.User;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Embeddable;
Expand All @@ -24,8 +25,8 @@ public int getCounts() {
return likes.size();
}

public boolean contains(String userName) {
public boolean contains(Like like) {
return likes.stream()
.anyMatch(like -> like.isOwnedBy(userName));
.anyMatch(l -> l.equals(like));
}
}
Expand Up @@ -48,6 +48,7 @@ void readHomeFeed_Success() {
//given
createMockPosts();

userRepository.save(UserFactory.user("kevin"));
HomeFeedRequestDto homeFeedRequestDto = HomeFeedRequestDto.builder()
.requestUserName("kevin")
.isGuest(false)
Expand Down
Expand Up @@ -194,7 +194,7 @@ void readUserFeed_validUser_ExceptionOccur() {

assertThat(actual).containsAll(expected);

verify(userRepository, times(1)).findByBasicProfile_Name(anyString());
verify(userRepository, times(2)).findByBasicProfile_Name(anyString());
}

@DisplayName("게스트 유저는 다른 유저의 홈 피드를 가져온다")
Expand Down Expand Up @@ -255,6 +255,8 @@ void readUserFeed_invalidUser_ExceptionOccur() {

given(userRepository.findByBasicProfile_Name("testUser"))
.willReturn(Optional.of(UserFactory.user("testUser")));
given(userRepository.findByBasicProfile_Name("invalidUser"))
.willReturn(Optional.of(UserFactory.user("invalidUser")));
given(postRepository.findAllPostsByUser(any(User.class), any(Pageable.class)))
.willReturn(posts);

Expand All @@ -273,6 +275,6 @@ void readUserFeed_invalidUser_ExceptionOccur() {

assertThat(actual).containsAll(expected);

verify(userRepository, times(1)).findByBasicProfile_Name(anyString());
verify(userRepository, times(2)).findByBasicProfile_Name(anyString());
}
}
Expand Up @@ -206,11 +206,6 @@ void addComment_InvalidContent_ExceptionThrown() {

User user = UserFactory.user("testuser");

given(postRepository.findById(anyLong()))
.willReturn(Optional.of(post));
given(userRepository.findByBasicProfile_Name(user.getName()))
.willReturn(Optional.of(user));

CommentRequestDto commentRequestDto =
new CommentRequestDto(user.getName(), "", post.getId());

Expand All @@ -219,9 +214,6 @@ void addComment_InvalidContent_ExceptionThrown() {
.isInstanceOf(CommentFormatException.class)
.extracting("errorCode")
.isEqualTo("F0002");

verify(postRepository, times(1)).findById(anyLong());
verify(userRepository, times(1)).findByBasicProfile_Name(anyString());
}

@DisplayName("존재하지 않는 사용자는 댓글을 등록할 수 없다.")
Expand Down
Expand Up @@ -11,7 +11,7 @@

class LikeTest {

@DisplayName("like의 소유자를 판단한다.")
@DisplayName("like의 동등성를 판단한다.")
@Test
void isOwnedBy() {
//given
Expand All @@ -22,7 +22,7 @@ void isOwnedBy() {
Like like = new Like(post, user);

//when
boolean ownedBy = like.isOwnedBy(userName);
boolean ownedBy = like.equals(new Like(post, user));

//then
assertThat(ownedBy).isTrue();
Expand Down
Expand Up @@ -17,20 +17,21 @@
class LikesTest {

private Likes likes;
private Post post;

@BeforeEach
void setUp() {
Post post = Post.builder().id(1L).build();
post = Post.builder().id(1L).build();

User testUser1 = UserFactory.user("testUser1");
User testUser2 = UserFactory.user("testUser2");
User testUser3 = UserFactory.user("testUser3");
User testUser1 = UserFactory.user(1L, "testUser1");
User testUser2 = UserFactory.user(2L, "testUser2");
User testUser3 = UserFactory.user(3L, "testUser3");

likes = new Likes(List.of(
new Like(post, testUser1),
new Like(post, testUser2),
new Like(post, testUser3)
));
new Like(post, testUser1),
new Like(post, testUser2),
new Like(post, testUser3)
));
}

@DisplayName("like의 개수를 확인한다.")
Expand All @@ -41,8 +42,9 @@ void getCounts() {

@DisplayName("특정 사용자의 like가 포함되어 있는지 확인한다.")
@ParameterizedTest
@CsvSource(value = {"testUser1,true", "testUser2,true", "noTestUser1,false"})
void contains(String userName, boolean expected) {
assertThat(likes.contains(userName)).isEqualTo(expected);
@CsvSource(value = {"1, testUser1, true", "2,testUser2, true", "4, noTestUser1, false"})
void contains(Long id, String userName, boolean expected) {
User user = UserFactory.user(id, userName);
assertThat(likes.contains(new Like(post, user))).isEqualTo(expected);
}
}

0 comments on commit c96d3e6

Please sign in to comment.