Search in sources :

Example 1 with Product

use of software.amazonaws.example.product.product.entity.Product in project serverless-java-frameworks-samples by aws-samples.

the class CreateProductFunction method apply.

@Override
public APIGatewayProxyResponseEvent apply(APIGatewayProxyRequestEvent requestEvent) {
    if (!requestEvent.getHttpMethod().equals(HttpMethod.PUT.name())) {
        return new APIGatewayProxyResponseEvent().withStatusCode(HttpStatusCode.METHOD_NOT_ALLOWED).withBody("Only PUT method is supported");
    }
    try {
        String id = requestEvent.getPathParameters().get("id");
        String jsonPayload = requestEvent.getBody();
        Product product = objectMapper.readValue(jsonPayload, Product.class);
        if (!product.getId().equals(id)) {
            return new APIGatewayProxyResponseEvent().withStatusCode(HttpStatusCode.BAD_REQUEST).withBody("Product ID in the body does not match path parameter");
        }
        productDao.putProduct(product);
        return new APIGatewayProxyResponseEvent().withStatusCode(HttpStatusCode.CREATED).withBody("Product with id = " + id + " created");
    } catch (Exception e) {
        e.printStackTrace();
        return new APIGatewayProxyResponseEvent().withStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR).withBody("Internal Server Error :: " + e.getMessage());
    }
}
Also used : Product(software.amazonaws.example.product.product.entity.Product) APIGatewayProxyResponseEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent)

Example 2 with Product

use of software.amazonaws.example.product.product.entity.Product in project serverless-java-frameworks-samples by aws-samples.

the class CreateProductHandler method handleRequest.

public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent requestEvent, Context context) {
    if (!requestEvent.getHttpMethod().equals(SdkHttpMethod.PUT.name())) {
        return new APIGatewayProxyResponseEvent().withStatusCode(HttpStatusCode.METHOD_NOT_ALLOWED).withBody("Only PUT method is supported");
    }
    try {
        String id = requestEvent.getPathParameters().get("id");
        String jsonPayload = requestEvent.getBody();
        Product product = objectMapper.readValue(jsonPayload, Product.class);
        if (!product.getId().equals(id)) {
            return new APIGatewayProxyResponseEvent().withStatusCode(HttpStatusCode.BAD_REQUEST).withBody("Product ID in the body does not match path parameter");
        }
        productDao.putProduct(product);
        return new APIGatewayProxyResponseEvent().withStatusCode(HttpStatusCode.CREATED).withBody("Product with id = " + id + " created");
    } catch (Exception e) {
        e.printStackTrace();
        return new APIGatewayProxyResponseEvent().withStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR).withBody("Internal Server Error");
    }
}
Also used : Product(software.amazonaws.example.product.product.entity.Product) APIGatewayProxyResponseEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent)

Example 3 with Product

use of software.amazonaws.example.product.product.entity.Product in project serverless-java-frameworks-samples by aws-samples.

the class ProductMapper method productFromDynamoDB.

public static Product productFromDynamoDB(Map<String, AttributeValue> items) {
    Product product = new Product();
    product.setId(items.get(PK).s());
    product.setName(items.get(NAME).s());
    product.setPrice(new BigDecimal(items.get(PRICE).n()));
    return product;
}
Also used : Product(software.amazonaws.example.product.product.entity.Product) BigDecimal(java.math.BigDecimal)

Example 4 with Product

use of software.amazonaws.example.product.product.entity.Product in project serverless-java-frameworks-samples by aws-samples.

the class DeleteProductFunction method apply.

@Override
public APIGatewayProxyResponseEvent apply(APIGatewayProxyRequestEvent requestEvent) {
    if (!requestEvent.getHttpMethod().equals(HttpMethod.DELETE.name())) {
        return new APIGatewayProxyResponseEvent().withStatusCode(HttpStatusCode.METHOD_NOT_ALLOWED).withBody("Only DELETE method is supported");
    }
    try {
        String id = requestEvent.getPathParameters().get("id");
        Optional<Product> product = productDao.getProduct(id);
        if (product.isEmpty()) {
            return new APIGatewayProxyResponseEvent().withStatusCode(HttpStatusCode.NOT_FOUND).withBody("Product with id = " + id + " not found");
        }
        productDao.deleteProduct(id);
        return new APIGatewayProxyResponseEvent().withStatusCode(HttpStatusCode.OK).withBody("Product with id = " + id + " deleted");
    } catch (Exception je) {
        je.printStackTrace();
        return new APIGatewayProxyResponseEvent().withStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR).withBody("Internal Server Error :: " + je.getMessage());
    }
}
Also used : Product(software.amazonaws.example.product.product.entity.Product) APIGatewayProxyResponseEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent)

Example 5 with Product

use of software.amazonaws.example.product.product.entity.Product in project serverless-java-frameworks-samples by aws-samples.

the class GetProductByIdFunction method apply.

@Override
public APIGatewayProxyResponseEvent apply(APIGatewayProxyRequestEvent requestEvent) {
    if (!requestEvent.getHttpMethod().equals(HttpMethod.GET.name())) {
        return new APIGatewayProxyResponseEvent().withStatusCode(HttpStatusCode.METHOD_NOT_ALLOWED).withBody("Only GET method is supported");
    }
    try {
        String id = requestEvent.getPathParameters().get("id");
        Optional<Product> product = productDao.getProduct(id);
        if (product.isEmpty()) {
            return new APIGatewayProxyResponseEvent().withStatusCode(HttpStatusCode.NOT_FOUND).withBody("Product with id = " + id + " not found");
        }
        return new APIGatewayProxyResponseEvent().withStatusCode(HttpStatusCode.OK).withBody(objectMapper.writeValueAsString(product.get()));
    } catch (Exception je) {
        je.printStackTrace();
        return new APIGatewayProxyResponseEvent().withStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR).withBody("Internal Server Error :: " + je.getMessage());
    }
}
Also used : Product(software.amazonaws.example.product.product.entity.Product) APIGatewayProxyResponseEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent)

Aggregations

Product (software.amazonaws.example.product.product.entity.Product)7 APIGatewayProxyResponseEvent (com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent)6 BigDecimal (java.math.BigDecimal)1