forked from bpo106/pallida-orientation-exam-retake
-
Notifications
You must be signed in to change notification settings - Fork 9
/
backend.js
74 lines (59 loc) · 2.13 KB
/
backend.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
'use strict'
const express = require('express');
const app = express();
const mysql = require('mysql');
app.use(express.json());
app.use(express.static(__dirname + '/assets'));
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'root',
database: 'clothing'
});
connection.connect(function (err){
if(err){
console.log('Error connecting to DB!');
return;
};
console.log('Connected to the Database.');
});
app.get('/', function (req, res){
res.sendFile(__dirname + '/assets/index.html');
});
app.get('/warehouse', function(req, res){
let sqlCode = `SELECT * FROM warehouse`;
connection.query(sqlCode, function(err, rows){
res.send({
"result": "ok",
"clothes": rows
});
});
});
app.get('/price-check', function (req, res) {
let sqlCode = `SELECT * FROM warehouse WHERE item_name LIKE "%${req.query.item}%" AND size="${req.query.size}"`;
let quantity = req.query.quantity;
connection.query(sqlCode, function (err, row){
// WARNING!! FIGYELEM!! EZEKET AZÉRT KELLETT KI-COMMENT-ELNEM, MERT A FELADAT SQL FILE-JA NEM TARTALMAZOTT VALID DARABSZÁMOKAT! (Mindegyik értéke NULL volt! Ezt jeleztem is az itt lévő mentoroknak! Így nem lehet leellenőrizni, ill. nincs értelme. Azért bennehagyom, mert kb. így lett volna a megoldása...)
// if (row.in_store > req.query.quantity){
// res.send({
// "result": "ok",
// "total_price": quantity * row[0].unit_price
// });
// } else {
// res.send({
// "result": "error, we don't have enough items in store"
// });
// };
if (req.query.quantity < 3) {
res.send({
"result": "please order at least 3, one for yourself, two for your friends"
});
} else {
res.send({
"result": "ok",
"total_price": quantity * row[0].unit_price,
});
};
});
});
app.listen(3000, () => console.log('Server is running (port:3000)...'));