package skilldesk.mapper;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import skilldesk.entity.Message;

import java.util.List;

public interface MessageMapper {

    @Select("SELECT id, content FROM messages ORDER BY id ASC LIMIT 1")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "content", column = "content")
    })
    Message findFirst();

    @Select("SELECT id, content FROM messages ORDER BY id ASC")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "content", column = "content")
    })
    List<Message> findAll();

    @Insert("INSERT INTO messages (content) VALUES (#{content})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    void insert(Message message);

    @Update("UPDATE messages SET content = #{content} WHERE id = #{id}")
    int update(@Param("id") Integer id, @Param("content") String content);

    @Delete("DELETE FROM messages WHERE id = #{id}")
    int deleteById(Integer id);
}
