<?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.contact.ContactMapper">

  <sql id="columns">
    id, category, name, affiliation, dept_name, title, phone, email
  </sql>

  <select id="findAll" resultType="kr.itn.itnhub.contact.Contact">
    select <include refid="columns"/>
    from contact
    <if test="category != null">
      where category = #{category}
    </if>
    order by category asc, name asc
  </select>

  <select id="findById" resultType="kr.itn.itnhub.contact.Contact">
    select <include refid="columns"/>
    from contact
    where id = #{id}
  </select>

  <insert id="insert" parameterType="kr.itn.itnhub.contact.Contact"
          useGeneratedKeys="true" keyProperty="id">
    insert into contact (category, name, affiliation, dept_name, title, phone, email)
    values (#{category}, #{name}, #{affiliation}, #{deptName}, #{title}, #{phone}, #{email})
  </insert>

  <update id="update" parameterType="kr.itn.itnhub.contact.Contact">
    update contact set
      category    = #{category},
      name        = #{name},
      affiliation = #{affiliation},
      dept_name   = #{deptName},
      title       = #{title},
      phone       = #{phone},
      email       = #{email},
      updated_at  = now()
    where id = #{id}
  </update>

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

</mapper>
