Skip to content

Commit

Permalink
Merge pull request #72 from KTB16Team/feature/71-refactor-code
Browse files Browse the repository at this point in the history
Feature: ChildComment 리팩토링
  • Loading branch information
yooooonshine authored Nov 14, 2024
2 parents 60c7961 + 4147326 commit 1090252
Show file tree
Hide file tree
Showing 16 changed files with 86 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ public void initializeData(MemberRepository memberRepo, PrivatePostRepository pr
ParentComment.builder()
.nickname("ParentComment" + i)
.content("This is a parent comment " + i)
.isDeleted(false)
.member(member)
.post(post)
.build();
Expand All @@ -135,7 +134,6 @@ public void initializeData(MemberRepository memberRepo, PrivatePostRepository pr
.content("This is a child comment " + i)
.member(member)
.nickname(member.getNickname())
.isDeleted(false)
.parentComment(parentComment)
.post(postRepo.getReferenceById(parentComment.getPost().getId()))
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public class ExceptionHandlingFilter extends OncePerRequestFilter {
protected void doFilterInternal(
HttpServletRequest request,
HttpServletResponse response,
FilterChain filterChain) throws ServletException, IOException {

FilterChain filterChain
) throws ServletException, IOException {
try {
filterChain.doFilter(request, response);
} catch (ApiException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ protected boolean shouldNotFilter(HttpServletRequest request) throws ServletExce
protected void doFilterInternal(
HttpServletRequest request,
HttpServletResponse response,
FilterChain filterChain) throws ServletException, IOException, ApiException {
FilterChain filterChain
) throws ServletException, IOException, ApiException {

final String refreshToken = jwtTokenProvider.extractRefreshToken(request).orElse(null);
final String accessToken = jwtTokenProvider.extractAccessToken(request).orElse(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ public LoginFilter(JwtTokenProvider jwtTokenProvider, MemberService memberServic
}

@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response
public Authentication attemptAuthentication(
HttpServletRequest request,
HttpServletResponse response
) throws AuthenticationException {
log.info("LoginFilter");

Expand All @@ -58,7 +60,8 @@ protected void successfulAuthentication(
HttpServletRequest request,
HttpServletResponse response,
FilterChain chain,
Authentication authResult) {
Authentication authResult
) {
String email = extractEmail(authResult);

// 회원 id 찾기
Expand All @@ -80,7 +83,8 @@ protected void successfulAuthentication(
protected void unsuccessfulAuthentication(
HttpServletRequest request,
HttpServletResponse response,
AuthenticationException failed) {
AuthenticationException failed
) {
String email = obtainEmail(request);
log.info("로그인 실패: {}", email);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import aimo.backend.domains.comment.dto.request.SaveChildCommentRequest;
import aimo.backend.domains.comment.dto.parameter.ValidAndDeleteParentCommentParameter;
import aimo.backend.domains.comment.dto.parameter.ValidAndUpdateChildCommentParameter;
import aimo.backend.domains.comment.dto.request.UpdateChildCommentRequest;
import aimo.backend.domains.comment.service.ChildCommentService;
import aimo.backend.common.util.memberLoader.MemberLoader;
import jakarta.validation.Valid;
Expand Down Expand Up @@ -45,7 +46,7 @@ public ResponseEntity<DataResponse<Void>> saveChildComment(
@PutMapping("comments/child/{childCommentId}")
public ResponseEntity<DataResponse<Void>> updateChildComment(
@PathVariable Long childCommentId,
@Valid @RequestBody SaveChildCommentRequest request
@Valid @RequestBody UpdateChildCommentRequest request
) {
Long memberId = MemberLoader.getMemberId();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ public class ParentCommentController {
@PostMapping("/{postId}/comments")
public ResponseEntity<DataResponse<Void>> saveParentComment(
@Valid @PathVariable Long postId,
@Valid @RequestBody SaveParentCommentRequest request) {
@Valid @RequestBody SaveParentCommentRequest request
) {
Long memberId = MemberLoader.getMemberId();
SaveParentCommentParameter parameter =
SaveParentCommentParameter.of(memberId, postId, request.content());

SaveParentCommentParameter parameter = SaveParentCommentParameter.of(memberId, postId, request.content());
parentCommentMemberService.saveParentComment(parameter);

return ResponseEntity.ok(DataResponse.ok());
Expand All @@ -42,10 +43,15 @@ public ResponseEntity<DataResponse<Void>> saveParentComment(
@PutMapping("comments/{commentId}")
public ResponseEntity<DataResponse<Void>> updateParentComment(
@Valid @PathVariable Long commentId,
@Valid @RequestBody UpdateParentCommentRequest request) {
@Valid @RequestBody UpdateParentCommentRequest request
) {
Long memberId = MemberLoader.getMemberId();
UpdateParentCommentParameter parameter =
UpdateParentCommentParameter.of(memberId, commentId, request.content());

UpdateParentCommentParameter parameter = UpdateParentCommentParameter.of(
memberId,
commentId,
request.content());

parentCommentMemberService.validateAndUpdateParentComment(parameter);

return ResponseEntity.ok(DataResponse.ok());
Expand All @@ -56,7 +62,9 @@ public ResponseEntity<DataResponse<Void>> deleteParentComment(
@Valid @PathVariable("commentId") Long commentId
) {
Long memberId = MemberLoader.getMemberId();

DeleteParentCommentParameter parameter = DeleteParentCommentParameter.of(memberId, commentId);

parentCommentMemberService.validateAndDeleteParentComment(parameter);

return ResponseEntity.ok(DataResponse.ok());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import jakarta.validation.constraints.Size;

public record SaveChildCommentRequest(
@Size(max = 1500, message = "댓글은 최대 500자까지 입력할 수 있습니다.")
@Size(max = 500, message = "댓글은 최대 500자까지 입력할 수 있습니다.")
String content
) {

public static SaveChildCommentRequest of(String content) {
public static SaveChildCommentRequest from(String content) {
return new SaveChildCommentRequest(content);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public record SaveParentCommentRequest(
String content
) {

public static SaveParentCommentRequest of(String content) {
public static SaveParentCommentRequest from(String content) {
return new SaveParentCommentRequest(content);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package aimo.backend.domains.comment.dto.request;

import jakarta.validation.constraints.Size;

public record UpdateChildCommentRequest(
@Size(max = 500, message = "댓글은 최대 500자까지 입력할 수 있습니다.")
String content
) {

public static UpdateChildCommentRequest from(String content) {
return new UpdateChildCommentRequest(content);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public record UpdateParentCommentRequest(
String content
) {

public static UpdateParentCommentRequest of(String content) {
public static UpdateParentCommentRequest from(String content) {
return new UpdateParentCommentRequest(content);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,14 @@ private ChildComment(
String nickname,
String content,
ParentComment parentComment,
Boolean isDeleted,
Member member,
Post post,
List<ChildCommentLike> childCommentLikes
) {
this.nickname = nickname;
this.content = content;
this.parentComment = parentComment;
this.isDeleted = isDeleted;
this.isDeleted = false;
this.member = member;
this.post = post;
this.childCommentLikes = childCommentLikes;
Expand All @@ -90,7 +89,6 @@ public static ChildComment of(
.nickname(member.getNickname())
.content(content)
.parentComment(parentComment)
.isDeleted(false)
.member(member)
.post(post)
.build();
Expand All @@ -106,13 +104,6 @@ public void deleteChildCommentSoftly() {
this.isDeleted = true;
}

public void deleteChildCommentSoftlyWithContent() {
this.member = null;
this.nickname = CommentConstants.DELETED_MEMBER.getValue();
this.isDeleted = true;
content = CommentConstants.DELETED_COMMENT.getValue();
}

public void updateChildComment(String content) {
this.content = content;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,14 @@ public Integer getLikesCount() {
private ParentComment(
String nickname,
String content,
Boolean isDeleted,
Member member,
Post post,
List<ChildComment> childComments,
List<ParentCommentLike> parentCommentLikes
) {
this.nickname = nickname;
this.content = content;
this.isDeleted = isDeleted;
this.isDeleted = false;
this.member = member;
this.post = post;
this.childComments = childComments;
Expand All @@ -84,29 +83,32 @@ public static ParentComment of(
return ParentComment.builder()
.nickname(member.getNickname())
.content(content)
.isDeleted(false)
.member(member)
.post(post)
.build();
}

// 부모 댓글 이름 삭제
public void deleteParentCommentSoftly() {
this.member = null;
this.nickname = CommentConstants.UNKNOWN_MEMBER.getValue();
this.isDeleted = true;
}

// 부모 댓글 이름, 내용 삭제
public void deleteParentCommentSoftlyWithContent() {
this.member = null;
this.nickname = CommentConstants.UNKNOWN_MEMBER.getValue();
this.isDeleted = true;
content = CommentConstants.DELETED_COMMENT.getValue();
}

// 부모 댓글 수정
public void updateContent(String content) {
this.content = content;
}

// 자식 댓글 삭제
public void deleteChildComment(Long childCommentId) {
childComments.removeIf(childComment -> childComment.getId().equals(childCommentId));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
import org.springframework.data.jpa.repository.JpaRepository;

import aimo.backend.domains.comment.entity.ChildComment;
import aimo.backend.domains.member.entity.Member;

public interface ChildCommentRepository extends JpaRepository<ChildComment, Long> {

Boolean existsByIdAndMember(Long id, Member member);
Boolean existsByIdAndMember_Id(Long id, Long memberId);

List<ChildComment> findByMemberId(Long memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

public interface ParentCommentRepository extends JpaRepository<ParentComment, Long> {

Boolean existsByIdAndMember(Long id, Member member);
Boolean existsByIdAndMember_Id(Long id, Long memberId);

List<ParentComment> findByMemberId(Long memberId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public class ChildCommentService {
private final MemberService memberService;

//자식 댓글 권한 확인
private void validateChildCommentAuthority(Member member, Long childCommentId) throws ApiException {
Boolean exists = childCommentRepository.existsByIdAndMember(childCommentId, member);
private void validateChildCommentAuthority(Long memberId, Long childCommentId) throws ApiException {
Boolean exists = childCommentRepository.existsByIdAndMember_Id(childCommentId, memberId);

if (!exists) {
throw ApiException.from(UNAUTHORIZED_CHILD_COMMENT);
Expand All @@ -39,27 +39,31 @@ private void validateChildCommentAuthority(Member member, Long childCommentId) t

//자식 댓글 저장
@Transactional(rollbackFor = ApiException.class)
public void saveChildComment(SaveChildCommentParameter saveChildCommentParameter) {
Long postId = saveChildCommentParameter.postId();
Long parentCommentId = saveChildCommentParameter.parentCommentId();
String content = saveChildCommentParameter.content();
Member member = memberService.findMemberById(saveChildCommentParameter.memberId());
Post post = postService.findById(postId);
public void saveChildComment(SaveChildCommentParameter parameter) {
Long postId = parameter.postId();
Long parentCommentId = parameter.parentCommentId();
String content = parameter.content();

Member member = memberService.findMemberById(parameter.memberId());
Post post = postService.findById(postId);
ParentComment parentComment = parentCommentMemberService.findById(parentCommentId);

ChildComment childComment = ChildComment.of(content, member, parentComment, post);
childCommentRepository.save(childComment);
}

//자식 댓글 수정
@Transactional(rollbackFor = ApiException.class)
public void validateAndUpdateChildComment(
ValidAndUpdateChildCommentParameter validAndUpdateChildCommentParameter) {
Member member = memberService.findMemberById(validAndUpdateChildCommentParameter.memberId());
Long childCommentId = validAndUpdateChildCommentParameter.childCommentId();
String content = validAndUpdateChildCommentParameter.content();
ValidAndUpdateChildCommentParameter parameter
) {
Long memberId = parameter.memberId();
Long childCommentId = parameter.childCommentId();
String content = parameter.content();

// 자식 댓글 권한 확인
validateChildCommentAuthority(memberId, childCommentId);

validateChildCommentAuthority(member, childCommentId);
ChildComment childComment = childCommentRepository.findById(childCommentId)
.orElseThrow(() -> ApiException.from(UNAUTHORIZED_CHILD_COMMENT));

Expand All @@ -72,17 +76,17 @@ public void validateAndDeleteChildComment(
ValidAndDeleteParentCommentParameter parameter
) {
Long childCommentId = parameter.childCommentId();

Member member = memberService.findMemberById(parameter.memberId());
Long memberId = parameter.memberId();

// 자식 댓글 권한 확인
validateChildCommentAuthority(member, childCommentId);
validateChildCommentAuthority(memberId, childCommentId);

ParentComment parentComment = childCommentRepository.findById(childCommentId)
.orElseThrow(() -> ApiException.from(CHILD_COMMENT_NOT_FOUND))
.getParentComment();

// 자식 댓글 삭제
parentComment.deleteChildComment(childCommentId);
childCommentRepository.deleteById(childCommentId);

// 자식 댓글이 없으며 부모 댓글이 삭제된 상태면 삭제
Expand Down
Loading

0 comments on commit 1090252

Please sign in to comment.