-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
161 lines (129 loc) · 3.59 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
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
const {
HTTP_OK_STATUS,
HTTP_CREATED_STATUS,
HTTP_NO_CONTENT_STATUS,
HTTP_NOT_FOUND_STATUS,
} = require('./HTTP-status');
const { getTalker, setTalker } = require('./fs-utils');
const {
isValidEmail,
isValidPassword,
isValidToken,
isValidNewTalkerName,
isValidNewTalkerAge,
isValidNewTalkerTalk,
isValidNewTalkerWatchedAtLength,
isValidNewTalkerWatchedAt,
isValidNewTalkerRate,
} = require('./middlewares/validations');
const PORT = '3000';
const MESSAGE_NOT_FOUND_TALKER = {
message: 'Pessoa palestrante não encontrada',
};
const tokenGenerator = () => {
const tokenActualy = JSON.stringify(Math.floor(Math.random() * 10000000000000000));
if (tokenActualy.length <= 15) {
return tokenGenerator();
}
return tokenActualy;
};
// não remova esse endpoint, e para o avaliador funcionar
app.get('/', (_request, response) => {
response.status(HTTP_OK_STATUS).send();
});
app.get(
'/talker/search',
isValidToken,
async (req, res) => {
const { q } = req.query;
const SPEAKERS = await getTalker();
const allSpeakersFilters = SPEAKERS.filter((e) => e.name.includes(q));
return res.status(HTTP_OK_STATUS).json(allSpeakersFilters);
},
);
app.get('/talker', async (_req, res) => {
const SPEAKERS = await getTalker();
if (SPEAKERS.length <= 0) {
return res.status(HTTP_OK_STATUS).json([]);
}
return res.status(HTTP_OK_STATUS).json(SPEAKERS);
});
app.get('/talker/:id', async (req, res) => {
const SPEAKERS = await getTalker();
const { id } = req.params;
const speaker = SPEAKERS.find((e) => e.id === Number(id));
if (!speaker) {
return res.status(HTTP_NOT_FOUND_STATUS).json(MESSAGE_NOT_FOUND_TALKER);
}
return res.status(HTTP_OK_STATUS).json(speaker);
});
app.post(
'/login',
isValidEmail,
isValidPassword,
(_req, res) => {
const token = tokenGenerator();
return res.status(HTTP_OK_STATUS).json({ token });
},
);
app.post(
'/talker',
isValidToken,
isValidNewTalkerName,
isValidNewTalkerAge,
isValidNewTalkerTalk,
isValidNewTalkerWatchedAtLength,
isValidNewTalkerWatchedAt,
isValidNewTalkerRate,
async (req, res) => {
const currentTalker = req.body;
const SPEAKERS = await getTalker();
const newId = (SPEAKERS.reduce((acc, curr) => {
if (curr.id > acc) return curr.id;
return acc;
}, 0)) + 1;
const newTalker = { id: newId, ...currentTalker };
SPEAKERS.push(newTalker);
await setTalker(SPEAKERS);
return res.status(HTTP_CREATED_STATUS).json(newTalker);
},
);
app.put(
'/talker/:id',
isValidToken,
isValidNewTalkerName,
isValidNewTalkerAge,
isValidNewTalkerTalk,
isValidNewTalkerWatchedAtLength,
isValidNewTalkerWatchedAt,
isValidNewTalkerRate,
async (req, res) => {
const currentTalker = req.body;
const { id } = req.params;
const NumberId = Number(id);
const SPEAKERS = await getTalker();
const newSpeakers = SPEAKERS.filter((e) => e.id !== NumberId);
const newTalker = { id: NumberId, ...currentTalker };
newSpeakers.push(newTalker);
await setTalker(newSpeakers);
return res.status(HTTP_OK_STATUS).json(newTalker);
},
);
app.delete(
'/talker/:id',
isValidToken,
async (req, res) => {
const { id } = req.params;
const SPEAKERS = await getTalker();
const newSpeakers = SPEAKERS.filter((e) => e.id !== Number(id));
await setTalker(newSpeakers);
return res.status(HTTP_NO_CONTENT_STATUS).send();
},
);
app.listen(PORT, () => {
console.log('Online');
});