Pārlūkot izejas kodu

ajout correction TP5 nodejs

Pierre Kraemer 5 gadi atpakaļ
vecāks
revīzija
3dd76d38d9

+ 86 - 0
NodeJS/correction_tp5/controllers/group.js

@@ -0,0 +1,86 @@
+const db = require('../models');
+
+module.exports = {
+	
+	get_all: (req, res, next) => {
+		return db.Group.findAll()
+		.then((groups) => res.json(groups))
+		.catch((err) => next(err));
+	},
+	
+	create: (req, res, next) => {
+		const data = {
+			title: req.body.title || ''
+		};
+		return db.Group.create(data)
+		.then((group) => res.json(group))
+		.catch((err) => next(err));
+	},
+	
+	get_by_id: (req, res, next) => {
+		return db.Group.findByPk(req.params.group_id)
+		.then((group) => {
+			if (!group) {
+				throw { status: 404, message: 'Requested Group not found' };
+			}
+			return res.json(group);
+		})
+		.catch((err) => next(err));
+	},
+	
+	update_by_id: (req, res, next) => {
+		return db.Group.findByPk(req.params.group_id)
+		.then((group) => {
+			if (!group) {
+				throw { status: 404, message: 'Requested Group not found' };
+			}
+			Object.assign(group, req.body);
+			return group.save();
+		})
+		.then((group) => res.json(group))
+		.catch((err) => next(err));
+	},
+	
+	delete_by_id: (req, res, next) => {
+		return db.Group.findByPk(req.params.group_id)
+		.then((group) => {
+			if (!group) {
+				throw { status: 404, message: 'Requested Group not found' };
+			}
+			return group.destroy();
+		})
+		.then(() => res.status(200).end())
+		.catch((err) => next(err));
+	},
+	
+	get_all_of_person: (req, res, next) => {
+		return req.person.getGroups()
+		.then((groups) => res.json(groups))
+		.catch((err) => next(err));
+	},
+	
+	add_to_person: (req, res, next) => {
+		return db.Group.findByPk(req.params.group_id)
+		.then((group) => {
+			if (!group) {
+				throw { status: 404, message: 'Requested Group not found' };
+			}
+			return req.person.addGroup(group);
+		})
+		.then(() => res.status(200).end())
+		.catch((err) => next(err));
+	},
+	
+	remove_from_person: (req, res, next) => {
+		return db.Group.findByPk(req.params.group_id)
+		.then((group) => {
+			if (!group) {
+				throw { status: 404, message: 'Requested Group not found' };
+			}
+			return req.person.removeGroup(group);
+		})
+		.then(() => res.status(200).end())
+		.catch((err) => next(err));
+	}
+
+};

+ 76 - 0
NodeJS/correction_tp5/controllers/mail_address.js

@@ -0,0 +1,76 @@
+const db = require('../models');
+
+module.exports = {
+	
+	get_all: (req, res, next) => {
+		let where = {};
+		if (req.query.type) {
+			where.type = req.query.type;
+		}
+		return req.person.getMailAddresses({
+			order: [ 'type' ],
+			where
+		})
+		.then((mailAddresses) => res.json(mailAddresses))
+		.catch((err) => next(err));
+	},
+	
+	get_by_id: (req, res, next) => {
+		return req.person.getMailAddresses({
+			where: {
+				id: req.params.mail_address_id
+			}
+		})
+		.then((mailAddresses) => {
+			if (mailAddresses.length === 0) {
+				throw { status: 404, message: 'Requested MailAddress not found' };
+			}
+			return res.json(mailAddresses[0]);
+		})
+		.catch((err) => next(err));
+	},
+
+	create: (req, res, next) => {
+		const data = {
+			address: req.body.address || '',
+			type: req.body.type || 'home'
+		};
+		return req.person.createMailAddress(data)
+		.then((mailAddress) => res.json(mailAddress))
+		.catch((err) => next(err));
+	},
+
+	update_by_id: (req, res, next) => {
+		return req.person.getMailAddresses({
+			where: {
+				id: req.params.mail_address_id
+			}
+		})
+		.then((mailAddresses) => {
+			if (mailAddresses.length === 0) {
+				throw { status: 404, message: 'Requested MailAddress not found' };
+			}
+			Object.assign(mailAddresses[0], req.body);
+			return mailAddresses[0].save();
+		})
+		.then((mailAddress) => res.json(mailAddress))
+		.catch((err) => next(err));
+	},
+
+	delete_by_id: (req, res, next) => {
+		return req.person.getMailAddresses({
+			where: {
+				id: req.params.mail_address_id
+			}
+		})
+		.then((mailAddresses) => {
+			if (mailAddresses.length === 0) {
+				throw { status: 404, message: 'Requested MailAddress not found' };
+			}
+			return mailAddresses[0].destroy();
+		})
+		.then(() => res.status(200).end())
+		.catch((err) => next(err));
+	}
+
+};

+ 80 - 0
NodeJS/correction_tp5/controllers/person.js

