public APIGatewayProxyResponseEvent handleRequest()

in lambda/src/main/java/com/amazon/example/ProcessingLambda.java [45:123]


    public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent request, Context context) {

        Map<String, String> query = request.getQueryStringParameters();

        LOGGER.info(String.format("[%s] Processed data", request));

        User user;
        String result = "";
        List<User> userList;

        String httpMethod = request.getHttpMethod();

        Map<String, String> pathParameters = request.getPathParameters();

        switch (httpMethod) {

            case "GET":
                Map<String, String> queryStringParameters = request.getQueryStringParameters();

                String userId = null;

                if (pathParameters != null)
                    userId = pathParameters.get("userId");
                else if (queryStringParameters != null)
                    userId = queryStringParameters.get("userId");

                if (userId == null || userId.length() == 0) {
                    LOGGER.info("Getting all users");
                    userList = userService.findAll();
                    LOGGER.info("GET: " + userList);
                    try {
                        result = mapper.writeValueAsString(userList);
                    } catch (JsonProcessingException exc) {
                        LOGGER.error(exc);
                    }
                } else {
                    user = userService.get(userId);
                    LOGGER.info("GET: " + user);

                    if (user.getUserId() == null)
                        result = "";
                    else {
                        try {
                            result = mapper.writeValueAsString(user);
                        } catch (JsonProcessingException exc) {
                            LOGGER.error(exc);
                        }
                    }
                }
                break;
            case "POST":
                String body = request.getBody();
                try {
                    User tmpUser = mapper.readValue(body, User.class);
                    tmpUser.setUserId(createUserId());

                    LOGGER.info("POST: " + tmpUser);
                    String tmpId = userService.add(tmpUser);

                    result = tmpId;
                }
                catch (JsonProcessingException exc) {
                    LOGGER.error(exc);
                }
                break;
            case "DELETE":
                if (pathParameters != null) {
                    String id = pathParameters.get("userId");
                    String tmpId = userService.delete(id);

                    LOGGER.info("DELETE: " + tmpId);

                    result = tmpId;
                }
                break;
        }

        return new APIGatewayProxyResponseEvent().withBody(result).withStatusCode(200);
    }