Skip to content

Commit

Permalink
feat: 이벤트 처리 로그 추가 (#85)
Browse files Browse the repository at this point in the history
* [#42] refactor: post like 패키지 구조 수정

* [#42] fix: 포스트 삭제 시 좋아요가 있으면 오류가 발생하는 문제 해결

* [#42] feat: 이벤트 처리 로그 AOP 구현

* [#42] chore: jacoco, sonarcloud에서 AOP 제거
  • Loading branch information
shin-mallang authored Nov 21, 2023
1 parent 98b50d3 commit bae06f3
Show file tree
Hide file tree
Showing 15 changed files with 155 additions and 29 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def excludeCoverage = [
'**/auth/infrastructure/oauth/*/**',
'**/auth/presentation/OauthController*',
'**/common/domain/CommonDomainModel*',
'**/common/log/**',
] + Qdomains

jacocoTestReport {
Expand Down Expand Up @@ -149,6 +150,7 @@ jacocoTestCoverageVerification {
'*.auth.infrastructure.oauth.*.*',
'*.OauthController',
'*.common.domain.CommonDomainModel*',
'*.common.log.*',
] + QdomainsInVerification

limit {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package com.mallang.category.domain.event;

import com.mallang.common.domain.DomainEvent;

public record CategoryDeletedEvent(
Long categoryId
) {
) implements DomainEvent {

@Override
public Long id() {
return categoryId();
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/mallang/common/domain/DomainEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.mallang.common.domain;

public interface DomainEvent {

Long id();
}
45 changes: 45 additions & 0 deletions src/main/java/com/mallang/common/log/event/EventHandleLogAop.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.mallang.common.log.event;

import com.mallang.common.domain.DomainEvent;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Slf4j
@RequiredArgsConstructor
@Aspect
@Component
public class EventHandleLogAop {

@Pointcut("@annotation(org.springframework.context.event.EventListener)")
public void eventListeners() {
}

@Pointcut("@annotation(org.springframework.transaction.event.TransactionalEventListener)")
public void transactionalEventListeners() {
}

@Before("(eventListeners() || transactionalEventListeners()) && args(event)")
public void handleEventLog(JoinPoint joinPoint, DomainEvent event) {
String className = getClassSimpleName(joinPoint);
String methodName = getMethodName(joinPoint);
log.info("Handle [{}(Domain Id: {})] by [{}.{}()]",
event.getClass().getSimpleName(),
event.id(),
className,
methodName);
}

private String getClassSimpleName(JoinPoint joinPoint) {
Class<?> clazz = joinPoint.getTarget().getClass();
return clazz.getSimpleName();
}

private String getMethodName(JoinPoint joinPoint) {
return joinPoint.getSignature().getName();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.mallang.post.application;

import com.mallang.post.domain.PostDeleteEvent;
import com.mallang.post.domain.like.PostLikeRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@RequiredArgsConstructor
@Transactional
@Service
public class PostLikeEventHandler {

private final PostLikeRepository postLikeRepository;

@EventListener(PostDeleteEvent.class)
void deletePostLike(PostDeleteEvent event) {
postLikeRepository.deleteAllByPostId(event.postId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import com.mallang.post.application.command.CancelPostLikeCommand;
import com.mallang.post.application.command.ClickPostLikeCommand;
import com.mallang.post.domain.Post;
import com.mallang.post.domain.PostLike;
import com.mallang.post.domain.PostLikeRepository;
import com.mallang.post.domain.PostLikeValidator;
import com.mallang.post.domain.PostRepository;
import com.mallang.post.domain.like.PostLike;
import com.mallang.post.domain.like.PostLikeRepository;
import com.mallang.post.domain.like.PostLikeValidator;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -27,7 +27,7 @@ public void click(ClickPostLikeCommand command) {
Post post = postRepository.getById(command.postId());
Member member = memberRepository.getById(command.memberId());
PostLike postLike = new PostLike(post, member);
postLike.click(postLikeValidator, command.postPassword());
postLike.like(postLikeValidator, command.postPassword());
postLikeRepository.save(postLike);
}

Expand Down
8 changes: 7 additions & 1 deletion src/main/java/com/mallang/post/domain/PostDeleteEvent.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package com.mallang.post.domain;

import com.mallang.common.domain.DomainEvent;
import java.time.LocalDateTime;

public record PostDeleteEvent(
Long postId,
LocalDateTime deletedDate
) {
) implements DomainEvent {

public PostDeleteEvent(Long postId) {
this(postId, LocalDateTime.now());
}

@Override
public Long id() {
return postId();
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.mallang.post.domain;
package com.mallang.post.domain.like;

import static jakarta.persistence.FetchType.LAZY;

import com.mallang.auth.domain.Member;
import com.mallang.common.domain.CommonDomainModel;
import com.mallang.post.domain.Post;
import jakarta.annotation.Nullable;
import jakarta.persistence.Entity;
import jakarta.persistence.JoinColumn;
Expand All @@ -30,7 +31,7 @@ public PostLike(Post post, Member member) {
this.member = member;
}

public void click(PostLikeValidator postLikeValidator, @Nullable String postPassword) {
public void like(PostLikeValidator postLikeValidator, @Nullable String postPassword) {
post.validatePostAccessibility(member, postPassword);
postLikeValidator.validateClickLike(post, member);
post.clickLike();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.mallang.post.domain;
package com.mallang.post.domain.like;

import com.mallang.auth.domain.Member;
import com.mallang.post.domain.Post;
import com.mallang.post.exception.NotFoundPostLikeException;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface PostLikeRepository extends JpaRepository<PostLike, Long> {

Expand All @@ -15,4 +19,8 @@ default PostLike getByPostIdAndMemberId(Long postId, Long memberId) {
}

Optional<PostLike> findByPostIdAndMemberId(Long postId, Long memberId);

@Modifying
@Query("DELETE FROM PostLike pl WHERE pl.post.id = :postId")
void deleteAllByPostId(@Param("postId") Long postId);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mallang.post.domain;
package com.mallang.post.domain.like;

import com.mallang.auth.domain.Member;
import com.mallang.post.domain.Post;
import com.mallang.post.exception.AlreadyLikedPostException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.mallang.post.query.dao.support;

import com.mallang.post.domain.PostLike;
import com.mallang.post.domain.like.PostLike;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.mallang.post.application.command.ClickPostLikeCommand;
import com.mallang.post.domain.Post;
import com.mallang.post.domain.PostRepository;
import com.mallang.post.domain.like.PostLikeRepository;
import com.mallang.post.exception.AlreadyLikedPostException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
Expand Down Expand Up @@ -40,6 +41,9 @@ class PostLikeServiceTest {
@Autowired
private PostLikeService postLikeService;

@Autowired
private PostLikeRepository postLikeRepository;

private Long memberId;
private String blogName;
private Long postId;
Expand Down Expand Up @@ -87,9 +91,23 @@ class 좋아요_시 {

// when
postLikeService.cancel(new CancelPostLikeCommand(postId, memberId, null));

// then
Post post = postRepository.getById(postId);
assertThat(post.getLikeCount()).isZero();
}

@Nested
class 포스트_삭제_시 {

@Test
void 좋아요도_삭제되어야_한다() {
postLikeService.click(new ClickPostLikeCommand(postId, memberId, null));

// when
postServiceTestHelper.포스트를_삭제한다(memberId, postId);

// then
assertThat(postLikeRepository.findByPostIdAndMemberId(postId, memberId)).isEmpty();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import static com.mallang.post.domain.visibility.PostVisibilityPolicy.Visibility.PUBLIC;

import com.mallang.post.application.command.CreatePostCommand;
import com.mallang.post.application.command.DeletePostCommand;
import com.mallang.post.domain.Post;
import com.mallang.post.domain.PostRepository;
import com.mallang.post.domain.visibility.PostVisibilityPolicy;
import java.util.Arrays;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.test.context.ActiveProfiles;
Expand Down Expand Up @@ -55,6 +57,10 @@ public class PostServiceTestHelper {
));
}

public void 포스트를_삭제한다(Long memberId, Long postId) {
postService.delete(new DeletePostCommand(memberId, List.of(postId)));
}

public Post 포스트를_조회한다(Long 포스트_ID) {
return postRepository.getById(포스트_ID);
}
Expand Down
Loading

0 comments on commit bae06f3

Please sign in to comment.