67 lines
2.1 KiB
JavaScript
67 lines
2.1 KiB
JavaScript
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)
|
|
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
|
|
}
|
|
}
|
|
}
|
|
//console.log(results, error, errorCode)
|
|
|
|
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 = '<a href="/">'+app.live.conf['shortname']+'</a>'
|
|
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 += ' → <a href="'+crumbs.slice(0,i).join('/')+'/'+crumbs[i]+'/">'+(crumbs[i].replace('-', ' '))+'</a>'
|
|
}
|
|
|
|
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
|
|
}
|