<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="kr.itn.itnhub.shared.SharedFileMapper">

  <!-- 목록에는 파일 내용을 싣지 않는다. 서식이 커지면 목록 조회가 무거워진다. -->
  <select id="findAll" resultType="kr.itn.itnhub.shared.SharedFile">
    select id, file_name, description, byte_size, uploaded_by,
           (extract(epoch from uploaded_at) * 1000)::bigint as uploaded_at
    from shared_file
    order by uploaded_at desc, id desc
  </select>

  <insert id="insert">
    insert into shared_file (file_name, description, content_type, byte_size, content, uploaded_by)
    values (#{fileName}, #{description}, #{contentType}, #{byteSize}, #{content}, #{uploadedBy})
  </insert>

  <update id="updateDescription">
    update shared_file set description = #{description} where id = #{id}
  </update>

  <select id="findContent" resultType="kr.itn.itnhub.shared.SharedFileMapper$Content">
    select file_name, content_type, content from shared_file where id = #{id}
  </select>

  <delete id="delete">
    delete from shared_file where id = #{id}
  </delete>

</mapper>
