person_ctrl.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. const db = require('./db');
  2. module.exports = {
  3. get_all(req, res, next) {
  4. db.Person.findAll().then(persons => {
  5. res.json(persons)
  6. }) .catch(error => {
  7. next(error);
  8. })
  9. },
  10. create(req, res, next) {
  11. if (req.query.firstname == undefined || req.query.lastname == undefined) {
  12. throw "Un des éléments est vide";
  13. }
  14. db.Person.create({lastname: req.query.firstname, firstname: req.query.lastname}).then(p => {
  15. res.json(p);
  16. }) .catch(function (error) {
  17. next(error);
  18. })
  19. },
  20. get_by_id(req, res, next) {
  21. db.Person.findByPk(req.params.person_id).then(persons => {
  22. if (!persons) {
  23. throw { status: 404, message: "Pas bonne id" }
  24. }
  25. res.json(persons)
  26. }) .catch(function (error) {
  27. next(error);
  28. })
  29. },
  30. update_by_id(req, res, next) {
  31. db.Person.findByPk(req.params.person_id).then(person => {
  32. if (!person) {
  33. throw "Pas bonne id";
  34. } else {
  35. person.update(req.body)
  36. .then(
  37. res.send("ok")
  38. )
  39. }
  40. }) .catch(function (error) {
  41. next(error);
  42. })
  43. },
  44. delete_by_id(req, res, next) {
  45. db.Person.destroy({
  46. where: {
  47. id: req.params.person_id
  48. }
  49. }) .then(data => {
  50. if (!data) {
  51. throw "Pas bonne id"
  52. }
  53. res.send("fait")}) .catch(function (error) {
  54. next(error);
  55. })
  56. },
  57. load_by_id(req, res, next) {
  58. db.Person.findByPk(req.params.person_id).then(person => {
  59. if (!person) {
  60. throw { status: 404, message: "Pas bonne id" }
  61. }
  62. req.person = person;
  63. next();
  64. }) .catch(function (error) {
  65. next(error);
  66. })
  67. }
  68. };