|
1 | | -const express = require("express"); |
2 | | -const routes = express(); |
3 | | -const bodyParser = require("body-parser"); |
4 | 1 | const AWS = require("aws-sdk"); |
5 | 2 | const uuid = require("uuid"); |
6 | 3 |
|
7 | | -const router = express.Router(); |
8 | | - |
9 | 4 | const dynamoDb = new AWS.DynamoDB.DocumentClient(); |
10 | 5 |
|
11 | 6 | const tableParams = { |
12 | 7 | TableName: process.env.DYNAMODB_TABLE |
13 | 8 | }; |
14 | 9 |
|
15 | | -router |
16 | | - .route("/todos") |
17 | | - .get(function list(req, res) { |
18 | | - dynamoDb.scan(tableParams, (error, result) => { |
19 | | - if (error) { |
20 | | - console.error(error); |
21 | | - res |
22 | | - .statusCode(error.statusCode || 501) |
23 | | - .send({ error: "Couldn't fetch the todos." }); |
24 | | - } else { |
25 | | - res.send(result.Items); |
26 | | - } |
27 | | - }); |
28 | | - }) |
29 | | - .post(function create(req, res) { |
30 | | - const timestamp = new Date().getTime(); |
31 | | - const { text } = req.body; |
32 | | - |
33 | | - if (typeof text !== "string") { |
34 | | - res.status(400).send({ error: "Couldn't create todo item." }); |
| 10 | +const listItem = (req, res) => { |
| 11 | + dynamoDb.scan(tableParams, (error, result) => { |
| 12 | + if (error) { |
| 13 | + console.error(error); |
| 14 | + res |
| 15 | + .statusCode(error.statusCode || 501) |
| 16 | + .send({ error: "Couldn't fetch the todos." }); |
| 17 | + } else { |
| 18 | + res.send(result.Items); |
| 19 | + } |
| 20 | + }); |
| 21 | +}; |
| 22 | + |
| 23 | +const createItem = (req, res) => { |
| 24 | + const timestamp = new Date().getTime(); |
| 25 | + const { text } = req.body; |
| 26 | + |
| 27 | + if (typeof text !== "string") { |
| 28 | + res.status(400).send({ error: "Couldn't create todo item." }); |
| 29 | + } |
| 30 | + |
| 31 | + const Item = { |
| 32 | + id: uuid.v1(), |
| 33 | + text: text, |
| 34 | + checked: false, |
| 35 | + createAt: timestamp, |
| 36 | + updateAt: timestamp |
| 37 | + }; |
| 38 | + |
| 39 | + const params = Object.assign({}, tableParams, { Item: Item }); |
| 40 | + |
| 41 | + dynamoDb.put(params, error => { |
| 42 | + if (error) { |
| 43 | + console.error(error); |
| 44 | + res.status(400).send("Couldn't create todo item."); |
| 45 | + } else { |
| 46 | + res.send(params); |
| 47 | + } |
| 48 | + }); |
| 49 | +}; |
| 50 | + |
| 51 | +const getItem = (req, res) => { |
| 52 | + const params = Object.assign({}, tableParams, { |
| 53 | + Key: { |
| 54 | + id: req.params.id |
| 55 | + } |
| 56 | + }); |
| 57 | + |
| 58 | + dynamoDb.get(params, (error, result) => { |
| 59 | + if (error) { |
| 60 | + console.error(error); |
| 61 | + res.status(400).send({ error: "Could not get todo item" }); |
| 62 | + } |
| 63 | + |
| 64 | + if (result.Item) { |
| 65 | + res.json(result.Item); |
| 66 | + } else { |
| 67 | + res.status(400).send({ error: "Item not found" }); |
35 | 68 | } |
| 69 | + }); |
| 70 | +}; |
36 | 71 |
|
37 | | - const Item = { |
38 | | - id: uuid.v1(), |
39 | | - text: text, |
40 | | - checked: false, |
41 | | - createAt: timestamp, |
42 | | - updateAt: timestamp |
43 | | - }; |
44 | | - |
45 | | - const params = Object.assign({}, tableParams, { Item: Item }); |
46 | | - |
47 | | - dynamoDb.put(params, error => { |
48 | | - if (error) { |
49 | | - console.error(error); |
50 | | - res.status(400).send("Couldn't create todo item."); |
51 | | - } else { |
52 | | - res.send(params); |
53 | | - } |
54 | | - }); |
| 72 | +const editItem = (req, res) => { |
| 73 | + const timestamp = new Date().getTime(); |
| 74 | + const { text, checked } = req.body; |
| 75 | + |
| 76 | + // validation |
| 77 | + if (typeof text !== "string" || typeof checked !== "boolean") { |
| 78 | + console.error("Validation failed"); |
| 79 | + res.status(400).send({ error: "Validation failed" }); |
| 80 | + } |
| 81 | + |
| 82 | + const params = Object.assign({}, tableParams, { |
| 83 | + Key: { |
| 84 | + id: req.params.id |
| 85 | + }, |
| 86 | + ExpressionAttributeNames: { |
| 87 | + "#todo_text": "text" |
| 88 | + }, |
| 89 | + ExpressionAttributeValues: { |
| 90 | + ":text": text, |
| 91 | + ":checked": checked, |
| 92 | + ":updatedAt": timestamp |
| 93 | + }, |
| 94 | + UpdateExpression: |
| 95 | + "SET #todo_text = :text, checked = :checked, updatedAt = :updatedAt", |
| 96 | + ReturnValues: "ALL_NEW" |
55 | 97 | }); |
56 | 98 |
|
57 | | -router |
58 | | - .route("/todos/:id") |
59 | | - .get(function get(req, res) { |
60 | | - const params = Object.assign({}, tableParams, { |
61 | | - Key: { |
62 | | - id: req.params.id |
63 | | - } |
64 | | - }); |
65 | | - |
66 | | - dynamoDb.get(params, (error, result) => { |
67 | | - if (error) { |
68 | | - console.error(error); |
69 | | - res.status(400).send({ error: "Could not get todo item" }); |
70 | | - } |
71 | | - |
72 | | - if (result.Item) { |
73 | | - res.json(result.Item); |
74 | | - } else { |
75 | | - res.status(400).send({ error: "Item not found" }); |
76 | | - } |
77 | | - }); |
78 | | - }) |
79 | | - .put(function update(req, res) { |
80 | | - const timestamp = new Date().getTime(); |
81 | | - const { text, checked } = req.body; |
82 | | - |
83 | | - // validation |
84 | | - if (typeof text !== "string" || typeof checked !== "boolean") { |
85 | | - console.error("Validation failed"); |
86 | | - res.status(400).send({ error: "Validation failed" }); |
| 99 | + dynamoDb.update(params, (error, result) => { |
| 100 | + if (error) { |
| 101 | + console.error(error); |
| 102 | + res |
| 103 | + .status(error.statusCoee || 501) |
| 104 | + .send({ error: "Could not update todo item" }); |
| 105 | + } else { |
| 106 | + res.send(result.Attributes); |
87 | 107 | } |
| 108 | + }); |
| 109 | +}; |
88 | 110 |
|
89 | | - const params = Object.assign({}, tableParams, { |
90 | | - Key: { |
91 | | - id: req.params.id |
92 | | - }, |
93 | | - ExpressionAttributeNames: { |
94 | | - "#todo_text": "text" |
95 | | - }, |
96 | | - ExpressionAttributeValues: { |
97 | | - ":text": text, |
98 | | - ":checked": checked, |
99 | | - ":updatedAt": timestamp |
100 | | - }, |
101 | | - UpdateExpression: |
102 | | - "SET #todo_text = :text, checked = :checked, updatedAt = :updatedAt", |
103 | | - ReturnValues: "ALL_NEW" |
104 | | - }); |
105 | | - |
106 | | - dynamoDb.update(params, (error, result) => { |
107 | | - if (error) { |
108 | | - console.error(error); |
109 | | - res |
110 | | - .status(error.statusCoee || 501) |
111 | | - .send({ error: "Could not update todo item" }); |
112 | | - } else { |
113 | | - res.send(result.Attributes); |
114 | | - } |
115 | | - }); |
116 | | - }) |
117 | | - .delete((req, res) => { |
118 | | - console.log(new Date().getTime()); |
119 | | - const params = Object.assign({}, tableParams, { |
120 | | - Key: { |
121 | | - id: req.params.id |
122 | | - } |
123 | | - }); |
124 | | - |
125 | | - dynamoDb.delete(params, error => { |
126 | | - if (error) { |
127 | | - console.error(error); |
128 | | - res.status(501).send({ error: "Could not delete toto item" }); |
129 | | - } else { |
130 | | - res.status(204).send(); |
131 | | - } |
132 | | - }); |
| 111 | +const deleteItem = (req, res) => { |
| 112 | + const params = Object.assign({}, tableParams, { |
| 113 | + Key: { |
| 114 | + id: req.params.id |
| 115 | + } |
133 | 116 | }); |
134 | 117 |
|
135 | | -routes.use(bodyParser.json({ strict: false })); |
136 | | -routes.use(router); |
| 118 | + dynamoDb.delete(params, error => { |
| 119 | + if (error) { |
| 120 | + console.error(error); |
| 121 | + res.status(501).send({ error: "Could not delete toto item" }); |
| 122 | + } else { |
| 123 | + res.status(204).send(); |
| 124 | + } |
| 125 | + }); |
| 126 | +}; |
137 | 127 |
|
138 | | -module.exports = routes; |
| 128 | +module.exports = { listItem, createItem, getItem, editItem, deleteItem }; |
0 commit comments