added get and create post

This commit is contained in:
SlimusMinus
2026-05-07 01:08:29 +03:00
parent 1718fa7337
commit 90016d2cd6
4 changed files with 27 additions and 67 deletions

View File

@@ -39,69 +39,21 @@ public class PostService {
private final FavoriteRepository favoriteRepository;
@CacheEvict(value = "posts", allEntries = true)
public PostResponse create(PostRequest request, List<MultipartFile> files) {
User user = userService.getCurrentUser();
public PostRequest create(PostRequest request) {
// User user = userService.getCurrentUser();
//
Post post = mapper.toEntity(request);
post.setCreatedAt(LocalDateTime.now());
post.setStatus(PostStatus.ACTIVE);
post.setUser(user);
Post savedPost = repository.save(post);
if (files != null) {
for (MultipartFile file : files) {
String url = storageService.save(file);
Image image = new Image();
image.setUrl(url);
image.setPost(savedPost);
imageRepository.save(image);
}
}
return mapper.toResponse(savedPost);
return request;
}
@Cacheable(value = "posts", key = "#filter.toString() + '_' + #pageable.pageNumber")
public Page<PostResponse> getFeed(PostFilter filter, Pageable pageable) {
User user = userService.getCurrentUser();
Page<Post> posts;
if (filter.getLat() != null) {
posts = repository.searchWithGeo(
filter.getType() != null ? filter.getType().name() : null,
filter.getCity(),
filter.getCategory(),
filter.getLat(),
filter.getLng(),
filter.getRadius(),
pageable
);
} else {
posts = repository.findAll(PostSpecification.withFilter(filter), pageable);
}
// 🔥 собираем id всех постов
List<Long> postIds = posts.getContent()
.stream()
.map(Post::getId)
.toList();
// 🔥 одним запросом получаем избранные
Set<Long> favoriteIds = new HashSet<>(
favoriteRepository.findFavoritePostIds(user.getId(), postIds)
);
return posts.map(post -> {
PostResponse response = mapper.toResponse(post);
response.setFavorite(favoriteIds.contains(post.getId()));
return response;
});
public List<Post> getFeed() {
return repository.findAll();
}
@Cacheable(value = "post", key = "#id")
@@ -125,8 +77,7 @@ public class PostService {
post.setTitle(request.getTitle());
post.setDescription(request.getDescription());
post.setCategory(request.getCategory());
post.setCity(request.getCity());
post.setDistrict(request.getDistrict());
post.setType(request.getType());
Post updated = repository.save(post);