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;
}
}