Clément Krebs пре 5 година
родитељ
комит
9f1e6662bf
4 измењених фајлова са 197 додато и 104 уклоњено
  1. 31 0
      db.js
  2. 72 0
      mail_address_ctrl.js
  3. 72 0
      person_ctrl.js
  4. 22 104
      server.js

+ 31 - 0
db.js

@@ -0,0 +1,31 @@
+const Sequelize = require('sequelize');
+const Model = Sequelize.Model;
+
+const sequelize = new Sequelize('w4aclem', 'clement.krebs', 'n4or2bgm', {
+    host: 'mysql.iutrs.unistra.fr',
+    dialect: 'mysql'
+});
+
+class Person extends Model {}
+Person.init({
+    lastname: Sequelize.STRING,
+    firstname: Sequelize.STRING
+}, {sequelize, modelName: 'Person'});
+
+class MailAddress extends Model {}
+MailAddress.init({
+    address: {
+        type: Sequelize.STRING,
+        validate: {
+            isEmail: true
+        }
+    },
+    type: Sequelize.ENUM('home', 'work')
+}, {sequelize, modelName: 'MailAddress'});
+
+MailAddress.belongsTo(Person, {onDelete: 'cascade'});
+Person.hasMany(MailAddress, {onDelete: 'cascade'});
+
+sequelize.sync();
+
+module.exports = {Person, MailAddress};

+ 72 - 0
mail_address_ctrl.js

@@ -0,0 +1,72 @@
+const db = require('./db');
+
+module.exports = {
+    get_all(req, res, next) {
+        req.person.getMailAddresses()
+        .then (mailAddresses => {
+            if (!mailAddresses) {
+                throw {status: 404, message: "Pas d'adresse"}
+            }
+            res.json(mailAddresses)
+        })
+        .catch(function(error) {
+            next(error);
+        })
+    },
+    create(req, res, next) {
+        if (!req.body.address || !req.body.type) {
+            throw {status: 400, message: "Manque type ou adresse"}
+        }
+        req.person.createMailAddress({address: req.body.address, type: req.body.type})
+        .then (mail => {
+            res.json(mail);
+        })
+        .catch(error => {
+            next(error);
+        })
+    },
+    get_by_id(req, res, next) {
+        req.person.getMailAddresses({where: {
+            id: req.params.mail_address_id
+        }})
+        .then (([m]) => {
+            if (!m) {
+                throw {status: 404, message: "Pas trouvé"}
+            }
+            res.json([m]);
+        })
+        .catch(error => {
+            next(error);
+        })
+    },
+    update_by_id(req, res, next) {
+        req.person.getMailAddresses({where: {
+            id: req.params.mail_address_id
+        }})
+        .then (([m]) => {
+            if (!m) {
+                throw {status: 404, message: "Pas trouvé 2"}
+            }
+            m.update(req.body);
+            res.json(m);
+        })
+        .catch(error => {
+            next(error);
+        })
+    },
+    delete_by_id(req, res, next) {
+        req.person.getMailAddresses({where: {
+            id: req.params.mail_address_id
+        }})
+        .then(([m]) => {
+            if (!m) {
+                throw {status: 404, message: "Pas trouvé 3"}
+            }
+            m.destroy();
+            res.send("fait")
+        })
+        .catch(error => {
+            next(error);
+        })
+    }
+};

+ 72 - 0
person_ctrl.js

@@ -0,0 +1,72 @@
+const db = require('./db');
+
+module.exports = {
+    get_all(req, res, next) {
+        db.Person.findAll().then(persons => {
+            res.json(persons)
+        }) .catch(error => {
+            next(error);
+        })
+    },
+
+    create(req, res, next) {
+        if (req.query.firstname == undefined || req.query.lastname == undefined) {
+            throw "Un des éléments est vide";
+        }
+        db.Person.create({lastname: req.query.firstname, firstname: req.query.lastname}).then(p => {
+            res.json(p);
+        }) .catch(function (error) {
+            next(error);
+        })  
+    },
+    get_by_id(req, res, next) {
+        db.Person.findByPk(req.params.person_id).then(persons => {
+            if (!persons) {
+                throw { status: 404, message: "Pas bonne id" }
+            }
+            res.json(persons)
+        }) .catch(function (error) {
+            next(error);
+        })
+    },
+    update_by_id(req, res, next) {
+        db.Person.findByPk(req.params.person_id).then(person => {
+            if (!person) {
+                throw "Pas bonne id";
+            } else {
+                person.update(req.body)
+                .then(
+                    res.send("ok")
+                )
+            }
+        }) .catch(function (error) {
+            next(error);
+        })
+
+
+    },
+    delete_by_id(req, res, next) {
+        db.Person.destroy({
+            where: {
+                id: req.params.person_id
+            }
+        }) .then(data => {
+            if (!data) {
+                throw "Pas bonne id"
+            }
+            res.send("fait")}) .catch(function (error) {
+            next(error);
+        })
+    },
+    load_by_id(req, res, next) {
+        db.Person.findByPk(req.params.person_id).then(person => {
+            if (!person) {
+                throw { status: 404, message: "Pas bonne id" }
+            }
+            req.person = person;
+            next();
+        }) .catch(function (error) {
+            next(error);
+        })
+    }
+};

