Skip to content

Commit

Permalink
[#171] feat: 포스트 제거 시 포스트에 대한 통계 제거
Browse files Browse the repository at this point in the history
  • Loading branch information
shin-mallang committed Dec 16, 2023
1 parent 29e4d83 commit 08cd837
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@
import java.time.LocalDate;
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;

public interface PostViewStatisticRepository extends JpaRepository<PostViewStatistic, Long> {

Optional<PostViewStatistic> findByPostIdAndStatisticDate(PostId postId, LocalDate localDate);

@Modifying
@Query("DELETE FROM PostViewStatistic s WHERE s.postId = :postId")
void deleteAllByPostId(PostId postId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.mallang.statistics.statistic.application;

import com.mallang.post.domain.PostDeleteEvent;
import com.mallang.statistics.statistic.PostViewStatisticRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@RequiredArgsConstructor
@Transactional
@Service
public class PostViewStatisticEventHandler {

private final PostViewStatisticRepository postViewStatisticRepository;

@EventListener(PostDeleteEvent.class)
void deletePostViewStatistic(PostDeleteEvent event) {
postViewStatisticRepository.deleteAllByPostId(event.postId());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.mallang.statistics.statistic.application;

import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;

import com.mallang.post.domain.PostDeleteEvent;
import com.mallang.post.domain.PostId;
import com.mallang.statistics.statistic.PostViewStatisticRepository;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator.ReplaceUnderscores;
import org.junit.jupiter.api.Test;

@DisplayName("포스트 조회 통계 이벤트 핸들러 (PostViewStatisticEventHandler) 은(는)")
@SuppressWarnings("NonAsciiCharacters")
@DisplayNameGeneration(ReplaceUnderscores.class)
class PostViewStatisticEventHandlerTest {

private final PostViewStatisticRepository postViewStatisticRepository = mock(PostViewStatisticRepository.class);
private final PostViewStatisticEventHandler postViewStatisticEventHandler = new PostViewStatisticEventHandler(
postViewStatisticRepository
);

@Test
void 포스트_제거_이벤트를_받아_해당_포스트의_조회_이력을_제거한다() {
// given
PostId postId = new PostId(1L, 1L);
PostDeleteEvent postDeleteEvent = new PostDeleteEvent(postId);

// when
postViewStatisticEventHandler.deletePostViewStatistic(postDeleteEvent);

// then
then(postViewStatisticRepository)
.should(times(1))
.deleteAllByPostId(postId);
}
}

0 comments on commit 08cd837

Please sign in to comment.