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

Skip to content

Commit fc0e376

Browse files
committed
moved homework class7
2 parents a320340 + 3a2395a commit fc0e376

File tree

358 files changed

+51729
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

358 files changed

+51729
-2
lines changed

README.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<<<<<<< HEAD
12
> Please help us improve and share your feedback! If you find better tutorials or links, please share them by opening a Pull Request.
23
34
# HackYourFuture - Node.js
@@ -33,4 +34,46 @@ There are many languages for this. We might've heard of Java, C, C++, Go, Python
3334

3435
There are two reasons why we at HYF choose Node.JS over others:
3536
1) You already know JavaScript, so it's easier to get started than other languages
36-
2) Node.js is great for making web APIs because it is asynchronous by nature and thus allows for high input/output. By this we mean that it allows many users to make very light requests at the same time.
37+
2) Node.js is great for making web APIs because it is asynchronous by nature and thus allows for high input/output. By this we mean that it allows many users to make very light requests at the same time.
38+
=======
39+
# TODO API
40+
41+
This is an Express application using `bodyParser` middleware to convert the request body to JSON.
42+
43+
There are currently four actions:
44+
45+
- `list` (`GET /todos`): Lists all todos
46+
- `create` (`POST /todos`): Creates a new todo
47+
- `update` (`PUT /todos/:id`): Updates the description of a todo
48+
- `remove` (`DELETE /todos/:id`): Deletes a todo
49+
50+
## Directory structure
51+
52+
- `actions`: Contains the actions as listed above, each as a express handler (function accepting request and response)
53+
- `data`: Contains the data file `todos.json`
54+
- `models`: Contains the Todo model class
55+
- `util`: Utility functions
56+
- `index.js` The application file
57+
58+
## Request body format
59+
60+
When calling the `create` or `update` actions, the request body should look like this:
61+
62+
```json
63+
{
64+
"todo": {
65+
"description": "(todo description)"
66+
}
67+
}
68+
```
69+
70+
Note that for these actions, the client should add the following header:
71+
72+
- `Content-Type`: `application/json`
73+
74+
In Postman, make sure to add this header, and set the Body type to "Raw".
75+
76+
## UUIDs
77+
78+
For IDs, this application uses "UUIDs" (Universally Unique IDs). They can be generated using the `uuid` package, and are guaranteed never to be the same.
79+
>>>>>>> class7-nodejs-week3/master

class7-nodejs-week3

Lines changed: 0 additions & 1 deletion
This file was deleted.

class7-week3/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# TODO API
2+
3+
This is an Express application using `bodyParser` middleware to convert the request body to JSON.
4+
5+
There are currently four actions:
6+
7+
- `list` (`GET /todos`): Lists all todos
8+
- `create` (`POST /todos`): Creates a new todo
9+
- `update` (`PUT /todos/:id`): Updates the description of a todo
10+
- `remove` (`DELETE /todos/:id`): Deletes a todo
11+
12+
## Directory structure
13+
14+
- `actions`: Contains the actions as listed above, each as a express handler (function accepting request and response)
15+
- `data`: Contains the data file `todos.json`
16+
- `models`: Contains the Todo model class
17+
- `util`: Utility functions
18+
- `index.js` The application file
19+
20+
## Request body format
21+
22+
When calling the `create` or `update` actions, the request body should look like this:
23+
24+
```json
25+
{
26+
"todo": {
27+
"description": "(todo description)"
28+
}
29+
}
30+
```
31+
32+
Note that for these actions, the client should add the following header:
33+
34+
- `Content-Type`: `application/json`
35+
36+
In Postman, make sure to add this header, and set the Body type to "Raw".
37+
38+
## UUIDs
39+
40+
For IDs, this application uses "UUIDs" (Universally Unique IDs). They can be generated using the `uuid` package, and are guaranteed never to be the same.

class7-week3/actions/create.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const Todo = require('../models/todo')
2+
const deserializeTodo = require('../util/deserializeTodo')
3+
4+
module.exports = function create(request, response) {
5+
6+
const todo = deserializeTodo(request, response)
7+
if (todo == null) { return }
8+
9+
Todo.create(todo.description, (error, todo) => {
10+
if (error) {
11+
console.error(error)
12+
response.status(500)
13+
response.json({error: 'Internal error'})
14+
} else {
15+
response.status(201)
16+
response.json({todo})
17+
}
18+
})
19+
20+
}

class7-week3/actions/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
list: require('./list'),
3+
create: require('./create'),
4+
update: require('./update'),
5+
remove: require('./remove')
6+
}

