Skip to content

Commit 412cbf4

Browse files
committed
[feat] 알림 100개 유지 및 최근 30일만 표시
1 parent 2d85def commit 412cbf4

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

src/main/java/com/daramg/server/notification/application/NotificationQueryService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.springframework.stereotype.Service;
1313
import org.springframework.transaction.annotation.Transactional;
1414

15+
import java.time.LocalDateTime;
1516
import java.util.List;
1617

1718
@Service
@@ -37,6 +38,6 @@ public PageResponseDto<NotificationResponseDto> getNotifications(User user, Page
3738
}
3839

3940
public long getUnreadCount(User user) {
40-
return notificationRepository.countByReceiverIdAndIsReadFalse(user.getId());
41+
return notificationRepository.countUnreadByReceiverIdSince(user.getId(), LocalDateTime.now().minusDays(30));
4142
}
4243
}

src/main/java/com/daramg/server/notification/event/NotificationEventListener.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,9 @@ public void handleNotificationEvent(NotificationEvent event) {
2828
);
2929

3030
notificationRepository.save(notification);
31+
32+
if (notificationRepository.countByReceiverId(event.receiver().getId()) > 100) {
33+
notificationRepository.deleteOldestByReceiverIdExceedingLimit(event.receiver().getId());
34+
}
3135
}
3236
}

src/main/java/com/daramg/server/notification/repository/NotificationQueryRepositoryImpl.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import lombok.RequiredArgsConstructor;
99
import org.springframework.stereotype.Repository;
1010

11+
import java.time.LocalDateTime;
1112
import java.util.List;
1213

1314
import static com.daramg.server.notification.domain.QNotification.notification;
@@ -27,7 +28,10 @@ public List<Notification> getNotificationsWithPaging(Long receiverId, PageReques
2728
.selectFrom(notification)
2829
.leftJoin(notification.sender, user).fetchJoin()
2930
.leftJoin(notification.post, post).fetchJoin()
30-
.where(notification.receiver.id.eq(receiverId));
31+
.where(
32+
notification.receiver.id.eq(receiverId),
33+
notification.createdAt.goe(LocalDateTime.now().minusDays(30))
34+
);
3135

3236
return pagingUtils.applyCursorPagination(
3337
query,

src/main/java/com/daramg/server/notification/repository/NotificationRepository.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,21 @@
55
import org.springframework.data.jpa.repository.Modifying;
66
import org.springframework.data.jpa.repository.Query;
77

8+
import java.time.LocalDateTime;
9+
810
public interface NotificationRepository extends JpaRepository<Notification, Long> {
911

10-
long countByReceiverIdAndIsReadFalse(Long receiverId);
12+
@Query("SELECT COUNT(n) FROM Notification n WHERE n.receiver.id = :receiverId AND n.isRead = false AND n.createdAt >= :since")
13+
long countUnreadByReceiverIdSince(Long receiverId, LocalDateTime since);
14+
15+
long countByReceiverId(Long receiverId);
1116

1217
@Modifying
1318
@Query("UPDATE Notification n SET n.isRead = true WHERE n.receiver.id = :receiverId AND n.isRead = false")
1419
void markAllAsReadByReceiverId(Long receiverId);
20+
21+
@Modifying
22+
@Query("DELETE FROM Notification n WHERE n.receiver.id = :receiverId AND n.id NOT IN " +
23+
"(SELECT n2.id FROM Notification n2 WHERE n2.receiver.id = :receiverId ORDER BY n2.createdAt DESC LIMIT 100)")
24+
void deleteOldestByReceiverIdExceedingLimit(Long receiverId);
1525
}

0 commit comments

Comments
 (0)