-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbitcone-admin.js
134 lines (111 loc) · 3.05 KB
/
bitcone-admin.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
var express = require("express");
var path = require("path");
var bodyParser = require("body-parser");
var cons = require("consolidate");
var dust = require("dustjs-helpers");
var pg = require("pg");
var QRCode = require('qrcode');
var uuid = require("uuid");
var randomItem = require('random-item');
var flavors = require("./flavors.json");
var app = express();
// temporary database stuff
const config = {
user: 'node',
database: 'twoscoopgames',
password: 'node123',
port: 5432
};
const pool = new pg.Pool(config);
app.engine('dust', cons.dust);
app.set('view engine', 'dust');
app.set('views', __dirname + '/views');
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
function createBitconeQR(bitconeuuid) {
QRCode.toFile(`./public/qrcodes/${bitconeuuid}.png`, `http://twoscoopgames.com/bitcone?id=${bitconeuuid}`, {
color: {
dark: '#000',
light: '#0000'
}
}, function (err) {
if (err) throw err
console.log('done');
});
}
app.get('/', (req, res, next) => {
pool.connect( (err, client, done) => {
if (err) {
console.log("There was an error connecting to the database", err);
}
client.query('SELECT * FROM bitcones ORDER BY created', (err, result) => {
if (err) {
console.log(err);
res.status(400).send(err);
}
result.rows.map( (row) => {
row.qrcode = '/qrcodes/' + row.uuid + '.png';
});
res.render('index', { bitcones: result.rows });
done();
});
});
});
app.post('/add', (req, res) => {
pool.connect( (err, client, done) =>{
if (err) {
console.log("There was an error connecting to the database", err);
}
console.log(req.body.quantity);
for (var i = 0; i < req.body.quantity; i++){
addBitcone(client, req.body.series, req.body.owner);
}
done();
res.redirect('/');
});
});
function addBitcone(client, series, owner){
var newBitconeId = uuid();
client.query(
"INSERT INTO bitcones(uuid, series, flavor, owner) VALUES($1, $2, $3, $4)",
[ newBitconeId, series, randomItem(flavors), owner]
);
createBitconeQR(newBitconeId);
}
app.delete('/delete/:uuid', (req, res) => {
pool.connect( (err, client, done) => {
if (err) {
console.log("There was an error connecting to the database", err);
}
client.query('DELETE FROM bitcones WHERE uuid = $1', [req.params.uuid]);
done();
res.sendStatus(200);
});
});
app.post('/edit', (req, res) => {
pool.connect( (err, client, done) => {
if (err) {
console.log("There was an error connecting to the database", err);
}
console.log(
req.body.uuid,
req.body.series,
req.body.flavor,
req.body.owner
);
client.query('UPDATE bitcones SET series=$2, flavor=$3, owner=$4 WHERE uuid = $1',
[
req.body.uuid,
req.body.series,
req.body.flavor,
req.body.owner
]
);
done();
res.redirect('/');
});
});
app.listen(3000, () => {
console.log('Server Started on port 3000');
});