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.
122 lines
3.4 KiB
122 lines
3.4 KiB
// admin/post.js
|
|
// Copyright (C) 2021 Digital Telepresence, LLC
|
|
// License: Apache-2.0
|
|
|
|
'use strict';
|
|
|
|
const DTP_COMPONENT_NAME = 'admin:post';
|
|
const express = require('express');
|
|
|
|
const { SiteController, SiteError } = require('../../../lib/site-lib');
|
|
|
|
class PostController 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 = 'post';
|
|
return next();
|
|
});
|
|
|
|
router.param('postId', this.populatePostId.bind(this));
|
|
|
|
router.post('/:postId', this.postUpdatePost.bind(this));
|
|
router.post('/', this.postCreatePost.bind(this));
|
|
|
|
router.get('/compose', this.getComposer.bind(this));
|
|
router.get('/:postId', this.getComposer.bind(this));
|
|
|
|
router.get('/', this.getIndex.bind(this));
|
|
|
|
return router;
|
|
}
|
|
|
|
async populatePostId (req, res, next, postId) {
|
|
const { post: postService } = this.dtp.services;
|
|
try {
|
|
res.locals.post = await postService.getById(postId);
|
|
if (!res.locals.post) {
|
|
throw new SiteError(404, 'Post not found');
|
|
}
|
|
return next();
|
|
} catch (error) {
|
|
this.log.error('failed to populate postId', { postId, error });
|
|
return next(error);
|
|
}
|
|
}
|
|
|
|
async postUpdatePost (req, res, next) {
|
|
const { post: postService } = this.dtp.services;
|
|
try {
|
|
await postService.update(res.locals.post, req.body);
|
|
res.redirect('/admin/post');
|
|
} catch (error) {
|
|
this.log.error('failed to update post', { newletterId: res.locals.post._id, error });
|
|
return next(error);
|
|
}
|
|
}
|
|
|
|
async postCreatePost (req, res, next) {
|
|
const { post: postService } = this.dtp.services;
|
|
try {
|
|
await postService.create(req.user, req.body);
|
|
res.redirect('/admin/post');
|
|
} catch (error) {
|
|
this.log.error('failed to create post', { error });
|
|
return next(error);
|
|
}
|
|
}
|
|
|
|
async getComposer (req, res) {
|
|
res.render('admin/post/editor');
|
|
}
|
|
|
|
async getIndex (req, res, next) {
|
|
const { post: postService } = this.dtp.services;
|
|
try {
|
|
res.locals.pagination = this.getPaginationParameters(req, 20);
|
|
res.locals.posts = await postService.getPosts(res.locals.pagination, ['draft', 'published', 'archived']);
|
|
res.render('admin/post/index');
|
|
} catch (error) {
|
|
this.log.error('failed to fetch posts', { error });
|
|
return next(error);
|
|
}
|
|
}
|
|
|
|
async deletePost (req, res) {
|
|
const { post: postService, displayEngine: displayEngineService } = this.dtp.services;
|
|
try {
|
|
const displayList = displayEngineService.createDisplayList('delete-post');
|
|
|
|
await postService.deletePost(res.locals.post);
|
|
|
|
displayList.removeElement(`li[data-post-id="${res.locals.post._id}"]`);
|
|
displayList.showNotification(
|
|
`Post "${res.locals.post.title}" deleted`,
|
|
'success',
|
|
'bottom-center',
|
|
3000,
|
|
);
|
|
res.status(200).json({ success: true, displayList });
|
|
} catch (error) {
|
|
this.log.error('failed to delete post', {
|
|
postId: res.local.post._id,
|
|
error,
|
|
});
|
|
res.status(error.statusCode || 500).json({
|
|
success: false,
|
|
message: error.message,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = async (dtp) => {
|
|
let controller = new PostController(dtp);
|
|
return controller;
|
|
};
|