A web application allowing people to create an account, configure a profile, and share a list of URLs on that profile.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

111 lines
3.0 KiB

// 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 { SiteController, SiteError } = require('../../../lib/site-lib');
class LinkController 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 = 'link';
return next();
});
router.param('linkId', this.populateLinkId.bind(this));
router.post('/:linkId', 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, next) {
const { link: linkService } = this.dtp.services;
try {
await linkService.update(res.locals.link, req.body);
res.redirect('/admin/link');
} catch (error) {
this.log.error('failed to update link', { linkId: res.locals.link._id, error });
return next(error);
}
}
async getLinkView (req, res) {
res.render('admin/link/view');
}
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, displayEngine: displayEngineService } = this.dtp.services;
try {
const displayList = displayEngineService.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;
};