1
0
mirror of https://github.com/matt-fidd/stratos.git synced 2026-01-01 18:39:32 +00:00

Refactored database connection system to use a shared pool per-request

This commit is contained in:
2022-03-23 23:29:55 +00:00
parent bd662661ee
commit 4c2a078530
14 changed files with 162 additions and 144 deletions

View File

@@ -13,7 +13,7 @@ router.get('/', (req, res) => {
});
router.get('/dashboard', async (req, res) => {
const u = await new User(null, req.session.userId);
const u = await new User(req.db, null, req.session.userId);
const recentTests = await u.getTests({ range: 'before' });
const upcomingTests = await u.getTests({ range: 'after' });
@@ -52,7 +52,7 @@ router.get('/dashboard', async (req, res) => {
router.all(/user\/(.{36})(\/.*)?/, async (req, res, next) => {
let u;
try {
u = await new User(null, req.params[0]);
u = await new User(req.db, null, req.params[0]);
} catch (e) {
return res.status(400).render('error', {
title: 'Stratos - Error',
@@ -63,14 +63,18 @@ router.all(/user\/(.{36})(\/.*)?/, async (req, res, next) => {
});
}
if (!await u.hasAccess(await new User(null, req.session.userId)))
if (!await u.hasAccess(await new User(
req.db,
null,
req.session.userId
)))
return res.redirect('/admin/dashboard');
next();
});
router.get('/user/:id', async (req, res) => {
const u = await new User(null, req.params.id);
const u = await new User(req.db, null, req.params.id);
return res.render('user', {
title: `Stratos - ${u.shortName}`,

View File

@@ -11,7 +11,7 @@ const User = require('../lib/User');
const Subject = require('../lib/Subject');
router.get('/classes', async (req, res) => {
const u = await new User(null, req.session.userId);
const u = await new User(req.db, null, req.session.userId);
return res.render('classes', {
title: 'Stratos - Classes',
@@ -23,7 +23,7 @@ router.get('/classes', async (req, res) => {
});
router.get('/class/add', async (req, res) => {
const subjects = await Subject.getAllSubjects();
const subjects = await Subject.getAllSubjects(req.db);
res.render('addClass', {
title: 'Stratos - Add class',
@@ -34,7 +34,7 @@ router.get('/class/add', async (req, res) => {
});
router.post('/class/add', async (req, res) => {
const a = await new Account(req.session.userId);
const a = await new Account(req.db, req.session.userId);
let fields;
try {
@@ -59,7 +59,7 @@ router.post('/class/add', async (req, res) => {
router.all(/class\/(.{36})(\/.*)?/, async (req, res, next) => {
let c;
try {
c = await new Class(req.params[0]);
c = await new Class(req.db, req.params[0]);
} catch (e) {
return res.status(400).render('error', {
title: 'Stratos - Error',
@@ -70,14 +70,17 @@ router.all(/class\/(.{36})(\/.*)?/, async (req, res, next) => {
});
}
if (!await c.hasAccess(await new User(null, req.session.userId)))
if (!await c.hasAccess(await new User(req.db,
null,
req.session.userId
)))
return res.redirect('/admin/classes');
next();
});
router.get('/class/:id', async (req, res) => {
const c = await new Class(req.params.id);
const c = await new Class(req.db, req.params.id);
const linkRoot = `/admin/class/${c.id}`;
const upcomingTests = await c.getTests({ range: 'after' });
const recentTests = await c.getTests({ range: 'before' });
@@ -127,7 +130,7 @@ router.get('/class/:id', async (req, res) => {
});
router.get('/class/:id/teachers', async (req, res) => {
const c = await new Class(req.params.id);
const c = await new Class(req.db, req.params.id);
return res.render('classUsers', {
title: `Stratos - ${c.name}`,
@@ -143,7 +146,7 @@ router.get('/class/:id/teachers', async (req, res) => {
});
router.get('/class/:id/members', async (req, res) => {
const c = await new Class(req.params.id);
const c = await new Class(req.db, req.params.id);
return res.render('classUsers', {
title: `Stratos - ${c.name}`,

View File

@@ -69,7 +69,10 @@ router.post('/login', async (req, res) => {
return res.status(400).json({ status: 'Invalid' });
}
const u = await User.getUserByEmail(fields.get('email')) ?? false;
const u = await User.getUserByEmail(
req.db,
fields.get('email')
) ?? false;
if (!u)
return res.redirect('/login');
@@ -106,6 +109,7 @@ router.post('/register', async (req, res) => {
let a;
try {
a = await Account.createAccount(
req.db,
fields.get('fname'),
fields.get('onames'),
fields.get('lname'),
@@ -140,7 +144,10 @@ router.post('/password-reset', async (req, res) => {
console.error(e);
}
const u = await User.getUserByEmail(fields.get('email')) ?? false;
const u = await User.getUserByEmail(
req.db,
fields.get('email')
) ?? false;
if (!u)
return res.redirect('/password-reset');
@@ -170,7 +177,7 @@ router.get('/password-reset/:uuid/:token', async (req, res) => {
let pr;
try {
pr = await new PasswordReset(uuid, token);
pr = await new PasswordReset(req.db, uuid, token);
} catch (e) {
console.error(e);
return res.redirect('/password-reset');
@@ -211,6 +218,7 @@ router.post('/change-password', async (req, res) => {
let pr;
try {
pr = await new PasswordReset(
req.db,
fields.get('uuid'),
fields.get('token')
);

View File

@@ -10,7 +10,7 @@ const User = require('../lib/User');
const Test = require('../lib/Test');
router.get('/tests', async (req, res) => {
const u = await new User(null, req.session.userId);
const u = await new User(req.db, null, req.session.userId);
return res.render('tests', {
title: 'Stratos - Tests',
@@ -22,7 +22,7 @@ router.get('/tests', async (req, res) => {
});
router.get('/test/add', async (req, res) => {
const a = await new Account(req.session.userId);
const a = await new Account(req.db, req.session.userId);
const promises = [
a.getTestTemplates(),
@@ -58,7 +58,10 @@ router.post('/test/add', async (req, res) => {
}
const testTemplateId = fields.get('testTemplate');
const tt = await new (require('../lib/TestTemplate'))(testTemplateId);
const tt = await new (require('../lib/TestTemplate'))(
req.db,
testTemplateId
);
const t = await tt.assignClass(
fields.get('class'),
@@ -76,7 +79,7 @@ router.get('/testTemplate/add', (req, res) => {
});
router.post('/testTemplate/add', async (req, res) => {
const a = await new Account(req.session.userId);
const a = await new Account(req.db, req.session.userId);
let fields;
try {
@@ -94,8 +97,11 @@ router.post('/testTemplate/add', async (req, res) => {
try {
await a.createTestTemplate(
fields.get('name'),
fields.get('mark'));
fields.get('mark')
);
} catch (e) {
console.error(e);
return res.render('error', {
title: 'Stratos - Error',
current: 'Tests',
@@ -110,7 +116,7 @@ router.post('/testTemplate/add', async (req, res) => {
router.all(/test\/(.{36})(\/.*)?/, async (req, res, next) => {
let t;
try {
t = await new Test(req.params[0]);
t = await new Test(req.db, req.params[0]);
} catch (e) {
return res.status(400).render('error', {
title: 'Stratos - Error',
@@ -121,14 +127,17 @@ router.all(/test\/(.{36})(\/.*)?/, async (req, res, next) => {
});
}
if (!await t.hasAccess(await new User(null, req.session.userId)))
if (!await t.hasAccess(await new User(
req.db, null,
req.session.userId
)))
return res.redirect('/admin/tests');
next();
});
router.get('/test/:id', async (req, res) => {
const t = await new Test(req.params.id);
const t = await new Test(req.db, req.params.id);
const linkRoot = `/admin/test/${t.id}`;
return res.render('test', {