-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
253 lines (212 loc) · 7.8 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
const express = require("express");
const app = express();
const postGree = require("pg");
const cors = require("cors");
const jwt = require("jsonwebtoken");
require('dotenv').config();
var dataBase;
if(process.env.PORT == undefined || process.env.PORT == 3001) {
dataBase = new postGree.Pool({
user: process.env.LOCAL_USER,
password: process.env.LOCAL_PASSWORD,
port: process.env.LOCAL_PORT,
database: process.env.LOCAL_DB,
host: process.env.LOCAL_HOST
});
} else {
dataBase = new postGree.Pool({
connectionString: process.env.CONNECTION_STRING
});
}
app.listen(process.env.PORT || 3001, () => {
console.log(`rodando server na porta ${process.env.PORT}`);
});
app.use(cors({ credentials:true, origin: process.env.PORT == undefined ? "http://localhost:3000" : "https://homeservice-ute7.onrender.com" }));
app.use(express.json());
app.use((req, res, next) => {
res.setHeader('Content-Type', 'application/json');
next();
});
dataBase.connect((err) => {
if (err) console.log(err);
else console.log("connected succesfully"), createDataBaseTables();
});
async function createDataBaseTables() {
let usersTable =
"CREATE TABLE IF NOT EXISTS users (id SERIAL PRIMARY KEY NOT NULL UNIQUE, username varchar(60) NOT NULL, email varchar(45) NOT NULL UNIQUE, password varchar(45) NOT NULL, active BOOLEAN NOT NULL);";
let servicesTable =
"CREATE TABLE IF NOT EXISTS services (id SERIAL PRIMARY KEY NOT NULL UNIQUE, user_id SERIAL NOT NULL, name varchar(45) NOT NULL, profession varchar(45) NOT NULL, city varchar(45) NOT NULL, city2 varchar(45) DEFAULT NULL, phone_number varchar(45) NOT NULL, description varchar(100) DEFAULT NULL, CONSTRAINT user_id FOREIGN KEY (user_id) REFERENCES users (id));";
let avaliationsTable =
"CREATE TABLE IF NOT EXISTS avaliations (id SERIAL PRIMARY KEY NOT NULL UNIQUE, service_id int NOT NULL, username varchar(60) NOT NULL, comment varchar(350) DEFAULT NULL, avaliation int NOT NULL,CONSTRAINT service_id FOREIGN KEY (service_id) REFERENCES services (id))";
var queries = [];
queries.push(usersTable, servicesTable, avaliationsTable);
for (let i = 0; i < queries.length; i++) {
try {
let res = await dataBase.query(queries[i]);
console.log(res);
} catch (err) {
console.log(err);
}
}
console.clear();
};
function verifyJWT(request, response) {
const token = jwt.decode(request);
const userId = parseInt(token.result.rows.map((value) => value.id));
if (userId != undefined) return userId;
else return response.status(401).end();
}
app.post("/registrosDeServicos", (request, response) => {
const { userToken } = request.body;
const userId = verifyJWT(userToken);
const { name } = request.body;
const { profession } = request.body;
const { city } = request.body;
const { city2 } = request.body;
const { phoneNumber } = request.body;
const { description } = request.body;
let SQL = `INSERT INTO services (user_id, name, profession, city, city2, phone_number, description) VALUES ( $1, $2, $3, $4, $5, $6, $7)`;
dataBase.query(
SQL,
[userId, name, profession, city, city2, phoneNumber, description],
(err, result) => {
if (err) console.log(err);
else response.send(result);
}
);
});
app.post("/getCards", (request, response) => {
const { userToken } = request.body;
const userId = verifyJWT(userToken);
let SQL = "SELECT * FROM services WHERE $1 = user_id";
dataBase.query(SQL, [userId], (err, result) => {
console.log(JSON.stringify(result));
if (err) console.log(err);
else response.send(result.rows);
});
});
app.post("/resultados", (request, response) => {
const { information } = request.body;
if (information != null && information != " " && information != "") {
SQL = `SELECT id , name, profession, city, city2, phone_number, description FROM services WHERE name ~* $1 OR profession ~* $1 OR city ~* $1 OR city2 ~* $1`;
dataBase.query(
SQL,
[information],
(err, result) => {
if (err) console.log(err);
else response.json(result.rows);
}
);
} else {
response.send("");
}
});
function _getUserNameJWT(request, response) {
const token = jwt.decode(request);
var username = JSON.stringify(token.result.rows.map((value) => value.username));
username = username.replace(/[[\]\\"]/g, "");
if (username != undefined) return username;
else return response.status(401).end();
}
app.post("/getUserName", (request, response) => {
const { userToken } = request.body;
const userName = _getUserNameJWT(userToken);
response.send(userName);
});
app.post("/registrarAvaliacao", (request, response) => {
const { serviceId } = request.body;
const { userToken } = request.body;
const username = _getUserNameJWT(userToken);
const { comment } = request.body;
const { avaliation } = request.body;
let SQL =
"INSERT INTO avaliations (service_id, username, comment, avaliation) VALUES ($1, $2, $3, $4)";
dataBase.query(
SQL,
[serviceId, username, comment, avaliation],
(err, result) => {
if (err) console.log(err);
else response.send(result);
}
);
});
app.post("/getAvaliations", (request, response) => {
const { serviceId } = request.body;
let SQL =
"SELECT id, username, comment, avaliation FROM avaliations WHERE $1 = service_id";
dataBase.query(SQL, [serviceId], (err, result) => {
if (err) console.log(err);
else response.send(result.rows);
});
});
app.post("/getEmailUsuario", (request, response) => {
const { email } = request.body;
let SQL = "SELECT email FROM users WHERE email = $1";
dataBase.query(SQL, [email], (err, result) => {
if (err) console.log(err);
else response.send(result.rows[0]);
});
});
app.post("/registroUsuario", (request, response) => {
const { userName } = request.body;
const { email } = request.body;
const { password } = request.body;
let SQL =
"INSERT INTO users (username, email, password, active) VALUES ($1, $2, $3, $4)";
dataBase.query(SQL, [userName, email, password, false], (err, result) => {
if (err) console.log(err);
else response.sendStatus(200), console.log(result);
});
});
app.post("/login", (request, response) => {
const { email } = request.body;
const { password } = request.body;
let token;
let SQL =
"SELECT id, username FROM users WHERE $1 = email AND $2 = password";
dataBase.query(SQL, [email, password], (err, result) => {
if (err) console.log(err);
else {
if (result.rows.length !== 0) {
token = jwt.sign({ result }, process.env.SECRET, { expiresIn: "1h" });
response.json({ auth: true, token, result });
} else response.send(null);
}
});
});
app.put("/editService", (request, response) => {
console.log(`request: ${JSON.stringify(request.body)}`)
const { id } = request.body;
const { name } = request.body;
const { profession } = request.body;
const { city } = request.body;
const { city2 } = request.body;
const { phoneNumber } = request.body;
const { description } = request.body;
let SQL =
"UPDATE services SET name = $1, profession = $2, city = $3, city2 = $4, phone_number = $5, description = $6 WHERE id = $7";
dataBase.query(
SQL,
[name, profession, city, city2, phoneNumber, description, id],
(err, result) => {
if (err) console.log(err);
else response.send(result.rows);
}
);
});
app.post("/deleteAvaliation", (request, response) => {
const { id } = request.body;
let SQL = "DELETE FROM avaliations WHERE service_id = $1";
dataBase.query(SQL, [id], (err, result) => {
if (err) console.log(err);
else response.send(result);
});
});
app.post("/deleteService", (request, response) => {
const { id } = request.body;
let SQL = "DELETE FROM services WHERE id = $1";
dataBase.query(SQL, [id], (err, result) => {
if (err) console.log(err);
else response.send(result);
});
});