Class PostDAO

java.lang.Object
org.example.dao.PostDAO

public class PostDAO extends Object
Data Access Object for Post entities. Handles CRUD operations and interaction with post_likes table.
  • Constructor Details

    • PostDAO

      public PostDAO()
  • Method Details

    • savePost

      public void savePost(String content, String author)
      Saves a new post to the database.
      Parameters:
      content - The text content of the post.
      author - The nickname of the post creator.
    • getAllPosts

      public List<Post> getAllPosts(String currentNickname)
      Retrieves all posts including an indicator if the current user has liked them.
      Parameters:
      currentNickname - The nickname of the logged-in user.
      Returns:
      List of posts sorted by ID descending.
    • searchByAuthor

      public List<Post> searchByAuthor(String authorQuery, String currentNickname)
      Searches for posts by author's nickname using a partial match.
    • updatePost

      public void updatePost(int postId, String newContent)
      Updates an existing post's content and sets the updated_at timestamp.
    • deletePost

      public void deletePost(int postId)
      Deletes a post from the database by ID.
    • toggleLike

      public void toggleLike(int postId, String nickname)
      Adds or removes a like and updates likes_count in a single transaction.
    • getTopPosts

      public List<Post> getTopPosts(int limit)
      Retrieves the most liked posts.
    • getTopAuthors

      public List<PostDAO.AuthorStats> getTopAuthors(int limit)
      Calculates posts count per author.