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.
66 lines
1.7 KiB
66 lines
1.7 KiB
// announcement.js
|
|
// Copyright (C) 2022 DTP Technologies, LLC
|
|
// License: Apache-2.0
|
|
|
|
'use strict';
|
|
|
|
const express = require('express');
|
|
|
|
const { SiteController, SiteError } = require('../../lib/site-lib');
|
|
|
|
class AnnouncementController extends SiteController {
|
|
|
|
constructor (dtp) {
|
|
super(dtp, module.exports);
|
|
}
|
|
|
|
async start ( ) {
|
|
const router = express.Router();
|
|
this.dtp.app.use('/announcement', router);
|
|
|
|
router.param('announcementId', this.populateAnnouncementId.bind(this));
|
|
|
|
router.get('/:announcementId', this.getAnnouncementView.bind(this));
|
|
router.get('/', this.getHome.bind(this));
|
|
|
|
return router;
|
|
}
|
|
|
|
async populateAnnouncementId (req, res, next, announcementId) {
|
|
const { announcement: announcementService } = this.dtp.services;
|
|
try {
|
|
res.locals.announcement = await announcementService.getById(announcementId);
|
|
if (!res.locals.announcement) {
|
|
this.log.error('announcement not found', { announcementId });
|
|
return next(new SiteError(404, 'Announcement not found'));
|
|
}
|
|
return next();
|
|
} catch (error) {
|
|
return next(error);
|
|
}
|
|
}
|
|
|
|
async getAnnouncementView (req, res) {
|
|
res.render('announcement/view');
|
|
}
|
|
|
|
async getHome (req, res, next) {
|
|
const { announcement: announcementService } = this.dtp.services;
|
|
try {
|
|
res.locals.pagination = this.getPaginationParameters(req, 10);
|
|
res.locals.announcements = await announcementService.getAnnouncements(res.locals.pagination);
|
|
res.render('announcement/index');
|
|
} catch (error) {
|
|
return next(error);
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
slug: 'announcement',
|
|
name: 'announcement',
|
|
create: async (dtp) => {
|
|
let controller = new AnnouncementController(dtp);
|
|
return controller;
|
|
},
|
|
};
|