Skip to content

Commit

Permalink
Merge pull request #207 from TeamDon-tBe/feat/#206
Browse files Browse the repository at this point in the history
[FEAT]인기글 관련 노티 발생 기능 구현
  • Loading branch information
Hong0329 authored May 9, 2024
2 parents b2dd1c2 + 0f29f55 commit 8d46de8
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,7 @@ default Content findContentByIdOrThrow(Long contentId) {
}

List<Content> findContentByMember(Member member);

@Query("SELECT c FROM Content c WHERE c.createdAt BETWEEN :startTime AND :endTime ORDER BY c.createdAt ASC")
List<Content> findAllContentsBetweenDatesOrderedByAsc(LocalDateTime startTime, LocalDateTime endTime);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

public interface MemberRepository extends JpaRepository<Member, Long> {
Expand Down Expand Up @@ -39,4 +40,7 @@ default Member findByRefreshTokenOrThrow(String refreshToken) {
void deleteMemberScheduledForDeletion(LocalDateTime currentDate);

Member findMemberBySocialId(String socialId);

@Query("SELECT m FROM Member m WHERE m.isDeleted = false")
List<Member> findAllActiveMembers();
}
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);
}
}
}
}

0 comments on commit 8d46de8

Please sign in to comment.