public APIGatewayProxyResponseEvent handleRequest()

in DeliveryApi/ApiHandlers/src/com/ilmlf/delivery/api/handlers/CreateSlots.java [85:129]


  public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {
    String returnVal = "";
    Integer httpStatus = 200;
    List<Slot> slotList = new ArrayList<>();

    try {
      String farmIdStr = input.getPathParameters().get("farm-id");
      String body = input.getBody();
      slotList = slotParser.parseAndCreateSlotList(body, farmIdStr);

    } catch (Exception e) {
      returnVal = "The data received is incomplete or invalid";
      httpStatus = 400;
      logger.error(e.getMessage(), e);

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

    if (!slotList.isEmpty()) {
      try {
        int rowsUpdated = slotService.insertSlotList(slotList);

        if (rowsUpdated == 0) {
          returnVal = "There was an error and the data could not be saved";
          httpStatus = 500;

          metricsLogger.putMetric("FailedToSaveSlots", 1, Unit.COUNT);
        } else {
          returnVal = "Slot data (" + rowsUpdated + ") was saved successfully";
        }

        metricsLogger.putMetric("SlotsCreated", rowsUpdated, Unit.COUNT);
      } catch (Exception e) {
        returnVal = "Error encountered while inserting the slot list";
        httpStatus = 500;
        logger.error(e.getMessage(), e);

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

    metricsLogger.flush();

    return ApiUtil.generateReturnData(httpStatus, returnVal);
  }