function adminonly(req, res, next) {
    if (!req.session.isadmin) {
        return res.redirect('/');  // Return to homepage if accessing restricted area.
    } else {
        next();
    }
}

function renderError(res, err, code = 500) {
    console.log(err);
    res.status(code).render('error', {error: err});
}

function renderAllRecords(res, err, result, template) {
    if (err) {
        renderError(res, err);
    } else {
        res.render(template, {allrecs: result});
    }
}

function renderOneRecord(res, err, result, template) {
    if (err) {
        renderError(res, err);
    } else if (!result || result.length === 0) {
        res.status(404).render(template, {onerec: null});
    } else {
        res.render(template, {onerec: result[0]});
    }
}

module.exports = {adminonly, renderError, renderAllRecords, renderOneRecord}