Correction of route /api/posts/last from lazy relations.
This commit is contained in:
@@ -4,6 +4,7 @@ import java.util.List;
|
|||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import org.codiki.core.entities.persistence.Post;
|
import org.codiki.core.entities.persistence.Post;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.data.jpa.repository.Query;
|
import org.springframework.data.jpa.repository.Query;
|
||||||
import org.springframework.data.repository.CrudRepository;
|
import org.springframework.data.repository.CrudRepository;
|
||||||
import org.springframework.data.repository.query.Param;
|
import org.springframework.data.repository.query.Param;
|
||||||
@@ -15,9 +16,9 @@ public interface PostRepository extends CrudRepository<Post, Long>, PostSearchRe
|
|||||||
@Query("SELECT p FROM Post p WHERE p.key = :postKey")
|
@Query("SELECT p FROM Post p WHERE p.key = :postKey")
|
||||||
Optional<Post> getByKey(@Param("postKey") final String pPostKey);
|
Optional<Post> getByKey(@Param("postKey") final String pPostKey);
|
||||||
|
|
||||||
@Query(value = "SELECT * FROM post p INNER JOIN \"user\" u ON u.id = p.creator_id ORDER BY p.creation_date DESC LIMIT :limit",
|
// TODO : Remove "JOIN FETCH u.role", actually necessary for JSON serialization
|
||||||
nativeQuery = true)
|
@Query("SELECT p FROM Post p JOIN FETCH p.author u JOIN FETCH u.role ORDER BY p.creationDate DESC")
|
||||||
List<Post> getLast(@Param("limit") final Integer pLimit);
|
List<Post> getLast(Pageable pPageable);
|
||||||
|
|
||||||
@Query(value = "SELECT * FROM post p INNER JOIN \"user\" u ON u.id = creator_id WHERE category_id = :categoryId ORDER BY creation_date DESC",
|
@Query(value = "SELECT * FROM post p INNER JOIN \"user\" u ON u.id = creator_id WHERE category_id = :categoryId ORDER BY creation_date DESC",
|
||||||
nativeQuery = true)
|
nativeQuery = true)
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import org.codiki.core.repositories.PostRepository;
|
|||||||
import org.codiki.core.services.ParserService;
|
import org.codiki.core.services.ParserService;
|
||||||
import org.codiki.core.services.UserService;
|
import org.codiki.core.services.UserService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.domain.PageRequest;
|
||||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
@@ -78,7 +79,7 @@ public class PostController {
|
|||||||
|
|
||||||
@GetMapping("/last")
|
@GetMapping("/last")
|
||||||
public List<PostDTO> getLast() {
|
public List<PostDTO> getLast() {
|
||||||
return postRepository.getLast(LIMIT_POSTS_HOME).stream()
|
return postRepository.getLast(PageRequest.of(0, LIMIT_POSTS_HOME)).stream()
|
||||||
.map(PostDTO::new).collect(Collectors.toList());
|
.map(PostDTO::new).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user