-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
137 lines (119 loc) · 3.12 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
const express = require('express');
const bodyParser = require('body-parser');
const generator = require('generate-password');
const { readFile, writeFile } = require('./helpers/readWriteFile');
const {
validateLogin,
validateToken,
validateName,
validateAge,
validateTalkField,
validateWatchedAt,
validateRate,
} = require('./middleware/validations');
const app = express();
app.use(bodyParser.json());
const HTTP_OK_STATUS = 200;
const PORT = '3000';
// Não remova esse endpoint, é para o avaliador funcionar
app.get('/', (_request, response) => {
response.status(HTTP_OK_STATUS).send();
});
app.listen(PORT, () => {
console.log('Online');
});
// Requisito 01
app.get('/talker', async (_req, res) => {
const data = await readFile();
if (data.length === 0) {
res.status(200).json([]);
} else {
res.status(200).json(data);
}
});
// Requisito 08
app.get('/talker/search',
validateToken,
async (req, res) => {
const { q } = req.query;
const data = await readFile();
const talkers = data.filter((talker) => talker.name.toLowerCase().includes(q.toLowerCase()));
if (!q) return res.status(200).json(data);
if (!talkers) return res.status(200).json([]);
res.status(200).json(talkers);
});
// Requisito 02
app.get('/talker/:id', async (req, res) => {
const data = await readFile();
const { id } = req.params;
const targetTalker = data.find((talker) => Number(talker.id) === Number(id));
if (!targetTalker) {
res.status(404).json({ message: 'Pessoa palestrante não encontrada' });
} else {
res.status(200).json(targetTalker);
}
});
// Requisito 03 e 04
app.post('/login', validateLogin, (_req, res) => {
const token = generator.generate({ length: 16, numbers: true });
res.status(200).json({ token });
});
// Requisito 05
app.post('/talker',
validateToken,
validateName,
validateAge,
validateTalkField,
validateWatchedAt,
validateRate,
async (req, res) => {
const { name, age, talk: { watchedAt, rate } } = req.body;
const data = await readFile();
data.push({ id: data.length + 1, name, age, talk: { watchedAt, rate } });
await writeFile(data);
res.status(201).json({
id: data.length,
name,
age,
talk: {
watchedAt,
rate,
},
});
});
// Requisito 06
app.put('/talker/:id',
validateToken,
validateName,
validateAge,
validateTalkField,
validateWatchedAt,
validateRate,
async (req, res) => {
const { id } = req.params;
const { name, age, talk: { watchedAt, rate } } = req.body;
const data = await readFile();
const talkerIndex = data.findIndex((talker) => Number(talker.id) === Number(id));
data[talkerIndex] = { name, age, id: Number(id), talk: { watchedAt, rate } };
await writeFile(data);
res.status(200).json({
name,
age,
id: Number(id),
talk: {
watchedAt,
rate,
},
});
});
// Requisito 07
app.delete('/talker/:id',
validateToken,
async (req, res) => {
const { id } = req.params;
const data = await readFile();
const talkerIndex = data.findIndex((talker) => Number(talker.id) === Number(id));
data.splice(talkerIndex, 1);
await writeFile(data);
res.status(204).end();
});