participant.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. const
  2. db = require('../models'),
  3. Sequelize = require('sequelize');
  4. module.exports = {
  5. get_all: (req, res, next) => {
  6. let where = {};
  7. if (req.query.nom) {
  8. where.nom = {
  9. [Sequelize.Op.like]: '%'+req.query.nom+'%'
  10. };
  11. }
  12. return db.Participant.findAll({
  13. order: [ 'nom' ],
  14. where
  15. })
  16. .then((participants) => res.json(participants))
  17. .catch((err) => next(err));
  18. },
  19. load_by_id: (req, res, next) => {
  20. return db.Participant.findByPk(req.params.participant_id)
  21. .then((participant) => {
  22. if (!participant) {
  23. throw { status: 404, message: 'Requested Participant not found' };
  24. }
  25. req.participant = participant;
  26. return next();
  27. })
  28. .catch((err) => next(err));
  29. },
  30. get_by_id: (req, res, next) => {
  31. return db.Participant.findByPk(req.params.participant_id)
  32. .then((participant) => {
  33. if (!participant) {
  34. throw { status: 404, message: 'Requested Participant not found' };
  35. }
  36. return res.json(participant);
  37. })
  38. .catch((err) => next(err));
  39. },
  40. create: (req, res, next) => {
  41. const data = {
  42. nom: req.body.nom || '',
  43. prenom: req.body.prenom || ''
  44. };
  45. return db.Participant.create(data)
  46. .then((participant) => res.json(participant))
  47. .catch((err) => next(err));
  48. },
  49. delete_by_id: (req, res, next) => {
  50. return db.Participant.findByPk(req.params.participant_id)
  51. .then((participant) => {
  52. if (!participant) {
  53. throw { status: 404, message: 'Requested Participant not found' };
  54. }
  55. return participant.destroy();
  56. })
  57. .then(() => res.status(200).end())
  58. .catch((err) => next(err));
  59. }
  60. };