diff --git a/Components/webserver.js b/Components/webserver.js index 7902cc0..63a106d 100644 --- a/Components/webserver.js +++ b/Components/webserver.js @@ -1,16 +1,37 @@ +// Forces NodeJS to disable "Silent errors", in other words, don't +'use strict'; + // Import things const express = require('express'); const app = express(); const server = require('http').createServer(app); const port = process.env.PORT || 3430; const test = require('../testdatabase.json'); +const bodyParser = require("body-parser"); +var mysql = require('mysql'); var site; // TODO: Add stuff for MySQL +var connection = mysql.createConnection({ + host : '127.0.0.1', + user : 'demo', + password : 'lqklvok', + database : 'mangoshelfdb' +}); + + +connection.query('SELECT * FROM stock', function (error, results, fields) { + if (error) throw error; + // connected! + console.log(results); +}); + // Set the templates folder for the thingy app.set('views', __dirname + '/../Templates'); app.set('view engine', 'ejs'); +app.use(bodyParser.urlencoded({ extended: false })); +app.use(bodyParser.json()); // Makes it so that the Public folder mounts under / app.use(express.static(__dirname + '/../Public')); @@ -18,18 +39,51 @@ app.use(express.static(__dirname + '/../Public')); // Main page app.get('/', function(req, res){ console.log('🌐 Client requested ' + req.url); - res.render('index', { test, site: 1 }); + res.render('index', { results, site: 1 }); console.log('✅ Client recieved ' + req.url); // console.log(test.cars.Nissan[1].model) }); -app.get('/inventory', function(req, res){ +app.get('/stock', function(req, res){ console.log('🌐 Client requested ' + req.url); - res.render('index', { test, site: 2 }); + connection.query('SELECT * FROM stock', function (error, results, fields) { + if (error) throw error; + // connected! + console.log(results); + res.render('index', { results, site: 2 }); + console.log('✅ Client recieved ' + req.url); + }); + // console.log(test.cars.Nissan[1].model) +}); + +app.get('/subbb', function(req, res){ + console.log('🌐 Client requested ' + req.url); + res.render('additem'); console.log('✅ Client recieved ' + req.url); // console.log(test.cars.Nissan[1].model) }); +app.post('/api/live/inventory/add', function(req, res){ + let name = req.body.name; + let desc = req.body.desc; + let status = req.body.status; + let category = req.body.category; + let amount = req.body.amount; + let location = req.body.location; + let orderid = req.body.orderid; + let notes = req.body.notes; + console.log('🌐 Client requested ' + req.url + ' with id ' + req.params.id); + res.render('index', { test, site: 1 }); + console.log('✅ Client recieved ' + req.url); + + connection.query("INSERT INTO `stock` (`name`, `description`, `status`, `category`, `amount`, `location`, `expiry`, `lastupdate`, `orderid`, `notes`) VALUES (${name}, '${desc}', ${status}, ${category}, ${amount}, ${location}, NULL, ${Date.now()}, ${orderid}, '${notes}');", function(error) {if (error){throw error;}}) + if (name && desc && status && category && amount && location && orderid && notes) { + console.log('boop!'); + } + // console.log(test.cars.Nissan[1].model) +}); + + // Simple way to expose a function exports = module.exports = rws; diff --git a/Templates/additem.ejs b/Templates/additem.ejs new file mode 100644 index 0000000..37ddc01 --- /dev/null +++ b/Templates/additem.ejs @@ -0,0 +1,30 @@ + + +
+ + + +