Skip to content

Commit

Permalink
refactor: Post 프로덕션 코드 리팩토링 (#252)
Browse files Browse the repository at this point in the history
* refactor: infrastructure 유닛테스트, comment 유닛테스트 리팩토링

* test: Post 관련 유닛테스트 리팩토링

* test: Post application 레이어 관련 테스트 리팩토링

* test: Post 인수 테스트 리팩토링

* test: PostControllerTest 리팩토링 추가

* test: post builder를 사용하도록 변경

* refactor: 리베이스 후 깨지는 테스트 수정 및 누락된 테스트 추가

refactor: Post의 Tag관련 비즈니스 로직 PostTags로 이동 (#259)

이동과 함께 Post의 Build 패턴 적용과 리팩토링.

refactor: PostService와 PostController의 책임 분리. 및 s3Storage 파일 저장 방식 추가 (#259)

feed관련과, 게시물 관련으로 분리하여 관심사를 맞춤.

refactor: 전체적인 import 정리 (#259)

refactor: 패키지 정리 (#259)

refactor: Post의 getImageUrl 중복 메서드 제거 (#259)

refactor: CommentResponse의 도메인 참조 제거 및 DtoAssembler 리팩토링 (#259)

refactor: Feed관련 컨트롤러, 서비스, PostDto어셈블러  리팩토링, 레이어에 맞지 않는 DTO 이동 (#259)

refactor: CommentRequestDto 패키지 변경 및 PostController 리팩토링 (#259)

refactor: PostResponseDto가 presentation~presentation까지 사용되던것을 분리 (#259)

refactor: showRepositories -> userRepositories 메서드명 변경 (#259)

refactor: RepositoryResponse, domain -> controller까지 사용 되던것을 분리 (#259)

style: domain을 제외한 레이어의 구글 컨벤션 체크 (#259)

refactor: PostImageUrlResponseDto Builder 사용하도록 변경

refactor: Post 컨벤션 및 메서드 순서 정리 (#259)

refactor: Post user 관련 메서드명 author로 변경 및 필드, 메서드 정리 (#249)

refactor: PostTag equals getId()로 변경 및 필드 순서 정리 (#249)

refactor: Like를 VO로 취급, Likes를 컨테이너로 취급하도록 변경 (#249)

refactor: Image VO로 취급 및 도움 메서드 네이밍 변경, PostContent validation로직 변경 (#249)

refactor: CommentContent VO 취급, comment 도움메서드 이름 변경 및 책임 변경 (#249)

refactor: Post equals getter를 사용하도록 변경 (#249)

refactor: 연관관계 도움 메서드 반환깂 없도록 변경 (#249)

style: 구글 컨벤션 적용 (#249)

refactor: 추상화 수준 평탄화 작업 (#249)
  • Loading branch information
bperhaps committed Aug 1, 2021
1 parent 32b131f commit 15cd49f
Show file tree
Hide file tree
Showing 71 changed files with 1,958 additions and 771 deletions.
@@ -1,7 +1,5 @@
package com.woowacourse.pickgit.authentication.application;

import com.woowacourse.pickgit.user.domain.User;

public interface JwtTokenProvider {

String createToken(String payload);
Expand Down
Expand Up @@ -4,7 +4,6 @@
import com.woowacourse.pickgit.authentication.domain.OAuthClient;
import com.woowacourse.pickgit.authentication.infrastructure.dto.OAuthAccessTokenRequest;
import com.woowacourse.pickgit.authentication.infrastructure.dto.OAuthAccessTokenResponse;
import com.woowacourse.pickgit.exception.platform.PlatformException;
import com.woowacourse.pickgit.exception.platform.PlatformHttpErrorException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
Expand Down
Expand Up @@ -5,7 +5,6 @@
import com.woowacourse.pickgit.authentication.presentation.interceptor.AuthHeader;
import javax.servlet.http.HttpServletRequest;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
Expand Down
@@ -1,6 +1,6 @@
package com.woowacourse.pickgit.common.network;

import com.woowacourse.pickgit.post.infrastructure.RestClient;
import com.woowacourse.pickgit.post.domain.util.RestClient;
import java.net.URI;
import java.util.Map;
import java.util.Set;
Expand Down
Expand Up @@ -4,11 +4,16 @@

public class PostNotFoundException extends PostException {

public PostNotFoundException(
String errorCode,
HttpStatus httpStatus,
String message)
{
private static final String ERROR_CODE = "P0002";
private static final HttpStatus HTTP_STATUS = HttpStatus.INTERNAL_SERVER_ERROR;
private static final String MESSAGE = "해당하는 게시물을 찾을 수 없습니다.";

public PostNotFoundException() {
this(ERROR_CODE, HTTP_STATUS, MESSAGE);
}

public PostNotFoundException(String errorCode, HttpStatus httpStatus,
String message) {
super(errorCode, httpStatus, message);
}
}
Expand Up @@ -3,6 +3,13 @@
import org.springframework.http.HttpStatus;

public class RepositoryParseException extends PostException {
private static final String ERROR_CODE = "V0001";
private static final HttpStatus HTTP_STATUS = HttpStatus.BAD_REQUEST;
private static final String MESSAGE = "레포지토리 목록을 불러올 수 없습니다.";

public RepositoryParseException() {
this(ERROR_CODE, HTTP_STATUS, MESSAGE);
}

public RepositoryParseException(
String errorCode, HttpStatus httpStatus, String message
Expand Down
Expand Up @@ -4,8 +4,15 @@

public class UserNotFoundException extends UserException {

public UserNotFoundException(String errorCode, HttpStatus httpStatus,
String message) {
private static final String ERROR_CODE = "U0001";
private static final HttpStatus HTTP_STATUS = HttpStatus.INTERNAL_SERVER_ERROR;
private static final String MESSAGE = "해당하는 사용자를 찾을 수 없습니다.";

public UserNotFoundException() {
this(ERROR_CODE, HTTP_STATUS, MESSAGE);
}

public UserNotFoundException(String errorCode, HttpStatus httpStatus, String message) {
super(errorCode, httpStatus, message);
}
}
@@ -1,46 +1,80 @@
package com.woowacourse.pickgit.post.application;

import com.woowacourse.pickgit.authentication.domain.user.AppUser;
import com.woowacourse.pickgit.post.application.dto.CommentResponse;
import static java.util.stream.Collectors.toList;

import com.woowacourse.pickgit.post.application.dto.response.CommentResponseDto;
import com.woowacourse.pickgit.post.application.dto.response.PostResponseDto;
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.stream.Collectors;
import java.util.function.Function;

public class PostDtoAssembler {

private PostDtoAssembler() {
}

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

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

return PostResponseDto.builder()
.id(post.getId())
.imageUrls(post.getImageUrls())
.githubRepoUrl(post.getGithubRepoUrl())
.content(post.getContent())
.authorName(post.getAuthorName())
.profileImageUrl(post.getAuthorProfileImage())
.likesCount(post.getLikeCounts())
.tags(tags)
.createdAt(post.getCreatedAt())
.updatedAt(post.getUpdatedAt())
.comments(comments)
.isLiked(isLikedBy(requestUser, post, isGuest))
.build();
}

private static PostResponseDto convertFrom(Post post, AppUser appUser) {
User postWriter = post.getUser();
List<String> tags = post.getTags()
private static List<CommentResponseDto> createCommentResponsesFrom(Post post) {
return post.getComments()
.stream()
.map(Tag::getName)
.collect(Collectors.toList());
List<CommentResponse> comments = post.getComments()
.map(toCommentResponse())
.collect(toList());
}

private static Function<Comment, CommentResponseDto> toCommentResponse() {
return comment -> CommentResponseDto.builder()
.id(comment.getId())
.profileImageUrl(comment.getProfileImageUrl())
.authorName(comment.getAuthorName())
.content(comment.getContent())
.isLiked(false)
.build();
}

private static List<String> createTagsFrom(Post post) {
return post.getTags()
.stream()
.map(CommentResponse::from)
.collect(Collectors.toList());

if (appUser.isGuest()) {
return new PostResponseDto(post.getId(), post.getImageUrls(), post.getGithubRepoUrl(),
post.getContent(), postWriter.getName(), postWriter.getImage(),
post.getLikeCounts(),
tags, post.getCreatedAt(), post.getUpdatedAt(), comments, null);
.map(Tag::getName)
.collect(toList());
}

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

return new PostResponseDto(post.getId(), post.getImageUrls(), post.getGithubRepoUrl(),
post.getContent(), postWriter.getName(), postWriter.getImage(),
post.getLikeCounts(),
tags, post.getCreatedAt(), post.getUpdatedAt(), comments, post.isLikedBy(appUser.getUsername()));
return post.isLikedBy(requestUser);
}
}
@@ -0,0 +1,82 @@
package com.woowacourse.pickgit.post.application;

import com.woowacourse.pickgit.exception.authentication.UnauthorizedException;
import com.woowacourse.pickgit.exception.user.UserNotFoundException;
import com.woowacourse.pickgit.post.application.dto.request.HomeFeedRequestDto;
import com.woowacourse.pickgit.post.application.dto.response.PostResponseDto;
import com.woowacourse.pickgit.post.domain.Post;
import com.woowacourse.pickgit.post.domain.repository.PostRepository;
import com.woowacourse.pickgit.user.domain.User;
import com.woowacourse.pickgit.user.domain.UserRepository;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true)
public class PostFeedService {

private final PostRepository postRepository;
private final UserRepository userRepository;

public PostFeedService(PostRepository postRepository, UserRepository userRepository) {
this.postRepository = postRepository;
this.userRepository = userRepository;
}

public List<PostResponseDto> homeFeed(HomeFeedRequestDto homeFeedRequestDto) {
return readFeed(homeFeedRequestDto, Optional.empty());
}

public List<PostResponseDto> myFeed(HomeFeedRequestDto homeFeedRequestDto) {
String userName = homeFeedRequestDto.getRequestUserName();

if (Objects.isNull(userName)) {
throw new UnauthorizedException();
}

return readFeed(homeFeedRequestDto, Optional.of(userName));
}

public List<PostResponseDto> userFeed(HomeFeedRequestDto homeFeedRequestDto, String userName) {
return readFeed(homeFeedRequestDto, Optional.of(userName));
}

private List<PostResponseDto> readFeed(
HomeFeedRequestDto homeFeedRequestDto,
Optional<String> userName
) {
var page = homeFeedRequestDto.getPage().intValue();
var limit = homeFeedRequestDto.getLimit().intValue();
var requestUserName = homeFeedRequestDto.getRequestUserName();
var isGuest = homeFeedRequestDto.isGuest();

Pageable pageable = PageRequest.of(page, limit);
List<Post> result = getPostsBy(userName, pageable);

User requestUser = findUserByName(requestUserName);

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

private List<Post> getPostsBy(Optional<String> userName, Pageable pageable) {
return userName
.map(this::findUserByName)
.map(target -> postRepository.findAllPostsByUser(target, pageable))
.orElse(postRepository.findAllPosts(pageable));
}

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

return userRepository
.findByBasicProfile_Name(userName)
.orElseThrow(UserNotFoundException::new);
}
}

0 comments on commit 15cd49f

Please sign in to comment.