// admin/link.js // Copyright (C) 2021 Digital Telepresence, LLC // License: Apache-2.0 'use strict'; const DTP_COMPONENT_NAME = 'admin:link'; const express = require('express'); const multer = require('multer'); const { SiteController, SiteError } = require('../../../lib/site-lib'); class LinkController extends SiteController { constructor (dtp) { super(dtp, DTP_COMPONENT_NAME); } async start ( ) { const router = express.Router(); const upload = multer({ dest: `/tmp/${this.dtp.config.domainKey}/uploads`}); router.use(async (req, res, next) => { res.locals.currentView = 'admin'; res.locals.adminView = 'link'; return next(); }); router.param('linkId', this.populateLinkId.bind(this)); router.post('/:linkId', upload.none(), this.postUpdateLink.bind(this)); router.get('/:linkId', this.getLinkView.bind(this)); router.get('/', this.getIndex.bind(this)); router.delete('/:linkId', this.deleteLink.bind(this)); return router; } async populateLinkId (req, res, next, linkId) { const { link: linkService } = this.dtp.services; try { res.locals.link = await linkService.getById(linkId); if (!res.locals.link) { throw new SiteError(404, 'Link not found'); } return next(); } catch (error) { this.log.error('failed to populate linkId', { linkId, error }); return next(error); } } async postUpdateLink (req, res) { const { link: linkService } = this.dtp.services; try { const displayList = this.createDisplayList('update-link'); await linkService.update(res.locals.link, req.body); displayList.showNotification( `Link ${res.locals.link.label} updated successfully`, 'success', 'bottom-center', 2000, ); res.status(200).json({ success: true, displayList }); } catch (error) { this.log.error('failed to update link', { linkId: res.locals.link._id, error }); return res.status(error.statusCode || 500).json({ success: false, message: error.message, }); } } async getLinkView (req, res) { res.render('admin/link/editor'); } async getIndex (req, res, next) { const { link: linkService } = this.dtp.services; try { res.locals.totalLinkCount = await linkService.getTotalCount(); res.locals.pagination = this.getPaginationParameters(req, 50); res.locals.links = await linkService.getAdmin(res.locals.pagination); res.render('admin/link/index'); } catch (error) { this.log.error('failed to fetch links', { error }); return next(error); } } async deleteLink (req, res) { const { link: linkService } = this.dtp.services; try { const displayList = this.createDisplayList('delete-link'); await linkService.remove(res.locals.link); displayList.removeElement(`li[data-link-id="${res.locals.link._id}"]`); displayList.showNotification( `Link "${res.locals.link.title}" deleted`, 'success', 'bottom-center', 3000, ); res.status(200).json({ success: true, displayList }); } catch (error) { this.log.error('failed to delete link', { linkId: res.local.link._id, error, }); res.status(error.statusCode || 500).json({ success: false, message: error.message, }); } } } module.exports = async (dtp) => { let controller = new LinkController(dtp); return controller; };