UNPKG

@fabrix/spool-cart

Version:

Spool - eCommerce Spool for Fabrix

130 lines (129 loc) 4.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const common_1 = require("@fabrix/fabrix/dist/common"); const errors_1 = require("@fabrix/spool-sequelize/dist/errors"); class TagController extends common_1.FabrixController { count(req, res) { const EventsService = this.app.services.EventsService; EventsService.count('Tag') .then(count => { const counts = { tags: count }; return res.json(counts); }) .catch(err => { return res.serverError(err); }); } findById(req, res) { const orm = this.app.models; const Tag = orm['Tag']; Tag.findByIdDefault(req.params.id, {}) .then(tag => { if (!tag) { throw new errors_1.ModelError('E_NOT_FOUND', `Tag id ${req.params.id} not found`); } return this.app.services.PermissionsService.sanitizeResult(req, tag); }) .then(result => { return res.json(result); }) .catch(err => { return res.serverError(err); }); } findByName(req, res) { const orm = this.app.models; const Tag = orm['Tag']; Tag.findOne({ name: req.params.name }) .then(tag => { if (!tag) { throw new errors_1.ModelError('E_NOT_FOUND', `Tag name ${req.params.name} not found`); } return this.app.services.PermissionsService.sanitizeResult(req, tag); }) .then(result => { return res.json(result); }) .catch(err => { return res.serverError(err); }); } findOne(req, res) { const orm = this.app.models; const Tag = orm['Tag']; const where = req.jsonCriteria(req.query.where); Tag.findOne({ where: where }) .then(tag => { return this.app.services.PermissionsService.sanitizeResult(req, tag); }) .then(result => { return res.json(result); }) .catch(err => { return res.serverError(err); }); } findAll(req, res) { const orm = this.app.models; const Tag = orm['Tag']; const limit = Math.max(0, req.query.limit || 10); const offset = Math.max(0, req.query.offset || 0); const sort = req.query.sort || [['created_at', 'DESC']]; const where = req.jsonCriteria(req.query.where); Tag.findAndCountAll({ where: where, order: sort, offset: offset, limit: limit }) .then(tags => { res.paginate(tags.count, limit, offset, sort); return this.app.services.PermissionsService.sanitizeResult(req, tags.rows); }) .then(result => { return res.json(result); }) .catch(err => { return res.serverError(err); }); } search(req, res) { const orm = this.app.models; const Tag = orm['Tag']; const limit = Math.max(0, req.query.limit || 10); const offset = Math.max(0, req.query.offset || 0); const sort = req.query.sort || [['created_at', 'DESC']]; const term = req.query.term; Tag.findAndCountAll({ where: { $or: [ { name: { $iLike: `%${term}%` } } ] }, order: sort, offset: offset, limit: limit }) .then(tags => { res.paginate(tags.count, limit, offset, sort); return this.app.services.PermissionsService.sanitizeResult(req, tags.rows); }) .then(result => { return res.json(result); }) .catch(err => { return res.serverError(err); }); } } exports.TagController = TagController;