<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.unilife.mapper.PostLikeMapper">
    
    <select id="isLiked" resultType="java.lang.Boolean">
        SELECT COUNT(*) > 0
        FROM post_likes
        WHERE post_id = #{postId} AND user_id = #{userId}
    </select>
    
    <insert id="insert">
        INSERT INTO post_likes (
            user_id, post_id, created_at
        ) VALUES (
            #{userId}, #{postId}, NOW()
        )
    </insert>
    
    <delete id="delete">
        DELETE FROM post_likes
        WHERE post_id = #{postId} AND user_id = #{userId}
    </delete>
    
    <select id="getCountByPostId" resultType="java.lang.Integer">
        SELECT COUNT(*)
        FROM post_likes
        WHERE post_id = #{postId}
    </select>
    
    <select id="getLikedPostIdsByUserId" resultType="java.lang.Long">
        SELECT post_id
        FROM post_likes
        WHERE user_id = #{userId}
        ORDER BY created_at DESC
    </select>
</mapper>