Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a29e0f9

Browse files
committed
Rewrote deserializeTodo and clarified action names
1 parent f015445 commit a29e0f9

13 files changed

+99
-132
lines changed

week3/README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,19 @@ middleware that automatically parses JSON from request body.
5959
There are 4 [CRUD](https://en.wikipedia.org/wiki/Create%2C_read%2C_update_and_delete)
6060
actions:
6161

62-
- Create (`POST /todos`)
62+
#### `createTodo` (`POST /todos`)
6363

6464
Creates a new to-do
6565

66-
- Read (`GET /todos`)
66+
#### `readTodos` (`GET /todos`)
6767

6868
Reads and lists all to-dos
6969

70-
- Update (`PUT /todos/:id`)
70+
#### `updateTodo` (`PUT /todos/:id`)
7171

7272
Updates the description of a to-do with ID `:id`
7373

74-
- Delete (`DELETE /todos/:id`)
74+
#### `deleteTodo` (`DELETE /todos/:id`)
7575

7676
Deletes a to-do with ID `:id`
7777

@@ -92,7 +92,8 @@ Note that for these actions, the client must add the following header:
9292

9393
- `Content-Type`: `application/json`
9494

95-
In Postman, make sure to add this header, and set the Body type to _Raw_.
95+
In Postman, make sure to add this header, and set the Body type to _raw_ and
96+
_JSON (application/json)_.
9697

9798
## UUIDs
9899

week3/homework/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
Read through the code from the lecture, make sure you understand the flow of the
66
program.
77

8-
Add three more actions and update your `README.md` to reflect your new actions.
8+
Add three more actions:
99

10-
### `clear` (`DELETE /todos`)
10+
### `clearTodos` (`DELETE /todos`)
1111

1212
Clears the list of to-dos
1313

week3/lecture/src/actions/create.js

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
3+
const deserializeTodo = require('./deserializeTodo');
4+
5+
function createTodo(todo, request, response) {
6+
deserializeTodo(request, response)
7+
.then(({ description }) => todo.create(description))
8+
.then(todo => {
9+
response.status(201);
10+
response.json({ todo });
11+
})
12+
.catch(({ message }) => {
13+
response.status(500);
14+
response.json({ error: message });
15+
});
16+
};
17+
18+
module.exports = createTodo;

week3/lecture/src/actions/delete.js

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
function deleteTodo(todo, request, response) {
4+
const id = request.params.id;
5+
6+
todo.delete_(id)
7+
.then(() => {
8+
response.status(204);
9+
response.end();
10+
})
11+
.catch(({ message }) => {
12+
response.status(500);
13+
response.json({ error: message });
14+
});
15+
};
16+
17+
module.exports = deleteTodo;

week3/lecture/src/actions/deserializeTodo.js

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
11
'use strict';
22

3-
function setError(response, error) {
4-
response.status(400);
5-
response.json({ error });
6-
response.end();
7-
}
8-
9-
function deserializeTodo(request, response) {
3+
async function deserializeTodo(request) {
104
const { todo } = request.body;
11-
if (todo == null) {
12-
setError(response, 'Specify a to-do');
13-
return null;
14-
}
5+
if (todo == null)
6+
throw new Error('todo not set');
157

168
if (todo.description != null)
179
todo.description = todo.description.trim();
1810

19-
if (todo.description == null || todo.description.length === 0) {
20-
setError(response, 'Specify a description');
21-
return null;
22-
}
11+
if (todo.description == null || todo.description.length === 0)
12+
throw new Error('description not set');
2313

2414
return todo;
2515
};

week3/lecture/src/actions/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
// CRUD actions
44
module.exports = {
5-
create: require('./create'),
6-
read: require('./read'),
7-
update: require('./update'),
8-
delete_: require('./delete')
5+
createTodo: require('./createTodo'),
6+
readTodos: require('./readTodos'),
7+
updateTodo: require('./updateTodo'),
8+
deleteTodo: require('./deleteTodo')
99
};

week3/lecture/src/actions/read.js

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
function readTodos(todo, request, response) {
4+
todo.read()
5+
.then(todos => {
6+
response.json({ todos });
7+
response.end();
8+
})
9+
.catch(({ message }) => {
10+
response.status(500);
11+
response.json({ error: message });
12+
});
13+
};
14+
15+
module.exports = readTodos;

week3/lecture/src/actions/update.js

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
const deserializeTodo = require('./deserializeTodo');
4+
5+
function updateTodo(todo, request, response) {
6+
deserializeTodo(request, response)
7+
.then(({ description }) => {
8+
const id = request.params.id;
9+
return todo.update(id, description);
10+
})
11+
.then(todo => {
12+
response.status(200);
13+
response.json({ todo });
14+
})
15+
.catch(({ message, code }) => {
16+
response.status(code === 'not-found' ? 404 : 500);
17+
response.json({ error: message });
18+
});
19+
};
20+
21+
module.exports = updateTodo;

week3/lecture/src/index.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,28 @@ const Express = require('express');
55

66
// import our CRUD actions
77
const {
8-
create,
9-
read,
10-
update,
11-
// delete is a JavaScript keyword, using delete_ instead
12-
delete_
8+
createTodo,
9+
readTodos,
10+
updateTodo,
11+
deleteTodo
1312
} = require('./actions');
1413

1514
const Todo = require('./todo');
1615

17-
const FILENAME = 'todos.json';
18-
19-
const PORT = 3000;
16+
const FILENAME = 'todos.json';
17+
const PORT = 3000;
18+
const TODO_SLUG = 'todos';
2019

2120
const todo = new Todo(FILENAME);
2221

2322
const app = Express();
2423

2524
app.use(bodyParser.json());
2625

27-
app.post('/todos', create.bind(null, todo));
28-
app.get('/todos', read.bind(null, todo));
29-
app.put('/todos/:id', update.bind(null, todo));
30-
app.delete('/todos/:id', delete_.bind(null, todo));
26+
app.post(`/${TODO_SLUG}`, createTodo.bind(null, todo));
27+
app.get(`/${TODO_SLUG}`, readTodos.bind(null, todo));
28+
app.put(`/${TODO_SLUG}/:id`, updateTodo.bind(null, todo));
29+
app.delete(`/${TODO_SLUG}/:id`, deleteTodo.bind(null, todo));
3130

3231
app.listen(PORT, error => {
3332
if (error)

0 commit comments

Comments
 (0)