-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.js
More file actions
103 lines (81 loc) · 2.3 KB
/
Copy pathhandler.js
File metadata and controls
103 lines (81 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
'use strict';
const DynamoDB = require('aws-sdk/clients/dynamodb');
const documentClient = new DynamoDB.DocumentClient({ region: 'us-east-1' });
const NOTES_TABLE_NAME = process.env.NOTES_TABLE_NAME;
const send = (statusCode, body) => {
return {
statusCode,
body: JSON.stringify(body),
}};
module.exports.createNotes = async (event, context, callback) => {
context.callbackWaitsForEmptyEventLoop = false;
let data = JSON.parse(event.body);
try {
const params = {
TableName: NOTES_TABLE_NAME,
Item:{
notesId: data.id,
title: data.title,
body: data.body,
},
ConditionExpression : 'attribute_not_exists(notesId)'
}
await documentClient.put(params).promise();
callback(null,send(201, data));
} catch (error) {
callback(null,send(500, error.message));
}
};
module.exports.updateNotes = async (event, context, callback) => {
let noteId = event.pathParameters.id;
let data = JSON.parse(event.body);
try {
const params = {
TableName: NOTES_TABLE_NAME,
Key: {
notesId: noteId
},
UpdateExpression: 'set #title = :title, #body = :body',
ExpressionAttributeNames: {
'#title': 'title',
'#body': 'body'
},
ExpressionAttributeValues: {
':title': data.title,
':body': data.body
},
ConditionExpression: 'attribute_exists(notesId)',
}
await documentClient.update(params).promise();
callback(null,send(200, data));
}
catch (error) {
callback(null, send(500, error.message));
}}
module.exports.deleteNotes = async (event, context, cb) => {
let noteId = event.pathParameters.id;
try {
const params = {
TableName: NOTES_TABLE_NAME,
Key: {
notesId: noteId
},
ConditionExpression: 'attribute_exists(notesId)',
}
await documentClient.delete(params).promise();
cb(null, send(200, `Note with id ${noteId} is deleted`));
} catch (error) {
cb(null, send(500, error.message));
}
};
module.exports.getAllNotes = async (event) => {
try {
const params = {
TableName: NOTES_TABLE_NAME,
};
const result = await documentClient.scan(params).promise();
return send(200, result.Items);
} catch (error) {
return send(500, error.message);
}
};