add guestbook entries

This commit is contained in:
b1ek 2023-02-20 01:12:09 +10:00
parent 4f12994bca
commit 7f674379d2
7 changed files with 52 additions and 35 deletions

View File

@ -31,15 +31,8 @@ module.exports = {
allowNull: false allowNull: false
}, },
time: { time: {
type: DataTypes.BIGINT type: DataTypes.BIGINT,
}, allowNull: false
createdAt: {
allowNull: false,
type: DataTypes.DATE
},
updatedAt: {
allowNull: false,
type: DataTypes.DATE
} }
}); });
}, },

View File

@ -46,7 +46,7 @@ module.exports = (sequelize, DataTypes) => {
} }
}, { }, {
sequelize, sequelize,
modelName: 'Guestbook', modelName: 'Guestbook'
}); });
return Guestbook; return Guestbook;
}; };

View File

@ -20,8 +20,11 @@ config = {
username: DB_USERNAME || config.username, username: DB_USERNAME || config.username,
password: DB_PASSWORD || config.password, password: DB_PASSWORD || config.password,
database: DB_DATABASE || config.database, database: DB_DATABASE || config.database,
host: DB_HOSTNAME || config.host host: DB_HOSTNAME || config.host,
} define: {
timestamps: false
}
};
let sequelize; let sequelize;
if (config.use_env_variable) { if (config.use_env_variable) {

View File

@ -0,0 +1,11 @@
.gb_entry_text {
padding: 0 8px;
vertical-align: middle;
}
.gb_entry_text .gb_entry_text_title {
font-size:9pt;
font-weight:bold;
margin:0;
padding:0;
padding-top:1em
}

View File

@ -3,21 +3,23 @@ const Sequelize = require('../models');
async function handler(req, res, next) { async function handler(req, res, next) {
try { try {
let data = {};
let sqldata = await Sequelize.Guestbook.findAll({
where: {
hidden: false
}
});
if (!sqldata) throw new Error('Failed to get guestbook entries');
for (let i = 0; i != sqldata.length; i++) {
data[sqldata[i].id] = sqldata[i];
}
res.send(await Helpers.ViewLoader.load('guestbook.pug', { res.send(await Helpers.ViewLoader.load('guestbook.pug', {
current_route: req.originalUrl, current_route: req.originalUrl,
ip: req.ip, ip: req.ip,
data: { data
// TODO: load from db
1: {
name: 'John Doe',
email: 'a@b.c',
text: 'hiiii',
hidemail: false,
ip: '0.0.0.0',
hidden: false,
time: Date.now()
}
}
})); }));
return; return;
} catch (err) { } catch (err) {
@ -25,15 +27,22 @@ async function handler(req, res, next) {
} }
} }
async function submit(req, res) { async function submit(req, res, next) {
const { name, email, message } = req.body; const { name, email, message } = req.body;
const hidemail = req.body.hidemail ? (req.body.hidemail == 'on' ? true : false) : false; const hidemail = req.body.hidemail ? (req.body.hidemail == 'on' ? true : false) : false;
res.send({ let data = await Sequelize.Guestbook.create({
name, email, message, hidemail name,
email,
text: message,
hidemail,
ip: req.ip,
hidden: false,
time: Math.floor(Date.now() / 1000)
}); });
if (!data) next(new Error('Failed to create a new record.'));
// console.log(Sequelize.Guestbook); res.redirect('/guestbook#gb_entry_' + data.id);
return; return;
} }

View File

@ -3,6 +3,7 @@ block root
- var title = 'Guestbook' - var title = 'Guestbook'
block head block head
<link rel='stylesheet' href='/static/ui/send_button.css'> <link rel='stylesheet' href='/static/ui/send_button.css'>
<link rel='stylesheet' href='/static/ui/gb_ui.css'>
block content block content
h1 Guestbook h1 Guestbook
p. p.
@ -55,8 +56,8 @@ block content
Sender: #{entry.name}<br/> Sender: #{entry.name}<br/>
Email: #{entry.email}<br/> Email: #{entry.email}<br/>
IP: #{entry.ip}<br/> IP: #{entry.ip}<br/>
Date: #{new Date(entry.time).toISOString()}<br/> // Date: #{new Date(entry.time).toISOString()}<br/>
td(width='80%' style='padding:0 8px') td(width='80%' class='gb_entry_text')
p(style='font-size:9pt;font-weight:bold;margin:0;padding:0;padding-top:1em') Message: p(class='gb_entry_text_title') Message:
p(style='margin:0;padding:0;font-size:10pt'). p(style='margin:0;padding:0;font-size:10pt').
hiii hiii

View File

@ -10,14 +10,14 @@ block root
} }
doctype html doctype html
html html(style='overflow-y:auto')
head head
title blek! Site #{title ? "- " + title : ""} title blek! Site #{title ? "- " + title : ""}
link(rel='stylesheet' href='/static/main.css') link(rel='stylesheet' href='/static/main.css')
block head block head
body body(style='overflow-y:auto')
table(width='100%' height='100%' class='body_table') table(width='100%' height='100%' class='body_table')
tr tr
td(class='side_menu') td(class='side_menu')