-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
169 lines (142 loc) · 3.79 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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
const morgan = require("morgan"); // Logs HTTP requests to the console in a clean format
const express = require("express"); // Node.js framework for web
const bodyParser = require("body-parser"); // Parses incoming request bodies in JSON format
require("dotenv").config(); // Library for environment variables
const { initializeDB } = require("./database");
const passDbAroundMiddleware = (db) => {
return (req, res, next) => {
req.db = db;
next();
};
};
// putting everything inside a function to avoid global variables
const main = async () => {
const db = await initializeDB();
const app = express(); // creates express app
// middleware
app.use(morgan("tiny"));
app.use(bodyParser.json());
app.use(passDbAroundMiddleware(db));
// get all existing tasks
app.get("/tasks", async (req, res) => {
if (req.headers["authorization"] != `Bearer ${process.env.TOKEN}`) {
return res.status(401).end();
}
const completed = req.query.completed;
try {
let tasks;
if (req.query.completed !== undefined) {
tasks = await req.db.Task.findAll({ where: { completed } });
} else {
tasks = await req.db.Task.findAll();
}
res.json(tasks.map((t) => t.toJSON()));
res.end();
} catch (error) {
console.error(error);
return res.status(500).end();
}
});
// create new task
app.post("/tasks", async (req, res) => {
console.log({ body: req.body });
const { title, completed } = req.body;
// check if there are errors
const errors = validateTask(title, completed);
if (errors.length > 0) {
return res.status(400).json({ errors });
}
let data;
try {
const task = await req.db.Task.create({ title, completed });
data = await task.toJSON();
res.json(data);
} catch (error) {
console.error(error);
return res.status(500).end();
}
});
// update task title
app.patch("/task/:id/title", async (req, res) => {
const { title } = req.body;
const { id } = req.params;
let data;
try {
await req.db.Task.update({ title }, { where: { id } });
data = { title };
res.json(data);
} catch (error) {
console.log(error);
return res.status(500).end();
}
});
// update completed status
app.patch("/task/:id/status", async (req, res) => {
const { completed } = req.body;
const { id } = req.params;
try {
await req.db.Task.update(
{
completed,
},
{
where: { id },
}
);
res.end();
} catch (error) {
console.log(error);
return res.status(500).end();
}
});
// delete task
app.delete("/task/:id", async (req, res) => {
const { id } = req.params;
try {
await req.db.Task.destroy({
where: { id },
});
res.end();
} catch (error) {
console.error(error);
return res.status(500).end();
}
});
// delete all completed tasks
app.delete("/tasks/completed", async (req, res) => {
try {
await req.db.Task.destroy({
where: { completed: true },
});
res.end();
} catch (error) {
console.log(error);
return res.status(500).end();
}
});
// get specific task
app.get("/task/:id", async (req, res) => {
const { id } = req.params;
try {
const task = await req.db.Task.findAll({
where: { id },
});
res.json(task);
} catch (error) {
console.log(error);
return res.status(500).end();
}
});
app.use(express.static("frontend"));
// get token
app.get("/token", (req, res) => {
const token = process.env.TOKEN;
res.json({ token });
});
// port
const port = 3000;
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});
};
main();