+ 22 - 104
server.js

@@ -1,125 +1,43 @@
-const Sequelize = require('sequelize');
-const Model = Sequelize.Model;
 const express = require('express');
 const server= express();
+
 const bd = require('body-parser');
+
 server.use(bd.json());
 
-server.use(function (err, req, res, next) {
-    console.error(err.stack);
-    res.status(500).send(err.stack);
-    
-});
+const personCtrl = require('./person_ctrl');
+const mailCtrl = require('./mail_address_ctrl');
 
-const personCtrl = {
-    get_all(res) {
-        Person.findAll().then(persons => {
-            res.send(JSON.stringify(persons, null, 4));
-        })
-    },
-
-    create(req, res, next) {
-        if (req.query.firstname == undefined || req.query.lastname == undefined) {
-            throw "Un des éléments est vide";
-        }
-        Person.create({lastname: req.query.firstname, firstname: req.query.lastname}).then(p => {
-            res.send("ok");
-        }) .catch(function (error) {
-            next(error);
-        })  
-    },
-    get_by_id(req, res, next) {
-    Person.findByPk(req.params.person_id).then(persons => {
-        if (!persons) {
-            throw "Pas bonne id"
-        }
-        res.send(JSON.stringify(persons, null, 4))
-    }) .catch(function (error) {
-        next(error);
-    })
-
-    },
-    update_by_id(req, res, next) {
-        Person.findByPk(req.params.person_id).then(persons => {
-            if (!persons) {
-                throw "Pas bonne id";
-            } else {
-                Person.update(req.body, {
-                    where: {
-                        id: req.params.person_id
-                    }
-                }) .then(
-                    res.send("ok")
-                )
-            }
-        }) .catch(function (error) {
-            next(error);
-        })
-
-
-    },
-    delete_by_id(req, res, next) {
-        Person.destroy({
-            where: {
-                id: req.params.person_id
-            }
-        }) .then(data => {
-            if (!data) {
-                throw "Pas bonne id"
-            }
-            res.send("fait")}) .catch(function (error) {
-            next(error);
-        })
-    }
-}
 
-const pCtrl = Object.create(personCtrl);
+server.get('/person', personCtrl.get_all);
 
-const sequelize = new Sequelize('w4aclem', 'clement.krebs', 'n4or2bgm', {
-    host: 'mysql.iutrs.unistra.fr',
-    dialect: 'mysql'
-});
+server.post('/person', personCtrl.create);
 
+server.get('/person/:person_id', personCtrl.get_by_id);
 
-class Person extends Model {}
-Person.init({
-    lastname: Sequelize.STRING,
-    firstname: Sequelize.STRING
-}, {sequelize, modelName: 'Person'});
+server.put('/person/:person_id', personCtrl.update_by_id);
 
-/*sequelize.sync()
-.then(() => Person.create({
-    lastname: 'moi',
-    firstname: 'toi'
-}))
-.then(moi => {
-    console.log(moi.toJSON());
-});*/
+server.delete('/person/:person_id', personCtrl.delete_by_id);
 
-server.get('/person', function (req, res) {
-    pCtrl.get_all(res);
-});
+server.get('/person/:person_id/mailAddress', personCtrl.load_by_id, mailCtrl.get_all);
 
-server.post('/person', function (req, res, next) {
-    pCtrl.create(req, res, next);
-});
 
-server.get('/person/:person_id', function (req, res, next) {
-    pCtrl.get_by_id(req, res, next);
-});
-
-server.put('/person/:person_id', function (req, res, next) {
-    pCtrl.update_by_id(req, res, next);
-});
-
-server.delete('/person/:person_id', function (req, res, next) {
-    pCtrl.delete_by_id(req, res, next);
-});
+server.post('/person/:person_id/mailAddress', personCtrl.load_by_id, mailCtrl.create);
 
+server.get('/person/:person_id/mailAddress/:mail_address_id', personCtrl.load_by_id, mailCtrl.get_by_id);
 
+server.put('/person/:person_id/mailAddress/:mail_address_id', personCtrl.load_by_id, mailCtrl.update_by_id);
 
+server.delete('/person/:person_id/mailAddress/:mail_address_id', personCtrl.load_by_id, mailCtrl.delete_by_id);
 
+server.use(function (err, req, res, next) {
+    if (err.status) {
+        res.status(err.status).send(err.message);
+    } else {
+        res.status(500).send(err.message);
+    }
+});
 
 server.listen(8000, function () {
     console.log('ouais ouais ouais !!!');
-});
+});