srd/plugins/srd-aedifex-router/route.js

57 lines
1.6 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})
}
res.render('index', {
content,
menu: app.live.menu,
title: 'gear',
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
}