-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
120 lines (88 loc) · 3.28 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
const express = require("express");
const cors = require('cors');
const mysql = require("mysql2");
const { json, response } = require("express");
const databaseConfiguration = require("./database/database-configuration.js");
const app = express();
//Porta de execucao do Server
const port = 3002;
//Permite com que use JSON para I/O
app.use(express.json());
//Permite "Access-Control-Allow-Origin"
app.use(cors());
//Inicia o servidor
app.listen(port, ()=>{
console.log(`Listening server on port: ${port}`)
});
//#region /GET Endpoints
app.get("/", (req, res) =>{
res.json( {message: "Hellooo World"});
console.log({message : "Haai"});
})
app.get("/bebidas", (req, res) =>{
let resultado;
//Método que futuramente usará diferentes usuários para acessar funcionalidaades
//com restrições específicas
const connection = mysql.createConnection({
host: databaseConfiguration.TEST_CONFIG.host,
port: databaseConfiguration.TEST_CONFIG.port,
user: databaseConfiguration.TEST_CONFIG.user,
password: databaseConfiguration.TEST_CONFIG.password,
database: databaseConfiguration.TEST_CONFIG.database
});
connection.query('SELECT * FROM bebidas', (error, result, fields) =>{
if(error) return console.log(error);
console.log("Resultado Query: ")
console.log(result);
res.send(result);
});
console.log("SQL END x");
return resultado;
});
app.get("/test2", (req, res) =>{
const connection = mysql.createConnection({
host: databaseConfiguration.TEST_CONFIG.host,
port: databaseConfiguration.TEST_CONFIG.port,
user: databaseConfiguration.TEST_CONFIG.user,
password: databaseConfiguration.TEST_CONFIG.password,
database: databaseConfiguration.TEST_CONFIG.database
});
connection.query('SELECT * FROM bebidas', (error, result, fields) =>{
if(error) return console.log(error);
console.log("[/test2] Resultado Query: ")
console.log(result);
res.send(result);
});
})
//#endregion
//#region POST endpoints
app.get("/bebida/:bebidaId", (req, res) =>{
const bebidaId = req.params.bebidaId;
//console.log(bebidaId);
const connection = mysql.createConnection(databaseConfiguration.DRINKHOUSE_CONFIG);
let sql = ` SELECT b.*, M.Marca from bebidas B
INNER JOIN marcas M
ON B.MarcaID = M.ID
WHERE b.id = ${bebidaId}`
connection.query(sql, (error, result, fields) =>{
if(error) return console.log(error);
//console.log("[/bebidas/id] Resultado Query: ")
//console.log(result);
res.send(result);
});
})
app.get("/bebidas-relacionadas/", (req, res) =>{
let queryParams = req.query;
let relatedDrinks = queryParams.relatedDrinks.toString();
console.log(relatedDrinks)
const connection = mysql.createConnection(databaseConfiguration.DRINKHOUSE_CONFIG);
let sql = ` SELECT id as Id, NomeBebida, url_imagem FROM BEBIDAS
WHERE ID IN(${relatedDrinks});`
connection.query(sql, (error, result, fields) =>{
if(error) return console.log(error);
console.log("[/bebidas-relacionadas/] Resultado Query: ")
console.log(result);
res.send(result);
});
})
//#endregion