added scheduler for delete old or found posts

This commit is contained in:
SlimusMinus
2026-06-25 00:48:55 +03:00
parent 6f90562982
commit ec5cc6cedd
8 changed files with 148 additions and 0 deletions

View File

@@ -214,4 +214,63 @@ public class PostService {
repository.delete(post);
}
public PostResponse updateStatus(Long id, String statusString) {
// Find the post
Post post = repository.findById(id)
.orElseThrow(() -> new ApiException(
ErrorCode.NOT_FOUND,
"Post not found",
HttpStatus.NOT_FOUND
));
User currentUser = userService.getCurrentUser();
if (!post.getUser().getId().equals(currentUser.getId())) {
throw new ApiException(
ErrorCode.FORBIDDEN,
"Only the post owner can update status",
HttpStatus.FORBIDDEN
);
}
// Parse status string to enum
PostStatus newStatus;
try {
newStatus = PostStatus.valueOf(statusString.toUpperCase());
} catch (IllegalArgumentException e) {
throw new ApiException(
ErrorCode.VALIDATION_ERROR,
"Invalid status: " + statusString + ". Must be ACTIVE or CLOSED",
HttpStatus.BAD_REQUEST
);
}
// Update status
post.setStatus(newStatus);
Post updated = repository.save(post);
// Convert to response with like count
PostResponse response = mapper.toResponse(updated);
// Count likes for this post
List<Long> postIds = List.of(id);
Map<Long, Long> likeCounts = favoriteRepository.countLikesByPostIds(postIds).stream()
.collect(Collectors.toMap(
row -> (Long) row[0],
row -> (Long) row[1]
));
response.setLikeCount(likeCounts.getOrDefault(id, 0L));
// Check if current user has favorited this post
try {
List<Long> favoriteIds = favoriteRepository.findFavoritePostIds(currentUser.getId(), postIds);
response.setFavorite(favoriteIds.contains(id));
} catch (Exception e) {
response.setFavorite(false);
}
return response;
}
}

View File

@@ -0,0 +1,59 @@
package com.krylov.refound.service;
import com.krylov.refound.entity.Post;
import com.krylov.refound.enums.PostStatus;
import com.krylov.refound.repository.FavoriteRepository;
import com.krylov.refound.repository.PostRepository;
import java.time.LocalDateTime;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Slf4j
@Service
@RequiredArgsConstructor
public class SchedulerService {
private final PostRepository postRepository;
private final FavoriteRepository favoriteRepository;
/**
* Runs every day at 00:01 to delete:
* 1. Posts with CLOSED status
* 2. Posts older than 6 months
*/
@Scheduled(cron = "0 1 0 * * ?")
@Transactional
public void deleteExpiredPosts() {
log.info("Starting scheduled cleanup of expired posts...");
LocalDateTime sixMonthsAgo = LocalDateTime.now().minusMonths(6);
// Find and delete posts with CLOSED status
List<Post> closedPosts = postRepository.findByStatus(PostStatus.CLOSED);
if (!closedPosts.isEmpty()) {
// Delete favorites first to avoid foreign key constraint violation
favoriteRepository.deleteByPostIn(closedPosts);
log.info("Deleted {} favorites for CLOSED posts", closedPosts.size());
postRepository.deleteAll(closedPosts);
log.info("Deleted {} posts with CLOSED status", closedPosts.size());
}
// Find and delete posts older than 6 months
List<Post> oldPosts = postRepository.findByCreatedAtBefore(sixMonthsAgo);
if (!oldPosts.isEmpty()) {
// Delete favorites first to avoid foreign key constraint violation
favoriteRepository.deleteByPostIn(oldPosts);
log.info("Deleted {} favorites for old posts", oldPosts.size());
postRepository.deleteAll(oldPosts);
log.info("Deleted {} posts older than 6 months", oldPosts.size());
}
log.info("Scheduled cleanup completed successfully");
}
}