Clément Krebs 5 lat temu
rodzic
commit
b4b617cb6d
4 zmienionych plików z 113 dodań i 0 usunięć
  1. 67 0
      controllers/group_ctrl.js
  2. 16 0
      models/group.js
  3. 1 0
      models/person.js
  4. 29 0
      routes/group_routes.js

+ 67 - 0
controllers/group_ctrl.js

@@ -0,0 +1,67 @@
+const db = require('../models');
+
+module.exports = {
+    get_all(req, res, next) {
+        db.Group.findAll()
+        .then(g => {
+            res.json(g)
+        })
+        .catch(error => {
+            next(error);
+        })
+    },
+    create(req, res, next) {
+        if (!req.body.title) {
+            throw {status: 400, message: "Titre vide"}
+        }
+        db.Group.create({title: req.body.title})
+        .then (g => {
+           res.json(g);
+        })
+        .catch(error => {
+            next(error);
+        })
+    },
+    get_by_id(req, res, next) {
+        db.Group.findByPk(req.params.group_id)
+        .then(g => {
+            if (!g) {
+                throw { status: 404, message: "Pas trouvé"}
+            }
+            res.json(g);
+        })
+        .catch(error => {
+            next(error);
+        })
+    },
+    update_by_id(req, res, next) {
+        db.Group.findByPk(req.params.group_id) 
+        .then ( g => {
+            if (!g) {
+                throw { status: 404, message: "Pas trouvé"}
+            } else {
+                g.update(req.body)
+                .then (res.json(g))
+            }
+        })
+        .catch (e => {
+            next(e);
+        })
+    },
+    delete_by_id(req, res, next) {
+        db.Group.destroy({
+            where: {
+                id: req.params.group_id
+            }
+        })
+        .then (g => {
+            if (!g) {
+                throw {status: 404, message: "Pas trouvé"}
+            }
+            res.send("fait");
+        })
+        .catch (e => {
+            next(e);
+        })
+    }
+}

+ 16 - 0
models/group.js

@@ -0,0 +1,16 @@
+const Sequelize = require('sequelize');
+const Model = Sequelize.Model;
+
+module.exports = (sequelize) => {
+    class Group extends Model {
+        static associate(db) {
+            Group.belongsToMany(db.Person, {through:'PersonGroup'});
+        };
+    }
+
+    Group.init({
+        title: Sequelize.STRING
+    }, {sequelize, modelName: 'Group'});
+    
+    return Group;
+};

+ 1 - 0
models/person.js

@@ -8,6 +8,7 @@ module.exports = (sequelize) => {
             Person.hasMany(db.MailAddress, {onDelete: 'cascade'});
             Person.hasMany(db.PostalAddress, {onDelete: 'cascade'});
             Person.hasMany(db.Phone, {onDelete: 'cascade'});
+            Person.belongsToMany(db.Group, {through:'PersonGroup'});
         };
     }
 

+ 29 - 0
routes/group_routes.js

@@ -0,0 +1,29 @@
+const person_ctrl = require('../controllers/group_ctrl');
+
+module.exports = [
+    {
+        url:'/group',
+        method: 'get',
+        func: person_ctrl.get_all
+    },
+    {
+        url:'/group',
+        method: 'post',
+        func: person_ctrl.create
+    },
+    {
+        url:'/group/:group_id',
+        method: 'get',
+        func: person_ctrl.get_by_id
+    },
+    {
+        url:'/group/:group_id',
+        method: 'put',
+        func: person_ctrl.update_by_id
+    },
+    {
+        url:'/group/:group_id',
+        method: 'delete',
+        func: person_ctrl.delete_by_id
+    }
+]