UNPKG

paradigm-core

Version:
205 lines (159 loc) 4.11 kB
const {r} = require('structure-core') const slug = require('../../lib/slugs') const {TaxonomyController} = require('../taxonomies') const CategoryModel = require('./model') /** * CategoriesController Class * * @public * @class CategoriesController */ class CategoryController extends TaxonomyController { /** * CategoriesController constructor * * @public * @constructor * @param {Object} options - Options */ constructor(options = {}) { super(Object.assign({}, { name: 'categories' }, options)) } /** * Create new category * * @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 category = new CategoryModel({ applicationId, logger: this.logger, organizationId }) var pkg = req.body pkg.applicationId = applicationId pkg.organizationId = organizationId pkg.slug = slug(pkg.title) pkg.status = 'active' const doc = await category.getBySlug(pkg.slug) // Don't re-create the same category if(doc) { return Promise.resolve(doc) } return category.create(pkg) } deleteById(req, res) { const category = new CategoryModel({ logger: this.logger, }) const categoryId = req.params.id const applicationId = req.headers.applicationid return new Promise( async (resolve, reject) => { try { const res = await category.deleteById(categoryId) await r .table('documents') .getAll(r.args([categoryId]), {index: 'categoryIds'}) .filter(function(doc) { return doc('applicationId').eq(applicationId) }) .update(function(doc) { return { categoryIds: doc('categoryIds').setDifference(r.expr([categoryId])) // Removes the category from all documents that contain it } }) resolve(res) } catch(e) { this.logger.error(e) reject(e) } }) } /** * Get all categories * * @public * @param {Object} req - Express req * @param {Object} res - Express res */ getAll(req, res) { const applicationId = req.headers.applicationid const organizationId = req.headers.organizationid const category = new CategoryModel({ applicationId, logger: this.logger, organizationId }) return category.getAll([]) } /** * Get category by id * * @public * @param {Object} req - Express req * @param {Object} res - Express res */ getById(req, res) { const category = new CategoryModel({ logger: this.logger, }) return category.getById(req.params.id) } /** * Get category by slug * * @public * @param {Object} req - Express req * @param {Object} res - Express res */ getBySlug(req, res) { const applicationId = req.headers.applicationid const organizationId = req.headers.organizationid const category = new CategoryModel({ applicationId, logger: this.logger, organizationId }) return category.getBySlug(req.params.slug) } /** * Match category by slug * * @public * @param {Object} req - Express req * @param {Object} res - Express res */ matchBySlug(req, res) { const applicationId = req.headers.applicationid const organizationId = req.headers.organizationid const category = new CategoryModel({ applicationId, logger: this.logger, organizationId }) return category.matchBySlug(req.params.slug) } /** * Update a category * * @public * @param {Object} req - Express req * @param {Object} res - Express res */ updateById(req, res) { const category = new CategoryModel({ logger: this.logger, }) const pkg = req.body pkg.slug = slug(pkg.title) return category.updateById(req.params.id, pkg) } } module.exports = CategoryController