const router = require('express').Router() const fs = require('fs') const path = require('path') module.exports = (plugin, app) => { router.get(['/db/:collection', '/db/:collection/:type', '/db/:collection/:type/:name'], async function(req, res) { let collection = app.live.db.getCollection(req.params.collection).chain() let results = [] let error = '' let errorCode = 0 if (!collection) { // TODO: 404 - collection missing } else { if (req.params.name) { results = collection.find({type: req.params.type, name: req.params.name}) if (results.length === 0) { error = `No such entry "${req.params.name}"` errorCode = 404 } } else if (req.params.type) { results = collection.find({type: req.params.type}) if (results.length === 0) { error = `No such collection type "${req.params.type}"` errorCode = 404 } } else { results = collection.find({}) if (results.length === 0) { error = `Collection empty` errorCode = 404 } } } if (req.query['sort-by']) { results.simplesort(req.query['sort-by']) } results = results.data() let content if (errorCode > 0) { content = error } else { content = app.dot.tables({...req.params, results, type: req.params.type, name: req.params.name, collection: req.params.collection}) } // build the location let loc = ''+app.live.conf['shortname']+'' let crumbs = req.path.split('/') let title = [...crumbs].reverse().filter(v=>v!==''&&v!=='db').map(v=>v.charAt(0).toUpperCase()+v.slice(1)).join(' - ') for (let i = 1; i < crumbs.length; i++) { if (crumbs[i] == '') continue loc += ' → '+(crumbs[i].replace('-', ' '))+'' } res.render('index', { content, menu: app.live.menu, location: loc, title: title, www_name: app.live.conf.www_name, www_copyright: app.live.conf.www_copyright, scripts: ['/js/gear.js'], }) }) plugin.log('added aedifex route') return router }