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>