From dc1457778ad0941a4a68e00f679c728ada3d7ff1 Mon Sep 17 00:00:00 2001 From: 60jong Date: Mon, 26 Feb 2024 19:16:24 +0900 Subject: [PATCH 1/5] =?UTF-8?q?fix=20:=20NotificationJdbcRepository?= =?UTF-8?q?=EB=A5=BC=20=EC=96=B4=EB=8C=91=ED=84=B0=20=ED=8C=A8=ED=84=B4?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EC=A0=91=EA=B7=BC=ED=95=98=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EC=88=98=EC=A0=95=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../IllegalAuthorException.java | 0 .../NotificationJdbcRepository.java | 12 +++++++++ .../NotificationJdbcRepositoryImpl.java} | 25 +++++++++++++------ .../NotificationRepository.java | 7 ++++-- .../NotificationRepositoryCustom.java | 2 +- .../NotificationRepositoryImpl.java | 6 ++--- .../NotificationRepositoryTest.java | 0 7 files changed, 38 insertions(+), 14 deletions(-) rename src/main/java/life/offonoff/ab/{domain/comment => exception}/IllegalAuthorException.java (100%) create mode 100644 src/main/java/life/offonoff/ab/repository/notfication/NotificationJdbcRepository.java rename src/main/java/life/offonoff/ab/repository/{notice/NotificationJdbcRepository.java => notfication/NotificationJdbcRepositoryImpl.java} (62%) rename src/main/java/life/offonoff/ab/repository/{notice => notfication}/NotificationRepository.java (56%) rename src/main/java/life/offonoff/ab/repository/{notice => notfication}/NotificationRepositoryCustom.java (80%) rename src/main/java/life/offonoff/ab/repository/{notice => notfication}/NotificationRepositoryImpl.java (65%) rename src/test/java/life/offonoff/ab/repository/{notice => notfication}/NotificationRepositoryTest.java (100%) diff --git a/src/main/java/life/offonoff/ab/domain/comment/IllegalAuthorException.java b/src/main/java/life/offonoff/ab/exception/IllegalAuthorException.java similarity index 100% rename from src/main/java/life/offonoff/ab/domain/comment/IllegalAuthorException.java rename to src/main/java/life/offonoff/ab/exception/IllegalAuthorException.java diff --git a/src/main/java/life/offonoff/ab/repository/notfication/NotificationJdbcRepository.java b/src/main/java/life/offonoff/ab/repository/notfication/NotificationJdbcRepository.java new file mode 100644 index 00000000..cbc0d0ac --- /dev/null +++ b/src/main/java/life/offonoff/ab/repository/notfication/NotificationJdbcRepository.java @@ -0,0 +1,12 @@ +package life.offonoff.ab.repository.notfication; + +import life.offonoff.ab.domain.notification.VoteResultNotification; + +import java.util.List; + +public interface NotificationJdbcRepository { + + void saveVoteResultNotificationsInBatch(List notifications); + + void deleteAllByTopicId(Long topicId); +} diff --git a/src/main/java/life/offonoff/ab/repository/notice/NotificationJdbcRepository.java b/src/main/java/life/offonoff/ab/repository/notfication/NotificationJdbcRepositoryImpl.java similarity index 62% rename from src/main/java/life/offonoff/ab/repository/notice/NotificationJdbcRepository.java rename to src/main/java/life/offonoff/ab/repository/notfication/NotificationJdbcRepositoryImpl.java index 41425bb1..ed9d0412 100644 --- a/src/main/java/life/offonoff/ab/repository/notice/NotificationJdbcRepository.java +++ b/src/main/java/life/offonoff/ab/repository/notfication/NotificationJdbcRepositoryImpl.java @@ -1,4 +1,4 @@ -package life.offonoff.ab.repository.notice; +package life.offonoff.ab.repository.notfication; import life.offonoff.ab.domain.notification.VoteResultNotification; import org.springframework.jdbc.core.JdbcTemplate; @@ -12,20 +12,21 @@ import static life.offonoff.ab.domain.notification.NotificationType.*; @Repository -public class NotificationJdbcRepository { +public class NotificationJdbcRepositoryImpl implements NotificationJdbcRepository { private final JdbcTemplate jdbcTemplate; - public NotificationJdbcRepository(DataSource dataSource) { + public NotificationJdbcRepositoryImpl(DataSource dataSource) { this.jdbcTemplate = new JdbcTemplate(dataSource); } - public void batchInsertVoteResultNotifications(List notifications) { + @Override + public void saveVoteResultNotificationsInBatch(List notifications) { Timestamp curTimeStamp = Timestamp.valueOf(LocalDateTime.now()); - String query = "insert into notification (member_id, notification_type, vote_result_id, created_at, updated_at)" + - "values (?, ?, ?, ?, ?);"; + String query = "insert into notification (member_id, notification_type, topic_id, vote_result_id, created_at, updated_at)" + + "values (?, ?, ?, ?, ?, ?);"; jdbcTemplate.batchUpdate(query, notifications, @@ -33,10 +34,18 @@ public void batchInsertVoteResultNotifications(List noti (pstmt, n) -> { pstmt.setLong(1, n.getReceiver().getId()); pstmt.setString(2, VOTE_RESULT_NOTIFICATION); - pstmt.setLong(3, n.getVoteResult().getId()); - pstmt.setTimestamp(4, curTimeStamp); + pstmt.setLong(3, n.getVoteResult().getTopicId()); + pstmt.setLong(4, n.getVoteResult().getId()); pstmt.setTimestamp(5, curTimeStamp); + pstmt.setTimestamp(6, curTimeStamp); }); } + @Override + public void deleteAllByTopicId(Long topicId) { + String query = "delete from notification where topic_id = ?"; + + jdbcTemplate.update(query, topicId); + } + } diff --git a/src/main/java/life/offonoff/ab/repository/notice/NotificationRepository.java b/src/main/java/life/offonoff/ab/repository/notfication/NotificationRepository.java similarity index 56% rename from src/main/java/life/offonoff/ab/repository/notice/NotificationRepository.java rename to src/main/java/life/offonoff/ab/repository/notfication/NotificationRepository.java index 51a38e72..41167702 100644 --- a/src/main/java/life/offonoff/ab/repository/notice/NotificationRepository.java +++ b/src/main/java/life/offonoff/ab/repository/notfication/NotificationRepository.java @@ -1,11 +1,14 @@ -package life.offonoff.ab.repository.notice; +package life.offonoff.ab.repository.notfication; import life.offonoff.ab.domain.notification.Notification; import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; -public interface NotificationRepository extends JpaRepository, NotificationRepositoryCustom { +public interface NotificationRepository extends JpaRepository, + NotificationRepositoryCustom, + NotificationJdbcRepository { + List findAllByReceiverIdOrderByCreatedAtDesc(Long receiverId); } diff --git a/src/main/java/life/offonoff/ab/repository/notice/NotificationRepositoryCustom.java b/src/main/java/life/offonoff/ab/repository/notfication/NotificationRepositoryCustom.java similarity index 80% rename from src/main/java/life/offonoff/ab/repository/notice/NotificationRepositoryCustom.java rename to src/main/java/life/offonoff/ab/repository/notfication/NotificationRepositoryCustom.java index 19396d87..b0b6fbfc 100644 --- a/src/main/java/life/offonoff/ab/repository/notice/NotificationRepositoryCustom.java +++ b/src/main/java/life/offonoff/ab/repository/notfication/NotificationRepositoryCustom.java @@ -1,4 +1,4 @@ -package life.offonoff.ab.repository.notice; +package life.offonoff.ab.repository.notfication; import life.offonoff.ab.domain.notification.VoteResultNotification; diff --git a/src/main/java/life/offonoff/ab/repository/notice/NotificationRepositoryImpl.java b/src/main/java/life/offonoff/ab/repository/notfication/NotificationRepositoryImpl.java similarity index 65% rename from src/main/java/life/offonoff/ab/repository/notice/NotificationRepositoryImpl.java rename to src/main/java/life/offonoff/ab/repository/notfication/NotificationRepositoryImpl.java index 96e0a6d7..c80b5c20 100644 --- a/src/main/java/life/offonoff/ab/repository/notice/NotificationRepositoryImpl.java +++ b/src/main/java/life/offonoff/ab/repository/notfication/NotificationRepositoryImpl.java @@ -1,4 +1,4 @@ -package life.offonoff.ab.repository.notice; +package life.offonoff.ab.repository.notfication; import life.offonoff.ab.domain.notification.VoteResultNotification; import lombok.RequiredArgsConstructor; @@ -10,10 +10,10 @@ @Repository public class NotificationRepositoryImpl implements NotificationRepositoryCustom { - private final NotificationJdbcRepository notificationJdbcRepository; + private final NotificationJdbcRepositoryImpl notificationJdbcRepository; @Override public void saveAll(List notifications) { - notificationJdbcRepository.batchInsertVoteResultNotifications(notifications); + notificationJdbcRepository.saveVoteResultNotificationsInBatch(notifications); } } diff --git a/src/test/java/life/offonoff/ab/repository/notice/NotificationRepositoryTest.java b/src/test/java/life/offonoff/ab/repository/notfication/NotificationRepositoryTest.java similarity index 100% rename from src/test/java/life/offonoff/ab/repository/notice/NotificationRepositoryTest.java rename to src/test/java/life/offonoff/ab/repository/notfication/NotificationRepositoryTest.java From eba01a869a6c58964c1c29181c3073421d981bb9 Mon Sep 17 00:00:00 2001 From: 60jong Date: Mon, 26 Feb 2024 19:17:22 +0900 Subject: [PATCH 2/5] =?UTF-8?q?feat=20:=20=ED=86=A0=ED=94=BD=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C=20=EC=8B=9C=20=EC=97=B0=EA=B4=80=EB=90=9C=20=EB=AA=A8?= =?UTF-8?q?=EB=93=A0=20=EC=97=94=ED=8B=B0=ED=8B=B0=EB=8F=84=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C=ED=95=9C=EB=8B=A4.=20(=EC=95=8C=EB=A6=BC=20=EC=AA=BD?= =?UTF-8?q?=EC=9D=80=20=EB=B6=84=EB=A6=AC=ED=95=B4=EC=84=9C=20Event?= =?UTF-8?q?=EB=A1=9C=20=EA=B4=80=EB=A6=AC=ED=95=9C=EB=8B=A4.)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/event/topic/TopicDeleteEvent.java | 15 +++++++++++++++ .../event/topic/TopicEventHandler.java | 10 ++++++++++ .../notification/NotificationService.java | 9 +++++++-- .../ab/application/service/TopicService.java | 7 +++++++ .../life/offonoff/ab/domain/comment/Comment.java | 1 + .../notification/VoteResultNotification.java | 1 + .../java/life/offonoff/ab/domain/topic/Topic.java | 3 +-- .../life/offonoff/ab/domain/vote/VoteResult.java | 8 +++++++- .../ab/exception/IllegalAuthorException.java | 2 +- .../notification/NotificationServiceTest.java | 2 +- ...otingTopicContainerServiceIntegrationTest.java | 2 +- .../offonoff/ab/configuration/TestJPAConfig.java | 6 +++--- .../notfication/NotificationRepositoryTest.java | 2 +- 13 files changed, 56 insertions(+), 12 deletions(-) create mode 100644 src/main/java/life/offonoff/ab/application/event/topic/TopicDeleteEvent.java diff --git a/src/main/java/life/offonoff/ab/application/event/topic/TopicDeleteEvent.java b/src/main/java/life/offonoff/ab/application/event/topic/TopicDeleteEvent.java new file mode 100644 index 00000000..d98d1c53 --- /dev/null +++ b/src/main/java/life/offonoff/ab/application/event/topic/TopicDeleteEvent.java @@ -0,0 +1,15 @@ +package life.offonoff.ab.application.event.topic; + +import life.offonoff.ab.domain.topic.Topic; +import lombok.Getter; +import org.springframework.context.ApplicationEvent; + +@Getter +public class TopicDeleteEvent { + + private final Topic topic; + + public TopicDeleteEvent(Topic topic) { + this.topic = topic; + } +} diff --git a/src/main/java/life/offonoff/ab/application/event/topic/TopicEventHandler.java b/src/main/java/life/offonoff/ab/application/event/topic/TopicEventHandler.java index d4ce93a8..ef97eeed 100644 --- a/src/main/java/life/offonoff/ab/application/event/topic/TopicEventHandler.java +++ b/src/main/java/life/offonoff/ab/application/event/topic/TopicEventHandler.java @@ -35,6 +35,16 @@ public void addTopic(TopicCreateEvent event) { votingTopicService.startVote(new VotingTopic(event.topic())); } + /** + * 투표 생성 이벤트 -> VotingTopicContainer에서 관리 추가 + */ + @EventListener + public void removeTopic(TopicDeleteEvent event) { + log.info("# Topic Delete / topic-id : {}, deadline : {}", event.getTopic().getId(), event.getTopic().getDeadline()); + + notificationService.removeAllByTopicId(event.getTopic().getId()); + } + /** * 투표 종료 이벤트 -> 투표 결과 만들어서 NoticeService로 공지 */ diff --git a/src/main/java/life/offonoff/ab/application/notification/NotificationService.java b/src/main/java/life/offonoff/ab/application/notification/NotificationService.java index 48470225..8494fa04 100644 --- a/src/main/java/life/offonoff/ab/application/notification/NotificationService.java +++ b/src/main/java/life/offonoff/ab/application/notification/NotificationService.java @@ -6,7 +6,7 @@ import life.offonoff.ab.domain.topic.Topic; import life.offonoff.ab.domain.vote.VoteResult; import life.offonoff.ab.repository.member.MemberRepository; -import life.offonoff.ab.repository.notice.NotificationRepository; +import life.offonoff.ab.repository.notfication.NotificationRepository; import life.offonoff.ab.web.response.notification.NotificationResponse; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -37,7 +37,7 @@ public void noticeVoteResult(VoteResult result) { // author's notification addAuthorsNotificationIfAuthorListeningVoteResult(result, notifications); - notificationRepository.saveAll(notifications); + notificationRepository.saveVoteResultNotificationsInBatch(notifications); } private List createVotersNotifications(VoteResult result, List voters) { @@ -90,5 +90,10 @@ public boolean shouldNoticeVoteCountForTopic(Topic topic) { // TODO : 투표 취소 후 다시 100단위를 넘었을 때 중복 알림 처리 && 추상화 return topic.getVoteCount() % voteCountUnit == 0; } + + @Transactional + public void removeAllByTopicId(Long topicId) { + notificationRepository.deleteAllByTopicId(topicId); + } } diff --git a/src/main/java/life/offonoff/ab/application/service/TopicService.java b/src/main/java/life/offonoff/ab/application/service/TopicService.java index 7c60887b..fe7d10ba 100644 --- a/src/main/java/life/offonoff/ab/application/service/TopicService.java +++ b/src/main/java/life/offonoff/ab/application/service/TopicService.java @@ -2,6 +2,7 @@ import life.offonoff.ab.application.event.report.TopicReportEvent; import life.offonoff.ab.application.event.topic.TopicCreateEvent; +import life.offonoff.ab.application.event.topic.TopicDeleteEvent; import life.offonoff.ab.application.event.topic.VotedEvent; import life.offonoff.ab.application.service.request.*; import life.offonoff.ab.domain.keyword.Keyword; @@ -97,6 +98,12 @@ public void deleteMembersTopic(final Long memberId, final Long topicId) { checkMemberCanTouchTopic(member, topic); + deleteTopic(topic); + } + + private void deleteTopic(Topic topic) { + eventPublisher.publishEvent(new TopicDeleteEvent(topic)); + topicRepository.delete(topic); } diff --git a/src/main/java/life/offonoff/ab/domain/comment/Comment.java b/src/main/java/life/offonoff/ab/domain/comment/Comment.java index 6da581e9..06a4a3e5 100644 --- a/src/main/java/life/offonoff/ab/domain/comment/Comment.java +++ b/src/main/java/life/offonoff/ab/domain/comment/Comment.java @@ -6,6 +6,7 @@ import life.offonoff.ab.domain.topic.Topic; import life.offonoff.ab.domain.topic.choice.ChoiceOption; import life.offonoff.ab.domain.vote.Vote; +import life.offonoff.ab.exception.IllegalAuthorException; import lombok.AccessLevel; import lombok.Getter; import lombok.NoArgsConstructor; diff --git a/src/main/java/life/offonoff/ab/domain/notification/VoteResultNotification.java b/src/main/java/life/offonoff/ab/domain/notification/VoteResultNotification.java index 297fab3a..f1d77be9 100644 --- a/src/main/java/life/offonoff/ab/domain/notification/VoteResultNotification.java +++ b/src/main/java/life/offonoff/ab/domain/notification/VoteResultNotification.java @@ -16,6 +16,7 @@ public class VoteResultNotification extends Notification { @ManyToOne(fetch = FetchType.EAGER) + @JoinColumn(name = "vote_result_id") private VoteResult voteResult; public VoteResultNotification(Member member, VoteResult voteResult) { diff --git a/src/main/java/life/offonoff/ab/domain/topic/Topic.java b/src/main/java/life/offonoff/ab/domain/topic/Topic.java index 3373cafa..34b176b4 100644 --- a/src/main/java/life/offonoff/ab/domain/topic/Topic.java +++ b/src/main/java/life/offonoff/ab/domain/topic/Topic.java @@ -58,8 +58,7 @@ public class Topic extends BaseEntity { @OneToMany(mappedBy = "topic", cascade = CascadeType.ALL, orphanRemoval = true) private List reports = new ArrayList<>(); - @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL) - @JoinColumn(name = "vote_result_id") + @OneToOne(mappedBy = "topic", cascade = CascadeType.ALL) private VoteResult voteResult; @Enumerated(EnumType.STRING) diff --git a/src/main/java/life/offonoff/ab/domain/vote/VoteResult.java b/src/main/java/life/offonoff/ab/domain/vote/VoteResult.java index ff4236a9..5a61669d 100644 --- a/src/main/java/life/offonoff/ab/domain/vote/VoteResult.java +++ b/src/main/java/life/offonoff/ab/domain/vote/VoteResult.java @@ -2,10 +2,15 @@ import jakarta.persistence.*; import life.offonoff.ab.domain.BaseEntity; +import life.offonoff.ab.domain.notification.Notification; +import life.offonoff.ab.domain.notification.VoteResultNotification; import life.offonoff.ab.domain.topic.Topic; import lombok.Getter; import lombok.NoArgsConstructor; +import java.util.ArrayList; +import java.util.List; + @Getter @NoArgsConstructor @Entity @@ -14,7 +19,8 @@ public class VoteResult extends BaseEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; - @OneToOne(fetch = FetchType.EAGER) + @OneToOne + @JoinColumn(name = "topic_id") private Topic topic; private int totalVoteCount; diff --git a/src/main/java/life/offonoff/ab/exception/IllegalAuthorException.java b/src/main/java/life/offonoff/ab/exception/IllegalAuthorException.java index f430474d..25c6468f 100644 --- a/src/main/java/life/offonoff/ab/exception/IllegalAuthorException.java +++ b/src/main/java/life/offonoff/ab/exception/IllegalAuthorException.java @@ -1,4 +1,4 @@ -package life.offonoff.ab.domain.comment; +package life.offonoff.ab.exception; import life.offonoff.ab.exception.AbCode; import life.offonoff.ab.exception.IllegalArgumentException; diff --git a/src/test/java/life/offonoff/ab/application/notification/NotificationServiceTest.java b/src/test/java/life/offonoff/ab/application/notification/NotificationServiceTest.java index fae2c8c5..3be3301e 100644 --- a/src/test/java/life/offonoff/ab/application/notification/NotificationServiceTest.java +++ b/src/test/java/life/offonoff/ab/application/notification/NotificationServiceTest.java @@ -5,7 +5,7 @@ import life.offonoff.ab.domain.topic.Topic; import life.offonoff.ab.domain.vote.VoteResult; import life.offonoff.ab.repository.member.MemberRepository; -import life.offonoff.ab.repository.notice.NotificationRepository; +import life.offonoff.ab.repository.notfication.NotificationRepository; import life.offonoff.ab.web.response.notification.NotificationResponse; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; diff --git a/src/test/java/life/offonoff/ab/application/service/vote/VotingTopicContainerServiceIntegrationTest.java b/src/test/java/life/offonoff/ab/application/service/vote/VotingTopicContainerServiceIntegrationTest.java index 89ce4978..1ffba16a 100644 --- a/src/test/java/life/offonoff/ab/application/service/vote/VotingTopicContainerServiceIntegrationTest.java +++ b/src/test/java/life/offonoff/ab/application/service/vote/VotingTopicContainerServiceIntegrationTest.java @@ -6,7 +6,7 @@ import life.offonoff.ab.domain.member.Member; import life.offonoff.ab.domain.topic.Topic; import life.offonoff.ab.repository.member.MemberRepository; -import life.offonoff.ab.repository.notice.NotificationRepository; +import life.offonoff.ab.repository.notfication.NotificationRepository; import life.offonoff.ab.repository.topic.TopicRepository; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; diff --git a/src/test/java/life/offonoff/ab/configuration/TestJPAConfig.java b/src/test/java/life/offonoff/ab/configuration/TestJPAConfig.java index e6065489..07036ee2 100644 --- a/src/test/java/life/offonoff/ab/configuration/TestJPAConfig.java +++ b/src/test/java/life/offonoff/ab/configuration/TestJPAConfig.java @@ -3,7 +3,7 @@ import com.querydsl.jpa.impl.JPAQueryFactory; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; -import life.offonoff.ab.repository.notice.NotificationJdbcRepository; +import life.offonoff.ab.repository.notfication.NotificationJdbcRepositoryImpl; import org.springframework.boot.test.context.TestConfiguration; import org.springframework.context.annotation.Bean; @@ -21,7 +21,7 @@ public JPAQueryFactory jpaQueryFactory() { } @Bean - public NotificationJdbcRepository notificationJdbcRepository(DataSource dataSource) { - return new NotificationJdbcRepository(dataSource); + public NotificationJdbcRepositoryImpl notificationJdbcRepository(DataSource dataSource) { + return new NotificationJdbcRepositoryImpl(dataSource); } } diff --git a/src/test/java/life/offonoff/ab/repository/notfication/NotificationRepositoryTest.java b/src/test/java/life/offonoff/ab/repository/notfication/NotificationRepositoryTest.java index 50fe8c17..7b596643 100644 --- a/src/test/java/life/offonoff/ab/repository/notfication/NotificationRepositoryTest.java +++ b/src/test/java/life/offonoff/ab/repository/notfication/NotificationRepositoryTest.java @@ -1,4 +1,4 @@ -package life.offonoff.ab.repository.notice; +package life.offonoff.ab.repository.notfication; import jakarta.persistence.EntityManager; import life.offonoff.ab.configuration.TestJPAConfig; From 1c62983bccd18e561ede2f7a4f51f70604a334ad Mon Sep 17 00:00:00 2001 From: 60jong Date: Mon, 26 Feb 2024 19:17:44 +0900 Subject: [PATCH 3/5] =?UTF-8?q?test=20:=20=EC=95=8C=EB=A6=BC=20=EC=A0=9C?= =?UTF-8?q?=EC=99=B8=20=ED=86=A0=ED=94=BD=20=EC=97=B0=EA=B4=80=EA=B4=80?= =?UTF-8?q?=EA=B3=84=20=EC=82=AD=EC=A0=9C=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/topic/TopicRepositoryTest.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/test/java/life/offonoff/ab/repository/topic/TopicRepositoryTest.java b/src/test/java/life/offonoff/ab/repository/topic/TopicRepositoryTest.java index 98b0402e..46ba8a3d 100644 --- a/src/test/java/life/offonoff/ab/repository/topic/TopicRepositoryTest.java +++ b/src/test/java/life/offonoff/ab/repository/topic/TopicRepositoryTest.java @@ -9,6 +9,7 @@ import life.offonoff.ab.domain.topic.TopicSide; import life.offonoff.ab.domain.topic.TopicStatus; import life.offonoff.ab.domain.topic.hide.HiddenTopic; +import life.offonoff.ab.domain.vote.VoteResult; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -291,6 +292,34 @@ void find_except_hidden() { assertThat(topics.getContent()).containsExactly(topic); } + @Test + void delete_topic() { + // given + Member author = TestMember.builder() + .build().buildMember(); + em.persist(author); + + Topic topic = TestTopic.builder() + .author(author) + .side(TopicSide.TOPIC_B) + .build().buildTopic(); + + topicRepository.save(topic); + + VoteResult voteResult = new VoteResult(); + voteResult.setTopic(topic); + em.persist(voteResult); + + // when + topicRepository.delete(topic); + + // then + assertAll( + () -> assertThat(topicRepository.findById(topic.getId())).isEmpty(), + () -> assertThat(em.find(VoteResult.class, voteResult.getId())).isNull() + ); + } + Pageable createVoteCountDescPageable(int page, int size) { return PageRequest.of(page, size, Sort.Direction.DESC, "voteCount"); } From f400de71b3ec70f049f1ff7936c080c021b45191 Mon Sep 17 00:00:00 2001 From: melonturtle Date: Mon, 26 Feb 2024 19:31:05 +0900 Subject: [PATCH 4/5] =?UTF-8?q?feat:=20=EC=BB=A8=ED=8A=B8=EB=A1=A4?= =?UTF-8?q?=EB=9F=AC=EB=8B=A8=20=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=8B=A4?= =?UTF-8?q?=ED=96=89=20=EC=A0=84,=20=EC=8B=A4=ED=96=89=20=ED=9B=84=20?= =?UTF-8?q?=EB=A1=9C=EA=B7=B8=20=EB=82=A8=EA=B8=B4=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../life/offonoff/ab/config/AopConfig.java | 9 ++++ .../common/aspect/LoggingAspectHandler.java | 44 +++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 src/main/java/life/offonoff/ab/config/AopConfig.java create mode 100644 src/main/java/life/offonoff/ab/web/common/aspect/LoggingAspectHandler.java diff --git a/src/main/java/life/offonoff/ab/config/AopConfig.java b/src/main/java/life/offonoff/ab/config/AopConfig.java new file mode 100644 index 00000000..f59492d3 --- /dev/null +++ b/src/main/java/life/offonoff/ab/config/AopConfig.java @@ -0,0 +1,9 @@ +package life.offonoff.ab.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.EnableAspectJAutoProxy; + +@Configuration +@EnableAspectJAutoProxy +public class AopConfig { +} diff --git a/src/main/java/life/offonoff/ab/web/common/aspect/LoggingAspectHandler.java b/src/main/java/life/offonoff/ab/web/common/aspect/LoggingAspectHandler.java new file mode 100644 index 00000000..09969237 --- /dev/null +++ b/src/main/java/life/offonoff/ab/web/common/aspect/LoggingAspectHandler.java @@ -0,0 +1,44 @@ +package life.offonoff.ab.web.common.aspect; + +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +import java.util.Optional; + +@Component +@Aspect +public class LoggingAspectHandler { + private final Logger log = LoggerFactory.getLogger(this.getClass().getSimpleName()); + @Pointcut("execution(* life.offonoff.ab.web.*.*(..))") + private void allControllers() { + } + + @Around("allControllers()") + public Object doReturnLogging(ProceedingJoinPoint joinPoint) throws Throwable { + String methodName = joinPoint.getSignature().toShortString(); + Object[] arguments = joinPoint.getArgs(); + + log.info("[BEFORE] METHOD={} PARAMETER={} WILL EXECUTE", methodName, arguments); + + Object returnedByMethod = joinPoint.proceed(arguments); + + log.info("[RETURNED] METHOD={} PARAMETER={} RETURNED={}", joinPoint.getSignature().toShortString(), joinPoint.getArgs(), returnedByMethod); + + return returnedByMethod; + } + + @AfterThrowing(value = "allControllers()", throwing = "exception") + public void doExceptionLogging(JoinPoint joinPoint, Exception exception) { + String ip = Optional.ofNullable((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()) + .map(attributes -> attributes.getRequest().getHeader("X-Real-IP")) + .orElse(""); + log.error("[EXCEPTION THROWN] METHOD={} PARAMETER={} THREW EXCEPTION={} | ip={}", + joinPoint.getSignature().toShortString(), joinPoint.getArgs(), exception, ip); + } +} From 7f8c79350ed56db4238977f9b6a032ec7d040a45 Mon Sep 17 00:00:00 2001 From: melonturtle Date: Mon, 26 Feb 2024 20:18:33 +0900 Subject: [PATCH 5/5] =?UTF-8?q?fix:=20=ED=86=A0=ED=94=BD=20=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C=20=EC=8B=9C=20stream=EC=97=90=EC=84=9C=20member=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=ED=95=98=EC=A7=80=20=EC=95=8A=EA=B3=A0=20?= =?UTF-8?q?=ED=95=9C=20=EB=B2=88=EB=A7=8C=20=EC=A1=B0=ED=9A=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Issue: #180 --- .../offonoff/ab/application/service/TopicService.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/java/life/offonoff/ab/application/service/TopicService.java b/src/main/java/life/offonoff/ab/application/service/TopicService.java index b27db2d4..53daae14 100644 --- a/src/main/java/life/offonoff/ab/application/service/TopicService.java +++ b/src/main/java/life/offonoff/ab/application/service/TopicService.java @@ -147,15 +147,16 @@ public Topic findTopic(final Long topicId) { * @param pageable * @return */ - public Slice findAll(final Long memberId, final TopicSearchRequest request, final Pageable pageable) { + public Slice findAll(final Long retrieverId, final TopicSearchRequest request, final Pageable pageable) { - Slice topics = topicRepository.findAll(memberId, request, pageable); + Slice topics = topicRepository.findAll(retrieverId, request, pageable); - if (memberId == null) { + if (retrieverId == null) { return topics.map(TopicResponse::from); } - return topics.map(topic -> TopicResponse.from(topic, findMember(memberId))); + Member retriever = findMember(retrieverId); + return topics.map(topic -> TopicResponse.from(topic, retriever)); } //== Hide ==//