<commit_before>package nuclibook.routes;

import nuclibook.constants.P;
import nuclibook.entity_utils.SecurityUtils;
import nuclibook.models.Booking;
import nuclibook.models.BookingSection;
import org.joda.time.DateTime;
import spark.Request;
import spark.Response;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class CalendarRoute extends DefaultRoute {

    @Override
    public Object handle(Request request, Response response) throws Exception {
        // necessary prelim routine
        prepareToHandle();

        // security check
        //TODO - what will this be? It's both viewing and editing an appointment, since you can book from the calendar.

        if (!SecurityUtils.requirePermission(P.VIEW_APPOINTMENTS, response)) return null;
        String start = request.queryParams("start");
        String end = request.queryParams("end");
        DateTime startDate = new DateTime(start);
        DateTime endDate = new DateTime(end);

        List<DateTime> allDays = new ArrayList<DateTime>();
        DateTime toAdd = startDate;
        for (int i = startDate.getDayOfWeek(); i <= endDate.getDayOfWeek(); i++) {
            allDays.add(toAdd);
            toAdd = startDate.plusDays(1);
        }

        //magix to retrieve bookings by date
        //TODO: magician, pls provide ;(((
        List<Booking> bookings = null;//BookingUtils.getBookingsByDateRange(startDate, endDate);

        //TODO create a HashMap for [Day, List<Booking] pairs

        StringBuilder jsonOutput = new StringBuilder();
        jsonOutput.append("{ 'week': [");

        //put in bookings
        for (int i = 0; i < allDays.size(); i++) {
            jsonOutput.append("{");
            jsonOutput.append("'day': '" + allDays.get(i) + "'");
            jsonOutput.append("'bookings': [");

            for (int j = 0; j < bookings.size(); j++) {
                jsonOutput.append("{");
                jsonOutput.append("'patientId': '" + bookings.get(i).getPatient().getId() + "'");
                jsonOutput.append("'therapyName': '" + bookings.get(i).getTherapy().getName() + "'");
                jsonOutput.append("'bookingSections': [");

                List<BookingSection> bookingSections = bookings.get(i).getBookingSections();
                for (int k = 0; k < bookingSections.size(); k++) {
                    jsonOutput.append("{");
                    jsonOutput.append("'start-time': '" + bookingSections.get(i).getStart() + "'");
                    jsonOutput.append("'end-time': '" + bookingSections.get(i).getEnd() + "'");

                    if (j == bookingSections.size() - 1) {
                        jsonOutput.append("}");
                    } else {
                        jsonOutput.append("}, ");
                    }
                }
                //close booking sections array
                jsonOutput.append("]");

                if (j == bookings.size() - 1) {
                    jsonOutput.append("}");
                } else {
                    jsonOutput.append("}, ");
                }
            }
            //close bookings array
            jsonOutput.append("]");

            if (i == allDays.size() - 1) {
                jsonOutput.append("}");
            } else {
                jsonOutput.append("}, ");
            }

        }
        //close days array and week immediately after
        jsonOutput.append("]}");

        return jsonOutput;
    }
}<commit_msg>changed json format to return a list with all bookings<commit_after>package nuclibook.routes;

import nuclibook.constants.P;
import nuclibook.entity_utils.BookingUtils;
import nuclibook.entity_utils.SecurityUtils;
import nuclibook.models.Booking;
import nuclibook.models.BookingSection;
import org.joda.time.DateTime;
import spark.Request;
import spark.Response;

import java.util.List;

public class CalendarRoute extends DefaultRoute {

    @Override
    public Object handle(Request request, Response response) throws Exception {
        // necessary prelim routine
        prepareToHandle();

        // security check
        if (!SecurityUtils.requirePermission(P.VIEW_APPOINTMENTS, response)) return null;

        // get start/end date
        String start = request.queryParams("start");
        String end = request.queryParams("end");
        DateTime startDate = new DateTime(start + " 00:00:00");
        DateTime endDate = new DateTime(end + " 23:59:59");

        // get bookings between start/end dates
        List<Booking> bookings = BookingUtils.getBookingsByDateRange(startDate, endDate);

        //build json
        StringBuilder jsonOutput = new StringBuilder();
        jsonOutput.append("{ 'bookings': [");

        for (int i = 0; i < bookings.size(); i++) {

            if (i == 0) {
                jsonOutput.append("{");
            } else {
                jsonOutput.append(", { ");
            }

            jsonOutput.append("'patientId': '").append(bookings.get(i).getPatient().getId() + "'");
            jsonOutput.append("'therapyName': '").append(bookings.get(i).getTherapy().getName() + "'");
            jsonOutput.append("'bookingSections': [");

            List<BookingSection> bookingSections = bookings.get(i).getBookingSections();
            for (int j = 0; j < bookingSections.size(); j++) {

                if (j == 0) {
                    jsonOutput.append("{");
                } else {
                    jsonOutput.append(", { ");
                }

                jsonOutput.append("'startTime': '").append(bookingSections.get(i).getStart() + "'");
                jsonOutput.append("'endTime': '").append(bookingSections.get(i).getEnd() + "'");
                jsonOutput.append("}");
            }

            jsonOutput.append("]}");
        }

        jsonOutput.append("]}");

        return jsonOutput;
    }
}
{'fraction_non_alphanumeric': 0.13829059829059828, 'fraction_numerical': 0.003931623931623932, 'mean_word_length': 2.082718651211802, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2134762', 'n_tokens_mistral': 1647, 'n_tokens_neox': 1557, 'n_words': 340}