MangoShelf/Server/webserver.js

109 lines
3.4 KiB
JavaScript
Raw Permalink Normal View History

2022-09-20 19:21:47 +02:00
// Forces NodeJS to disable "Silent errors"
2022-09-20 12:19:01 +02:00
'use strict';
2022-09-16 21:57:13 +02:00
// Import things
const express = require('express');
const app = express();
const server = require('http').createServer(app);
const port = process.env.PORT || 3430;
2022-09-19 23:55:23 +02:00
const test = require('../testdatabase.json');
2022-09-20 12:19:01 +02:00
const bodyParser = require("body-parser");
var mysql = require('mysql');
2022-09-19 23:55:23 +02:00
var site;
// TODO: Add stuff for MySQL
2022-09-16 21:57:13 +02:00
2022-09-20 12:19:01 +02:00
var connection = mysql.createConnection({
host : '127.0.0.1',
user : 'demo',
2022-09-20 12:19:01 +02:00
password : 'lqklvok',
database : 'mangoshelfdb'
});
connection.query('SELECT * FROM stock', function (error, results, fields) {
if (error) throw error;
// connected!
2022-09-20 19:21:47 +02:00
console.log('MySQL connected!');
2022-09-20 12:19:01 +02:00
});
2022-09-16 21:57:13 +02:00
// Set the templates folder for the thingy
app.set('views', __dirname + '/../Templates');
app.set('view engine', 'ejs');
2022-09-20 12:19:01 +02:00
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
2022-09-16 21:57:13 +02:00
// Makes it so that the Public folder mounts under /
app.use(express.static(__dirname + '/../Public'));
// Main page
app.get('/', function(req, res){
2022-09-20 19:21:47 +02:00
res.redirect('/stock')
2022-09-19 23:55:23 +02:00
// console.log(test.cars.Nissan[1].model)
});
2022-09-20 12:19:01 +02:00
app.get('/stock', function(req, res){
console.log('🌐 Client requested ' + req.url);
connection.query('SELECT * FROM stock', function (error, results, fields) {
if (error) throw error;
// connected!
2022-09-20 19:21:47 +02:00
//console.log(results);
2022-09-20 12:19:01 +02:00
res.render('index', { results, site: 2 });
console.log('✅ Client recieved ' + req.url);
});
// console.log(test.cars.Nissan[1].model)
});
2022-09-20 19:21:47 +02:00
app.get('/stock/new', function(req, res){
var results = null;
2022-09-19 23:55:23 +02:00
console.log('🌐 Client requested ' + req.url);
2022-09-20 19:21:47 +02:00
res.render('index', { results, site: 21 });
2022-09-19 23:55:23 +02:00
// console.log(test.cars.Nissan[1].model)
2022-09-16 21:57:13 +02:00
});
2022-09-20 12:19:01 +02:00
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);
console.log('✅ Client recieved ' + req.url);
2022-09-20 19:21:47 +02:00
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');
});
2022-09-20 12:19:01 +02:00
if (name && desc && status && category && amount && location && orderid && notes) {
console.log('boop!');
}
// console.log(test.cars.Nissan[1].model)
});
2022-09-20 19:21:47 +02:00
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');
});
});
2022-09-20 12:19:01 +02:00
2022-09-16 21:57:13 +02:00
// Simple way to expose a function
exports = module.exports = rws;
// rws = RunWebServer
function rws() {
server.listen(port, () => {
console.log('✅ Webserver is running ( %d )', port);
});
};