-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
60 lines (54 loc) · 1.58 KB
/
server.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
require('dotenv').config();
const express = require('express');
const app = express();
const { createPool } = require('mysql2/promise');
const sql = createPool({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
port: process.env.DB_PORT,
database: process.env.DB_NAME
});
app.use(express.json());
app.get('/locations', async(req, res) => {
try {
const [locations] = await sql.query('SELECT * FROM location');
return res.json(locations);
}
catch(error) {
return res.status(500).json({
message: 'Internal server error',
});
}
});
app.get('/customers', async(req, res) => {
try {
const [customers] = await sql.query('SELECT * FROM customer');
return res.json(customers);
}
catch(error) {
return res.status(500).json({
message: 'Internal server error',
});
}
});
app.post('/customers', async(req, res) => {
try {
const { name } = req.body;
const [locations] = await sql.query('SELECT * FROM location ORDER BY RAND() LIMIT 1');
const locationId = locations.at(0).id;
const [result] = await sql.query(`INSERT INTO customer(name, location_id) VALUES("${name}", ${locationId})`);
return res.status(201).json({
id: result.insertId,
name,
location_id: locationId
});
}
catch(error) {
console.log(error);
return res.status(500).json({
message: 'Internal server error',
});
}
});
app.listen(process.env.NODE_PORT || 5000);