Skip to content

Commit

Permalink
Merge branch 'IT-Pick:dev' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
minseok1015 authored Aug 19, 2024
2 parents a3516a9 + 6e9e96b commit 6b1f4de
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 5 deletions.
26 changes: 26 additions & 0 deletions src/main/java/store/itpick/backend/service/AlarmService.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class AlarmService {
private final CommentRepository commentRepository;
private final UserRepository userRepository;
private final AlarmRepository alarmRepository;
private final DebateRepository debateRepository;


@Transactional
Expand Down Expand Up @@ -67,6 +68,31 @@ public void createAlarmComment(long commentId, long userId) {
}


@Transactional
public void createAlarmTrend(List<Debate> trendDebateList) {
for (Debate trendDebate : trendDebateList) {

User user = trendDebate.getUser();
// Step 1: 알람의 수를 확인
Long alarmCount = alarmRepository.countByUser(user);

// Step 2: 알람이 30개 이상이면 오래된 알람 삭제
if (alarmCount >= 30) {
List<Alarm> 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<AlarmResponse> getAlarms(long userId) {
User user = getUser(userId, userRepository);
Expand Down
18 changes: 14 additions & 4 deletions src/main/java/store/itpick/backend/service/DebateService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -299,12 +296,21 @@ public void deleteDebate(Long debateId, long userId) {
}

@Transactional
public void updateHotDebate() {
public List<Debate> 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<TrendDebate> trendDebateList = trendDebateRepository.findAll();
for(TrendDebate trendDebate : trendDebateList){
Optional<Debate> updateDebate = debateRepository.getDebateByDebateId(trendDebate.getTrendDebateId());
if (updateDebate.isPresent()) {
Debate debate = updateDebate.get();
debate.setOnTrend(false);
debateRepository.save(debate);
}
}
// 기존의 TrendDebate 삭제
trendDebateRepository.deleteAllInBatch();

Expand All @@ -318,9 +324,13 @@ public void updateHotDebate() {
.updateAt(endTime) // 업데이트된 시간 저장
.build();

debate.setOnTrend(true);
debateRepository.save(debate);

trendDebateRepository.save(trendDebate);

}
return debateList;

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand Down Expand Up @@ -150,7 +151,7 @@ public void performWeeklyTasks() {

@Scheduled(cron = "0 30 * * * *")
public void updateTrendDebate(){
debateService.updateHotDebate();
alarmService.createAlarmTrend(debateService.updateHotDebate());
}

}
13 changes: 13 additions & 0 deletions src/main/java/store/itpick/backend/util/Redis.java
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,17 @@ private static int getWeight(String key) {
}
return -1;
}

public List<String> getKeywordListByRedisKey(String redisKey) {
ZSetOperations<String, Object> zSetOperations = redisTemplate.opsForZSet();

if (zSetOperations.size(redisKey) < 10) {
return null;
}
List<String> keywordList = new ArrayList<>();
for (Object keyword : zSetOperations.reverseRange(redisKey, 0, 9)) {
keywordList.add((String) keyword);
}
return keywordList;
}
}
10 changes: 10 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

---

Expand Down Expand Up @@ -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

---

Expand Down

0 comments on commit 6b1f4de

Please sign in to comment.