// admin/host.js // Copyright (C) 2021 Digital Telepresence, LLC // License: Apache-2.0 'use strict'; const DTP_COMPONENT_NAME = 'admin:host'; const express = require('express'); const mongoose = require('mongoose'); const NetHost = mongoose.model('NetHost'); const NetHostStats = mongoose.model('NetHostStats'); const { /*SiteError,*/ SiteController } = require('../../../lib/site-lib'); class HostController extends SiteController { constructor (dtp) { super(dtp, DTP_COMPONENT_NAME); } async start ( ) { const router = express.Router(); router.use(async (req, res, next) => { res.locals.currentView = 'admin'; res.locals.adminView = 'host'; return next(); }); router.param('hostId', this.populateHostId.bind(this)); router.get('/:hostId', this.getHostView.bind(this)); router.get('/', this.getHomeView.bind(this)); return router; } async populateHostId (req, res, next, hostId) { try { res.locals.host = await NetHost.findOne({ _id: hostId }); return next(); } catch (error) { return next(error); } } async getHostView (req, res, next) { try { res.locals.stats = await NetHostStats .find({ host: res.locals.host._id }) .sort({ created: -1 }) .limit(30) .lean(); res.locals.stats = res.locals.stats.reverse(); res.render('admin/host/view'); } catch (error) { return next(error); } } async getHomeView (req, res, next) { try { res.locals.hosts = await NetHost.find({ status: { $ne: 'inactive' } }); res.render('admin/host/index'); } catch (error) { return next(error); } } } module.exports = async (dtp) => { let controller = new HostController(dtp); return controller; };