Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
136 lines (120 loc) · 3.97 KB
/
Copy pathapp.js
File metadata and controls
136 lines (120 loc) · 3.97 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
require('dotenv').config()
const express = require('express')
const sqlite = require('sqlite3')
const app = express()
const db = new sqlite.Database("todos.db")
const PORT = process.env.PORT || 8080
app.use( express.json() )
app.use((req, res, next) => {
console.log(`Handling request for ${req.method} ${req.path}`)
next()
})
app.use((req, res, next) => {
if(req.method == 'POST' && req.headers['content-type'] != 'application/json'){
return res.status(400).json({error: 'Missing header Content-Type: application/json'})
}
next()
})
app.get('/alltodolists', (req, res) => {
db.get("SELECT * FROM todo_lists", function(err, row){
res.json(row)
})
})
app.post('/newtodolist', (req, res) => {
const {title, color} = req.body
if(!(title || color)){
return res.status(400).json({error: "Invalid body"})
}
db.run(`INSERT INTO todo_lists(title,color) VALUES (?, ?)`, [title,color], function(error){
if(error){
return res.status(500).json({error})
}
db.get(`SELECT * FROM todo_lists WHERE id = ?`, [this.lastID], function(err, row){
res.json({message:"Success", data:row})
})
})
})
app.post('/newtodo', (req, res) => {
const {todoListID, content} = req.body
if(!(todoListID || content)){
return res.status(400).json({error: 'Invalid body'})
}
db.run(`INSERT INTO todos(content, todo_list_id) VALUES (?,?)`, [content, todoListID], function(error){
if(error){
return res.status(500).json({error})
}
db.get(`SELECT * FROM todos WHERE id = ?`, [this.lastID], function(err, row){
res.json({message: 'Success', data:row})
})
})
})
app.get('/gettodolists', (req, res) => {
db.all(`SELECT * FROM todo_lists`, function(error, rows){
if(error){
return res.status(500).json({error})
}
res.json({message: 'Success', data: rows})
})
})
app.get('/gettodo', (req, res) => {
const {id} = req.body
if(!id){
return res.status(400).json({error: 'Invalid body'})
}
db.get(`SELECT * FROM todos WHERE id = ?`, [id], function(error, row){
if(error){
res.status(500).json({error})
}
res.json({message: 'Success', data: row})
})
})
app.post('/updatetodolist', (req, res) => {
const {id, title, color} = req.body
if(!(id || title || color)){
return res.status(400).json({error: 'Invalid body'})
}
db.run(`UPDATE todos SET title = ?, color = ? WHERE id = ?`, [title,color,id], function(error){
if(error){
return res.status(400).json({error})
}
res.json({message: 'Todo list updated'})
})
})
app.post('/updatetodo', (req, res) => {
const {id, content, done} = req.body
if(!(id || content || done)){
return res.status(400).json({error: "Invalid body"})
}
db.run(`UPDATE todos SET content = ?, done = ? WHERE id = ?`,
[content,done,id],
function(error){
if(error){
return res.status(500).json({error})
}
res.json({message: 'Todo updated'})
}
)
})
app.post('/deletetodolist', (req, res) => {
const {id} = req.body
if(!id){
return res.status(400).json({error: 'Invalid body, missing id'})
}
db.run(`DELETE FROM todo_lists WHERE id = ?`, [id], function(error){
if(error){
return res.status(500).json({error})
}
if(this.changes == 0){
return res.status(404).json({error: `Todo list with id ${id} not found`})
}
res.json({message: 'Todo list deleted'})
})
})
db.serialize(() => {
db.run("DROP TABLE IF EXISTS todo_lists")
db.run("DROP TABLE IF EXISTS todos")
db.run(`CREATE TABLE "todo_lists" ("id" INTEGER,"title" TEXT NOT NULL,"color" TEXT NOT NULL DEFAULT 'white',PRIMARY KEY("id" AUTOINCREMENT));`)
db.run(`CREATE TABLE "todos" ("id" INTEGER,"content" TEXT NOT NULL DEFAULT '',"done" INTEGER NOT NULL DEFAULT 0,"todo_list_id" INTEGER,PRIMARY KEY("id" AUTOINCREMENT),FOREIGN KEY("todo_list_id") REFERENCES "todo_lists"("id"));`)
db.get("PRAGMA foreign_keys = ON") // Enable SQL error on foreign key constraints
app.listen(PORT, () => console.log("Running on port " + PORT))
})