Interface PostRepository
- All Superinterfaces:
org.springframework.data.repository.CrudRepository<PostEntity,,Long> org.springframework.data.jpa.repository.JpaRepository<PostEntity,,Long> org.springframework.data.repository.ListCrudRepository<PostEntity,,Long> org.springframework.data.repository.ListPagingAndSortingRepository<PostEntity,,Long> org.springframework.data.repository.PagingAndSortingRepository<PostEntity,,Long> org.springframework.data.repository.query.QueryByExampleExecutor<PostEntity>,org.springframework.data.repository.Repository<PostEntity,Long>
public interface PostRepository
extends org.springframework.data.jpa.repository.JpaRepository<PostEntity,Long>
JPA repository for managing post persistence operations.
-
Method Summary
Modifier and TypeMethodDescriptiondeleteByIdReturningCount(Long postId) Deletes a post by its ID and returns the deleted post's ID.booleanexistsById(long postId) Checks if a post exists by its ID.booleanexistsByIdAndAuthorId(long postId, long authorId) Checks if a post exists by its ID and author ID.findByAuthorId(long userId) Retrieves all posts authored by the specified user.findById(long postId) Retrieves a post by its ID.findBySlug(String slug) Retrieves a post by its unique slug identifier.Methods inherited from interface org.springframework.data.repository.CrudRepository
count, delete, deleteAll, deleteAll, deleteAllById, deleteById, existsById, findById, saveMethods inherited from interface org.springframework.data.jpa.repository.JpaRepository
deleteAllByIdInBatch, deleteAllInBatch, deleteAllInBatch, deleteInBatch, findAll, findAll, flush, getById, getOne, getReferenceById, saveAllAndFlush, saveAndFlushMethods inherited from interface org.springframework.data.repository.ListCrudRepository
findAll, findAllById, saveAllMethods inherited from interface org.springframework.data.repository.ListPagingAndSortingRepository
findAllMethods inherited from interface org.springframework.data.repository.PagingAndSortingRepository
findAllMethods inherited from interface org.springframework.data.repository.query.QueryByExampleExecutor
count, exists, findAll, findBy, findOne
-
Method Details
-
findByAuthorId
Retrieves all posts authored by the specified user.- Parameters:
userId- the ID of the author- Returns:
- list of posts by the author
-
deleteByIdReturningCount
@Query(value="DELETE FROM posts WHERE id = :id RETURNING id", nativeQuery=true) Long deleteByIdReturningCount(@Param("id") Long postId) Deletes a post by its ID and returns the deleted post's ID.- Parameters:
postId- the ID of the post to delete- Returns:
- the ID of the deleted post, or null if no post was found
-
findBySlug
Retrieves a post by its unique slug identifier.- Parameters:
slug- the unique slug of the post- Returns:
- optional containing the post if found, empty otherwise
-
existsById
boolean existsById(long postId) Checks if a post exists by its ID.- Parameters:
postId- the ID of the post- Returns:
- true if the post exists, false otherwise
-
existsByIdAndAuthorId
boolean existsByIdAndAuthorId(long postId, long authorId) Checks if a post exists by its ID and author ID.- Parameters:
postId- the ID of the postauthorId- the ID of the author- Returns:
- true if the post exists and belongs to the author, false otherwise
-
findById
Retrieves a post by its ID.- Parameters:
postId- the ID of the post- Returns:
- optional containing the post if found, empty otherwise
-