// admin/domain.js // Copyright (C) 2021 Digital Telepresence, LLC // License: Apache-2.0 'use strict'; const express = require('express'); const { /*SiteError,*/ SiteController } = require('../../../lib/site-lib'); class DomainController extends SiteController { constructor (dtp) { super(dtp, 'admin:domain'); } async start ( ) { const router = express.Router(); router.use(async (req, res, next) => { res.locals.currentView = 'admin'; res.locals.adminView = 'domain'; return next(); }); router.param('domainId', this.populateDomainId.bind(this)); router.post('/:domainId', this.postUpdateDomain.bind(this)); router.post('/', this.postCreateDomain.bind(this)); router.get('/create', this.getCreateForm.bind(this)); router.get('/:domainId', this.getDomainView.bind(this)); router.get('/', this.getHomeView.bind(this)); return router; } async populateDomainId (req, res, next, domainId) { const { domain: domainService } = this.dtp.services; try { res.locals.domain = await domainService.getById(domainId); return next(); } catch (error) { return next(error); } } async postUpdateDomain (req, res, next) { const { domain: domainService } = this.dtp.services; try { await domainService.update(res.locals.domain, req.body); res.redirect('/admin/domain'); } catch (error) { return next(error); } } async postCreateDomain (req, res, next) { const { domain: domainService } = this.dtp.services; try { res.locals.domain = await domainService.create(req.body); res.redirect(`/admin/domain/${res.locals.domain._id}`); } catch (error) { return next(error); } } async getDomainView (req, res) { res.render('admin/domain/form'); } async getCreateForm (req, res) { res.render('admin/domain/form'); } async getHomeView (req, res, next) { const { domain: domainService } = this.dtp.services; try { res.locals.pagination = this.getPaginationParameters(req, 10); res.locals.domains = await domainService.getDomains(res.locals.pagination); res.render('admin/domain/index'); } catch (error) { return next(error); } } } module.exports = async (dtp) => { let controller = new DomainController(dtp); return controller; };