-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
100 lines (87 loc) · 2.83 KB
/
index.js
File metadata and controls
100 lines (87 loc) · 2.83 KB
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
var express = require('express'),
cors = require('cors'),
bodyParser = require('body-parser'),
app = express();
var Sequelize = require('sequelize'),
sequelize = null;
if (process.env.DATABASE_URL) {
// the application is executed on Heroku ... use the postgres database
sequelize = new Sequelize(process.env.DATABASE_URL);
}
else {
sequelize = new Sequelize('database', 'root', null, {
host: 'localhost',
dialect: 'sqlite',
storage: 'database.sqlite'
});
}
sequelize.define("TodoList", {
name: Sequelize.STRING
});
sequelize.define("Todo", {
name: Sequelize.STRING,
assignee: Sequelize.STRING,
done: {
type: Sequelize.BOOLEAN,
defaultValue: false
}
});
const Todo = sequelize.models.Todo;
const TodoList = sequelize.models.TodoList;
TodoList.hasMany(Todo, {as: 'todos'});
app.use(cors()); // Enable CORS
app.use(bodyParser.json({strict: false})); // Use JSON body parser
// Todo lists
app.get('/todo-lists', (req, res, next) => {
console.log('Fetched list of todo lists.');
TodoList.findAll().then(todoLists => res.json(todoLists));
});
app.post('/todo-lists', (req, res, next) => {
const name = req.body.name;
console.log('Created a todo list with name ' + name);
TodoList.create({name: name}).then(todo => {
res.json({url: 'http://gofore-todo.herokuapp.com/todo-lists/' + todo.id});
});
});
app.get('/todo-lists/:id', (req, res, next) => {
const id = req.params.id;
console.log('Fetched todo list with an id ' + id);
TodoList.findById(id)
.then(todoList => todoList.getTodos()
.then(todos => res.json({name: todoList.name, todos: todos})));
});
app.post('/todo-lists/:id', (req, res, next) => {
const id = req.params.id;
console.log('Created a todo item for list with an id ' + id);
TodoList.findById(id)
.then((todoList) => todoList.createTodo(req.body)
.then(todo => res.json(todo)));
});
app.delete('/todo-lists/:id', (req, res, next) => {
const id = req.params.id;
console.log('Deleted todo list with id ' + id);
TodoList.findById(id)
.then(todoList => todoList.destroy().then(() => res.json({})));
});
app.get('/todos/:id', (req, res, next) => {
const id = req.params.id;
console.log('Fetched todo with an id ' + id);
Todo.findById(id)
.then(todo => res.json(todo));
});
// Todo items
app.put('/todos/:id', (req, res, next) => {
const id = req.params.id;
console.log('Updated todo item with an id ' + id);
Todo.findById(id).then(todo => todo.update(req.body).then(() => res.json({})));
});
app.delete('/todos/:id', (req, res, next) => {
const id = req.params.id;
console.log('Deleted todo item with an id ' + id);
Todo.findById(id).then(todo => todo.destroy().then(() => res.json({})));
});
sequelize.sync().then(() => {
app.listen(process.env.PORT || 8080, function(){
console.log('CORS-enabled web server listening on port 80');
});
});