added functional favorites posts
This commit is contained in:
@@ -30,4 +30,12 @@ public class FavoriteController {
|
||||
log.info("get all likes");
|
||||
return service.getFavorites();
|
||||
}
|
||||
|
||||
@GetMapping("/posts")
|
||||
public List<PostResponse> getAllFavoritesPosts(){
|
||||
log.info("get all favorites posts");
|
||||
return service.getAllFavoritesPosts();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.krylov.refound.entity.User;
|
||||
import java.util.List;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
public interface FavoriteRepository extends JpaRepository<Favorite, Long> {
|
||||
|
||||
@@ -29,4 +30,7 @@ public interface FavoriteRepository extends JpaRepository<Favorite, Long> {
|
||||
GROUP BY f.post.id
|
||||
\s""")
|
||||
List<Object[]> countLikesByPostIds(List<Long> postIds);
|
||||
|
||||
@Query("select f.post.id from Favorite f where f.user.id = :userId")
|
||||
List<Long> findPostIdsByUserId(@Param("userId") Long userId);
|
||||
}
|
||||
|
||||
@@ -50,4 +50,7 @@ public interface PostRepository extends JpaRepository<Post, Long>, JpaSpecificat
|
||||
""")
|
||||
List<MapMarkerDto> findMapMarkers(@Param("excludedStatuses") Collection<PostStatus> excludedStatuses);
|
||||
|
||||
@Query("SELECT p FROM Post p WHERE p.id = :postIds")
|
||||
List<Long> findFavoritePostIds(List<Long> postIds);
|
||||
|
||||
}
|
||||
@@ -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,20 +20,12 @@ 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;
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<PostResponse> getFavorites() {
|
||||
User user = userService.getCurrentUser();
|
||||
return repository.findByUser(user)
|
||||
.stream()
|
||||
.map(Favorite::getPost)
|
||||
.map(mapper::toResponse)
|
||||
.toList();
|
||||
}
|
||||
private final PostService postService;
|
||||
|
||||
@Transactional
|
||||
public void toggle(Long postId) {
|
||||
@@ -53,4 +47,39 @@ public class FavoriteService {
|
||||
repository.save(f);
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<PostResponse> getFavorites() {
|
||||
User user = userService.getCurrentUser();
|
||||
return repository.findByUser(user)
|
||||
.stream()
|
||||
.map(Favorite::getPost)
|
||||
.map(mapper::toResponse)
|
||||
.toList();
|
||||
}
|
||||
|
||||
public List<PostResponse> getAllFavoritesPosts() {
|
||||
User user = userService.getCurrentUser();
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -325,7 +325,7 @@ public class PostService {
|
||||
}
|
||||
}
|
||||
|
||||
private Map<Long, Long> countLikes(List<Long> postIds) {
|
||||
public Map<Long, Long> countLikes(List<Long> postIds) {
|
||||
if (postIds.isEmpty()) {
|
||||
return Map.of();
|
||||
}
|
||||
@@ -340,7 +340,7 @@ public class PostService {
|
||||
* Возвращает id постов, лайкнутых текущим пользователем, либо пустой набор,
|
||||
* если пользователь не авторизован.
|
||||
*/
|
||||
private Set<Long> currentUserFavoriteIds(List<Long> postIds) {
|
||||
public Set<Long> currentUserFavoriteIds(List<Long> postIds) {
|
||||
if (postIds.isEmpty()) {
|
||||
return Set.of();
|
||||
}
|
||||
@@ -360,7 +360,7 @@ public class PostService {
|
||||
}
|
||||
}
|
||||
|
||||
private PostResponse toResponse(Post post, Map<Long, Long> likeCounts, Set<Long> userFavoriteIds, boolean includeUserRole) {
|
||||
public PostResponse toResponse(Post post, Map<Long, Long> likeCounts, Set<Long> userFavoriteIds, boolean includeUserRole) {
|
||||
PostResponse response = mapper.toResponse(post);
|
||||
response.setLikeCount(likeCounts.getOrDefault(post.getId(), 0L));
|
||||
response.setFavorite(userFavoriteIds.contains(post.getId()));
|
||||
|
||||
Reference in New Issue
Block a user