start backend
This commit is contained in:
@@ -1,4 +1,149 @@
|
||||
package com.krylov.refound.service;
|
||||
|
||||
import com.krylov.refound.dto.PostFilter;
|
||||
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.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 com.krylov.refound.util.PostSpecification;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
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.http.HttpStatus;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class PostService {
|
||||
|
||||
private final PostRepository repository;
|
||||
private final ImageRepository imageRepository;
|
||||
private final FileStorageService storageService;
|
||||
private final PostMapper mapper;
|
||||
private final UserService userService;
|
||||
private final FavoriteRepository favoriteRepository;
|
||||
|
||||
@CacheEvict(value = "posts", allEntries = true)
|
||||
public PostResponse create(PostRequest request, List<MultipartFile> files) {
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@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;
|
||||
});
|
||||
}
|
||||
|
||||
@Cacheable(value = "post", key = "#id")
|
||||
public PostResponse getById(Long id) {
|
||||
Post post = repository.findById(id)
|
||||
.orElseThrow(() -> new ApiException(ErrorCode.NOT_FOUND, "Post not found", HttpStatus.NOT_FOUND));
|
||||
|
||||
return mapper.toResponse(post);
|
||||
}
|
||||
|
||||
@CacheEvict(value = {"posts", "post"}, allEntries = true)
|
||||
public PostResponse update(Long id, PostRequest request) {
|
||||
|
||||
Post post = repository.findById(id)
|
||||
.orElseThrow(() -> new ApiException(
|
||||
ErrorCode.NOT_FOUND,
|
||||
"Post not found",
|
||||
HttpStatus.NOT_FOUND
|
||||
));
|
||||
|
||||
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);
|
||||
|
||||
return mapper.toResponse(updated);
|
||||
}
|
||||
|
||||
@CacheEvict(value = {"posts", "post"}, allEntries = true)
|
||||
public void delete(Long id) {
|
||||
|
||||
Post post = repository.findById(id)
|
||||
.orElseThrow(() -> new ApiException(
|
||||
ErrorCode.NOT_FOUND,
|
||||
"Post not found",
|
||||
HttpStatus.NOT_FOUND
|
||||
));
|
||||
|
||||
repository.delete(post);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user