action.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. const db = require('../models');
  2. module.exports = {
  3. get_all: (req, res, next) => {
  4. let where = {};
  5. if (req.query.date) {
  6. where.date = req.query.date;
  7. }
  8. return req.participant.getActions({
  9. order: [ 'date' ],
  10. where
  11. })
  12. .then((actions) => res.json(actions))
  13. .catch((err) => next(err));
  14. },
  15. get_all_all: (req, res, next) => {
  16. let where = {};
  17. if (req.query.date) {
  18. where.date = {
  19. [Sequelize.Op.like]: '%'+req.query.date+'%'
  20. };
  21. }
  22. return db.Action.findAll({
  23. order: [ 'date' ],
  24. where
  25. })
  26. .then((actions) => res.json(actions))
  27. .catch((err) => next(err));
  28. },
  29. get_by_id: (req, res, next) => {
  30. return req.participant.getActions({
  31. where: {
  32. id: req.params.date_id
  33. }
  34. })
  35. .then((actions) => {
  36. if (actions.length === 0) {
  37. throw { status: 404, message: 'Requested action not found' };
  38. }
  39. return res.json(actions[0]);
  40. })
  41. .catch((err) => next(err));
  42. },
  43. create: (req, res, next) => {
  44. const data = {
  45. date: req.body.date || new Date()
  46. };
  47. return req.participant.createAction({...data, TacheId: req.tache.id})
  48. .then((action) => res.json(action))
  49. .catch((err) => next(err));
  50. },
  51. delete_by_id: (req, res, next) => {
  52. return req.participant.getActions({
  53. where: {
  54. id: req.params.date_id
  55. }
  56. })
  57. .then((actions) => {
  58. if (actions.length === 0) {
  59. throw { status: 404, message: 'Requested action not found' };
  60. }
  61. return actions[0].destroy();
  62. })
  63. .then(() => res.status(200).end())
  64. .catch((err) => next(err));
  65. }
  66. };