-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
97 lines (88 loc) · 2.25 KB
/
index.js
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
const express = require("express");
const bodyParser = require("body-parser");
const db = require("./config/database");
require("dotenv").config();
const Todo = require("./models/todo.model");
const app = express();
app.use(bodyParser.urlencoded({extended:false}))
//Test DB
db.authenticate()
.then(() => console.log("Database connected"))
.catch((e) => console.log("Error:" + e));
app.get("/", (req, res) => {
res.send("Hello world");
});
app.get("/todo", (req, res, next) => {
Todo.findAll()
.then((model) => {
res.json({
error: false,
data: model
})
})
.catch(error => res.json({
error: true,
data: [],
error: error
}))
});
app.get("/todo/:id", (req, res, next) => {
const id =req.params.id
Todo.findByPk(id)
.then((model) => {
res.json({
error: false,
data: model
})
})
.catch(error => res.json({
error: true,
data: [],
error: error
}))
});
app.put('/todo/update/:id',bodyParser.json(),(req, res,next)=>{
const id =req.params.id
const {item,description}=req.body
Todo.update({item:item},{where:{id:id}})
.then((uTodo) => {
console.log('got item and updated ' + uTodo);
res.json({
error: false,
data: uTodo
})
})
.catch(error => res.json({
error: true,
data: [],
error: error
}))
})
app.delete('/todo/delete/:id' ,(req, res,next)=>{
const id =req.params.id
Todo.destroy({where:{id:id}})
.then((dTodo) => {
console.log('got item and deleted ' + dTodo);
res.json({
error: false,
data: dTodo
})
})
.catch(error => res.json({
error: true,
data: [],
error: error
}))
})
app.post("/todo", bodyParser.json(),(req, res, next) => {
const {item,description}=req.body
Todo.create({item:item,description:description})
.then((model) => {
res.status(200).send(model);
})
.catch((e) => {
res.status(400).send("Error:" + e);
});
});
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`server running on PORT ${PORT}`));