I have the below AWS lambda function which I have created with the intention of defining soft deleting and hard deleting my product.
exports.handler = async (event) => {
const product = JSON.parse(event.body);
let sql;
let msg;
let values;
if (product.action == 'delete') {
sql = "UPDATE product_table SET isDeleted=1, deletedon=? WHERE product_id=?";
msg = 'The product has been sucessfully deleted';
values = new Date();
} else if (product.action == 'restore') {
sql = "UPDATE product_table SET isDeleted=0, deletedon=? WHERE product_id=?";
msg = 'The product has been successfully restored';
values = null;
}
try {
const data = await new Promise((resolve, reject) => {
connection.getConnection(function (err) {
if (err) {
reject(err);
}
connection.query(sql, [values, product.product_id], function (err, result) {
if (err) {
console.log("Error->" + err);
reject(err);
}
resolve(result);
});
});
});
return {
statusCode: 200,
body: JSON.stringify(msg)
};
} catch (err) {
return {
statusCode: 400,
body: JSON.stringify(err.message)
};
}
};
The idea is to use a single delete method from API and pass id and action values in the JSON object to goggle between delete and restore.
I was able to get this working on Postman, but not from my Angular Application. I am passing the values sam as I would do with POST method. Can you please help me on what I am missing?
Below is the angular code:
deleteProductData(product_id:number,action:string) {
const header: HttpHeaders = new HttpHeaders()
.append('Content-Type', 'application/json; charset=UTF-8')
const httpOptions = {
headers: header,
body:JSON.stringify({ "product_id": product_id,"action":action })
};
return this.http.delete<any>(this.productGetUrl, httpOptions);
}
Found the issues. My configurations were incorrect.
I was trying to send the data for the delete method inside a 'body', not knowing that the delete method only supported queryStringParameters or pathParameters.
I was able to fix the issue by refactoring my backend similar to how I configured a GET request.