-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
81 lines (70 loc) · 2.47 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const express = require('express');
const bodyParser = require('body-parser');
const qrcode = require('qrcode');
const app = express();
const port = 3000;
app.use(express.urlencoded({ extended: false }));
app.set('view engine', 'ejs');
app.use(express.static('public'));
const { Pool } = require("pg")
const dotenv = require("dotenv")
dotenv.config()
// SET UP DATABASE LINK
const pool = new Pool({
user: process.env.PGUSER,
host: process.env.PGHOST,
database: process.env.PGDATABASE,
password: process.env.PGPASSWORD,
port: process.env.PGPORT
});
pool.connect();
// Pass Number Function
let passNumberFunction = function () {
var result = '';
var characters = 'A0Z19C7XD2W8ETF7GU3HS6IRJ5Q40KP5LO3M9MN9B826OPA1Q4BD3RF0ES2GTI9UH0VJW1XL4YN7Z';
for (var i = 0; i < 8; i++) {
result += characters.charAt(Math.floor(Math.random() * 8));
}
return result;
}
let passNumber = passNumberFunction();
let timestamp = new Date();
// TESTING TO REMOVE DUPLICATE PASS NUMBERS
// let passNumberFunction = function () {
// var result = '';
// var characters = 'A0Z19C7XD2W8ETF7GU3HS6IRJ5Q40KP5LO3M9MN9B826OPA1Q4BD3RF0ES2GTI9UH0VJW1XL4YN7Z';
// for (var i = 0; i < 8; i++) {
// result += characters.charAt(Math.floor(Math.random() * 8));
// }
// let firstTryPassNumber = pool.query('SELECT pass_number FROM lounge_guest', (err, results) => {
// return results.rows;
// })
// if (result != firstTryPassNumber) {
// return result;
// } else {
// passNumberFunction();
// }
// }
// let passNumber = passNumberFunction();
// let timestamp = new Date();
app.get('/', (req, res) => {
res.render('index.ejs');
});
app.post('/guest_details_submit', (req, res) => {
let surname = req.body.surname;
let firstName = req.body.firstName;
let email = req.body.email;
let qrCodeNumber = [passNumber + " " + surname];
pool.query(
"INSERT INTO lounge_guest (pass_number, surname, first_name, email, timestamp, qr_code_number) VALUES ($1, $2, $3, $4, $5, $6) returning *",
[passNumber, surname, firstName, email, timestamp, qrCodeNumber])
let loungeGuest = [passNumber, surname, firstName, email];
console.log(loungeGuest);
qrcode.toDataURL(qrCodeNumber, (err, src) => {
res.render('createPDF.ejs', { loungeGuest: loungeGuest, qrcodepass: src })
});
// res.status(201).send("data is sent");
});
app.listen(port, () => {
console.log(`App listening to port ${port}`);
})