@@ -0,0 +1,80 @@
+const
+db = require('../models'),
+Sequelize = require('sequelize');
+
+module.exports = {
+	
+	get_all: (req, res, next) => {
+		let where = {};
+		if (req.query.name) {
+			where.lastname = {
+				[Sequelize.Op.like]: '%'+req.query.name+'%'
+			};
+		}
+		return db.Person.findAll({
+			order: [ 'lastname' ],
+			where
+		})
+		.then((people) => res.json(people))
+		.catch((err) => next(err));
+	},
+	
+	load_by_id: (req, res, next) => {
+		return db.Person.findByPk(req.params.person_id)
+		.then((person) => {
+			if (!person) {
+				throw { status: 404, message: 'Requested Person not found' };
+			}
+			req.person = person;
+			return next();
+		})
+		.catch((err) => next(err));
+	},
+	
+	get_by_id: (req, res, next) => {
+		return db.Person.findByPk(req.params.person_id)
+		.then((person) => {
+			if (!person) {
+				throw { status: 404, message: 'Requested Person not found' };
+			}
+			return res.json(person);
+		})
+		.catch((err) => next(err));
+	},
+	
+	create: (req, res, next) => {
+		const data = {
+			firstname: req.body.firstname || '',
+			lastname: req.body.lastname || ''
+		};
+		return db.Person.create(data)
+		.then((person) => res.json(person))
+		.catch((err) => next(err));
+	},
+	
+	update_by_id: (req, res, next) => {
+		return db.Person.findByPk(req.params.person_id)
+		.then((person) => {
+			if (!person) {
+				throw { status: 404, message: 'Requested Person not found' };
+			}
+			Object.assign(person, req.body);
+			return person.save();
+		})
+		.then((person) => res.json(person))
+		.catch((err) => next(err));
+	},
+	
+	delete_by_id: (req, res, next) => {
+		return db.Person.findByPk(req.params.person_id)
+		.then((person) => {
+			if (!person) {
+				throw { status: 404, message: 'Requested Person not found' };
+			}
+			return person.destroy();
+		})
+		.then(() => res.status(200).end())
+		.catch((err) => next(err));
+	}
+
+};

+ 76 - 0
NodeJS/correction_tp5/controllers/phone.js

@@ -0,0 +1,76 @@
+const db = require('../models');
+
+module.exports = {
+	
+	get_all: (req, res, next) => {
+		let where = {};
+		if (req.query.type) {
+			where.type = req.query.type;
+		}
+		return req.person.getPhones({
+			order: [ 'type' ],
+			where
+		})
+		.then((phones) => res.json(phones))
+		.catch((err) => next(err));
+	},
+	
+	get_by_id: (req, res, next) => {
+		return req.person.getPhones({
+			where: {
+				id: req.params.phone_id
+			}
+		})
+		.then((phones) => {
+			if (phones.length === 0) {
+				throw { status: 404, message: 'Requested Phone not found' };
+			}
+			return res.json(phones[0]);
+		})
+		.catch((err) => next(err));
+	},
+
+	create: (req, res, next) => {
+		const data = {
+			number: req.body.number || '',
+			type: req.body.type || 'home'
+		};
+		return req.person.createPhone(data)
+		.then((phone) => res.json(phone))
+		.catch((err) => next(err));
+	},
+
+	update_by_id: (req, res, next) => {
+		return req.person.getPhone({
+			where: {
+				id: req.params.phone_id
+			}
+		})
+		.then((phones) => {
+			if (phones.length === 0) {
+				throw { status: 404, message: 'Requested Phone not found' };
+			}
+			Object.assign(phones[0], req.body);
+			return phones[0].save();
+		})
+		.then((phone) => res.json(phone))
+		.catch((err) => next(err));
+	},
+
+	delete_by_id: (req, res, next) => {
+		return req.person.getPhones({
+			where: {
+				id: req.params.phone_id
+			}
+		})
+		.then((phones) => {
+			if (phones.length === 0) {
+				throw { status: 404, message: 'Requested Phone not found' };
+			}
+			return phones[0].destroy();
+		})
+		.then(() => res.status(200).end())
+		.catch((err) => next(err));
+	}
+
+};

+ 78 - 0
NodeJS/correction_tp5/controllers/postal_address.js

