diff --git a/src/main/java/store/itpick/backend/service/AlarmService.java b/src/main/java/store/itpick/backend/service/AlarmService.java index 42b395d..16f1e87 100644 --- a/src/main/java/store/itpick/backend/service/AlarmService.java +++ b/src/main/java/store/itpick/backend/service/AlarmService.java @@ -34,6 +34,7 @@ public class AlarmService { private final CommentRepository commentRepository; private final UserRepository userRepository; private final AlarmRepository alarmRepository; + private final DebateRepository debateRepository; @Transactional @@ -67,6 +68,31 @@ public void createAlarmComment(long commentId, long userId) { } + @Transactional + public void createAlarmTrend(List trendDebateList) { + for (Debate trendDebate : trendDebateList) { + + User user = trendDebate.getUser(); + // Step 1: 알람의 수를 확인 + Long alarmCount = alarmRepository.countByUser(user); + + // Step 2: 알람이 30개 이상이면 오래된 알람 삭제 + if (alarmCount >= 30) { + List alarms = alarmRepository.findByUserOrderByCreateAtAsc(user); + Alarm oldestAlarm = alarms.get(0); // 가장 오래된 알람 + alarmRepository.delete(oldestAlarm); + } + // + Alarm alarm = Alarm.builder() + .debate(trendDebate) + .user(user) + .createAt(Timestamp.valueOf(LocalDateTime.now())) + .build(); + alarmRepository.save(alarm); + } + } + + @Transactional public List getAlarms(long userId) { User user = getUser(userId, userRepository); diff --git a/src/main/java/store/itpick/backend/service/DebateService.java b/src/main/java/store/itpick/backend/service/DebateService.java index e4f5481..d4da7a0 100644 --- a/src/main/java/store/itpick/backend/service/DebateService.java +++ b/src/main/java/store/itpick/backend/service/DebateService.java @@ -8,10 +8,7 @@ import store.itpick.backend.common.exception.DebateException; import store.itpick.backend.common.exception.AuthException; import store.itpick.backend.common.exception.UserException; -import store.itpick.backend.common.exception.jwt.unauthorized.JwtInvalidTokenException; -import store.itpick.backend.common.exception.jwt.unauthorized.JwtUnauthorizedTokenException; import store.itpick.backend.dto.debate.*; -import store.itpick.backend.dto.keyword.SearchDTO; import store.itpick.backend.dto.vote.PostVoteRequest; import store.itpick.backend.jwt.JwtProvider; import store.itpick.backend.model.*; @@ -299,12 +296,21 @@ public void deleteDebate(Long debateId, long userId) { } @Transactional - public void updateHotDebate() { + public List updateHotDebate() { // 현재 시간 및 48시간 전 시간 계산 Timestamp endTime = new Timestamp(System.currentTimeMillis()); Timestamp startTime = new Timestamp(endTime.getTime() - 3 * 24 * 60 * 60 * 1000); // 3일 전 시간 PageRequest pageRequest = PageRequest.of(0, 3); // 상위 3개만 가져오기 + List trendDebateList = trendDebateRepository.findAll(); + for(TrendDebate trendDebate : trendDebateList){ + Optional updateDebate = debateRepository.getDebateByDebateId(trendDebate.getTrendDebateId()); + if (updateDebate.isPresent()) { + Debate debate = updateDebate.get(); + debate.setOnTrend(false); + debateRepository.save(debate); + } + } // 기존의 TrendDebate 삭제 trendDebateRepository.deleteAllInBatch(); @@ -318,9 +324,13 @@ public void updateHotDebate() { .updateAt(endTime) // 업데이트된 시간 저장 .build(); + debate.setOnTrend(true); + debateRepository.save(debate); + trendDebateRepository.save(trendDebate); } + return debateList; } diff --git a/src/main/java/store/itpick/backend/service/SchedulerService.java b/src/main/java/store/itpick/backend/service/SchedulerService.java index b7e1624..84f8dff 100644 --- a/src/main/java/store/itpick/backend/service/SchedulerService.java +++ b/src/main/java/store/itpick/backend/service/SchedulerService.java @@ -25,6 +25,7 @@ public class SchedulerService { private final SeleniumService seleniumService; private final KeywordService keywordService; private final DebateService debateService; + private final AlarmService alarmService; private final Redis redis; @@ -150,7 +151,7 @@ public void performWeeklyTasks() { @Scheduled(cron = "0 30 * * * *") public void updateTrendDebate(){ - debateService.updateHotDebate(); + alarmService.createAlarmTrend(debateService.updateHotDebate()); } } diff --git a/src/main/java/store/itpick/backend/util/Redis.java b/src/main/java/store/itpick/backend/util/Redis.java index 6c87530..5e9f79c 100644 --- a/src/main/java/store/itpick/backend/util/Redis.java +++ b/src/main/java/store/itpick/backend/util/Redis.java @@ -252,4 +252,17 @@ private static int getWeight(String key) { } return -1; } + + public List getKeywordListByRedisKey(String redisKey) { + ZSetOperations zSetOperations = redisTemplate.opsForZSet(); + + if (zSetOperations.size(redisKey) < 10) { + return null; + } + List keywordList = new ArrayList<>(); + for (Object keyword : zSetOperations.reverseRange(redisKey, 0, 9)) { + keywordList.add((String) keyword); + } + return keywordList; + } } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 03a536d..8201c6b 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -55,6 +55,11 @@ spring: timeout: 5000 writetimeout: 5000 auth-code-expiration-millis: 1800000 + servlet: + multipart: + enabled: true + max-file-size: 5MB + max-request-size: 5MB --- @@ -101,6 +106,11 @@ spring: timeout: 5000 writetimeout: 5000 auth-code-expiration-millis: 1800000 + servlet: + multipart: + enabled: true + max-file-size: 5MB + max-request-size: 5MB ---