0

I had a workable API backend design which can retrieve a user's all appointments, which are shown below. Now, instead of retrieve all, I'm planning to retrieve a user's appointments within future 30 days (from current time) from database, but I have no idea how to set limit to achieve this. Can anyone please help me with this?

Database.java

    public List<Appointment> listUserAppointments(
            String uid,
            @Nullable String since,
            @Nullable String until,
            @Nullable AppointmentStatus status){
        try {
            AppointmentMapper mapper = session.getMapper(AppointmentMapper.class);
            return mapper.getAppointmentsByUserId(uid, since, until, status);
        } finally {
            if (!keepAlive) close();
        }
    }

    public Appointment getAppointment(String appointment_id) {
        try {
            AppointmentMapper mapper = session.getMapper(AppointmentMapper.class);
            return mapper.getAppointmentById(appointment_id);
        } finally {
            if (!keepAlive) close();
        }
    }

AppointmentMapper.java

public interface AppointmentMapper {

    /*
        You can pass multiple parameters to a mapper method.
        If you do, they will be named by the literal "param" followed
        by their position in the parameter list by default,
        for example: #{param1}, #{param2} etc.
        If you wish to change the name of the parameters (multiple only),
        then you can use the @Param("paramName") annotation on the parameter.
     */
    Appointment getAppointmentById(String id);

    List<Appointment> getAppointmentsByUserId(
            @Param("uid")  String uid,
            @Param("since")  String since,
            @Param("until")  String until,
            @Param("status") AppointmentStatus status);

    void updateUserNoteById(Appointment appointment);

    void updateStatusById(Appointment appointment);

    void insertAppointment(Appointment appointment);

    void updateAppointment(Appointment appointment);
}

AppointmentAPI.java

    @GET
    @Path("me/appointments")
    @Secured(UserRole.PATIENT)
    @JSONP(queryParam = "callback")
    @Produces(MediaType.APPLICATION_JSON)
    public Response listMyAppointments(
            @Context SecurityContext sc,
            @QueryParam("since") String since,
            @QueryParam("until") String until,
            @QueryParam("is_confirmed") Boolean is_confirmed) {

        String uid = sc.getUserPrincipal().getName();
        List<Appointment> results = retrieveUserAppointments(uid, since, until, is_confirmed);

        return Response.ok(results).build();
    }

AppointmentMapper.xml

<?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.sec.lib.AppointmentMapper">
    <select id="getAppointmentById" parameterType="String" resultType="com.sec.entity.Appointment">
        SELECT * FROM Appointment WHERE id= #{id}
    </select>

    <select id="getAppointmentsByUserId" resultType="com.sec.entity.Appointment">
        SELECT *
        FROM Appointment
        WHERE uid= #{uid}
        <choose>
            <when test="since != null and until != null">
                AND date BETWEEN #{since} AND #{until}
            </when>
            <when test="since != null and until == null">
                AND date > #{since}
            </when>
            <when test="since == null and until != null">
                <![CDATA[
                AND date < #{until}
                ]]>
            </when>
        </choose>
        <choose>
            <when test="status == null">
                AND status != 'CANCELLED'
            </when>
            <otherwise>
                AND status = #{status}
            </otherwise>
        </choose>
    </select>

    <update id="updateUserNoteById" parameterType="com.sec.entity.Appointment">
        UPDATE Appointment SET
          user_note= #{user_note}
        WHERE id= #{id}
    </update>

    <update id="updateStatusById" parameterType="com.sec.entity.Appointment">
        UPDATE Appointment SET
          status= #{status}
        WHERE id= #{id}
    </update>

    <insert id="insertAppointment" parameterType="com.sec.entity.Appointment">
        INSERT INTO Appointment (id, uid, did, title, detail, date_create, date_change, date, duration, note, status)
        VALUES (#{id}, #{uid}, #{did}, #{title}, #{detail}, #{date_create}, #{date_change}, #{date}, #{duration}, #{note}, #{status});
    </insert>

    <update id="updateAppointment" parameterType="com.sec.entity.Appointment">
        UPDATE Appointment SET
        did= #{did},
        title= #{title},
        detail= #{detail},
        date_create= #{date_create},
        date_change= #{date_change},
        date= #{date},
        duration= #{duration},
        note= #{note},
        status= #{status}
        WHERE id= #{id}
    </update>
</mapper>
Fred
  • 73
  • 2
  • 7
  • So what exactly is not working? From a first glance I would say the query parameters of type string won't match your db [date types](https://stackoverflow.com/questions/2305973/java-util-date-vs-java-sql-date/2306051#2306051) – Lennart Stoop Oct 14 '20 at 18:49
  • Well, you can ignore that since and until Param, since they are not crucial to my work before and I made no change to them. The original version will retrieve a user's all appointment. Now I am trying to get appointments within future 30 days only, so where should I edit and how? – Fred Oct 14 '20 at 19:06

0 Answers0