Skip to content

Commit

Permalink
[OING-286] refactor: post 모듈 전체적으로 리팩터링 (#206)
Browse files Browse the repository at this point in the history
* refactor: refact memberRealEmoji anti pattern

* refactor: refact memberPostReaction anti pattern

* refactor: refact memberPostComment anti pattern

* refactor: change post module table name

* refactor: change entity name in post module

* fix: add primary key sql

* feature: add transactional annotation

* refactor: delete transactional annotation in controller

* refactor: validate logic in post controller

* refactor: rename service test file

* refactor: refact deleteRealEmoji logic
  • Loading branch information
Ji-soo708 authored Mar 25, 2024
1 parent 971b5d7 commit bfa1f5d
Show file tree
Hide file tree
Showing 94 changed files with 2,016 additions and 1,996 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import org.springframework.context.ApplicationEvent;

@Getter
public class MemberPostCommentCreatedEvent extends ApplicationEvent {
public class CommentCreatedEvent extends ApplicationEvent {

private final String commentId;
private final String memberId;

public MemberPostCommentCreatedEvent(Object source, String commentId, String memberId) {
public CommentCreatedEvent(Object source, String commentId, String memberId) {
super(source);
this.commentId = commentId;
this.memberId = memberId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import org.springframework.context.ApplicationEvent;

@Getter
public class MemberPostCommentDeletedEvent extends ApplicationEvent {
public class CommentDeletedEvent extends ApplicationEvent {

private final String commentId;
private final String memberId;

public MemberPostCommentDeletedEvent(Object source, String commentId, String memberId) {
public CommentDeletedEvent(Object source, String commentId, String memberId) {
super(source);
this.commentId = commentId;
this.memberId = memberId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import org.springframework.context.ApplicationEvent;

@Getter
public class MemberPostCreatedEvent extends ApplicationEvent {
public class PostCreatedEvent extends ApplicationEvent {

private final String postId;
private final String memberId;

public MemberPostCreatedEvent(Object source, String postId, String memberId) {
public PostCreatedEvent(Object source, String postId, String memberId) {
super(source);
this.postId = postId;
this.memberId = memberId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import org.springframework.context.ApplicationEvent;

@Getter
public class MemberPostDeletedEvent extends ApplicationEvent {
public class PostDeletedEvent extends ApplicationEvent {

private final String postId;
private final String memberId;

public MemberPostDeletedEvent(Object source, String postId, String memberId) {
public PostDeletedEvent(Object source, String postId, String memberId) {
super(source);
this.postId = postId;
this.memberId = memberId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import org.springframework.context.ApplicationEvent;

@Getter
public class MemberPostReactionCreatedEvent extends ApplicationEvent {
public class ReactionCreatedEvent extends ApplicationEvent {

private final String reactionId;
private final String memberId;

public MemberPostReactionCreatedEvent(Object source, String reactionId, String memberId) {
public ReactionCreatedEvent(Object source, String reactionId, String memberId) {
super(source);
this.reactionId = reactionId;
this.memberId = memberId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import org.springframework.context.ApplicationEvent;

@Getter
public class MemberPostReactionDeletedEvent extends ApplicationEvent {
public class ReactionDeletedEvent extends ApplicationEvent {

private final String reactionId;
private final String memberId;

public MemberPostReactionDeletedEvent(Object source, String reactionId, String memberId) {
public ReactionDeletedEvent(Object source, String reactionId, String memberId) {
super(source);
this.reactionId = reactionId;
this.memberId = memberId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import org.springframework.context.ApplicationEvent;

@Getter
public class MemberPostRealEmojiCreatedEvent extends ApplicationEvent {
public class RealEmojiCreatedEvent extends ApplicationEvent {

private final String realEmojiId;
private final String memberId;

public MemberPostRealEmojiCreatedEvent(Object source, String realEmojiId, String memberId) {
public RealEmojiCreatedEvent(Object source, String realEmojiId, String memberId) {
super(source);
this.realEmojiId = realEmojiId;
this.memberId = memberId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import org.springframework.context.ApplicationEvent;

@Getter
public class MemberPostRealEmojiDeletedEvent extends ApplicationEvent {
public class RealEmojiDeletedEvent extends ApplicationEvent {

private final String realEmojiId;
private final String memberId;

public MemberPostRealEmojiDeletedEvent(Object source, String realEmojiId, String memberId) {
public RealEmojiDeletedEvent(Object source, String realEmojiId, String memberId) {
super(source);
this.realEmojiId = realEmojiId;
this.memberId = memberId;
Expand Down
6 changes: 3 additions & 3 deletions common/src/main/java/com/oing/exception/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ public enum ErrorCode {
MEMBER_NOT_FOUND("MB0001", "Member not found"),
MEMBER_ALREADY_EXISTS("MB0002", "Member With That Credentials Already Exists"),
/**
* MemberPost Related Errors
* Post Related Errors
*/
POST_NOT_FOUND("PO0001", "Post not found"),

/**
* MemberEmoji Related Errors
* Emoji Related Errors
*/
EMOJI_ALREADY_EXISTS("EM0001", "Emoji already exists"),
EMOJI_NOT_FOUND("EM0002", "Emoji not found"),
/**
* MemberComment Related Errors
* Comment Related Errors
*/
POST_COMMENT_NOT_FOUND("CM0001", "Comment not found"),
/**
Expand Down
66 changes: 32 additions & 34 deletions family/src/main/java/com/oing/event/FamilyScoreEventListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,98 +22,96 @@ public class FamilyScoreEventListener {
@Async
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void onMemberPostCreatedEvent(MemberPostCreatedEvent memberPostCreatedEvent) {
log.info("Event of uploading Post({}) by Member({}) listened, adding family score", memberPostCreatedEvent.getPostId(), memberPostCreatedEvent.getMemberId());
public void onPostCreatedEvent(PostCreatedEvent postCreatedEvent) {
log.info("Event of uploading Post({}) by Member({}) listened, adding family score", postCreatedEvent.getPostId(), postCreatedEvent.getMemberId());

String familyId = memberBridge.getFamilyIdByMemberId(memberPostCreatedEvent.getMemberId());
String familyId = memberBridge.getFamilyIdByMemberId(postCreatedEvent.getMemberId());
familyService.getFamilyByIdWithLock(familyId).addNewPostScore();

log.info("Uploading Post({}) score of Family({}) added", memberPostCreatedEvent.getPostId(), familyId);
log.info("Uploading Post({}) score of Family({}) added", postCreatedEvent.getPostId(), familyId);
}

@Async
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void onMemberPostDeletedEvent(MemberPostDeletedEvent memberPostDeletedEvent) {
log.info("Event of deleting Post({}) by Member({}) listened, subtracting family score", memberPostDeletedEvent.getPostId(), memberPostDeletedEvent.getMemberId());
public void onPostDeletedEvent(PostDeletedEvent postDeletedEvent) {
log.info("Event of deleting Post({}) by Member({}) listened, subtracting family score", postDeletedEvent.getPostId(), postDeletedEvent.getMemberId());

String familyId = memberBridge.getFamilyIdByMemberId(memberPostDeletedEvent.getMemberId());
String familyId = memberBridge.getFamilyIdByMemberId(postDeletedEvent.getMemberId());
familyService.getFamilyByIdWithLock(familyId).subtractNewPostScore();

log.info("Deleting Post({}) score of Family({}) subtracted", memberPostDeletedEvent.getPostId(), familyId);
log.info("Deleting Post({}) score of Family({}) subtracted", postDeletedEvent.getPostId(), familyId);
}

@Async
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void onMemberPostCommentCreatedEvent(MemberPostCommentCreatedEvent memberPostCommentCreatedEvent) {
log.info("Event of uploading Comment({}) by Member({}) listened, adding family score", memberPostCommentCreatedEvent.getCommentId(), memberPostCommentCreatedEvent.getMemberId());
public void onPostCommentCreatedEvent(CommentCreatedEvent commentCreatedEvent) {
log.info("Event of uploading Comment({}) by Member({}) listened, adding family score", commentCreatedEvent.getCommentId(), commentCreatedEvent.getMemberId());

String familyId = memberBridge.getFamilyIdByMemberId(memberPostCommentCreatedEvent.getMemberId());
String familyId = memberBridge.getFamilyIdByMemberId(commentCreatedEvent.getMemberId());
familyService.getFamilyByIdWithLock(familyId).addNewCommentScore();

log.info("Uploading Comment({}) score of Family({}) added", memberPostCommentCreatedEvent.getCommentId(), familyId);
log.info("Uploading Comment({}) score of Family({}) added", commentCreatedEvent.getCommentId(), familyId);
}

@Async
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void onMemberPostCommentDeletedEvent(MemberPostCommentDeletedEvent memberPostCommentDeletedEvent) {
log.info("Event of Comment({}) deleted by Member({}) listened, subtracting family score", memberPostCommentDeletedEvent.getCommentId(), memberPostCommentDeletedEvent.getMemberId());
public void onPostCommentDeletedEvent(CommentDeletedEvent commentDeletedEvent) {
log.info("Event of Comment({}) deleted by Member({}) listened, subtracting family score", commentDeletedEvent.getCommentId(), commentDeletedEvent.getMemberId());

String familyId = memberBridge.getFamilyIdByMemberId(memberPostCommentDeletedEvent.getMemberId());
String familyId = memberBridge.getFamilyIdByMemberId(commentDeletedEvent.getMemberId());
familyService.getFamilyByIdWithLock(familyId).subtractNewCommentScore();

log.info("Deleting Comment({}) score of Family({}) subtracted", memberPostCommentDeletedEvent.getCommentId(), familyId);
log.info("Deleting Comment({}) score of Family({}) subtracted", commentDeletedEvent.getCommentId(), familyId);
}

@Async
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void onMemberPostReactionCreatedEvent(MemberPostReactionCreatedEvent memberPostReactionCreatedEvent) {
log.info("Event of uploading Reaction({}) by Member({}) listened, adding family score", memberPostReactionCreatedEvent.getReactionId(), memberPostReactionCreatedEvent.getMemberId());
public void onPostReactionCreatedEvent(ReactionCreatedEvent reactionCreatedEvent) {
log.info("Event of uploading Reaction({}) by Member({}) listened, adding family score", reactionCreatedEvent.getReactionId(), reactionCreatedEvent.getMemberId());

String familyId = memberBridge.getFamilyIdByMemberId(memberPostReactionCreatedEvent.getMemberId());
String familyId = memberBridge.getFamilyIdByMemberId(reactionCreatedEvent.getMemberId());
familyService.getFamilyByIdWithLock(familyId).addNewReactionScore();

log.info("Uploading Reaction({}) score of Family({}) added", memberPostReactionCreatedEvent.getReactionId(), familyId);
log.info("Uploading Reaction({}) score of Family({}) added", reactionCreatedEvent.getReactionId(), familyId);
}

@Async
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void onMemberPostReactionDeletedEvent(MemberPostReactionDeletedEvent memberPostReactionDeletedEvent) {
log.info("Event of uploading Reaction({}) by Member({}) listened, adding family score", memberPostReactionDeletedEvent.getReactionId(), memberPostReactionDeletedEvent.getMemberId());
public void onPostReactionDeletedEvent(ReactionDeletedEvent reactionDeletedEvent) {
log.info("Event of uploading Reaction({}) by Member({}) listened, adding family score", reactionDeletedEvent.getReactionId(), reactionDeletedEvent.getMemberId());

String familyId = memberBridge.getFamilyIdByMemberId(memberPostReactionDeletedEvent.getMemberId());
String familyId = memberBridge.getFamilyIdByMemberId(reactionDeletedEvent.getMemberId());
familyService.getFamilyByIdWithLock(familyId).subtractNewReactionScore();

log.info("Deleting Reaction({}) score of Family({}) subtracted", memberPostReactionDeletedEvent.getReactionId(), familyId);
log.info("Deleting Reaction({}) score of Family({}) subtracted", reactionDeletedEvent.getReactionId(), familyId);
}

@Async
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void onMemberPostRealEmojiCreatedEvent(MemberPostRealEmojiCreatedEvent memberPostRealEmojiCreatedEvent) {
log.info("Event of new uploading Emoji({}) by Member({}) listened, adding family score", memberPostRealEmojiCreatedEvent.getRealEmojiId(), memberPostRealEmojiCreatedEvent.getMemberId());
public void onPostRealEmojiCreatedEvent(RealEmojiCreatedEvent realEmojiCreatedEvent) {
log.info("Event of new uploading Emoji({}) by Member({}) listened, adding family score", realEmojiCreatedEvent.getRealEmojiId(), realEmojiCreatedEvent.getMemberId());

String familyId = memberBridge.getFamilyIdByMemberId(memberPostRealEmojiCreatedEvent.getMemberId());
String familyId = memberBridge.getFamilyIdByMemberId(realEmojiCreatedEvent.getMemberId());
familyService.getFamilyByIdWithLock(familyId).addNewRealEmojiScore();

log.info("Uploading real Emoji({}) score of Family({}) added", memberPostRealEmojiCreatedEvent.getRealEmojiId(), familyId);
log.info("Uploading real Emoji({}) score of Family({}) added", realEmojiCreatedEvent.getRealEmojiId(), familyId);
}

@Async
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void onMemberPostRealEmojiDeletedEvent(MemberPostRealEmojiDeletedEvent memberPostRealEmojiDeletedEvent) {
log.info("Event of real Emoji({}) deleted by Member({}) listened, subtracting family score", memberPostRealEmojiDeletedEvent.getRealEmojiId(), memberPostRealEmojiDeletedEvent.getMemberId());
public void onPostRealEmojiDeletedEvent(RealEmojiDeletedEvent realEmojiDeletedEvent) {
log.info("Event of real Emoji({}) deleted by Member({}) listened, subtracting family score", realEmojiDeletedEvent.getRealEmojiId(), realEmojiDeletedEvent.getMemberId());

String familyId = memberBridge.getFamilyIdByMemberId(memberPostRealEmojiDeletedEvent.getMemberId());
String familyId = memberBridge.getFamilyIdByMemberId(realEmojiDeletedEvent.getMemberId());
familyService.getFamilyByIdWithLock(familyId).subtractNewRealEmojiScore();

log.info("Deleting real Emoji({}) score of Family({}) subtracted", memberPostRealEmojiDeletedEvent.getRealEmojiId(), familyId);
log.info("Deleting real Emoji({}) score of Family({}) subtracted", realEmojiDeletedEvent.getRealEmojiId(), familyId);
}


}
22 changes: 11 additions & 11 deletions gateway/src/main/java/com/oing/controller/CalendarController.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.oing.controller;

import com.oing.domain.BannerImageType;
import com.oing.domain.MemberPost;
import com.oing.domain.Post;
import com.oing.dto.response.ArrayResponse;
import com.oing.dto.response.BannerResponse;
import com.oing.dto.response.CalendarResponse;
import com.oing.dto.response.FamilyMonthlyStatisticsResponse;
import com.oing.restapi.CalendarApi;
import com.oing.service.FamilyService;
import com.oing.service.MemberPostService;
import com.oing.service.MemberService;
import com.oing.service.PostService;
import com.oing.util.OptimizedImageUrlGenerator;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
Expand All @@ -25,7 +25,7 @@
public class CalendarController implements CalendarApi {

private final MemberService memberService;
private final MemberPostService memberPostService;
private final PostService postService;
private final FamilyService familyService;

private final OptimizedImageUrlGenerator optimizedImageUrlGenerator;
Expand All @@ -39,23 +39,23 @@ public ArrayResponse<CalendarResponse> getMonthlyCalendar(String yearMonth, Stri
LocalDate startDate = LocalDate.parse(yearMonth + "-01"); // yyyy-MM-dd 패턴으로 파싱
LocalDate endDate = startDate.plusMonths(1);

List<MemberPost> daysLatestPosts = memberPostService.findLatestPostOfEveryday(startDate, endDate, familyId);
List<Post> daysLatestPosts = postService.findLatestPostOfEveryday(startDate, endDate, familyId);
List<CalendarResponse> calendarResponses = convertToCalendarResponse(daysLatestPosts, familyId);
return new ArrayResponse<>(calendarResponses);
}

private List<CalendarResponse> convertToCalendarResponse(List<MemberPost> daysLatestPosts, String familyId) {
private List<CalendarResponse> convertToCalendarResponse(List<Post> daysLatestPosts, String familyId) {
List<CalendarResponse> calendarResponses = new ArrayList<>();

for (MemberPost dayLatestPost : daysLatestPosts) {
for (Post dayLatestPost : daysLatestPosts) {
LocalDate postDate = dayLatestPost.getCreatedAt().toLocalDate();

// 탈퇴한 회원을 제외하고 allFamilyMembersUploaded 기본값이 true이므로, 탈퇴한 회원이 allFamilyMembersUploaded 계산에 영향을 미치지 않음
// edge case: 글을 업로드하지 않은 회원이 탈퇴하면, 과거 날짜들의 allFamilyMembersUploaded이 true로 변함 -> 핸들링할 수 없는 케이스
List<String> familyMembersIds = memberService.findFamilyMembersIdsByFamilyJoinAtBefore(familyId, postDate.plusDays(1));
boolean allFamilyMembersUploaded = true;
for (String memberId : familyMembersIds) {
if (!memberPostService.existsByMemberIdAndFamilyIdAndCreatedAt(memberId, familyId, postDate)) {
if (!postService.existsByMemberIdAndFamilyIdAndCreatedAt(memberId, familyId, postDate)) {
allFamilyMembersUploaded = false;
break;
}
Expand Down Expand Up @@ -83,7 +83,7 @@ public BannerResponse getBanner(String yearMonth, String familyId) {
/* 배너를 위한 필드 조회 */
/* 정적 필드 조회 */
int familyTopPercentage = familyService.getFamilyTopPercentage(familyId, startDate);
List<MemberPost> familyPosts = memberPostService.findAllByFamilyIdAndCreatedAtBetween(familyId, startDate, endDate);
List<Post> familyPosts = postService.findAllByFamilyIdAndCreatedAtBetween(familyId, startDate, endDate);
int familyPostsCount = familyPosts.size();
int familyInteractionCount = familyPosts.stream().mapToInt((memberPost -> memberPost.getCommentCnt() + memberPost.getReactionCnt() + memberPost.getRealEmojiCnt())).sum();

Expand All @@ -96,10 +96,10 @@ public BannerResponse getBanner(String yearMonth, String familyId) {
while (startDate.isBefore(endDate)) {
boolean allFamilyMembersUploaded = true;

if (memberPostService.existsByFamilyIdAndCreatedAt(familyId, startDate)) {
if (postService.existsByFamilyIdAndCreatedAt(familyId, startDate)) {
List<String> familyMembersIds = memberService.findFamilyMembersIdsByFamilyJoinAtBefore(familyId, startDate.plusDays(1));
for (String memberId : familyMembersIds) {
if (!memberPostService.existsByMemberIdAndFamilyIdAndCreatedAt(memberId, familyId, startDate)) {
if (!postService.existsByMemberIdAndFamilyIdAndCreatedAt(memberId, familyId, startDate)) {
allFamilyMembersUploaded = false;
break;
}
Expand Down Expand Up @@ -170,7 +170,7 @@ public FamilyMonthlyStatisticsResponse getSummary(String yearMonth, String login
int month = Integer.parseInt(yearMonthArray[1]);

String familyId = memberService.findFamilyIdByMemberId(loginMemberId);
long monthlyPostCount = memberPostService.countMonthlyPostByFamilyId(year, month, familyId);
long monthlyPostCount = postService.countMonthlyPostByFamilyId(year, month, familyId);
return new FamilyMonthlyStatisticsResponse((int) monthlyPostCount);
}
}
Loading

0 comments on commit bfa1f5d

Please sign in to comment.