Skip to content

Commit

Permalink
[OING-173] feat: MVP 2차 추억 캘린더 배너 구현 (#132)
Browse files Browse the repository at this point in the history
* feat: Add familyScore definition

* feat: Add JpaEntity and SpringEvent to add score per user activity

* feat: Add the events to substract score per canceled user activity

* fix: Change FamilyScoreEventListener annotation to fix not committed score event

* feat: Impl the code to calculate the familyTopPercentage in getBanner API

* feat: Impl the code to count family activity in getBanner API

* feat: Add counting code to banners dynamic fields(allFamilyMembersUploadedDays, allFamilyMembersUploadedStreaks) and Impl getBanner API with a lot of comments

* fix: Fix broken test code with changed Member entity

* fix: Handle divide by zero error from calculateFamilyTopPercentile method

* fix: Replace inclusiveToday as endDate from the code to calculate dynamic field of getBanner API to fix wrongly selected period

* chore: Clear the commenting at redis cache annotation in MemberPostController
  • Loading branch information
Kwon770 authored and CChuYong committed Jan 31, 2024
1 parent 7140813 commit ddd031a
Show file tree
Hide file tree
Showing 39 changed files with 609 additions and 50 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ out/

application-local.yaml
.env
data.sql

### QueryDSL ###
generated/
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.oing.event;

import lombok.Getter;
import org.springframework.context.ApplicationEvent;

@Getter
public class MemberPostCommentCreatedEvent extends ApplicationEvent {

private final String memberId;

public MemberPostCommentCreatedEvent(Object source, String memberId) {
super(source);
this.memberId = memberId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.oing.event;

import lombok.Getter;
import org.springframework.context.ApplicationEvent;

@Getter
public class MemberPostCommentDeletedEvent extends ApplicationEvent {

private final String memberId;

public MemberPostCommentDeletedEvent(Object source, String memberId) {
super(source);
this.memberId = memberId;
}
}
15 changes: 15 additions & 0 deletions common/src/main/java/com/oing/event/MemberPostCreatedEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.oing.event;

import lombok.Getter;
import org.springframework.context.ApplicationEvent;

@Getter
public class MemberPostCreatedEvent extends ApplicationEvent {

private final String memberId;

public MemberPostCreatedEvent(Object source, String memberId) {
super(source);
this.memberId = memberId;
}
}
15 changes: 15 additions & 0 deletions common/src/main/java/com/oing/event/MemberPostDeletedEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.oing.event;

import lombok.Getter;
import org.springframework.context.ApplicationEvent;

@Getter
public class MemberPostDeletedEvent extends ApplicationEvent {

private final String memberId;

public MemberPostDeletedEvent(Object source, String memberId) {
super(source);
this.memberId = memberId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.oing.event;

import lombok.Getter;
import org.springframework.context.ApplicationEvent;

@Getter
public class MemberPostReactionCreatedEvent extends ApplicationEvent {

private final String memberId;

public MemberPostReactionCreatedEvent(Object source, String memberId) {
super(source);
this.memberId = memberId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.oing.event;

import lombok.Getter;
import org.springframework.context.ApplicationEvent;

@Getter
public class MemberPostReactionDeletedEvent extends ApplicationEvent {

private final String memberId;

public MemberPostReactionDeletedEvent(Object source, String memberId) {
super(source);
this.memberId = memberId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.oing.event;

import lombok.Getter;
import org.springframework.context.ApplicationEvent;

@Getter
public class MemberPostRealEmojiCreatedEvent extends ApplicationEvent {

private final String memberId;

public MemberPostRealEmojiCreatedEvent(Object source, String memberId) {
super(source);
this.memberId = memberId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.oing.event;

import lombok.Getter;
import org.springframework.context.ApplicationEvent;

@Getter
public class MemberPostRealEmojiDeletedEvent extends ApplicationEvent {

private final String memberId;

public MemberPostRealEmojiDeletedEvent(Object source, String memberId) {
super(source);
this.memberId = memberId;
}
}
62 changes: 62 additions & 0 deletions family/src/main/java/com/oing/domain/Family.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,66 @@ public class Family extends BaseEntity {
@Id
@Column(name = "family_id", columnDefinition = "CHAR(26)", nullable = false)
private String id;

@Column(name = "score", nullable = false)
private Integer score = 0;


public Family(String id) {
this.id = id;
}

private static final int NEW_POST_SCORE = 20;
private static final int ALL_FAMILY_MEMBERS_POSTS_UPLOADED_SCORE = 50;
private static final int NEW_COMMENT_SCORE = 5;
private static final int NEW_REACTION_SCORE = 1;
private static final int NEW_REAL_EMOJI_SCORE = 3;

public void addNewPostScore() {
addScore(NEW_POST_SCORE);
}

public void subtractNewPostScore() {
subtractScore(NEW_POST_SCORE);
}

public void addAllFamilyMembersPostsUploadedScore() {
addScore(ALL_FAMILY_MEMBERS_POSTS_UPLOADED_SCORE);
}

public void subtractAllFamilyMembersPostsUploadedScore() {
subtractScore(ALL_FAMILY_MEMBERS_POSTS_UPLOADED_SCORE);
}

public void addNewCommentScore() {
addScore(NEW_COMMENT_SCORE);
}

public void subtractNewCommentScore() {
subtractScore(NEW_COMMENT_SCORE);
}

public void addNewReactionScore() {
addScore(NEW_REACTION_SCORE);
}

public void subtractNewReactionScore() {
subtractScore(NEW_REACTION_SCORE);
}

public void addNewRealEmojiScore() {
addScore(NEW_REAL_EMOJI_SCORE);
}

public void subtractNewRealEmojiScore() {
subtractScore(NEW_REAL_EMOJI_SCORE);
}

private void addScore(int score) {
this.score += score;
}

private void subtractScore(int score) {
this.score -= score;
}
}
79 changes: 79 additions & 0 deletions family/src/main/java/com/oing/event/FamilyScoreEventListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.oing.event;

import com.oing.service.FamilyService;
import com.oing.service.MemberBridge;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

@Component
@RequiredArgsConstructor
public class FamilyScoreEventListener {

public final MemberBridge memberBridge;
public final FamilyService familyService;

// TODO: 1. BEFORE_COMMIT, 2. Async 모두 작동 안함. 원인 파악 후 리펙토링 필요.
// @TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT)
// @Async
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void onMemberPostCreatedEvent(MemberPostCreatedEvent memberPostCreatedEvent) {
String familyId = memberBridge.getFamilyIdByMemberId(memberPostCreatedEvent.getMemberId());
familyService.getFamilyById(familyId).addNewPostScore();
}

@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void onMemberPostDeletedEvent(MemberPostDeletedEvent memberPostDeletedEvent) {
String familyId = memberBridge.getFamilyIdByMemberId(memberPostDeletedEvent.getMemberId());
familyService.getFamilyById(familyId).subtractNewPostScore();
}

@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void onMemberPostCommentCreatedEvent(MemberPostCommentCreatedEvent memberPostCommentCreatedEvent) {
String familyId = memberBridge.getFamilyIdByMemberId(memberPostCommentCreatedEvent.getMemberId());
familyService.getFamilyById(familyId).addNewCommentScore();
}

@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void onMemberPostCommentDeletedEvent(MemberPostCommentDeletedEvent memberPostCommentDeletedEvent) {
String familyId = memberBridge.getFamilyIdByMemberId(memberPostCommentDeletedEvent.getMemberId());
familyService.getFamilyById(familyId).subtractNewCommentScore();
}

@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void onMemberPostReactionCreatedEvent(MemberPostReactionCreatedEvent memberPostReactionCreatedEvent) {
String familyId = memberBridge.getFamilyIdByMemberId(memberPostReactionCreatedEvent.getMemberId());
familyService.getFamilyById(familyId).addNewReactionScore();
}

@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void onMemberPostReactionDeletedEvent(MemberPostReactionDeletedEvent memberPostReactionDeletedEvent) {
String familyId = memberBridge.getFamilyIdByMemberId(memberPostReactionDeletedEvent.getMemberId());
familyService.getFamilyById(familyId).subtractNewReactionScore();
}

@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void onMemberPostRealEmojiCreatedEvent(MemberPostRealEmojiCreatedEvent memberPostRealEmojiCreatedEvent) {
String familyId = memberBridge.getFamilyIdByMemberId(memberPostRealEmojiCreatedEvent.getMemberId());
familyService.getFamilyById(familyId).addNewRealEmojiScore();
}

@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void onMemberPostRealEmojiDeletedEvent(MemberPostRealEmojiDeletedEvent memberPostRealEmojiDeletedEvent) {
String familyId = memberBridge.getFamilyIdByMemberId(memberPostRealEmojiDeletedEvent.getMemberId());
familyService.getFamilyById(familyId).subtractNewRealEmojiScore();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
import org.springframework.data.jpa.repository.JpaRepository;

public interface FamilyRepository extends JpaRepository<Family, String> {
long countByScoreGreaterThanEqual(int familyScore);
}
13 changes: 13 additions & 0 deletions family/src/main/java/com/oing/service/FamilyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,17 @@ public Family getFamilyById(String familyId) {
.findById(familyId)
.orElseThrow(FamilyNotFoundException::new);
}

public int calculateFamilyTopPercentile(String familyId) {
long allFamiliesCount = familyRepository.count();
int familyScore = getFamilyById(familyId).getScore();
long familyRank = familyRepository.countByScoreGreaterThanEqual(familyScore);

// handle divide by zero error
if (allFamiliesCount == 0) {
return 0;
}

return (100 - (int) ((familyRank / (double) allFamiliesCount) * 100));
}
}
Loading

0 comments on commit ddd031a

Please sign in to comment.