added functional favorites posts
This commit is contained in:
@@ -10,6 +10,8 @@ import com.krylov.refound.mapper.PostMapper;
|
||||
import com.krylov.refound.repository.FavoriteRepository;
|
||||
import com.krylov.refound.repository.PostRepository;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -18,10 +20,33 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class FavoriteService {
|
||||
|
||||
private final FavoriteRepository repository;
|
||||
private final PostRepository postRepository;
|
||||
private final UserService userService;
|
||||
private final PostMapper mapper;
|
||||
private final PostService postService;
|
||||
|
||||
@Transactional
|
||||
public void toggle(Long postId) {
|
||||
|
||||
User user = userService.getCurrentUser();
|
||||
|
||||
Post post = postRepository.findById(postId).orElseThrow(() -> new ApiException(
|
||||
ErrorCode.NOT_FOUND,
|
||||
"Post not found",
|
||||
HttpStatus.NOT_FOUND
|
||||
));
|
||||
|
||||
if (repository.existsByUserAndPost(user, post)) {
|
||||
repository.deleteByUserAndPost(user, post);
|
||||
} else {
|
||||
Favorite f = new Favorite();
|
||||
f.setUser(user);
|
||||
f.setPost(post);
|
||||
repository.save(f);
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<PostResponse> getFavorites() {
|
||||
@@ -33,24 +58,28 @@ public class FavoriteService {
|
||||
.toList();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void toggle(Long postId) {
|
||||
|
||||
public List<PostResponse> getAllFavoritesPosts() {
|
||||
User user = userService.getCurrentUser();
|
||||
|
||||
Post post = postRepository.findById(postId).orElseThrow(() -> new ApiException(
|
||||
ErrorCode.NOT_FOUND,
|
||||
"Post not found",
|
||||
HttpStatus.NOT_FOUND
|
||||
));
|
||||
|
||||
if (repository.existsByUserAndPost(user, post)) {
|
||||
repository.deleteByUserAndPost(user, post);
|
||||
} else {
|
||||
Favorite f = new Favorite();
|
||||
f.setUser(user);
|
||||
f.setPost(post);
|
||||
repository.save(f);
|
||||
if (user == null) {
|
||||
return List.of();
|
||||
}
|
||||
|
||||
List<Long> favoritePostIds = repository.findPostIdsByUserId(user.getId());
|
||||
if (favoritePostIds.isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
|
||||
List<Post> posts = postRepository.findAllById(favoritePostIds);
|
||||
|
||||
List<Long> postIds = posts.stream()
|
||||
.map(Post::getId)
|
||||
.toList();
|
||||
|
||||
Map<Long, Long> likeCounts = postService.countLikes(postIds);
|
||||
Set<Long> userFavoriteIds = postService.currentUserFavoriteIds(postIds); // тут будет == postIds, но ок для единообразия
|
||||
|
||||
return posts.stream()
|
||||
.map(post -> postService.toResponse(post, likeCounts, userFavoriteIds, true))
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user