UNPKG

@fabrix/spool-cart

Version:

Spool - eCommerce Spool for Fabrix

137 lines (136 loc) 4.59 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"); const Validator = require("../../validator"); class ReviewController extends common_1.FabrixController { generalStats(req, res) { return res.json({}); } count(req, res) { const EventsService = this.app.services.EventsService; EventsService.count('ProductReview') .then(count => { const counts = { reviews: count }; return res.json(counts); }) .catch(err => { return res.serverError(err); }); } findById(req, res) { const orm = this.app.models; const Review = orm['ProductReview']; const id = req.params.id; Review.findById(id, {}) .then(review => { if (!review) { throw new errors_1.ModelError('E_NOT_FOUND', `Review id ${id} not found`); } return this.app.services.PermissionsService.sanitizeResult(req, review); }) .then(result => { return res.json(result); }) .catch(err => { return res.serverError(err); }); } findAll(req, res) { const Review = this.app.models['ProductReview']; 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); Review.findAndCountAll({ order: sort, offset: offset, limit: limit, where: where }) .then(reviews => { res.paginate(reviews.count, limit, offset, sort); return this.app.services.PermissionsService.sanitizeResult(req, reviews.rows); }) .then(result => { return res.json(result); }) .catch(err => { return res.serverError(err); }); } search(req, res) { const Review = this.app.models['ProductReview']; 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); Review.findAndCountAll({ order: sort, offset: offset, limit: limit, where: where }) .then(reviews => { res.paginate(reviews.count, limit, offset, sort); return this.app.services.PermissionsService.sanitizeResult(req, reviews.rows); }) .then(result => { return res.json(result); }) .catch(err => { return res.serverError(err); }); } create(req, res) { const ReviewService = this.app.services.ReviewService; Validator.validateReview.create(req.body) .then(values => { return ReviewService.create(req.body); }) .then(review => { return this.app.services.PermissionsService.sanitizeResult(req, review); }) .then(result => { return res.json(result); }) .catch(err => { return res.serverError(err); }); } update(req, res) { const ReviewService = this.app.services.ReviewService; Validator.validateReview.update(req.body) .then(values => { return ReviewService.update(req.body); }) .then(review => { return this.app.services.PermissionsService.sanitizeResult(req, review); }) .then(result => { return res.json(result); }) .catch(err => { return res.serverError(err); }); } destroy(req, res) { const ReviewService = this.app.services.ReviewService; Validator.validateReview.destroy(req.body) .then(values => { return ReviewService.destroy(req.body); }) .then(review => { return this.app.services.PermissionsService.sanitizeResult(req, review); }) .then(result => { return res.json(result); }) .catch(err => { return res.serverError(err); }); } } exports.ReviewController = ReviewController;