Clément Krebs 5 år sedan
förälder
incheckning
ffd3b8c0b8

+ 9 - 1
controllers/mail_address_ctrl.js

@@ -2,7 +2,15 @@ const db = require('../models');
 
 module.exports = {
     get_all(req, res, next) {
-        req.person.getMailAddresses()
+        let param = {
+            order: ['type']
+        };
+        if (req.query.type) {
+            param.where = {
+                type: req.query.type
+            };
+        }
+        req.person.getMailAddresses(param)
         .then (mailAddresses => {
             if (!mailAddresses) {
                 throw {status: 404, message: "Pas d'adresse"}

+ 9 - 1
controllers/person_ctrl.js

@@ -2,7 +2,15 @@ const db = require('../models');
 
 module.exports = {
     get_all(req, res, next) {
-        db.Person.findAll().then(persons => {
+        let param = {
+            order: ['lastname']
+        };
+        if (req.query.lastname) {
+            param.where = {
+                lastname: req.query.lastname
+            };
+        }
+        db.Person.findAll(param).then(persons => {
             res.json(persons)
         }) .catch(error => {
             next(error);

+ 80 - 0
controllers/phone_ctrl.js

@@ -0,0 +1,80 @@
+const db = require('../models');
+
+module.exports = {
+    get_all(req, res, next) {
+        let param = {
+            order: ['type']
+        };
+        if (req.query.type) {
+            param.where = {
+                type: req.query.type
+            };
+        }
+        req.person.getPhones(param)
+        .then(p => {
+            if (!p) {
+                throw {status: 404, message: "Pas de tel"}
+            }
+            res.json(p)
+        })
+        .catch(function(error) {
+            next(error);
+        })
+    },
+    create(req, res, next) {
+        if (!req.body.number || !req.body.type) {
+            throw {status: 400, message: "Manque type ou tel"}
+        }
+        req.person.createPhone({number: req.body.number, type: req.body.type})
+        .then (p => {
+            res.json(p);
+        })
+        .catch(error => {
+            next(error);
+        })
+    },
+    get_by_id(req, res, next) {
+        req.person.getPhones({where: {
+            id: req.params.phone_id
+        }})
+        .then (([p]) => {
+            if (!p) {
+                throw {status: 404, message: "Pas trouvé"}
+            }
+            res.json([p]);
+        })
+        .catch(error => {
+            next(error);
+        })
+    },
+    update_by_id(req, res, next) {
+        req.person.getPhones({where: {
+            id: req.params.phone_id
+        }})
+        .then (([p]) => {
+            if (!p) {
+                throw {status: 404, message: "Pas trouvé 2"}
+            }
+            p.update(req.body);
+            res.json([p]);
+        })
+        .catch(error => {
+            next(error);
+        })
+    },
+    delete_by_id(req, res ,next) {
+        req.person.getPhones({where: {
+            id: req.params.phone_id
+        }})
+        .then (([p]) => {
+            if (!p) {
+                throw {status: 404, message: "Pas trouvé 3"}
+            }
+            p.destroy();
+            res.send("fait");
+        })
+        .catch(error => {
+            next(error);
+        })
+    }
+}

+ 80 - 0
controllers/postal_address_ctrl.js

@@ -0,0 +1,80 @@
+const db = require('../models');
+
+module.exports = {
+    get_all(req, res, next) {
+        let param = {
+            order: ['type']
+        };
+        if (req.query.type) {
+            param.where = {
+                    type: req.query.type
+                };
+        }
+        req.person.getPostalAddresses(param)
+        .then (pa => {
+            if (!pa) {
+                throw {status: 404, message: "Pas d'adresse"}
+            }
+            res.json(pa);
+        })
+        .catch(function (error) {
+            next(error);
+        })
+    },
+    create(req, res, next) {
+        if (!req.body.postalAddress || !req.body.type) {
+            throw {status: 400, message: "Manque type ou adresse"}
+        }
+        req.person.createPostalAddress({postalAddress: req.body.postalAddress, type: req.body.type})
+        .then (pa => {
+            res.json(pa);
+        })
+        .catch(error => {
+            next(error);
+        })
+    },
+    get_by_id(req, res, next) {
+        req.person.getPostalAddresses({where: {
+            id: req.params.postal_address_id
+        }})
+        .then (([pa]) => {
+            if (!pa) {
+                throw {status: 404, message: "Pas trouvé"}
+            }
+            res.json([pa]);
+        })
+        .catch(error => {
+            next(error);
+        })
+    },
+    update_by_id(req, res, next) {
+        req.person.getPostalAddresses({where: {
+            id: req.params.postal_address_id
+        }})
+        .then (([pa]) => {
+            if (!pa) {
+                throw {status: 404, message: "Pas trouvé 2"}
+            }
+            pa.update(req.body);
+            res.json(pa);
+        })
+        .catch(error => {
+            next(error);
+        })
+    },
+    delete_by_id(req, res, next) {
+        req.person.getPostalAddresses({where: {
+            id: req.params.postal_address_id
+        }})
+        .then(([pa]) => {
+            if (!pa) {
+                throw {status: 404, message: "Pas trouvé 3"}
+            }
+            pa.destroy();
+            res.send("Fait")
+        })
+        .catch(error => {
+            next(error);
+        })
+    }
+};

+ 3 - 3
models/mail_adress.js

@@ -6,7 +6,7 @@ module.exports = (sequelize) => {
     class MailAddress extends Model {
         static associate(db) {
             MailAddress.belongsTo(db.Person, {onDelete: 'cascade'});
-        }
+        };
     }
 
     MailAddress.init({
@@ -14,8 +14,8 @@ module.exports = (sequelize) => {
             type: Sequelize.STRING,
             validate: {
             isEmail: true
-        }
-    },
+            }
+        },
         type: Sequelize.ENUM('home', 'work')
     }, {sequelize, modelName: 'MailAddress'});
 

+ 2 - 0
models/person.js

@@ -6,6 +6,8 @@ module.exports = (sequelize) => {
     class Person extends Model {
         static associate(db) {
             Person.hasMany(db.MailAddress, {onDelete: 'cascade'});
+            Person.hasMany(db.PostalAddress, {onDelete: 'cascade'});
+            Person.hasMany(db.Phone, {onDelete: 'cascade'});
         };
     }
 

+ 18 - 0
models/phone.js

@@ -0,0 +1,18 @@
+const Sequelize = require('sequelize');
+const Model = Sequelize.Model;
+
+module.exports = (sequelize) => {
+    class Phone extends Model {
+        static associate(db) {
+            Phone.belongsTo(db.Person, {onDelete: 'cascade'});
+        };
+    }
+
+    Phone.init({
+        number: Sequelize.STRING,
+        type: Sequelize.ENUM('home', 'work')
+
+    }, {sequelize, modelName: 'Phone'});
+
+    return Phone
+};

+ 18 - 0
models/postal_address.js

@@ -0,0 +1,18 @@
+const Sequelize = require('sequelize');
+const Model = Sequelize.Model;
+
+module.exports = (sequelize) => {
+    class PostalAddress extends Model {
+        static associate(db) {
+            PostalAddress.belongsTo(db.Person, {onDelete: 'cascade'});
+        };
+    }
+
+    PostalAddress.init({
+        postalAddress: Sequelize.STRING,
+        type: Sequelize.ENUM('home', 'work')
+    }, {sequelize, modelName: 'PostalAddress'});
+
+    return PostalAddress;
+
+};

+ 30 - 0
routes/phone_routes.js

@@ -0,0 +1,30 @@
+const phone_ctrl = require('../controllers/phone_ctrl');
+const person_ctrl = require('../controllers/person_ctrl');
+
+module.exports = [
+    {
+        url: '/person/:person_id/phone',
+        method: 'get',
+        func: [person_ctrl.load_by_id, phone_ctrl.get_all]
+    },
+    {
+        url: '/person/:person_id/phone',
+        method: 'post',
+        func: [person_ctrl.load_by_id, phone_ctrl.create]
+    },
+    {
+        url: '/person/:person_id/phone/:phone_id',
+        method: 'get',
+        func: [person_ctrl.load_by_id, phone_ctrl.get_by_id]
+    },
+    {
+        url: '/person/:person_id/phone/:phone_id',
+        method: 'put',
+        func: [person_ctrl.load_by_id, phone_ctrl.update_by_id]
+    },
+    {
+        url: '/person/:person_id/phone/:phone_id',
+        method: 'delete',
+        func: [person_ctrl.load_by_id, phone_ctrl.delete_by_id]
+    }
+]

+ 30 - 0
routes/postal_routes.js

@@ -0,0 +1,30 @@
+const postal_ctrl = require('../controllers/postal_address_ctrl');
+const person_ctrl = require('../controllers/person_ctrl');
+
+module.exports = [
+    {
+        url: '/person/:person_id/postalAddress',
+        method: 'get',
+        func: [person_ctrl.load_by_id, postal_ctrl.get_all]
+    },
+    {
+        url: '/person/:person_id/postalAddress',
+        method: 'post',
+        func: [person_ctrl.load_by_id, postal_ctrl.create]
+    },
+    {
+        url: '/person/:person_id/postalAddress/:postal_address_id',
+        method: 'get',
+        func: [person_ctrl.load_by_id, postal_ctrl.get_by_id]
+    },
+    {
+        url: '/person/:person_id/postalAddress/:postal_address_id',
+        method: 'put',
+        func: [person_ctrl.load_by_id, postal_ctrl.update_by_id]
+    },
+    {
+        url: '/person/:person_id/postalAddress/:postal_address_id',
+        method: 'delete',
+        func: [person_ctrl.load_by_id, postal_ctrl.delete_by_id]
+    }
+]