UNPKG

paradigm-channels

Version:
299 lines (214 loc) 5.34 kB
const ChannelModel = require('./model') const RootController = require('structure-root-controller') const slug = require('paradigm-slugs') /** * ChannelsController Class * * @public * @class ChannelsController */ class Controller extends RootController { /** * ChannelsController constructor * * @public * @constructor * @param {Object} options - Options */ constructor(options = {}) { super(Object.assign({}, { name: 'channels' }, options)) } /** * Create new channel * * @public * @param {Object} req - Express req * @param {Object} res - Express res */ async create(req, res) { const applicationId = req.headers.applicationid const organizationId = req.headers.organizationid const channelModel = new ChannelModel({ applicationId, logger: this.logger, organizationId }) const pkg = req.body return new Promise( async (resolve, reject) => { try { pkg.slug = slug(pkg.slug || pkg.title) pkg.status = 'active' if(!pkg.applicationId) { pkg.applicationId = applicationId } if(!pkg.organizationId) { pkg.organizationId = organizationId } const doc = await channelModel.getBySlug(pkg.slug) // Don't re-create the same channel if(doc) { return reject({ code: 'CHANNEL_EXISTS' }) } const channel = await channelModel.create(pkg) resolve(channel) } catch(e) { this.logger.error(e) reject(e) } }) } deleteById(req, res) { const channel = new ChannelModel({ logger: this.logger, }) return channel.deleteById(req.params.id) } /** * Get all channels * * @public * @param {Object} req - Express req * @param {Object} res - Express res */ getAll(req, res) { const channel = new ChannelModel({ applicationId: req.headers.applicationid, logger: this.logger, organizationId: req.headers.organizationid }) return channel.getAll([]) } /** * Get channel by id * * @public * @param {Object} req - Express req * @param {Object} res - Express res */ getById(req, res) { const channel = new ChannelModel({ logger: this.logger, }) return channel.getById(req.params.id) } /** * Get channel by slug * * @public * @param {Object} req - Express req * @param {Object} res - Express res */ getBySlug(req, res) { const channel = new ChannelModel({ applicationId: req.headers.applicationid, logger: this.logger, organizationId: req.headers.organizationid }) return channel.getBySlug(req.params.slug) } /** * Get resouces of channel * * @public * @param {Object} req - Express req * @param {Object} res - Express res */ getResources(req, res) { const channel = new ChannelModel({ applicationId: req.headers.applicationid, logger: this.logger, organizationId: req.headers.organizationid, }) return new Promise( async (resolve, reject) => { try { let items = [] // Get single resource if(req.params.resource) { items = await channel.getSingleResource(req.params, req.query) // Get all resources } else { items = await channel.getAllResources(req.params, req.query) } const pkg = { resources: items.resources } resolve(pkg) } catch(e) { this.logger.error(e) reject(e) } }) } /** * Get RSS data for channel * * @public * @param {Object} req - Express req * @param {Object} res - Express res */ getRSS(req, res) { const channel = new ChannelModel({ applicationId: req.headers.applicationid, logger: this.logger, organizationId: req.headers.organizationid, }) return new Promise( async (resolve, reject) => { try { let items = [] items = await channel.getAllResources(req.params, req.query, { rss: true }) const pkg = { resources: items.resources } resolve(pkg) } catch(e) { this.logger.error(e) reject(e) } }) } /** * Match channel by slug * * @public * @param {Object} req - Express req * @param {Object} res - Express res */ matchBySlug(req, res) { const channel = new ChannelModel({ applicationId: req.headers.applicationid, logger: this.logger, organizationId: req.headers.organizationid }) return channel.matchBySlug(req.params.slug) } /** * Update a channel * * @public * @param {Object} req - Express req * @param {Object} res - Express res */ updateById(req, res) { const channelModel = new ChannelModel({ logger: this.logger, }) const pkg = req.body if(pkg.slug) pkg.slug = slug(pkg.slug) return new Promise( async (resolve, reject) => { try { const channel = await channelModel.updateById(req.params.id, pkg) resolve(channel) } catch(e) { this.logger.error(e) reject(e) } }) } } module.exports = Controller