class7-week3/actions/list.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const Todo = require('../models/todo')
2+
3+
module.exports = function list(request, response) {
4+
5+
// Load todos asynchronously (with a callback)
6+
Todo.load((error, todos) => {
7+
if (error) {
8+
response.status(500)
9+
response.json({error: 'Internal error'})
10+
} else {
11+
response.json({todos})
12+
response.end()
13+
}
14+
})
15+
16+
}

class7-week3/actions/remove.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const Todo = require('../models/todo')
2+
3+
module.exports = function create(request, response) {
4+
5+
const id = request.params.id
6+
7+
Todo.remove(id, error => {
8+
if (error) {
9+
console.error(error)
10+
response.status(500)
11+
response.json({error: 'Internal error'})
12+
} else {
13+
response.status(204)
14+
response.end()
15+
}
16+
})
17+
18+
}

class7-week3/actions/update.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const Todo = require('../models/todo')
2+
const deserializeTodo = require('../util/deserializeTodo')
3+
4+
module.exports = function update(request, response) {
5+
6+
const id = request.params.id
7+
const todo = deserializeTodo(request, response)
8+
if (todo == null) { return }
9+
10+
Todo.update(id, todo.description, (error, todo) => {
11+
if (error == null) {
12+
response.json({todo})
13+
} else if (error.name === 'NotFound') {
14+
response.status(404)
15+
response.json({error: error.message})
16+
} else {
17+
console.error(error)
18+
response.status(500)
19+
response.json({error: 'Internal error'})
20+
}
21+
})
22+
23+
}

class7-week3/data/todos.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"id":"f3e2462d-0bfb-46f2-8ea8-4fc7020f0f30","description":"Go into bed","done":false},{"id":"a2e2a68e-1a49-4fb5-ba66-f618a523e631","description":"Go into bed","done":false},{"id":"c8f9c333-2372-41c9-bc7b-3448b61f3371","description":"Do something","done":false},{"id":"bde9faac-582c-4536-8d01-b98252618340","description":"Blablabla","done":false}]

class7-week3/index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const Express = require('express')
2+
const bodyParser = require('body-parser')
3+
4+
const app = Express()
5+
6+
app.use(bodyParser.json())
7+
8+
const {list, create, update, remove} = require('./actions')
9+
10+
app.get('/todos', list)
11+
app.post('/todos', create)
12+
app.put('/todos/:id', update)
13+
app.delete('/todos/:id', remove)
14+
15+
app.listen(3000)

class7-week3/models/todo.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
const FS = require('fs')
2+
const Path = require('path')
3+
const uuid = require('uuid/v4')
4+
5+
const filename = Path.resolve(__dirname, '../data/todos.json')
6+
7+
class Todo {
8+
9+
load(callback) {
10+
FS.readFile(filename, 'utf-8', (error, data) => {
11+
if (error) {
12+
callback(error)
13+
} else {
14+
callback(null, JSON.parse(data))
15+
}
16+
})
17+
}
18+
19+
save(todos, callback) {
20+
FS.writeFile(filename, JSON.stringify(todos), callback)
21+
}
22+
23+
create(description, callback) {
24+
this.load((error, todos) => {
25+
if (error) { callback(error); return }
26+
27+
const todo = {
28+
id: uuid(),
29+
description,
30+
done: false
31+
}
32+
todos.push(todo)
33+
34+
this.save(todos, error => {
35+
if (error) { callback(error); return }
36+
37+
callback(null, todo)
38+
})
39+
})
40+
}
41+
42+
update(id, description, callback) {
43+
this.load((error, todos) => {
44+
if (error) { callback(error); return }
45+
46+
const todo = todos.find(t => t.id === id)
47+
if (todo == null) {
48+
const error = new Error(`Todo with ID ${id} does not exist`)
49+
error.name = 'NotFound'
50+
51+
callback(error)
52+
return
53+
}
54+
55+
todo.description = description
56+
57+
this.save(todos, error => {
58+
if (error) { callback(error); return }
59+
60+
callback(null, todo)
61+
})
62+
})
63+
}
64+
65+
remove(id, callback) {
66+
this.load((error, todos) => {
67+
if (error) { callback(error); return }
68+
69+
todos = todos.filter(t => t.id !== id)
70+
71+
this.save(todos, error => {
72+
if (error) { callback(error); return }
73+
callback()
74+
})
75+
})
76+
}
77+
78+
}
79+
80+
module.exports = new Todo()

class7-week3/node_modules/.bin/mime

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

class7-week3/node_modules/.bin/uuid

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)