-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.js
149 lines (141 loc) · 3.49 KB
/
handler.js
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
'use strict';
const AWS = require('aws-sdk');
const db = new AWS.DynamoDB.DocumentClient({ apiVersion: '2012-08-10' });
const uuid = require('uuid/v4');
const postsTable = process.env.POSTS_TABLE;
// Create a response
function response(statusCode, message) {
return {
statusCode: statusCode,
body: JSON.stringify(message)
};
}
function sortByDate(a, b) {
if (a.createdAt > b.createdAt) {
return -1;
} else return 1;
}
// Create a post
module.exports.createPost = (event, context, callback) => {
const reqBody = JSON.parse(event.body);
if (
!reqBody.title ||
reqBody.title.trim() === '' ||
!reqBody.body ||
reqBody.body.trim() === ''
) {
return callback(
null,
response(400, {
error: 'Post must have a title and body and they must not be empty'
})
);
}
const post = {
id: uuid(),
createdAt: new Date().toISOString(),
userId: 1,
title: reqBody.title,
body: reqBody.body
};
return db
.put({
TableName: postsTable,
Item: post
})
.promise()
.then(() => {
callback(null, response(201, post));
})
.catch((err) => response(null, response(err.statusCode, err)));
};
// Get all posts
module.exports.getAllPosts = (event, context, callback) => {
return db
.scan({
TableName: postsTable
})
.promise()
.then((res) => {
callback(null, response(200, res.Items.sort(sortByDate)));
})
.catch((err) => callback(null, response(err.statusCode, err)));
};
// Get number of posts
module.exports.getPosts = (event, context, callback) => {
const numberOfPosts = event.pathParameters.number;
const params = {
TableName: postsTable,
Limit: numberOfPosts
};
return db
.scan(params)
.promise()
.then((res) => {
callback(null, response(200, res.Items.sort(sortByDate)));
})
.catch((err) => callback(null, response(err.statusCode, err)));
};
// Get a single post
module.exports.getPost = (event, context, callback) => {
const id = event.pathParameters.id;
const params = {
Key: {
id: id
},
TableName: postsTable
};
return db
.get(params)
.promise()
.then((res) => {
if (res.Item) callback(null, response(200, res.Item));
else callback(null, response(404, { error: 'Post not found' }));
})
.catch((err) => callback(null, response(err.statusCode, err)));
};
// Update a post
module.exports.updatePost = (event, context, callback) => {
const id = event.pathParameters.id;
const reqBody = JSON.parse(event.body);
const { body, title } = reqBody;
const params = {
Key: {
id: id
},
TableName: postsTable,
ConditionExpression: 'attribute_exists(id)',
UpdateExpression: 'SET title = :title, body = :body',
ExpressionAttributeValues: {
':title': title,
':body': body
},
ReturnValues: 'ALL_NEW'
};
console.log('Updating');
return db
.update(params)
.promise()
.then((res) => {
console.log(res);
callback(null, response(200, res.Attributes));
})
.catch((err) => callback(null, response(err.statusCode, err)));
};
// Delete a post
module.exports.deletePost = (event, context, callback) => {
const id = event.pathParameters.id;
const params = {
Key: {
id: id
},
TableName: postsTable
};
return db
.delete(params)
.promise()
.then(() =>
callback(null, response(200, { message: 'Post deleted successfully' }))
)
.catch((err) => callback(null, response(err.statusCode, err)));
};