added filter on title and description on post
This commit is contained in:
@@ -13,6 +13,7 @@ import com.krylov.refound.mapper.PostMapper;
|
||||
import com.krylov.refound.repository.FavoriteRepository;
|
||||
import com.krylov.refound.repository.ImageRepository;
|
||||
import com.krylov.refound.repository.PostRepository;
|
||||
import com.krylov.refound.util.PostSpecification;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
@@ -28,6 +29,9 @@ import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.cache.annotation.CacheEvict;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
@@ -83,22 +87,24 @@ public class PostService {
|
||||
return request;
|
||||
}
|
||||
|
||||
public List<PostResponse> getFeed() {
|
||||
List<Post> posts = repository.findAll();
|
||||
public Page<PostResponse> getFeed(String search, Pageable pageable) {
|
||||
Specification<Post> spec = PostSpecification.hasSearch(search);
|
||||
Page<Post> postsPage = repository.findAll(spec, pageable);
|
||||
|
||||
// Get all post IDs
|
||||
List<Long> postIds = posts.stream()
|
||||
List<Long> postIds = postsPage.getContent().stream()
|
||||
.map(Post::getId)
|
||||
.toList();
|
||||
|
||||
// Count likes for each post
|
||||
if (postIds.isEmpty()) {
|
||||
return Page.empty(pageable);
|
||||
}
|
||||
|
||||
Map<Long, Long> likeCounts = favoriteRepository.countLikesByPostIds(postIds).stream()
|
||||
.collect(Collectors.toMap(
|
||||
row -> (Long) row[0], // post ID
|
||||
row -> (Long) row[1] // like count
|
||||
row -> (Long) row[0],
|
||||
row -> (Long) row[1]
|
||||
));
|
||||
|
||||
// Get current user's favorites if authenticated
|
||||
Set<Long> userFavoriteIds = new HashSet<>();
|
||||
try {
|
||||
User currentUser = userService.getCurrentUser();
|
||||
@@ -109,18 +115,14 @@ public class PostService {
|
||||
// User not authenticated - that's OK
|
||||
}
|
||||
|
||||
// Set like counts and favorite status
|
||||
final Set<Long> finalUserFavoriteIds = userFavoriteIds;
|
||||
return posts.stream()
|
||||
.map(post -> {
|
||||
PostResponse response = mapper.toResponse(post);
|
||||
response.setLikeCount(likeCounts.getOrDefault(post.getId(), 0L));
|
||||
response.setFavorite(finalUserFavoriteIds.contains(post.getId()));
|
||||
response.setUserRole(post.getUser().getRole());
|
||||
return response;
|
||||
})
|
||||
.toList();
|
||||
|
||||
return postsPage.map(post -> {
|
||||
PostResponse response = mapper.toResponse(post);
|
||||
response.setLikeCount(likeCounts.getOrDefault(post.getId(), 0L));
|
||||
response.setFavorite(finalUserFavoriteIds.contains(post.getId()));
|
||||
response.setUserRole(post.getUser().getRole());
|
||||
return response;
|
||||
});
|
||||
}
|
||||
|
||||
public List <PostResponse> getById(Long id) {
|
||||
|
||||
Reference in New Issue
Block a user