| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- 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);
- })
- }
- };
|