group_ctrl.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. const db = require('../models');
  2. module.exports = {
  3. get_all(req, res, next) {
  4. db.Group.findAll()
  5. .then(g => {
  6. res.json(g)
  7. })
  8. .catch(error => {
  9. next(error);
  10. })
  11. },
  12. create(req, res, next) {
  13. if (!req.body.title) {
  14. throw {status: 400, message: "Titre vide"}
  15. }
  16. db.Group.create({title: req.body.title})
  17. .then (g => {
  18. res.json(g);
  19. })
  20. .catch(error => {
  21. next(error);
  22. })
  23. },
  24. get_by_id(req, res, next) {
  25. db.Group.findByPk(req.params.group_id)
  26. .then(g => {
  27. if (!g) {
  28. throw { status: 404, message: "Pas trouvé"}
  29. }
  30. res.json(g);
  31. })
  32. .catch(error => {
  33. next(error);
  34. })
  35. },
  36. update_by_id(req, res, next) {
  37. db.Group.findByPk(req.params.group_id)
  38. .then ( g => {
  39. if (!g) {
  40. throw { status: 404, message: "Pas trouvé"}
  41. } else {
  42. g.update(req.body)
  43. .then (res.json(g))
  44. }
  45. })
  46. .catch (e => {
  47. next(e);
  48. })
  49. },
  50. delete_by_id(req, res, next) {
  51. db.Group.destroy({
  52. where: {
  53. id: req.params.group_id
  54. }
  55. })
  56. .then (g => {
  57. if (!g) {
  58. throw {status: 404, message: "Pas trouvé"}
  59. }
  60. res.send("fait");
  61. })
  62. .catch (e => {
  63. next(e);
  64. })
  65. }
  66. }