Skip to content

Commit

Permalink
Merge branch 'main' into topic
Browse files Browse the repository at this point in the history
  • Loading branch information
60jong committed Feb 26, 2024
2 parents c2c0217 + 2020414 commit 428fcc3
Show file tree
Hide file tree
Showing 21 changed files with 181 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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로 공지
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -37,7 +37,7 @@ public void noticeVoteResult(VoteResult result) {
// author's notification
addAuthorsNotificationIfAuthorListeningVoteResult(result, notifications);

notificationRepository.saveAll(notifications);
notificationRepository.saveVoteResultNotificationsInBatch(notifications);
}

private List<VoteResultNotification> createVotersNotifications(VoteResult result, List<Member> voters) {
Expand Down Expand Up @@ -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);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -140,15 +147,16 @@ public Topic findTopic(final Long topicId) {
* @param pageable
* @return
*/
public Slice<TopicResponse> findAll(final Long memberId, final TopicSearchRequest request, final Pageable pageable) {
public Slice<TopicResponse> findAll(final Long retrieverId, final TopicSearchRequest request, final Pageable pageable) {

Slice<Topic> topics = topicRepository.findAll(memberId, request, pageable);
Slice<Topic> 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));
}

public TopicResponse findById(Long memberId, Long topicId) {
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/life/offonoff/ab/config/AopConfig.java
Original file line number Diff line number Diff line change
@@ -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 {
}
1 change: 1 addition & 0 deletions src/main/java/life/offonoff/ab/domain/comment/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/life/offonoff/ab/domain/topic/Topic.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ public class Topic extends BaseEntity {
@OneToMany(mappedBy = "topic", cascade = CascadeType.REMOVE, orphanRemoval = true)
private List<TopicReport> 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)
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/life/offonoff/ab/domain/vote/VoteResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<VoteResultNotification> notifications);

void deleteAllByTopicId(Long topicId);
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,31 +12,40 @@
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<VoteResultNotification> notifications) {
@Override
public void saveVoteResultNotificationsInBatch(List<VoteResultNotification> 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,
1000,
(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);
}

}
Original file line number Diff line number Diff line change
@@ -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<Notification, Long>, NotificationRepositoryCustom {
public interface NotificationRepository extends JpaRepository<Notification, Long>,
NotificationRepositoryCustom,
NotificationJdbcRepository {

List<Notification> findAllByReceiverIdOrderByCreatedAtDesc(Long receiverId);
}

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package life.offonoff.ab.repository.notice;
package life.offonoff.ab.repository.notfication;

import life.offonoff.ab.domain.notification.VoteResultNotification;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,10 +10,10 @@
@Repository
public class NotificationRepositoryImpl implements NotificationRepositoryCustom {

private final NotificationJdbcRepository notificationJdbcRepository;
private final NotificationJdbcRepositoryImpl notificationJdbcRepository;

@Override
public void saveAll(List<VoteResultNotification> notifications) {
notificationJdbcRepository.batchInsertVoteResultNotifications(notifications);
notificationJdbcRepository.saveVoteResultNotificationsInBatch(notifications);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Loading

0 comments on commit 428fcc3

Please sign in to comment.