Skip to content

Commit

Permalink
refactor: replace notification count in member entity with a reposito…
Browse files Browse the repository at this point in the history
…ry method to retrieve number of unread responses
  • Loading branch information
becooq81 committed Jul 25, 2024
1 parent 5c07c0c commit 3af47c8
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 13 deletions.
5 changes: 0 additions & 5 deletions src/main/java/org/poolc/api/member/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@ public class Member extends TimestampEntity implements UserDetails {
@JoinColumn(name = "badge_id")
private Badge badge;

@Column(name = "notification_count", columnDefinition = "bigint default 0")
private Long notificationCount=0L;

protected Member() {
}

Expand Down Expand Up @@ -284,6 +281,4 @@ public void deleteBadge(){
this.badge = null;
}

public void addNotificationCount() { this.notificationCount ++; }
public void deductNotificationCount() {this.notificationCount --;}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.poolc.api.member.domain.Member;
import org.poolc.api.notification.domain.Notification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

Expand All @@ -12,5 +13,6 @@ public interface NotificationRepository extends JpaRepository<Notification, Long

List<Notification> findAllByReceiverId(String receiverId);

@Query("SELECT COUNT(n) FROM Notification n WHERE n.receiverId = :receiverId AND n.readStatus = false")
Long countByReceiverIdAndReadIsFalse(String receiverId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public NotificationSummaryResponse getUnreadNotificationsForMember(Member member
.sorted(Comparator.comparing(Notification::getCreatedAt).reversed())
.map(NotificationResponse::of)
.collect(Collectors.toList());
return NotificationSummaryResponse.of(member.getNotificationCount(), responses);
long unreadCount = notificationRepository.countByReceiverIdAndReadIsFalse(member.getLoginID());
return NotificationSummaryResponse.of(unreadCount, responses);
}

@Transactional
Expand All @@ -40,14 +41,13 @@ public NotificationSummaryResponse getAllNotificationsForMember(Member member) {
.sorted(Comparator.comparing(Notification::getCreatedAt).reversed())
.map(NotificationResponse::of)
.collect(Collectors.toList());

return NotificationSummaryResponse.of(member.getNotificationCount(), responses);
long unreadCount = notificationRepository.countByReceiverIdAndReadIsFalse(member.getLoginID());
return NotificationSummaryResponse.of(unreadCount, responses);
}
@Transactional
public void createBadgeNotification(Member receiver) {
Notification notification = new Notification(receiver.getLoginID(), NotificationType.BADGE);
notificationRepository.save(notification);
receiver.addNotificationCount();
//sendRealTimeNotification(notification);
}

Expand All @@ -56,23 +56,20 @@ public void createMessageNotification(String senderId, String receiverId, Long m
Member receiver = getMemberByLoginID(receiverId);
Notification notification = new Notification(senderId, receiverId, messageId, NotificationType.MESSAGE);
notificationRepository.save(notification);
receiver.addNotificationCount();
}

@Transactional
public void createCommentNotification(String senderId, String receiverId, Long postId) {
Member sender = getMemberByLoginID(senderId);
Member receiver = getMemberByLoginID(receiverId);
notificationRepository.save(new Notification(senderId, receiverId, postId, NotificationType.POST));
receiver.addNotificationCount();
}

@Transactional
public void createRecommentNotification(String senderId, String receiverId, Long postId, Long parentCommentId) {
Member sender = getMemberByLoginID(senderId);
Member receiver = getMemberByLoginID(receiverId);
notificationRepository.save(new Notification(senderId, receiverId, parentCommentId, NotificationType.COMMENT));
receiver.addNotificationCount();
}

@Transactional
Expand All @@ -81,7 +78,6 @@ public NotificationResponse readNotification(Member member, Long notificationId)
.orElseThrow(() -> new NoSuchElementException("No notification found with given id."));
checkIfSelf(member, notification);
notification.memberReads();
member.deductNotificationCount();
return NotificationResponse.of(notification);
}

Expand Down

0 comments on commit 3af47c8

Please sign in to comment.