server.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. const Sequelize = require('sequelize');
  2. const Model = Sequelize.Model;
  3. const express = require('express');
  4. const server= express();
  5. const bd = require('body-parser');
  6. server.use(bd.json());
  7. server.use(function (err, req, res, next) {
  8. console.error(err.stack);
  9. res.status(500).send(err.stack);
  10. });
  11. const personCtrl = {
  12. get_all(res) {
  13. Person.findAll().then(persons => {
  14. res.send(JSON.stringify(persons, null, 4));
  15. })
  16. },
  17. create(req, res, next) {
  18. if (req.query.firstname == undefined || req.query.lastname == undefined) {
  19. throw "Un des éléments est vide";
  20. }
  21. Person.create({lastname: req.query.firstname, firstname: req.query.lastname}).then(p => {
  22. res.send("ok");
  23. }) .catch(function (error) {
  24. next(error);
  25. })
  26. },
  27. get_by_id(req, res, next) {
  28. Person.findByPk(req.params.person_id).then(persons => {
  29. if (!persons) {
  30. throw "Pas bonne id"
  31. }
  32. res.send(JSON.stringify(persons, null, 4))
  33. }) .catch(function (error) {
  34. next(error);
  35. })
  36. },
  37. update_by_id(req, res, next) {
  38. Person.findByPk(req.params.person_id).then(persons => {
  39. if (!persons) {
  40. throw "Pas bonne id";
  41. } else {
  42. Person.update(req.body, {
  43. where: {
  44. id: req.params.person_id
  45. }
  46. }) .then(
  47. res.send("ok")
  48. )
  49. }
  50. }) .catch(function (error) {
  51. next(error);
  52. })
  53. },
  54. delete_by_id(req, res, next) {
  55. Person.destroy({
  56. where: {
  57. id: req.params.person_id
  58. }
  59. }) .then(data => {
  60. if (!data) {
  61. throw "Pas bonne id"
  62. }
  63. res.send("fait")}) .catch(function (error) {
  64. next(error);
  65. })
  66. }
  67. }
  68. const pCtrl = Object.create(personCtrl);
  69. const sequelize = new Sequelize('w4aclem', 'clement.krebs', 'n4or2bgm', {
  70. host: 'mysql.iutrs.unistra.fr',
  71. dialect: 'mysql'
  72. });
  73. class Person extends Model {}
  74. Person.init({
  75. lastname: Sequelize.STRING,
  76. firstname: Sequelize.STRING
  77. }, {sequelize, modelName: 'Person'});
  78. /*sequelize.sync()
  79. .then(() => Person.create({
  80. lastname: 'moi',
  81. firstname: 'toi'
  82. }))
  83. .then(moi => {
  84. console.log(moi.toJSON());
  85. });*/
  86. server.get('/person', function (req, res) {
  87. pCtrl.get_all(res);
  88. });
  89. server.post('/person', function (req, res, next) {
  90. pCtrl.create(req, res, next);
  91. });
  92. server.get('/person/:person_id', function (req, res, next) {
  93. pCtrl.get_by_id(req, res, next);
  94. });
  95. server.put('/person/:person_id', function (req, res, next) {
  96. pCtrl.update_by_id(req, res, next);
  97. });
  98. server.delete('/person/:person_id', function (req, res, next) {
  99. pCtrl.delete_by_id(req, res, next);
  100. });
  101. server.listen(8000, function () {
  102. console.log('ouais ouais ouais !!!');
  103. });