admin.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. var User = require('../models/User');
  2. let Watch = require('../models/Watch');
  3. let Category = require('../models/Category');
  4. var mongoose = require('mongoose');
  5. exports.listWatches = (req, res) => {
  6. Watch.find({}, null, { sort: '-id' }, function (err, watch) {
  7. if (err) {
  8. console.log(err);
  9. } else {
  10. res.render('admin/list-watches', {
  11. title: 'List watches',
  12. watch: watch
  13. });
  14. }
  15. }).populate('criador');
  16. }
  17. exports.listCategoriesGet = (req, res) => {
  18. Category.find({}, null, { sort: '-id' }, function (err, categories) {
  19. if (err) {
  20. console.log(err);
  21. } else {
  22. res.render('admin/list-categories', {
  23. title: 'List Categories',
  24. categories: categories
  25. });
  26. }
  27. }).populate('criador');
  28. }
  29. /**
  30. * PUT /admin/list-categories
  31. * Add or update a new category.
  32. */
  33. exports.listCategoriesPut = function(req, res, next) {
  34. req.assert('title', 'O campo título precisa ser preenchido.').notEmpty();
  35. req.assert('nid', 'O campo nid precisa ser preenchido.').notEmpty();
  36. var errors = req.validationErrors();
  37. if (errors) {
  38. req.flash('error', errors);
  39. return res.redirect('/admin/list-categories');
  40. }
  41. Category.findOne({ nid: req.body.nid }, function(err, category) {
  42. if (category) {
  43. req.flash('error', { msg: 'O Number Id inserido já existe. Tente outro.' });
  44. return res.redirect('/admin/list-categories');
  45. }
  46. // Para salvar no BD
  47. category = new Category({
  48. criador: req.user.id,
  49. nid: req.body.nid,
  50. format: req.body.format,
  51. preposition: req.body.preposition,
  52. title: req.body.title
  53. });
  54. category.save(function(err) {
  55. //req.logIn(campanha, function(err) {
  56. req.flash('success', { msg: 'Categoria inserida com sucesso.' });
  57. res.redirect('/admin/list-categories');
  58. //});
  59. });
  60. });
  61. };