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

[#95] 페어 프로그래밍으로 코드 리팩터링 하기 #102

Merged
merged 35 commits into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
aa267e5
update: 메서드 이름 변경
JoosungKwon Jan 29, 2023
4541891
fix: 로컬 개발 환경에서 만든 클래스가 푸쉬 되어 수정
JoosungKwon Jan 29, 2023
1a7f4db
refactor: User 리팩터링
hyena0608 Jan 30, 2023
b72c47b
refactor: User 리팩터링 - 2
hyena0608 Jan 30, 2023
31e2831
refactor: User 리팩터링 - 3
hyena0608 Jan 30, 2023
4b1b65f
refactor: Comment 리팩터링
hyena0608 Jan 30, 2023
b69e10f
refactor: Like 리팩터링 - 1
hyena0608 Jan 30, 2023
b2db5ba
chore: 리팩터링을 위한 TODOLIST 정리
hyena0608 Jan 30, 2023
e85704f
chore: 리팩터링을 위한 TODOLIST 정리
hyena0608 Jan 30, 2023
76e2eaa
refactor: 유저 리팩토링 중
JoosungKwon Jan 30, 2023
c1755b6
refactor: 시리즈 리팩터링 중
JoosungKwon Jan 30, 2023
4e2c2ab
refactor: 포스트 리팩터링
JoosungKwon Jan 30, 2023
49deeb6
refactor: 좋아요 기능 리팩터링
JoosungKwon Jan 30, 2023
dd1bfa0
refactor: 댓글 기능 리팩터링
JoosungKwon Jan 30, 2023
d650222
refactor: oauth user 리팩터링
JoosungKwon Jan 30, 2023
7f1d13e
refactor: 기타 리팩터링
JoosungKwon Jan 30, 2023
798befe
choieunbi 님이 변경한 내용
JoosungKwon Jan 30, 2023
381863d
hyunseo 님이 변경한 내용
JoosungKwon Jan 30, 2023
74d1b6a
chore: 리팩터링을 위한 TODOLIST 정리
hyena0608 Jan 30, 2023
89705e3
refactor: User 리팩터링
hyena0608 Jan 30, 2023
41564bd
chore: 리팩터링을 위한 TODOLIST 정리
hyena0608 Jan 30, 2023
31d558b
refactor: 댓글 리팩터링
hyena0608 Jan 30, 2023
85985ef
test: 댓글 서비스 테스트 수정
hyena0608 Jan 30, 2023
0009304
test: 테스트 코드 수정
JoosungKwon Jan 31, 2023
2330096
test: 댓글 컨트롤러 테스트 수정
hyena0608 Jan 31, 2023
c0ab2c1
test: 게시글 컨트롤러 예외 테스트 응답 필드 추가
hyena0608 Jan 31, 2023
b0623e0
test: 댓글, 좋아요, 게시글 테스트 수정
hyena0608 Feb 1, 2023
8f87f05
refactor: 시리즈 부족한 기능 추가 및 최적화
JoosungKwon Feb 1, 2023
b43c369
update: 게시물 비공개 처리 및 게시물 전체 조회 리턴값 변경
hikarigin99 Feb 1, 2023
ef29e5a
refactor: Pageable Custom 구현
hikarigin99 Feb 1, 2023
1dd37da
refactor: 좋아요 기능 리팩토링
hikarigin99 Feb 1, 2023
285d27d
feat: 접근 권한 Exception
hikarigin99 Feb 1, 2023
bf1af05
refactor: 테스트 코드 리팩토링
hikarigin99 Feb 1, 2023
28ec57c
refactor: swagger server port 변경 및 Jacoco excludes 추가
hikarigin99 Feb 1, 2023
6e1afab
fix: 머지 충돌 해결
JoosungKwon Feb 2, 2023
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
7 changes: 4 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client' // OAuth2-Client dependency
implementation 'org.springframework.boot:spring-boot-starter-actuator' // Actuator dependency
implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE' // AWS S3 dependency

testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
Expand Down Expand Up @@ -85,7 +85,7 @@ dependencyManagement {
}

openapi3 {
setServer("http://localhost:8080")
setServer("http://43.201.105.123")
title = "restdocs-swagger API Documentation"
description = "Spring REST Docs with SwaggerUI."
version = "0.0.1"
Expand Down Expand Up @@ -156,7 +156,8 @@ jacocoTestCoverageVerification {
'*.global*',
'*.series*',
'*.comment*',
'*.dto*'
'*.dto*',
'*.exception.*'
]

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,26 @@ public class CommentController {

private final CommentService commentService;

@PostMapping("/{post_id}/comments")
public ResponseEntity<Void> save(
@PathVariable(name = "post_id") Long postId,
@Valid @RequestBody CreateCommentRequest request,
@PostMapping("/{postId}/comments")
public ResponseEntity<SingleCommentResponse> createComment(
@PathVariable(name = "postId") Long postId,
@Valid @RequestBody CreateCommentRequest createCommentRequest,
@AuthenticationPrincipal JwtAuthentication user
) {
commentService.save(request, user.id(), postId);
return ResponseEntity.status(CREATED).build();
SingleCommentResponse singleCommentResponse
= commentService.createComment(createCommentRequest, user.id(), postId);
return ResponseEntity.status(CREATED).body(singleCommentResponse);
}

@PatchMapping("/{post_id}/comments/{id}")
public ResponseEntity<Void> update(
@PathVariable(name = "post_id") Long postId,
@PathVariable(name = "id") Long commentId,
@Valid @RequestBody UpdateCommentRequest request,
@PatchMapping("/{postId}/comments/{commentId}")
public ResponseEntity<SingleCommentResponse> updateComment(
@PathVariable(name = "postId") Long postId,
@PathVariable(name = "commentId") Long commentId,
@Valid @RequestBody UpdateCommentRequest updateCommentRequest,
@AuthenticationPrincipal JwtAuthentication user
) {
commentService.update(request, user.id(), commentId);
return ResponseEntity.ok().build();
SingleCommentResponse singleCommentResponse
= commentService.updateComment(updateCommentRequest, user.id(), postId, commentId);
return ResponseEntity.ok(singleCommentResponse);
}
}
42 changes: 42 additions & 0 deletions src/main/java/com/prgrms/prolog/domain/comment/dto/CommentDto.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,59 @@
package com.prgrms.prolog.domain.comment.dto;

import static com.prgrms.prolog.domain.user.dto.UserDto.*;

import javax.validation.constraints.NotBlank;

import com.prgrms.prolog.domain.comment.model.Comment;
import com.prgrms.prolog.domain.post.model.Post;
import com.prgrms.prolog.domain.user.model.User;

public class CommentDto {

public record CreateCommentRequest(
@NotBlank String content
) {
public static CreateCommentRequest from(String content) {
return new CreateCommentRequest(content);
}

public static Comment from(
CreateCommentRequest createCommentRequest,
User user,
Post post
) {
return Comment.builder()
.content(createCommentRequest.content)
.user(user)
.post(post)
.build();
}
}

public record UpdateCommentRequest(
@NotBlank String content
) {
public static UpdateCommentRequest from(String content) {
return new UpdateCommentRequest(content);
}
}

public record SingleCommentResponse(
SimpleUserInfo user,
String content
) {
public static SingleCommentResponse from(UserResponse user, String content) {
return new SingleCommentResponse(SimpleUserInfo.from(user), content);
}
}

public record SimpleUserInfo(
Long userId,
String nickName,
String profileImgUrl
) {
public static SimpleUserInfo from(UserResponse user) {
return new SimpleUserInfo(user.id(), user.nickName(), user.profileImgUrl());
}
}
}
26 changes: 9 additions & 17 deletions src/main/java/com/prgrms/prolog/domain/comment/model/Comment.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.prgrms.prolog.domain.comment.model;

import static com.prgrms.prolog.global.util.ValidateUtil.*;
import static javax.persistence.FetchType.*;
import static javax.persistence.GenerationType.*;

Expand All @@ -12,8 +13,6 @@
import javax.persistence.ManyToOne;
import javax.validation.constraints.Size;

import org.springframework.util.Assert;

import com.prgrms.prolog.domain.post.model.Post;
import com.prgrms.prolog.domain.user.model.User;
import com.prgrms.prolog.global.common.BaseEntity;
Expand All @@ -22,9 +21,7 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
Expand Down Expand Up @@ -56,16 +53,6 @@ public Comment(String content, Post post, User user) {
this.user = Objects.requireNonNull(user, "exception.user.require");
}

public void changeContent(String content) {
this.content = validateContent(content);
}

private String validateContent(String content) {
Assert.hasText(content, CONTENT_OVER_LENGTH_MESSAGE);
Assert.isTrue(content.length() <= CONTENT_MAX_SIZE, CONTENT_OVER_LENGTH_MESSAGE);
return content;
}

public void setPost(Post post) {
if (this.post != null) {
this.post.getComments().remove(this);
Expand All @@ -74,9 +61,14 @@ public void setPost(Post post) {
post.getComments().add(this);
}

public boolean checkUserEmail(String email) {
Assert.notNull(user, "exception.user.notExists");
return this.user.checkSameEmail(email);
public void changeContent(String content) {
this.content = validateContent(content);
}

private String validateContent(String content) {
checkOverLength(content, CONTENT_MAX_SIZE, CONTENT_OVER_LENGTH_MESSAGE);
checkText(content, CONTENT_OVER_LENGTH_MESSAGE);
return content;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static com.prgrms.prolog.domain.comment.dto.CommentDto.*;

public interface CommentService {
Long save(CreateCommentRequest request, Long userId, Long postId);
Long update(UpdateCommentRequest request, Long userId, Long commentId);
SingleCommentResponse createComment(CreateCommentRequest createCommentRequest, Long userId, Long postId);

SingleCommentResponse updateComment(UpdateCommentRequest updateCommentRequest, Long userId, Long postId, Long commentId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static com.prgrms.prolog.domain.comment.dto.CommentDto.*;

import java.util.Objects;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
Expand All @@ -10,13 +12,14 @@
import com.prgrms.prolog.domain.comment.repository.CommentRepository;
import com.prgrms.prolog.domain.post.model.Post;
import com.prgrms.prolog.domain.post.repository.PostRepository;
import com.prgrms.prolog.domain.user.dto.UserDto;
import com.prgrms.prolog.domain.user.model.User;
import com.prgrms.prolog.domain.user.repository.UserRepository;

import lombok.RequiredArgsConstructor;

@Transactional(readOnly = true)
@RequiredArgsConstructor
@Transactional(readOnly = true)
@Service
public class CommentServiceImpl implements CommentService {

Expand All @@ -26,28 +29,28 @@ public class CommentServiceImpl implements CommentService {

@Override
@Transactional
public Long save(CreateCommentRequest request, Long userId, Long postId) {
Post findPost = getFindPostBy(postId);
public SingleCommentResponse createComment(CreateCommentRequest createCommentRequest, Long userId, Long postId) {
User findUser = getFindUserBy(userId);
Comment comment = buildComment(request, findPost, findUser);
return commentRepository.save(comment).getId();
Post findPost = getFindPostBy(postId);
Comment comment = CreateCommentRequest.from(createCommentRequest, findUser, findPost);
Comment savedComment = commentRepository.save(comment);
return SingleCommentResponse.from(UserDto.UserResponse.from(findUser), savedComment.getContent());
}

@Override
@Transactional
public Long update(UpdateCommentRequest request, Long userId, Long commentId) {
public SingleCommentResponse updateComment(UpdateCommentRequest updateCommentRequest, Long userId, Long postId, Long commentId) {
Post findPost = getFindPostBy(postId);
User findUser = getFindUserBy(userId);
Comment findComment = commentRepository.joinUserByCommentId(commentId);

validatePostComment(findPost, findComment);
validateUserComment(findUser, findComment);

validateCommentNotNull(findComment);
findComment.changeContent(request.content());
return findComment.getId();
}
findComment.changeContent(updateCommentRequest.content());

private Comment buildComment(CreateCommentRequest request, Post findPost, User findUser) {
return Comment.builder()
.content(request.content())
.post(findPost)
.user(findUser)
.build();
return SingleCommentResponse.from(UserDto.UserResponse.from(findUser), updateCommentRequest.content());
}

private User getFindUserBy(Long userId) {
Expand All @@ -63,4 +66,14 @@ private Post getFindPostBy(Long postId) {
private void validateCommentNotNull(Comment comment) {
Assert.notNull(comment, "exception.comment.notExists");
}

private void validateUserComment(User findUser, Comment findComment) {
Assert.isTrue(!Objects.equals(findComment.getUser().getId(), findUser.getId()),
"exception.comment.user.notSame");
}

private void validatePostComment(Post findPost, Comment findComment) {
Assert.isTrue(!Objects.equals(findPost.getId(), findComment.getPost().getId()),
"exception.comment.post.notSame");
}
}
31 changes: 17 additions & 14 deletions src/main/java/com/prgrms/prolog/domain/like/api/LikeController.java
Original file line number Diff line number Diff line change
@@ -1,44 +1,47 @@
package com.prgrms.prolog.domain.like.api;

import javax.validation.Valid;
import java.net.URI;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.UriComponentsBuilder;

import com.prgrms.prolog.domain.like.dto.LikeDto;
import com.prgrms.prolog.domain.like.dto.LikeDto.likeRequest;
import com.prgrms.prolog.domain.like.dto.LikeDto.LikeRequest;
import com.prgrms.prolog.domain.like.service.LikeServiceImpl;
import com.prgrms.prolog.global.jwt.JwtAuthentication;

import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/like")
@RestController
public class LikeController {

private final LikeServiceImpl likeService;

@PostMapping(value = "/{postId}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void insert(
public ResponseEntity<Void> insert(
@PathVariable Long postId,
@AuthenticationPrincipal JwtAuthentication user
) {
LikeDto.likeRequest request = new likeRequest(user.id(), postId);
likeService.save(request);
final LikeRequest likeRequest = new LikeRequest(user.id(), postId);
Long likeId = likeService.save(likeRequest);
URI location = UriComponentsBuilder.fromUriString("/api/v1/like/" + postId + "/" + likeId).build().toUri();
return ResponseEntity.created(location).build();
}

@DeleteMapping
@ResponseStatus(HttpStatus.NO_CONTENT)
public void delete(@RequestBody @Valid likeRequest likeRequest) {
@DeleteMapping("/{postId}")
public ResponseEntity<Void> delete(
@PathVariable Long postId,
@AuthenticationPrincipal JwtAuthentication user
) {
final LikeRequest likeRequest = new LikeRequest(user.id(), postId);
likeService.cancel(likeRequest);
return ResponseEntity.noContent().build();
}
}
15 changes: 13 additions & 2 deletions src/main/java/com/prgrms/prolog/domain/like/dto/LikeDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,20 @@

import javax.validation.constraints.NotNull;

import com.prgrms.prolog.domain.like.model.Like;
import com.prgrms.prolog.domain.post.model.Post;
import com.prgrms.prolog.domain.user.model.User;

public class LikeDto {

public record likeRequest(@NotNull Long userId,
@NotNull Long postId) {
public record LikeRequest(@NotNull Long userId,
@NotNull Long postId) {

public Like from(User user, Post post) {
return Like.builder()
.user(user)
.post(post)
.build();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.prgrms.prolog.domain.like.service;

import com.prgrms.prolog.domain.like.dto.LikeDto;
import static com.prgrms.prolog.domain.like.dto.LikeDto.*;

public interface LikeService {
Long save(LikeDto.likeRequest likeRequest);
Long save(LikeRequest likeRequest);

void cancel(LikeDto.likeRequest likeRequest);
void cancel(LikeRequest likeRequest);
}
Loading