package top.lejings.demo.mapper;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import top.lejings.demo.pojo.Posts;

import java.util.List;
@Mapper
public interface PostsMapper {

    List<Posts> postsOverview(String by);


    Posts postsEssay(Integer post_id);

    void postsNew(Posts posts);
    @Select("SELECT p.post_id,p.user_id,COUNT(CASE WHEN r.type = 'like' THEN 1 END) AS Likes,u.username,p.title,p.updated_at FROM posts p JOIN users u ON p.user_id = u.user_id LEFT JOIN reactions r ON p.post_id = r.post_id AND r.type = 'like' WHERE u.user_id = ${user_id} GROUP BY p.post_id, p.user_id, u.username, p.title, p.updated_at, p.priority ORDER BY ${by} DESC;")
    List<Posts> postsOverviewAndId(String by, Integer user_id);

    @Delete("DELETE FROM posts WHERE post_id = #{post_id}")
    void deletePost(Integer post_id);

    @Update("UPDATE posts SET title = #{title}, content = #{content} WHERE post_id = #{post_id};")
    void postsUpdate(Posts posts);


    List<Posts> searchByTitle(String title);


    List<Posts> searchByUser(String username);

}
