Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions takeNotes-src/deleteNote/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// default imports
const AWS = require("aws-sdk");
const DDB = new AWS.DynamoDB({ apiVersion: "2012-10-08" });
const { v4: uuidv4 } = require("uuid");

// environment variables
const { TABLE_NAME, ENDPOINT_OVERRIDE, REGION } = process.env;
const options = { region: REGION };
AWS.config.update({ region: REGION });

if (ENDPOINT_OVERRIDE !== "") {
options.endpoint = ENDPOINT_OVERRIDE;
}

const docClient = new AWS.DynamoDB.DocumentClient(options);

// response helper
const response = (statusCode, body, additionalHeaders) => ({
statusCode,
body: JSON.stringify(body),
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
...additionalHeaders,
},
});

function isValidRequest(context, event) {
let isIdValid =
event !== null &&
event.pathParameters !== null &&
event.pathParameters.id !== null &&
event.pathParameters.noteIdx !== null;

return isIdValid;
}

function updateRecord(recordId, noteIdx) {
let d = new Date();
console.log("record id: " + recordId + " noteIdx: " + noteIdx);
const params = {
TableName: TABLE_NAME,
Key: {
id: recordId,
},
UpdateExpression: `SET updated = :u REMOVE docBody.notes.#noteId`,
ExpressionAttributeNames: { "#noteId": noteIdx },
ExpressionAttributeValues: {
":u": d.toISOString(),
},
ConditionExpression: "attribute_exists(docBody.notes.#noteId)",
ReturnValues: "ALL_NEW",
};
console.log("params: " + params);
return docClient.update(params);
}

// Lambda Handler
exports.deleteNote = async (event, context, callback) => {
console.log("event: " + event);
console.log("body: " + event.body);
if (!isValidRequest(context, event)) {
return response(400, { message: "Error: Invalid request" });
}

try {
let data = await updateRecord(
event.pathParameters.id,
event.pathParameters.noteIdx
).promise();
return response(200, data);
} catch (err) {
return response(400, { message: err.message });
}
};
109 changes: 109 additions & 0 deletions takeNotes-src/deleteNote/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions takeNotes-src/deleteNote/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "post",
"version": "1.0.0",
"description": "POST handler for TakeNotes users API",
"main": "src/app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"style": "npx prettier --write ."
},
"author": "",
"license": "ISC",
"dependencies": {
"aws-sdk": "^2.834.0",
"uuid": "^8.3.2"
}
}
25 changes: 25 additions & 0 deletions template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,31 @@ Resources:
RestApiId: !Ref TakeNotesApi
Auth:
Authorizer: CognitoAuthorizer
DeleteTakeNotesUserNotes:
Type: AWS::Serverless::Function
Properties:
CodeUri: takeNotes-src/deleteNote/
Handler: app.deleteNote
Tracing: Active
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref TakeNotesTable
- CloudWatchPutMetricPolicy: {}
Environment:
Variables:
TABLE_NAME: !Ref TakeNotesTable
AWS_NODEJS_CONNECTION_REUSE_ENABLED: "1"
ENDPOINT_OVERRIDE: ""
REGION: !Ref AWS::Region
Events:
DeleteNote:
Type: Api
Properties:
Path: /users/{id}/notes/{noteIdx}
Method: DELETE
RestApiId: !Ref TakeNotesApi
Auth:
Authorizer: CognitoAuthorizer
DeleteTakeNotesUserEntry:
Type: AWS::Serverless::Function
Properties:
Expand Down