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());
}
}
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");
}
}
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;
}
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());
}
}
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());
}
}
Aggregations