From 70333d72c88b1d7ade4f5d63e4dcf67a6c8bf481 Mon Sep 17 00:00:00 2001 From: nightly Date: Tue, 20 Sep 2022 19:21:47 +0200 Subject: [PATCH] Finalize project for demo. --- Components/webserver.js | 42 ++++++++++++++-------- Dockerfile | 18 ++++++++++ Templates/additem.ejs | 30 ---------------- Templates/index.ejs | 78 ++++++++++++++++++++++++++++++++++++----- 4 files changed, 116 insertions(+), 52 deletions(-) create mode 100644 Dockerfile delete mode 100644 Templates/additem.ejs diff --git a/Components/webserver.js b/Components/webserver.js index 63a106d..217030d 100644 --- a/Components/webserver.js +++ b/Components/webserver.js @@ -1,4 +1,4 @@ -// Forces NodeJS to disable "Silent errors", in other words, don't +// Forces NodeJS to disable "Silent errors" 'use strict'; // Import things @@ -14,8 +14,8 @@ var site; // TODO: Add stuff for MySQL var connection = mysql.createConnection({ - host : '127.0.0.1', - user : 'demo', + host : '192.168.0.56', + user : 'mangosdemo', password : 'lqklvok', database : 'mangoshelfdb' }); @@ -24,7 +24,7 @@ var connection = mysql.createConnection({ connection.query('SELECT * FROM stock', function (error, results, fields) { if (error) throw error; // connected! - console.log(results); + console.log('MySQL connected!'); }); // Set the templates folder for the thingy @@ -38,9 +38,7 @@ app.use(express.static(__dirname + '/../Public')); // Main page app.get('/', function(req, res){ - console.log('🌐 Client requested ' + req.url); - res.render('index', { results, site: 1 }); - console.log('✅ Client recieved ' + req.url); + res.redirect('/stock') // console.log(test.cars.Nissan[1].model) }); @@ -49,17 +47,16 @@ app.get('/stock', function(req, res){ connection.query('SELECT * FROM stock', function (error, results, fields) { if (error) throw error; // connected! - console.log(results); + //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){ +app.get('/stock/new', function(req, res){ + var results = null; console.log('🌐 Client requested ' + req.url); - res.render('additem'); - console.log('✅ Client recieved ' + req.url); + res.render('index', { results, site: 21 }); // console.log(test.cars.Nissan[1].model) }); @@ -73,16 +70,33 @@ app.post('/api/live/inventory/add', function(req, res){ 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;}}) + var sql = "INSERT INTO stock (name, description, status, category, amount, location, orderid, notes) VALUES (?)"; + var values = [name, desc, status, category, amount, location, orderid, notes]; + connection.query(sql, [values], function (err, result) { + if (err) throw err; + console.log("Number of records inserted: " + result.affectedRows); + res.redirect('/stock'); + }); if (name && desc && status && category && amount && location && orderid && notes) { console.log('boop!'); } // console.log(test.cars.Nissan[1].model) }); +app.get('/api/live/inventory/delete/:thinGid', function(req, res){ + var idtodelete = req.params.thinGid; + console.log(idtodelete); + var sql = "DELETE FROM stock WHERE id = ?"; + var values = [idtodelete]; + connection.query(sql, [values], function (err, result) { + if (err) throw err; + console.log("Number of records inserted: " + result.affectedRows); + res.redirect('/stock'); + }); +}); + // Simple way to expose a function exports = module.exports = rws; diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..fd41420 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,18 @@ +# Use node as the "base" for our project +FROM node:17 +# Create app directory +WORKDIR /usr/src/app +# Enviroment variables +# (https://docs.docker.com/engine/reference/builder/ go here and scroll down bla bla bla) +# Install app dependencies +# A wildcard is used to ensure both package.json AND package-lock.json are copied +COPY package*.json ./ +RUN npm i +# If you are building your code for production +# RUN npm ci --only=production +# Bundle app source +COPY . . +# Expose port 3000 +EXPOSE 3430 +# Run it. +CMD [ "node", "index.js" ] \ No newline at end of file diff --git a/Templates/additem.ejs b/Templates/additem.ejs deleted file mode 100644 index 37ddc01..0000000 --- a/Templates/additem.ejs +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - Document - - -
- - - - - - - - - - - - - - - - - -
- - \ No newline at end of file diff --git a/Templates/index.ejs b/Templates/index.ejs index 652ecc4..d81b05f 100644 --- a/Templates/index.ejs +++ b/Templates/index.ejs @@ -19,21 +19,83 @@
+ <% if(site == 21) { %> +

New item in stock

+
+
+
+ + +
Name for the item.
+
+
+ + +
Description for the item.
+
+
+ + +
+
+ + +
+
+ + +
Amount of this item.
+
+
+ + +
+
+ + +
Amount of this item.
+
+
+ + +
Write a note for the item.
+
+ +
+ <% } %> <% if(site == 2) { %>

Inventory

+
+ @@ -65,7 +127,7 @@ - + <% } %>
<%= results[i].lastupdate %> <%= results[i].orderid %> <%= results[i].notes %>