|
|
|
|
@@ -4,11 +4,13 @@ import com.krylov.refound.dto.PostRequest;
|
|
|
|
|
import com.krylov.refound.dto.PostResponse;
|
|
|
|
|
import com.krylov.refound.entity.Image;
|
|
|
|
|
import com.krylov.refound.entity.Post;
|
|
|
|
|
import com.krylov.refound.entity.User;
|
|
|
|
|
import com.krylov.refound.enums.ErrorCode;
|
|
|
|
|
import com.krylov.refound.enums.PostCategory;
|
|
|
|
|
import com.krylov.refound.enums.PostStatus;
|
|
|
|
|
import com.krylov.refound.exception.ApiException;
|
|
|
|
|
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 java.io.IOException;
|
|
|
|
|
@@ -16,8 +18,12 @@ import java.nio.file.Files;
|
|
|
|
|
import java.nio.file.Path;
|
|
|
|
|
import java.nio.file.Paths;
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
import java.util.UUID;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.cache.annotation.CacheEvict;
|
|
|
|
|
@@ -36,6 +42,7 @@ public class PostService {
|
|
|
|
|
private final ImageRepository imageRepository;
|
|
|
|
|
private final NominatimService nominatimService;
|
|
|
|
|
private final UserService userService;
|
|
|
|
|
private final FavoriteRepository favoriteRepository;
|
|
|
|
|
|
|
|
|
|
public PostRequest create(PostRequest request, MultipartFile image) {
|
|
|
|
|
|
|
|
|
|
@@ -66,17 +73,76 @@ public class PostService {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<PostResponse> getFeed() {
|
|
|
|
|
return repository.findAll().stream()
|
|
|
|
|
List<Post> posts = repository.findAll();
|
|
|
|
|
|
|
|
|
|
// Get all post IDs
|
|
|
|
|
List<Long> postIds = posts.stream()
|
|
|
|
|
.map(Post::getId)
|
|
|
|
|
.toList();
|
|
|
|
|
|
|
|
|
|
// Count likes for each post
|
|
|
|
|
Map<Long, Long> likeCounts = favoriteRepository.countLikesByPostIds(postIds).stream()
|
|
|
|
|
.collect(Collectors.toMap(
|
|
|
|
|
row -> (Long) row[0], // post ID
|
|
|
|
|
row -> (Long) row[1] // like count
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
// Get current user's favorites if authenticated
|
|
|
|
|
Set<Long> userFavoriteIds = new HashSet<>();
|
|
|
|
|
try {
|
|
|
|
|
User currentUser = userService.getCurrentUser();
|
|
|
|
|
userFavoriteIds = new HashSet<>(
|
|
|
|
|
favoriteRepository.findFavoritePostIds(currentUser.getId(), postIds)
|
|
|
|
|
);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
// 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);
|
|
|
|
|
log.info("Post {}: images = {}, phone = {}", post.getId(), response.getImages()); // ← сюда
|
|
|
|
|
response.setLikeCount(likeCounts.getOrDefault(post.getId(), 0L));
|
|
|
|
|
response.setFavorite(finalUserFavoriteIds.contains(post.getId()));
|
|
|
|
|
return response;
|
|
|
|
|
}).toList();
|
|
|
|
|
})
|
|
|
|
|
.toList();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List <PostResponse> getById(Long id) {
|
|
|
|
|
return repository.findByUserId(id).stream().map(mapper::toResponse).toList();
|
|
|
|
|
List<Post> posts = repository.findByUserId(id);
|
|
|
|
|
|
|
|
|
|
List<Long> postIds = posts.stream()
|
|
|
|
|
.map(Post::getId)
|
|
|
|
|
.toList();
|
|
|
|
|
|
|
|
|
|
Map<Long, Long> likeCounts = favoriteRepository.countLikesByPostIds(postIds).stream()
|
|
|
|
|
.collect(Collectors.toMap(
|
|
|
|
|
row -> (Long) row[0],
|
|
|
|
|
row -> (Long) row[1]
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
Set<Long> userFavoriteIds = new HashSet<>();
|
|
|
|
|
try {
|
|
|
|
|
User currentUser = userService.getCurrentUser();
|
|
|
|
|
userFavoriteIds = new HashSet<>(
|
|
|
|
|
favoriteRepository.findFavoritePostIds(currentUser.getId(), postIds)
|
|
|
|
|
);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
// User not authenticated
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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()));
|
|
|
|
|
return response;
|
|
|
|
|
})
|
|
|
|
|
.toList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public PostResponse update(Long id, PostRequest request, MultipartFile image) {
|
|
|
|
|
|