-
-
Save richardgrantserverless/7e56420bb8bb1dc2e3a74111bc1108ff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.serverless; | |
... | |
import com.serverless.dal.Product; | |
public class DeleteProductHandler implements RequestHandler<Map<String, Object>, ApiGatewayResponse> { | |
private final Logger logger = Logger.getLogger(this.getClass()); | |
@Override | |
public ApiGatewayResponse handleRequest(Map<String, Object> input, Context context) { | |
try { | |
// get the 'pathParameters' from input | |
Map<String,String> pathParameters = (Map<String,String>)input.get("pathParameters"); | |
String productId = pathParameters.get("id"); | |
// get the Product by id | |
Boolean success = new Product().delete(productId); | |
// send the response back | |
if (success) { | |
return ApiGatewayResponse.builder() | |
.setStatusCode(204) | |
.setHeaders(Collections.singletonMap("X-Powered-By", "AWS Lambda & Serverless")) | |
.build(); | |
} else { | |
return ApiGatewayResponse.builder() | |
.setStatusCode(404) | |
.setObjectBody("Product with id: '" + productId + "' not found.") | |
.setHeaders(Collections.singletonMap("X-Powered-By", "AWS Lambda & Serverless")) | |
.build(); | |
} | |
} catch (Exception ex) { | |
logger.error("Error in deleting product: " + ex); | |
// send the error response back | |
... | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment