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

[#232] 유저 테스트 코드 리팩토링 #249

Merged
merged 20 commits into from Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,13 +1,13 @@
package com.woowacourse.pickgit.user.application;

import com.woowacourse.pickgit.authentication.domain.user.AppUser;
import com.woowacourse.pickgit.user.application.dto.AuthUserServiceDto;
import com.woowacourse.pickgit.user.application.dto.FollowServiceDto;
import com.woowacourse.pickgit.user.application.dto.UserProfileServiceDto;
import com.woowacourse.pickgit.user.domain.User;
import com.woowacourse.pickgit.user.domain.UserRepository;
import com.woowacourse.pickgit.exception.user.InvalidUserException;
import com.woowacourse.pickgit.exception.user.SameSourceTargetUserException;
import com.woowacourse.pickgit.user.application.dto.request.AuthUserRequestDto;
import com.woowacourse.pickgit.user.application.dto.response.FollowResponseDto;
import com.woowacourse.pickgit.user.application.dto.response.UserProfileResponseDto;
import com.woowacourse.pickgit.user.domain.User;
import com.woowacourse.pickgit.user.domain.UserRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -22,60 +22,88 @@ public UserService(UserRepository userRepository) {
}

@Transactional(readOnly = true)
public UserProfileServiceDto getMyUserProfile(AuthUserServiceDto authUserServiceDto) {
User user = findUserByName(authUserServiceDto.getGithubName());

return new UserProfileServiceDto(
user.getName(), user.getImage(), user.getDescription(),
user.getFollowerCount(), user.getFollowingCount(), user.getPostCount(),
user.getGithubUrl(), user.getCompany(), user.getLocation(),
user.getWebsite(), user.getTwitter(), null
);
public UserProfileResponseDto getMyUserProfile(AuthUserRequestDto requestDto) {
User user = findUserByName(requestDto.getGithubName());

return UserProfileResponseDto.builder()
.name(user.getName())
.image(user.getImage())
.description(user.getDescription())
.followerCount(user.getFollowerCount())
.followingCount(user.getFollowingCount())
.postCount(user.getPostCount())
.githubUrl(user.getGithubUrl())
.company(user.getCompany())
.location(user.getLocation())
.website(user.getWebsite())
.twitter(user.getTwitter())
.following(false)
.build();
}

@Transactional(readOnly = true)
public UserProfileServiceDto getUserProfile(AppUser appUser, String targetUsername) {
User targetUser = findUserByName(targetUsername);

if (appUser.isGuest()) {
return new UserProfileServiceDto(
targetUser.getName(), targetUser.getImage(), targetUser.getDescription(),
targetUser.getFollowerCount(), targetUser.getFollowingCount(), targetUser.getPostCount(),
targetUser.getGithubUrl(), targetUser.getCompany(), targetUser.getLocation(),
targetUser.getWebsite(), targetUser.getTwitter(), null
);
public UserProfileResponseDto getUserProfile(AppUser user, String targetName) {
User target = findUserByName(targetName);

if (user.isGuest()) {
return UserProfileResponseDto.builder()
.name(target.getName())
.image(target.getImage())
.description(target.getDescription())
.followerCount(target.getFollowerCount())
.followingCount(target.getFollowingCount())
.postCount(target.getPostCount())
.githubUrl(target.getGithubUrl())
.company(target.getCompany())
.location(target.getLocation())
.website(target.getWebsite())
.twitter(target.getTwitter())
.following(null)
.build();
}

User sourceUser = findUserByName(appUser.getUsername());

return new UserProfileServiceDto(
targetUser.getName(), targetUser.getImage(), targetUser.getDescription(),
targetUser.getFollowerCount(), targetUser.getFollowingCount(), targetUser.getPostCount(),
targetUser.getGithubUrl(), targetUser.getCompany(), targetUser.getLocation(),
targetUser.getWebsite(), targetUser.getTwitter(), sourceUser.isFollowing(targetUser)
);
User source = findUserByName(user.getUsername());

return UserProfileResponseDto.builder()
.name(target.getName())
.image(target.getImage())
.description(target.getDescription())
.followerCount(target.getFollowerCount())
.followingCount(target.getFollowingCount())
.postCount(target.getPostCount())
.githubUrl(target.getGithubUrl())
.company(target.getCompany())
.location(target.getLocation())
.website(target.getWebsite())
.twitter(target.getTwitter())
.following(source.isFollowing(target))
.build();
}

public FollowServiceDto followUser(AuthUserServiceDto authUserServiceDto,
String targetUsername) {
User source = findUserByName(authUserServiceDto.getGithubName());
User target = findUserByName(targetUsername);
public FollowResponseDto followUser(AuthUserRequestDto requestDto, String targetName) {
User source = findUserByName(requestDto.getGithubName());
User target = findUserByName(targetName);

validateDifferentSourceTarget(source, target);
source.follow(target);

return new FollowServiceDto(target.getFollowerCount(), true);
return FollowResponseDto.builder()
.followerCount(target.getFollowerCount())
.isFollowing(true)
.build();
}

public FollowServiceDto unfollowUser(AuthUserServiceDto authUserServiceDto,
String targetUsername) {
User source = findUserByName(authUserServiceDto.getGithubName());
User target = findUserByName(targetUsername);
public FollowResponseDto unfollowUser(AuthUserRequestDto requestDto, String targetName) {
User source = findUserByName(requestDto.getGithubName());
User target = findUserByName(targetName);

validateDifferentSourceTarget(source, target);
source.unfollow(target);

return new FollowServiceDto(target.getFollowerCount(), false);
return FollowResponseDto.builder()
.followerCount(target.getFollowerCount())
.isFollowing(false)
.build();
}

private User findUserByName(String githubName) {
Expand All @@ -85,7 +113,7 @@ private User findUserByName(String githubName) {
}

private void validateDifferentSourceTarget(User source, User target) {
if (source.getId() == target.getId()) {
if (source.getId().equals(target.getId())) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

👀👍

throw new SameSourceTargetUserException();
}
}
Expand Down

This file was deleted.

@@ -0,0 +1,20 @@
package com.woowacourse.pickgit.user.application.dto.request;

import lombok.Builder;

@Builder
public class AuthUserRequestDto {

private String githubName;

private AuthUserRequestDto() {
}

public AuthUserRequestDto(String githubName) {
this.githubName = githubName;
}

public String getGithubName() {
return githubName;
}
}
@@ -1,11 +1,17 @@
package com.woowacourse.pickgit.user.application.dto;
package com.woowacourse.pickgit.user.application.dto.response;

public class FollowServiceDto {
import lombok.Builder;

@Builder
public class FollowResponseDto {

private boolean isFollowing;
private int followerCount;
private boolean isFollowing;

private FollowResponseDto() {
}

public FollowServiceDto(int followerCount, boolean isFollowing) {
public FollowResponseDto(int followerCount, boolean isFollowing) {
this.followerCount = followerCount;
this.isFollowing = isFollowing;
}
Expand Down
@@ -1,29 +1,31 @@
package com.woowacourse.pickgit.user.application.dto;
package com.woowacourse.pickgit.user.application.dto.response;

import lombok.Builder;

@Builder
public class UserProfileServiceDto {

private final String name;
private final String image;
private final String description;

private final int followerCount;
private final int followingCount;
private final int postCount;

private final String githubUrl;
private final String company;
private final String location;
private final String website;
private final String twitter;

private final Boolean following;
public class UserProfileResponseDto {

private String name;
private String image;
private String description;
private int followerCount;
private int followingCount;
private int postCount;
private String githubUrl;
private String company;
private String location;
private String website;
private String twitter;
private Boolean following;

private UserProfileResponseDto() {
}

public UserProfileServiceDto(String name, String image, String description,
int followerCount, int followingCount, int postCount, String githubUrl, String company,
String location, String website, String twitter, Boolean following) {
public UserProfileResponseDto(
String name, String image, String description,
int followerCount, int followingCount, int postCount,
String githubUrl, String company, String location, String website, String twitter,
Boolean following) {
this.name = name;
this.image = image;
this.description = description;
Expand Down
Expand Up @@ -44,25 +44,19 @@ protected User() {
public User(
Long id,
BasicProfile basicProfile,
GithubProfile githubProfile
) {
GithubProfile githubProfile) {
this.id = id;
this.basicProfile = basicProfile;
this.githubProfile = githubProfile;
}

public User(
BasicProfile basicProfile,
GithubProfile githubProfile
) {
GithubProfile githubProfile) {
this.basicProfile = basicProfile;
this.githubProfile = githubProfile;
}

public void changeBasicProfile(BasicProfile basicProfile) {
this.basicProfile = basicProfile;
}

public void changeGithubProfile(GithubProfile githubProfile) {
this.githubProfile = githubProfile;
}
Expand All @@ -77,18 +71,22 @@ public void follow(User target) {
target.followers.add(follow);
}


public void unfollow(User target) {
Follow follow = new Follow(this, target);

if (!this.followings.existFollow(follow)) {
throw new InvalidFollowException();
}

this.followings.remove(follow);
target.followers.remove(follow);
}

public void addComment(Post post, Comment comment) {
comment.toPost(post)
.writeBy(this);
post.addComment(comment);
}

public Boolean isFollowing(User targetUser) {
return this.followings.isFollowing(targetUser);
}
Expand Down Expand Up @@ -149,12 +147,6 @@ public String getTwitter() {
return githubProfile.getTwitter();
}

public void addComment(Post post, Comment comment) {
comment.toPost(post)
.writeBy(this);
post.addComment(comment);
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
Expand Up @@ -39,6 +39,10 @@ public Follow(User source, User target) {
this.target = target;
}

public boolean isFollowing(User targetUser) {
return this.target.equals(targetUser);
}

public Long getId() {
return id;
}
Expand Down
Expand Up @@ -10,7 +10,12 @@
@Embeddable
public class Followers {

@OneToMany(mappedBy = "target", fetch = FetchType.LAZY, cascade = CascadeType.PERSIST, orphanRemoval = true)
@OneToMany(
mappedBy = "target",
fetch = FetchType.LAZY,
cascade = CascadeType.PERSIST,
orphanRemoval = true
)
private List<Follow> followers = new ArrayList<>();

public Followers() {
Expand Down
Expand Up @@ -11,7 +11,12 @@
@Embeddable
public class Followings {

@OneToMany(mappedBy = "source", fetch = FetchType.LAZY, cascade = CascadeType.PERSIST, orphanRemoval = true)
@OneToMany(
mappedBy = "source",
fetch = FetchType.LAZY,
cascade = CascadeType.PERSIST,
orphanRemoval = true
)
private List<Follow> followings = new ArrayList<>();

public Followings() {
Expand All @@ -35,8 +40,6 @@ public void remove(Follow follow) {

public Boolean isFollowing(User targetUser) {
return followings.stream()
.filter(follow -> follow.getTarget().equals(targetUser))
.findAny().isPresent();
.anyMatch(follow -> follow.isFollowing(targetUser));
}
}