-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
159 lines (128 loc) · 3.66 KB
/
app.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
const express = require('express');
const mysql = require('mysql');
const bodyParser = require('body-parser');
const PORT = process.env.PORT || 5500;
const app = express();
app.use(bodyParser.json());
// MySql
const connection = mysql.createConnection({
host: 'fishsavior.mysql.database.azure.com',
database: 'fishsaviordb',
user: 'fishadmin@fishsavior',
password: 'M@st3rkey'
});
// Route
app.get('/', (req, res) => {
res.send('Welcome to my API!');
});
// all locations
app.get('/locations', (req, res) => {
const sql = "SELECT *, replace(fishsaviordb.locations.coordenadas,' ','') as coord FROM fishsaviordb.locations";
connection.query(sql, (error, results) => {
if (error) throw error;
if (results.length > 0) {
res.json(results);
} else {
res.send('Not result');
}
});
});
// all alerts
app.get('/alerts', (req, res) => {
const sql = "SELECT * FROM fishsaviordb.alerts";
connection.query(sql, (error, results) => {
if (error) throw error;
if (results.length > 0) {
res.json(results);
} else {
res.send('Not result');
}
});
});
// alerts by id
app.get('/alerts/:id', (req, res) => {
const { id } = req.params;
const sql = `SELECT * FROM fishsaviordb.alerts WHERE id = ${id}}`;
connection.query(sql, (error, results) => {
if (error) throw error;
if (results.length > 0) {
res.json(results);
} else {
res.send('Not result');
}
});
});
// location by id
app.get('/location/:id', (req, res) => {
const { id } = req.params;
const sql = `SELECT * FROM fishsaviordb.locations WHERE id = ${id}`;
connection.query(sql, (error, result) => {
if (error) throw error;
if (result.length > 0) {
res.json(result);
} else {
res.send('Not result');
}
});
});
// add location
app.post('/add', (req, res) => {
const sql = 'INSERT INTO fishsaviordb.locations SET ?';
const customerObj = {
id: req.body.id,
nombre: req.body.nombre,
hora: req.body.hora,
fecha: req.body.fecha,
coordenadas: req.body.coordenadas,
altura: req.body.altura,
temperatura: req.body.temperatura,
velicidadviento: req.body.velicidadviento,
observaciones: req.body.observaciones
};
connection.query(sql, customerObj, error => {
if (error) throw error;
res.send('Location created!');
});
});
// add alert
app.post('/add', (req, res) => {
const sql = 'INSERT INTO fishsaviordb.alerts SET ?';
const customerObj = {
id: req.body.id,
idalert: req.body.idalert,
nombre: req.body.nombre,
hora: req.body.hora,
fecha: req.body.fecha,
coordenadas: req.body.coordenadas,
altura: req.body.altura,
temperatura: req.body.temperatura,
velicidadviento: req.body.velicidadviento
};
connection.query(sql, customerObj, error => {
if (error) throw error;
res.send('Alert created!');
});
});
app.put('/update/:id', (req, res) => {
const { id } = req.params;
const { name, city } = req.body;
const sql = `UPDATE fishsaviordb.locations SET nombre = '${name}' WHERE id =${id}`;
connection.query(sql, error => {
if (error) throw error;
res.send('Location updated!');
});
});
app.delete('/delete/:id', (req, res) => {
const { id } = req.params;
const sql = `DELETE FROM fishsaviordb.locations WHERE id= ${id}`;
connection.query(sql, error => {
if (error) throw error;
res.send('Location deleted');
});
});
// Check connect
connection.connect(error => {
if (error) throw error;
console.log('Database server running!');
});
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));