public APIGatewayProxyResponseEvent handleRequest()

in DeliveryApi/ApiHandlers/src/com/ilmlf/delivery/api/handlers/GetSlots.java [72:114]


  public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {
    logger.info("Starting to handle GetSlots request");
    String returnVal = "";
    Integer httpStatus = 200;
    LocalDate availableSlotsBeginDate = LocalDate.now(ZoneId.of("UTC"));
    LocalDate availableSlotsEndDate = availableSlotsBeginDate.plus(Period.ofDays(14)); // Only retrieve next two weeks
    Integer farmId;
    ArrayList<Slot> slotArray;

    try {
      farmId = Integer.parseInt(input.getPathParameters().get("farm-id"));

    } catch (NumberFormatException exception) {
      throw new RuntimeException("Farm id must not be blank, and must be a valid integer");
    }

    try {
      slotArray = slotService.getSlots(farmId, availableSlotsBeginDate, availableSlotsEndDate);

      if (slotArray.isEmpty()) {
        httpStatus = 400;
        returnVal = "No slots found matching the farm id";

        metricsLogger.putMetric("NoSlotsFound", 1, Unit.COUNT);
      } else {
        JSONArray slotJsonArray = new JSONArray(slotArray);
        returnVal = slotJsonArray.toString();

        metricsLogger.putMetric("SlotsReturned", slotJsonArray.length(), Unit.COUNT);
      }

    } catch (SQLException exception) {
      httpStatus = 500;
      returnVal = "Error encountered while retrieving slots from database";
      logger.error(exception.getMessage(), exception);

      metricsLogger.putMetric("SqlException", 1, Unit.COUNT);
    }

    metricsLogger.flush();

    return ApiUtil.generateReturnData(httpStatus, returnVal);
  }