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

Skip to content

Commit d543d4b

Browse files
authored
Merge pull request HackYourFuture#114 from HackYourFuture/still-more-updates
Updating coursework
2 parents c6861e3 + a29e0f9 commit d543d4b

File tree

21 files changed

+151
-163
lines changed

21 files changed

+151
-163
lines changed

.vscode/settings.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"eslint.autoFixOnSave": true
3-
}
2+
"eslint.autoFixOnSave": true,
3+
"editor.tabSize": 2
4+
}

week1/homework/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
'use strict';
22

3-
// Write the homework code in this file
3+
// TODO: Write the homework code in this file

week2/homework/README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,13 @@ node index.js reset
6363

6464
## Bonus assignment
6565

66-
- Use JSON to store to-dos and add following commands
66+
- Use JSON to store to-dos
6767
- Split each action (i.e. read, write, etc.) into a separate file
6868
- Use [commander](https://www.npmjs.com/package/commander) library to implement
6969
command line interface
7070

71+
Add following commands:
72+
7173
### `update`
7274

7375
Updates a to-do item with new text:
@@ -79,4 +81,5 @@ node index.js update 3 "Brush teeth"
7981
### Things to consider
8082

8183
- What representation you use in your file (CSV, TSV, JSON, etc).
82-
- Handle edge cases, i.e. control what happens if user enters unexpected input
84+
- Handle edge cases, i.e. control what happens if user enters unexpected input,
85+
e.g. `remove -100`.

week2/homework/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
'use strict';
22

3-
// Write the homework code in this file
3+
// TODO: Write the homework code in this file

week2/lecture/src/index.js

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

33
const fs = require('fs');
44

5-
const STORE_FILE_NAME = 'store.txt';
5+
const DEFAULT_ENCODING = 'utf8';
6+
const STORE_FILE_NAME = 'store.txt';
67

78
function readFile() {
89
return new Promise(
910
resolve => fs.readFile(
1011
STORE_FILE_NAME,
11-
(err, data) => resolve(err ? '' : data.toString())
12+
DEFAULT_ENCODING,
13+
(err, data) => resolve(err ? '' : data)
1214
)
1315
);
1416
}
1517

16-
function writeFile(...text) {
18+
function appendFile(...text) {
1719
return new Promise(
1820
(resolve, reject) => fs.appendFile(
1921
STORE_FILE_NAME,
@@ -51,7 +53,7 @@ switch (cmd) {
5153
break;
5254

5355
case 'write':
54-
writeFile(...args)
56+
appendFile(...args)
5557
.then(() => console.log('Wrote to-do to file'))
5658
.then(() => readFile())
5759
.then(data => console.log(`\nTo-Dos:\n${data}`))

week3/README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
## Last week's summary
1818

19-
Last week we made a CLI To-Do application. This week we are going to rewrite
19+
Last week we made a CLI To-Do application. This week we are going to rewrite it
2020
into an Express-based server.
2121

2222
## Testing with Postman
@@ -53,25 +53,25 @@ Documentation:
5353

5454
### To-Do API
5555

56-
This is an Express application using [body-parser](https://github.com/expressjs/body-parser) middleware that automatically parser
57-
JSON from request body.
56+
This week we are going to write an Express application using [body-parser](https://github.com/expressjs/body-parser)
57+
middleware that automatically parses JSON from request body.
5858

59-
There are currently 4 [CRUD](https://en.wikipedia.org/wiki/Create%2C_read%2C_update_and_delete)
59+
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: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,31 @@
22

33
### Assignment
44

5-
- Read through the code from the lecture, make sure you understand the flow of
6-
the program
7-
- Add three more actions
8-
- `clear` (`DELETE /todos`) which will clear the list of todos
9-
- `markAsDone` (`POST /todos/:id/done`) which will set the `done` flag of a single todo to `true`
10-
- `markAsNotDone` (`DELETE /todos/:id/done`) which will set the `done` flag of a single todo to `false`
11-
- Update your README to reflect your new actions!
12-
13-
Take care of the following:
14-
15-
- All requests that need a body should be in JSON, and follow the request structure of the other actions
16-
- All responses should be in JSON, and follow the response structure of the other actions
5+
Read through the code from the lecture, make sure you understand the flow of the
6+
program.
7+
8+
Add three more actions:
9+
10+
### `clearTodos` (`DELETE /todos`)
11+
12+
Clears the list of to-dos
13+
14+
### `markAsDone` (`POST /todos/:id/done`)
15+
16+
Sets the `done` flag of a single to-do to `true`
17+
18+
### `markAsNotDone` (`DELETE /todos/:id/done`)
19+
20+
Sets the `done` flag of a single to-do to `false`
21+
22+
## Requirements
23+
24+
- All requests that need a body should be in JSON format, and follow the request
25+
structure of the other actions
26+
- All responses should be in JSON format, and follow the response structure of
27+
the other actions
1728
- Follow the anatomy of the project
18-
- Make your code DRY (see https://en.wikipedia.org/wiki/Don%27t_repeat_yourself)
19-
- Follow the REST design principles: use the proper method, response status codes, and consistent URL paths
29+
- Make sure your code is [DRY](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself)
30+
- Follow the REST design principles: use the proper method, response status
31+
codes, and consistent URL paths
2032
- Test your API using Postman

week3/homework/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
'use strict';
22

3-
// Write the homework code in this file
3+
// TODO: Write the homework code in this file

week3/lecture/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "hyf-node-week-3-todo",
33
"version": "1.0.0",
4-
"description": "HackYourFuture Node.js Week 3 - Todo App",
4+
"description": "HackYourFuture Node.js Week 3 - To-Do App",
55
"repository": "https://github.com/HackYourFuture/nodejs.git",
66
"main": "src/index.js",
77
"author": "George Sapkin",

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: 6 additions & 16 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) {
10-
const todo = request.body.todo;
11-
if (todo == null) {
12-
setError(response, 'Specify a todo');
13-
return null;
14-
}
3+
async function deserializeTodo(request) {
4+
const { todo } = request.body;
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.

0 commit comments

Comments
 (0)