From 02ea958ec2f53fe835afa6c3d4a44a493185ce80 Mon Sep 17 00:00:00 2001 From: KrishnaSolo Date: Fri, 12 Feb 2021 12:21:23 -0500 Subject: [PATCH 01/12] Added handler for put notes request --- takeNotes-src/putNotes/.prettierignore | 106 ++++++++++++++++++++++ takeNotes-src/putNotes/app.js | 72 +++++++++++++++ takeNotes-src/putNotes/package-lock.json | 109 +++++++++++++++++++++++ takeNotes-src/putNotes/package.json | 16 ++++ template.yaml | 25 ++++++ 5 files changed, 328 insertions(+) create mode 100644 takeNotes-src/putNotes/.prettierignore create mode 100644 takeNotes-src/putNotes/app.js create mode 100644 takeNotes-src/putNotes/package-lock.json create mode 100644 takeNotes-src/putNotes/package.json diff --git a/takeNotes-src/putNotes/.prettierignore b/takeNotes-src/putNotes/.prettierignore new file mode 100644 index 0000000..195a343 --- /dev/null +++ b/takeNotes-src/putNotes/.prettierignore @@ -0,0 +1,106 @@ +*.DS_Store +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +lerna-debug.log* +*.json + +# Diagnostic reports (https://nodejs.org/api/report.html) +report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage +*.lcov + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# TypeScript v1 declaration files +typings/ + +# TypeScript cache +*.tsbuildinfo + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Microbundle cache +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variables file +.env +.env.test + +# parcel-bundler cache (https://parceljs.org/) +.cache + +# Next.js build output +.next + +# Nuxt.js build / generate output +.nuxt +dist + +# Gatsby files +.cache/ +# Comment in the public line in if your project uses Gatsby and *not* Next.js +# https://nextjs.org/blog/next-9-1#public-directory-support +# public + +# vuepress build output +.vuepress/dist + +# Serverless directories +.serverless/ + +# FuseBox cache +.fusebox/ + +# DynamoDB Local files +.dynamodb/ + +# TernJS port file +.tern-port \ No newline at end of file diff --git a/takeNotes-src/putNotes/app.js b/takeNotes-src/putNotes/app.js new file mode 100644 index 0000000..2143c0f --- /dev/null +++ b/takeNotes-src/putNotes/app.js @@ -0,0 +1,72 @@ +// default imports +const AWS = require("aws-sdk"); +const DDB = new AWS.DynamoDB({ apiVersion: "2012-10-08" }); + +// 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; + + let body = event.body; + let isBodyValid = body !== null && body.notes !== null; + + return isIdValid && isBodyValid; +} + +function updateRecord(recordId, eventBody) { + let d = new Date(); + const params = { + TableName: TABLE_NAME, + Key: { + id: recordId, + }, + UpdateExpression: "set updated = :u, docBody.notes = :n", + ExpressionAttributeValues: { + ":u": d.toISOString(), + ":n": eventBody.notes, + }, + ReturnValues: "ALL_NEW", + }; + + return docClient.update(params); +} + +// Lambda Handler +exports.putNotes = async (event, context, callback) => { + if (!isValidRequest(context, event)) { + return response(400, { message: "Error: Invalid request" }); + } + + try { + let data = await updateRecord( + event.pathParameters.id, + JSON.parse(event.body) + ).promise(); + return response(200, data); + } catch (err) { + return response(400, { message: err.message }); + } +}; diff --git a/takeNotes-src/putNotes/package-lock.json b/takeNotes-src/putNotes/package-lock.json new file mode 100644 index 0000000..3151d88 --- /dev/null +++ b/takeNotes-src/putNotes/package-lock.json @@ -0,0 +1,109 @@ +{ + "name": "post", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "aws-sdk": { + "version": "2.834.0", + "resolved": "https://registry.npmjs.org/aws-sdk/-/aws-sdk-2.834.0.tgz", + "integrity": "sha512-9WRULrn4qAmgXI+tEW/IG5s/6ixJGZqjPOrmJsFZQev7/WRkxAZmJAjcwd4Ifm/jsJbXx2FSwO76gOPEvu2LqA==", + "requires": { + "buffer": "4.9.2", + "events": "1.1.1", + "ieee754": "1.1.13", + "jmespath": "0.15.0", + "querystring": "0.2.0", + "sax": "1.2.1", + "url": "0.10.3", + "uuid": "3.3.2", + "xml2js": "0.4.19" + }, + "dependencies": { + "uuid": { + "version": "3.3.2", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.3.2.tgz", + "integrity": "sha512-yXJmeNaw3DnnKAOKJE51sL/ZaYfWJRl1pK9dr19YFCu0ObS231AB1/LbqTKRAQ5kw8A90rA6fr4riOUpTZvQZA==" + } + } + }, + "base64-js": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz", + "integrity": "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==" + }, + "buffer": { + "version": "4.9.2", + "resolved": "https://registry.npmjs.org/buffer/-/buffer-4.9.2.tgz", + "integrity": "sha512-xq+q3SRMOxGivLhBNaUdC64hDTQwejJ+H0T/NB1XMtTVEwNTrfFF3gAxiyW0Bu/xWEGhjVKgUcMhCrUy2+uCWg==", + "requires": { + "base64-js": "^1.0.2", + "ieee754": "^1.1.4", + "isarray": "^1.0.0" + } + }, + "events": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/events/-/events-1.1.1.tgz", + "integrity": "sha1-nr23Y1rQmccNzEwqH1AEKI6L2SQ=" + }, + "ieee754": { + "version": "1.1.13", + "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.1.13.tgz", + "integrity": "sha512-4vf7I2LYV/HaWerSo3XmlMkp5eZ83i+/CDluXi/IGTs/O1sejBNhTtnxzmRZfvOUqj7lZjqHkeTvpgSFDlWZTg==" + }, + "isarray": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", + "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=" + }, + "jmespath": { + "version": "0.15.0", + "resolved": "https://registry.npmjs.org/jmespath/-/jmespath-0.15.0.tgz", + "integrity": "sha1-o/Iiqarp+Wb10nx5ZRDigJF2Qhc=" + }, + "punycode": { + "version": "1.3.2", + "resolved": "https://registry.npmjs.org/punycode/-/punycode-1.3.2.tgz", + "integrity": "sha1-llOgNvt8HuQjQvIyXM7v6jkmxI0=" + }, + "querystring": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/querystring/-/querystring-0.2.0.tgz", + "integrity": "sha1-sgmEkgO7Jd+CDadW50cAWHhSFiA=" + }, + "sax": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/sax/-/sax-1.2.1.tgz", + "integrity": "sha1-e45lYZCyKOgaZq6nSEgNgozS03o=" + }, + "url": { + "version": "0.10.3", + "resolved": "https://registry.npmjs.org/url/-/url-0.10.3.tgz", + "integrity": "sha1-Ah5NnHcF8hu/N9A861h2dAJ3TGQ=", + "requires": { + "punycode": "1.3.2", + "querystring": "0.2.0" + } + }, + "uuid": { + "version": "8.3.2", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", + "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==" + }, + "xml2js": { + "version": "0.4.19", + "resolved": "https://registry.npmjs.org/xml2js/-/xml2js-0.4.19.tgz", + "integrity": "sha512-esZnJZJOiJR9wWKMyuvSE1y6Dq5LCuJanqhxslH2bxM6duahNZ+HMpCLhBQGZkbX6xRf8x1Y2eJlgt2q3qo49Q==", + "requires": { + "sax": ">=0.6.0", + "xmlbuilder": "~9.0.1" + } + }, + "xmlbuilder": { + "version": "9.0.7", + "resolved": "https://registry.npmjs.org/xmlbuilder/-/xmlbuilder-9.0.7.tgz", + "integrity": "sha1-Ey7mPS7FVlxVfiD0wi35rKaGsQ0=" + } + } +} diff --git a/takeNotes-src/putNotes/package.json b/takeNotes-src/putNotes/package.json new file mode 100644 index 0000000..9202874 --- /dev/null +++ b/takeNotes-src/putNotes/package.json @@ -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" + } +} diff --git a/template.yaml b/template.yaml index 34e66a4..9d374e0 100644 --- a/template.yaml +++ b/template.yaml @@ -199,6 +199,31 @@ Resources: RestApiId: !Ref TakeNotesApi Auth: Authorizer: CognitoAuthorizer + PutTakeNotesUserNotes: + Type: AWS::Serverless::Function + Properties: + CodeUri: takeNotes-src/putNotes/ + Handler: app.putNotes + 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: + PutJournal: + Type: Api + Properties: + Path: /users/{id}/notes + Method: PUT + RestApiId: !Ref TakeNotesApi + Auth: + Authorizer: CognitoAuthorizer PutTakeNotesUserWeek: Type: AWS::Serverless::Function Properties: From 134ebb351b330bdb1d10e0fbd8ae10a8c22b6753 Mon Sep 17 00:00:00 2001 From: KrishnaSolo Date: Fri, 19 Feb 2021 23:37:10 -0500 Subject: [PATCH 02/12] Updated notes to use ID now and added logger --- takeNotes-src/postUser/app.js | 34 ++++++++++++++-------------------- takeNotes-src/putNotes/app.js | 3 ++- template.yaml | 2 +- 3 files changed, 17 insertions(+), 22 deletions(-) diff --git a/takeNotes-src/postUser/app.js b/takeNotes-src/postUser/app.js index e2ddca9..4529d45 100644 --- a/takeNotes-src/postUser/app.js +++ b/takeNotes-src/postUser/app.js @@ -28,6 +28,9 @@ const response = (statusCode, body, additionalHeaders) => ({ }, }); +//logger helper +const logger = (valueName, value) => console.log(`${valueName}: ${value}`); + function isValidRequest(context, event) { const body = JSON.parse(event.body); return ( @@ -36,8 +39,8 @@ function isValidRequest(context, event) { body.cognitoId !== null && body.email !== null && body.startDate !== null && - body.endDate !== null - ) + body.endDate !== null + ); } let getDateFromISO = (date) => new Date(date); @@ -50,8 +53,7 @@ let getNumWeeks = (start, end) => { let getWeeks = (start, end) => { let numWeeks = getNumWeeks(start, end); - console.log("numWeeks"); - console.log(numWeeks); + logger("numweeks", numWeeks); let dateString = new Date().toISOString(); let weeks = new Array(numWeeks); for (var i = 0; i < numWeeks; i++) { @@ -70,10 +72,7 @@ let getWeeks = (start, end) => { * @return {object} */ let generateDoc = (attributes) => { - let [start, end] = [ - attributes.startDate, - attributes.endDate - ]; + let [start, end] = [attributes.startDate, attributes.endDate]; let weeks = getWeeks(start, end); return { email: attributes.email, @@ -82,7 +81,7 @@ let generateDoc = (attributes) => { journal: { weeks: weeks, }, - notes: [], + notes: {}, }; }; @@ -104,8 +103,7 @@ function addRecord(event) { Item: itemBody, ReturnValues: "ALL_OLD", }; - console.log("params"); - console.log(params); + logger("params", params); // Return the new object return [docClient.put(params), params]; @@ -113,12 +111,9 @@ function addRecord(event) { // Lambda Handler exports.postUser = async (event, context, callback) => { - console.log("event"); - console.log(event); - console.log("event type"); - console.log(typeof(event)); - console.log("callback"); - console.log(callback); + logger("event", event); + logger("event type", typeof event); + logger("callback", callback); if (!isValidRequest(context, event)) { return response(400, { message: "Error: Invalid request" }); } @@ -129,11 +124,10 @@ exports.postUser = async (event, context, callback) => { return response(200, { promise: dbPromise, - input: dbInput + input: dbInput, }); } catch (err) { - console.log("err"); - console.log(err.message); + logger("error", err.message); return response(500, { message: err.message }); } }; diff --git a/takeNotes-src/putNotes/app.js b/takeNotes-src/putNotes/app.js index 2143c0f..ec5936b 100644 --- a/takeNotes-src/putNotes/app.js +++ b/takeNotes-src/putNotes/app.js @@ -1,6 +1,7 @@ // default imports const AWS = require("aws-sdk"); const DDB = new AWS.DynamoDB({ apiVersion: "2012-10-08" }); +import { v4 as uuidv4 } from "uuid"; // environment variables const { TABLE_NAME, ENDPOINT_OVERRIDE, REGION } = process.env; @@ -43,7 +44,7 @@ function updateRecord(recordId, eventBody) { Key: { id: recordId, }, - UpdateExpression: "set updated = :u, docBody.notes = :n", + UpdateExpression: `set updated = :u, docBody.notes.${uuidv4()} = :n`, ExpressionAttributeValues: { ":u": d.toISOString(), ":n": eventBody.notes, diff --git a/template.yaml b/template.yaml index 9d374e0..261ae53 100644 --- a/template.yaml +++ b/template.yaml @@ -216,7 +216,7 @@ Resources: ENDPOINT_OVERRIDE: "" REGION: !Ref AWS::Region Events: - PutJournal: + PutNotes: Type: Api Properties: Path: /users/{id}/notes From 4b91abe02ea0aeebbece4554ee2789ee0db58d37 Mon Sep 17 00:00:00 2001 From: KrishnaSolo Date: Sat, 20 Feb 2021 00:05:40 -0500 Subject: [PATCH 03/12] fixed uuid import --- takeNotes-src/putNotes/app.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/takeNotes-src/putNotes/app.js b/takeNotes-src/putNotes/app.js index ec5936b..723e074 100644 --- a/takeNotes-src/putNotes/app.js +++ b/takeNotes-src/putNotes/app.js @@ -1,7 +1,7 @@ // default imports const AWS = require("aws-sdk"); const DDB = new AWS.DynamoDB({ apiVersion: "2012-10-08" }); -import { v4 as uuidv4 } from "uuid"; +const { v4: uuidv4 } = require("uuid"); // environment variables const { TABLE_NAME, ENDPOINT_OVERRIDE, REGION } = process.env; From cf896edcf724997467dade166bbdd9e05e5d9e95 Mon Sep 17 00:00:00 2001 From: KrishnaSolo Date: Sat, 20 Feb 2021 00:29:15 -0500 Subject: [PATCH 04/12] aded logging --- takeNotes-src/putNotes/app.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/takeNotes-src/putNotes/app.js b/takeNotes-src/putNotes/app.js index 723e074..d2552d9 100644 --- a/takeNotes-src/putNotes/app.js +++ b/takeNotes-src/putNotes/app.js @@ -57,6 +57,8 @@ function updateRecord(recordId, eventBody) { // Lambda Handler exports.putNotes = async (event, context, callback) => { + console.log("event: " + event); + console.log("body: " + event.body); if (!isValidRequest(context, event)) { return response(400, { message: "Error: Invalid request" }); } From 19c72269f639aedf887805e2e3ebed202f1592c7 Mon Sep 17 00:00:00 2001 From: KrishnaSolo Date: Sat, 20 Feb 2021 00:46:09 -0500 Subject: [PATCH 05/12] Updated id to using expressions values --- takeNotes-src/putNotes/app.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/takeNotes-src/putNotes/app.js b/takeNotes-src/putNotes/app.js index d2552d9..6b5a67b 100644 --- a/takeNotes-src/putNotes/app.js +++ b/takeNotes-src/putNotes/app.js @@ -44,14 +44,15 @@ function updateRecord(recordId, eventBody) { Key: { id: recordId, }, - UpdateExpression: `set updated = :u, docBody.notes.${uuidv4()} = :n`, + UpdateExpression: `set updated = :u, docBody.notes.#pr = :n`, ExpressionAttributeValues: { + "#pr": uuidv4().replace("-", ""), ":u": d.toISOString(), ":n": eventBody.notes, }, ReturnValues: "ALL_NEW", }; - + console.log("params: " + params); return docClient.update(params); } From f5bfabfa0ad5c7050d8a37c5f248dad256a0c5d4 Mon Sep 17 00:00:00 2001 From: KrishnaSolo Date: Sat, 20 Feb 2021 01:01:34 -0500 Subject: [PATCH 06/12] more logging --- takeNotes-src/putNotes/app.js | 1 + 1 file changed, 1 insertion(+) diff --git a/takeNotes-src/putNotes/app.js b/takeNotes-src/putNotes/app.js index 6b5a67b..1d773ea 100644 --- a/takeNotes-src/putNotes/app.js +++ b/takeNotes-src/putNotes/app.js @@ -39,6 +39,7 @@ function isValidRequest(context, event) { function updateRecord(recordId, eventBody) { let d = new Date(); + console.log("record id: " + recordId + " eventBody: " + eventBody.notes); const params = { TableName: TABLE_NAME, Key: { From dc58ee59ae4f323ad6121a47d3011b43a43b14cb Mon Sep 17 00:00:00 2001 From: KrishnaSolo Date: Sat, 20 Feb 2021 01:02:30 -0500 Subject: [PATCH 07/12] trying hardcoded value --- takeNotes-src/putNotes/app.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/takeNotes-src/putNotes/app.js b/takeNotes-src/putNotes/app.js index 1d773ea..f0df54d 100644 --- a/takeNotes-src/putNotes/app.js +++ b/takeNotes-src/putNotes/app.js @@ -47,7 +47,7 @@ function updateRecord(recordId, eventBody) { }, UpdateExpression: `set updated = :u, docBody.notes.#pr = :n`, ExpressionAttributeValues: { - "#pr": uuidv4().replace("-", ""), + "#pr": "idk", ":u": d.toISOString(), ":n": eventBody.notes, }, From a308d069fc99ecb7cd86d7da0f1776e3dfebe32a Mon Sep 17 00:00:00 2001 From: KrishnaSolo Date: Sat, 20 Feb 2021 01:16:14 -0500 Subject: [PATCH 08/12] fixed incorrect update --- takeNotes-src/putNotes/app.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/takeNotes-src/putNotes/app.js b/takeNotes-src/putNotes/app.js index f0df54d..c6253b7 100644 --- a/takeNotes-src/putNotes/app.js +++ b/takeNotes-src/putNotes/app.js @@ -46,11 +46,12 @@ function updateRecord(recordId, eventBody) { id: recordId, }, UpdateExpression: `set updated = :u, docBody.notes.#pr = :n`, + ExpressionAttributeNames = { "#pr" : "idk" }, ExpressionAttributeValues: { - "#pr": "idk", ":u": d.toISOString(), ":n": eventBody.notes, }, + ConditionExpression = "attribute_not_exists(docBody.notes.#pr)", ReturnValues: "ALL_NEW", }; console.log("params: " + params); From 0ac45634e20cb5fcb775ec47d0928930cfb7644f Mon Sep 17 00:00:00 2001 From: KrishnaSolo Date: Sat, 20 Feb 2021 01:27:20 -0500 Subject: [PATCH 09/12] fixed incorrect update statment --- takeNotes-src/putNotes/app.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/takeNotes-src/putNotes/app.js b/takeNotes-src/putNotes/app.js index c6253b7..cebe309 100644 --- a/takeNotes-src/putNotes/app.js +++ b/takeNotes-src/putNotes/app.js @@ -46,12 +46,12 @@ function updateRecord(recordId, eventBody) { id: recordId, }, UpdateExpression: `set updated = :u, docBody.notes.#pr = :n`, - ExpressionAttributeNames = { "#pr" : "idk" }, + ExpressionAttributeNames: { "#pr": "idk" }, ExpressionAttributeValues: { ":u": d.toISOString(), ":n": eventBody.notes, }, - ConditionExpression = "attribute_not_exists(docBody.notes.#pr)", + ConditionExpression: "attribute_not_exists(docBody.notes.#pr)", ReturnValues: "ALL_NEW", }; console.log("params: " + params); From b6814b4ea31afe57370e7b94d1eb3ffa786ae22b Mon Sep 17 00:00:00 2001 From: KrishnaSolo Date: Sat, 20 Feb 2021 01:34:11 -0500 Subject: [PATCH 10/12] fix id generation --- takeNotes-src/putNotes/app.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/takeNotes-src/putNotes/app.js b/takeNotes-src/putNotes/app.js index cebe309..57d952c 100644 --- a/takeNotes-src/putNotes/app.js +++ b/takeNotes-src/putNotes/app.js @@ -46,7 +46,7 @@ function updateRecord(recordId, eventBody) { id: recordId, }, UpdateExpression: `set updated = :u, docBody.notes.#pr = :n`, - ExpressionAttributeNames: { "#pr": "idk" }, + ExpressionAttributeNames: { "#pr": uuidv4().replace("-", "") }, ExpressionAttributeValues: { ":u": d.toISOString(), ":n": eventBody.notes, From c557020ab92f01ded9589990e39a39cdbaa8dd31 Mon Sep 17 00:00:00 2001 From: KrishnaSolo Date: Sat, 20 Feb 2021 01:42:28 -0500 Subject: [PATCH 11/12] updated namings --- takeNotes-src/putNotes/app.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/takeNotes-src/putNotes/app.js b/takeNotes-src/putNotes/app.js index 57d952c..e030c66 100644 --- a/takeNotes-src/putNotes/app.js +++ b/takeNotes-src/putNotes/app.js @@ -37,7 +37,7 @@ function isValidRequest(context, event) { return isIdValid && isBodyValid; } -function updateRecord(recordId, eventBody) { +function updateRecord(recordId, eventBody, id) { let d = new Date(); console.log("record id: " + recordId + " eventBody: " + eventBody.notes); const params = { @@ -45,13 +45,13 @@ function updateRecord(recordId, eventBody) { Key: { id: recordId, }, - UpdateExpression: `set updated = :u, docBody.notes.#pr = :n`, - ExpressionAttributeNames: { "#pr": uuidv4().replace("-", "") }, + UpdateExpression: `set updated = :u, docBody.notes.#noteId = :n`, + ExpressionAttributeNames: { "#noteId": id }, ExpressionAttributeValues: { ":u": d.toISOString(), ":n": eventBody.notes, }, - ConditionExpression: "attribute_not_exists(docBody.notes.#pr)", + ConditionExpression: "attribute_not_exists(docBody.notes.#noteId)", ReturnValues: "ALL_NEW", }; console.log("params: " + params); @@ -67,11 +67,13 @@ exports.putNotes = async (event, context, callback) => { } try { + const id = uuidv4(); let data = await updateRecord( event.pathParameters.id, - JSON.parse(event.body) + JSON.parse(event.body), + id ).promise(); - return response(200, data); + return response(200, { data: data, id: id }); } catch (err) { return response(400, { message: err.message }); } From fdc157665e88181821941cc0d35d0f9b4a1d959f Mon Sep 17 00:00:00 2001 From: KrishnaSolo Date: Sun, 21 Feb 2021 14:31:26 -0500 Subject: [PATCH 12/12] updated put to only do updates --- takeNotes-src/putNotes/app.js | 11 +++++------ template.yaml | 2 +- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/takeNotes-src/putNotes/app.js b/takeNotes-src/putNotes/app.js index e030c66..78e7785 100644 --- a/takeNotes-src/putNotes/app.js +++ b/takeNotes-src/putNotes/app.js @@ -37,7 +37,7 @@ function isValidRequest(context, event) { return isIdValid && isBodyValid; } -function updateRecord(recordId, eventBody, id) { +function updateRecord(recordId, eventBody, noteIdx) { let d = new Date(); console.log("record id: " + recordId + " eventBody: " + eventBody.notes); const params = { @@ -46,12 +46,12 @@ function updateRecord(recordId, eventBody, id) { id: recordId, }, UpdateExpression: `set updated = :u, docBody.notes.#noteId = :n`, - ExpressionAttributeNames: { "#noteId": id }, + ExpressionAttributeNames: { "#noteId": noteIdx }, ExpressionAttributeValues: { ":u": d.toISOString(), ":n": eventBody.notes, }, - ConditionExpression: "attribute_not_exists(docBody.notes.#noteId)", + ConditionExpression: "attribute_exists(docBody.notes.#noteId)", ReturnValues: "ALL_NEW", }; console.log("params: " + params); @@ -67,13 +67,12 @@ exports.putNotes = async (event, context, callback) => { } try { - const id = uuidv4(); let data = await updateRecord( event.pathParameters.id, JSON.parse(event.body), - id + event.pathParameters.noteIdx ).promise(); - return response(200, { data: data, id: id }); + return response(200, data); } catch (err) { return response(400, { message: err.message }); } diff --git a/template.yaml b/template.yaml index 261ae53..b24f792 100644 --- a/template.yaml +++ b/template.yaml @@ -219,7 +219,7 @@ Resources: PutNotes: Type: Api Properties: - Path: /users/{id}/notes + Path: /users/{id}/notes/{noteIdx} Method: PUT RestApiId: !Ref TakeNotesApi Auth: