product-hero.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. const ProductHero = require('../models/product-hero');
  2. exports.createHero = function (req, res, next) {
  3. const productData = req.body;
  4. const productHero = new ProductHero(productData);
  5. productHero.product = productData.product;
  6. productHero.save((errors, createdHero) => {
  7. if (errors) {
  8. return res.status(422).send(errors);
  9. }
  10. return res.json(createdHero);
  11. });
  12. };
  13. exports.getProductHeroes = function(req, res, next) {
  14. ProductHero.find({})
  15. .populate('product')
  16. .sort({createdAt: -1})
  17. .exec(function(errors, heroes) {
  18. if (errors) {
  19. return res.status(422).send(errors);
  20. }
  21. return res.json(heroes);
  22. })
  23. }
  24. exports.updateProductHeroes = function(req, res, next) {
  25. const id = req.params.id;
  26. ProductHero.findById(id)
  27. .populate('product')
  28. .exec(function(errors, hero) {
  29. if (errors) {
  30. return res.status(422).send(errors);
  31. }
  32. hero.set({createdAt: new Date()})
  33. hero.save((errors, updatedHero) => {
  34. if (errors) {
  35. return res.status(422).send(errors);
  36. }
  37. return res.json(updatedHero);
  38. })
  39. })
  40. }