-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[FEAT]인기글 관련 노티 발생 기능 구현
- Loading branch information
Showing
3 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
...ver/src/main/java/com/dontbe/www/DontBeServer/common/crontab/PopularContentScheduler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package com.dontbe.www.DontBeServer.common.crontab; | ||
|
||
import com.dontbe.www.DontBeServer.api.comment.repository.CommentRepository; | ||
import com.dontbe.www.DontBeServer.api.content.domain.Content; | ||
import com.dontbe.www.DontBeServer.api.content.repository.ContentLikedRepository; | ||
import com.dontbe.www.DontBeServer.api.content.repository.ContentRepository; | ||
import com.dontbe.www.DontBeServer.api.member.domain.Member; | ||
import com.dontbe.www.DontBeServer.api.member.repository.MemberRepository; | ||
import com.dontbe.www.DontBeServer.api.notification.domain.Notification; | ||
import com.dontbe.www.DontBeServer.api.notification.repository.NotificationRepository; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.AbstractMap; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@Service | ||
public class PopularContentScheduler { | ||
private final ContentRepository contentRepository; | ||
private final ContentLikedRepository contentLikedRepository; | ||
private final CommentRepository commentRepository; | ||
private final NotificationRepository notificationRepository; | ||
private final MemberRepository memberRepository; | ||
|
||
public PopularContentScheduler(ContentRepository contentRepository, ContentLikedRepository contentLikedRepository, | ||
CommentRepository commentRepository, NotificationRepository notificationRepository, MemberRepository memberRepository){ | ||
this.contentRepository = contentRepository; | ||
this.contentLikedRepository = contentLikedRepository; | ||
this.commentRepository = commentRepository; | ||
this.notificationRepository = notificationRepository; | ||
this.memberRepository = memberRepository; | ||
} | ||
|
||
@Scheduled(cron = "0 0 4 * * ?") | ||
@Transactional | ||
public void popularContentNotification() { | ||
LocalDateTime startOfYesterday = LocalDateTime.now().minusDays(1).toLocalDate().atStartOfDay(); | ||
LocalDateTime endOfYesterday = startOfYesterday.plusDays(1).minusSeconds(1); | ||
|
||
List<Content> contents = contentRepository.findAllContentsBetweenDatesOrderedByAsc(startOfYesterday, endOfYesterday); | ||
|
||
Content topContent = contents.stream() | ||
.map(c -> new AbstractMap.SimpleEntry<>(c, contentLikedRepository.countByContent(c) + commentRepository.countByContent(c) * 1.6)) | ||
.filter(e -> e.getValue() >= 5) | ||
.max(Comparator.comparingDouble(Map.Entry::getValue)) | ||
.map(Map.Entry::getKey) | ||
.orElse(null); | ||
|
||
if (topContent != null) { | ||
Member topContentWriter = topContent.getMember(); | ||
|
||
Notification popularWriterNotification = Notification.builder() | ||
.notificationTargetMember(topContentWriter) | ||
.notificationTriggerMemberId(-1L) | ||
.notificationTriggerType("popularWriter") | ||
.notificationTriggerId(topContent.getId()) | ||
.isNotificationChecked(false) | ||
.notificationText("") | ||
.build(); | ||
Notification savedPopularWriterNotification = notificationRepository.save(popularWriterNotification); | ||
|
||
List<Member> activeMembers = memberRepository.findAllActiveMembers(); | ||
|
||
for (Member activeMember : activeMembers) { | ||
Notification popularContentNotification = Notification.builder() | ||
.notificationTargetMember(activeMember) | ||
.notificationTriggerMemberId(-1L) | ||
.notificationTriggerType("popularContent") | ||
.notificationTriggerId(topContent.getId()) | ||
.isNotificationChecked(false) | ||
.notificationText(topContent.getContentText()) | ||
.build(); | ||
Notification savedPopularContentNotification = notificationRepository.save(popularContentNotification); | ||
} | ||
} | ||
} | ||
} |