@@ -0,0 +1,78 @@
+const db = require('../models');
+
+module.exports = {
+	
+	get_all: (req, res, next) => {
+		let where = {};
+		if (req.query.type) {
+			where.type = req.query.type;
+		}
+		return req.person.getPostalAddresses({
+			order: [ 'type' ],
+			where
+		})
+		.then((postalAddresses) => res.json(postalAddresses))
+		.catch((err) => next(err));
+	},
+	
+	get_by_id: (req, res, next) => {
+		return req.person.getPostalAddresses({
+			where: {
+				id: req.params.postal_address_id
+			}
+		})
+		.then((postalAddresses) => {
+			if (postalAddresses.length === 0) {
+				throw { status: 404, message: 'Requested PostalAddress not found' };
+			}
+			return res.json(postalAddresses[0]);
+		})
+		.catch((err) => next(err));
+	},
+
+	create: (req, res, next) => {
+		const data = {
+			address: req.body.address || '',
+			city: req.body.city || '',
+			country: req.body.country || '',
+			type: req.body.type || 'home'
+		};
+		return req.person.createPostalAddress(data)
+		.then((postalAddress) => res.json(postalAddress))
+		.catch((err) => next(err));
+	},
+
+	update_by_id: (req, res, next) => {
+		return req.person.getPostalAddresses({
+			where: {
+				id: req.params.postal_address_id
+			}
+		})
+		.then((postalAddresses) => {
+			if (postalAddresses.length === 0) {
+				throw { status: 404, message: 'Requested PostalAddress not found' };
+			}
+			Object.assign(postalAddresses[0], req.body);
+			return postalAddresses[0].save();
+		})
+		.then((postalAddress) => res.json(postalAddress))
+		.catch((err) => next(err));
+	},
+
+	delete_by_id: (req, res, next) => {
+		return req.person.getPostalAddresses({
+			where: {
+				id: req.params.postal_address_id
+			}
+		})
+		.then((postalAddresses) => {
+			if (postalAddresses.length === 0) {
+				throw { status: 404, message: 'Requested PostalAddress not found' };
+			}
+			return postalAddresses[0].destroy();
+		})
+		.then(() => res.status(200).end())
+		.catch((err) => next(err));
+	}
+
+};

+ 19 - 0
NodeJS/correction_tp5/models/group.js

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

+ 29 - 0
NodeJS/correction_tp5/models/index.js

@@ -0,0 +1,29 @@
+const
+fs = require('fs'),
+Sequelize = require('sequelize');
+
+// create Sequelize instance
+const sequelize = new Sequelize('w4a', 'w4a', 'w4aw4aw4a', {
+	host: 'localhost',
+	port: 3306,
+	dialect: 'mysql',
+	dialectOptions: { decimalNumbers: true }
+	// logging: false
+});
+
+const db = {};
+
+fs.readdirSync(__dirname)
+.filter((filename) => filename !== 'index.js')
+.forEach((filename) => {
+	const model = require('./' + filename)(sequelize);
+	db[model.name] = model;
+});
+
+Object.keys(db).forEach((modelName) => {
+	db[modelName].associate(db);
+});
+
+sequelize.sync();
+
+module.exports = db;

+ 25 - 0
NodeJS/correction_tp5/models/mail_address.js

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

+ 24 - 0
NodeJS/correction_tp5/models/person.js

@@ -0,0 +1,24 @@
+const Sequelize = require('sequelize');
+
+module.exports = (sequelize) => {
+	
+	class Person extends Sequelize.Model {
+		static associate(db) {
+			Person.hasMany(db.MailAddress, { onDelete: 'cascade' });
+			Person.hasMany(db.Phone, { onDelete: 'cascade' });
+			Person.hasMany(db.PostalAddress, { onDelete: 'cascade' });
+			Person.belongsToMany(db.Group, { through: 'PersonGroup' });
+		}
+	}
+
+	Person.init({
+		lastname: Sequelize.STRING,
+		firstname: Sequelize.STRING
+	}, {
+		sequelize,
+		modelName: 'Person'
+	});
+	
+	return Person;
+
+};

+ 20 - 0
NodeJS/correction_tp5/models/phone.js

@@ -0,0 +1,20 @@
+const Sequelize = require('sequelize');
+
+module.exports = (sequelize) => {
+
+	class Phone extends Sequelize.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;
+};

+ 22 - 0
NodeJS/correction_tp5/models/postal_address.js

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

+ 47 - 0
NodeJS/correction_tp5/routes/group.js

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

+ 14 - 0
NodeJS/correction_tp5/routes/index.js

@@ -0,0 +1,14 @@
+const
+fs = require('fs');
+
+module.exports = function (app) {
+	
+	fs.readdirSync(__dirname)
+	.filter((filename) => filename !== 'index.js')
+	.forEach((filename) => {
+		require('./' + filename).forEach((r) => {
+			app[r.method](r.url, r.func);
+		});
+	});
+
+};

+ 32 - 0
NodeJS/correction_tp5/routes/mail_address.js

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

+ 31 - 0
NodeJS/correction_tp5/routes/person.js

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

+ 32 - 0
NodeJS/correction_tp5/routes/phone.js

@@ -0,0 +1,32 @@
+const person_ctrl = require('../controllers/person');
+const phone_ctrl = require('../controllers/phone');
+
+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 ]
+	}
+	
+];

+ 32 - 0
NodeJS/correction_tp5/routes/postal_address.js

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

+ 28 - 0
NodeJS/correction_tp5/server.js

@@ -0,0 +1,28 @@
+const
+express = require('express'),
+bodyParser = require('body-parser'),
+cors = require('cors');
+
+const app = express();
+
+app.use(cors());
+app.use(bodyParser.json());
+
+// register routes
+require('./routes')(app);
+
+// register error handling middleware
+app.use((err, req, res, next) => {
+	if (err.status === undefined) {
+		return res.status(500).send(err.message);
+	} else {
+		return res.status(err.status).send(err.message);
+	}
+});
+
+// launch server
+const server = app.listen(3000, () => {
+	const host = server.address().address;
+	const port = server.address().port;
+	console.log('App listening at http://%s:%s', host, port);
+});