From abffdc9438020b22fb66d22bb8614650d0a36d65 Mon Sep 17 00:00:00 2001 From: dbroqua Date: Sat, 6 Oct 2018 16:17:27 +0200 Subject: [PATCH] Added Vegetable <-> Properties --- middleware/VegetableProperties.js | 95 +++++++++++++++++++++++++++++++ routes/vegetables.js | 74 ++++++++++++++++++++++++ 2 files changed, 169 insertions(+) create mode 100644 middleware/VegetableProperties.js diff --git a/middleware/VegetableProperties.js b/middleware/VegetableProperties.js new file mode 100644 index 0000000..874d2a9 --- /dev/null +++ b/middleware/VegetableProperties.js @@ -0,0 +1,95 @@ +const Vegetables = require('./Vegetables') +const vegetableProperties = require('../models').vegetableProperties + +class VegetableProperties { + createOne (req, callback) { + Vegetables.getOne(req, (err, item) => { + if (err) { + callback(err, item) + return false + } + + req.body.vegetableId = req.params.vegetablesId + + vegetableProperties.create(req.body) + .then(item => { + callback(null, item) + }) + .catch(e => { + callback(e, null) + }) + }) + } + + static getOne (req, callback) { + Vegetables.getOne(req, (err, item) => { + if (err) { + callback(err, item) + return false + } + vegetableProperties.findById( + req.params.propertyId, + { + include: ['Vegetable'] + }) + .then(item => { + if (!item) { + callback(new Error('Item vegetable property not found'), 404) + return false + } + callback(null, item) + }) + .catch((e) => { + callback(e, null) + }) + }) + } + + patchOne (req, callback) { + VegetableProperties.getOne(req, (err, item) => { + if (err) { + callback(err, item) + return false + } + + delete req.body.vegetableId + + item.update(req.body) + .then(animal => { + callback(null, animal) + }) + .catch(e => { + callback(e, null) + }) + }) + } + + deleteOne (req, callback) { + VegetableProperties.getOne(req, (err, item) => { + if (err) { + callback(err, item) + return false + } + + vegetableProperties.destroy({ + where: { + id: req.params.propertyId + } + }) + .then(deleted => { + if (deleted === 0) { + callback(new Error('Error when trying to delete item')) + return false + } + + callback(null, null) + }) + .catch(e => { + console.log('this case?') + callback(e, null) + }) + }) + } +} + +module.exports = VegetableProperties diff --git a/routes/vegetables.js b/routes/vegetables.js index 1db5eb9..6afd42f 100644 --- a/routes/vegetables.js +++ b/routes/vegetables.js @@ -2,13 +2,17 @@ const express = require('express') const router = express.Router() const Vegetables = require('../middleware/Vegetables') const Pictures = require('../middleware/Pictures') +const Properties = require('../middleware/VegetableProperties') module.exports = function (passport) { const basePath = '/api/types/:vegetableTypesId/vegetables' const itemPath = basePath + '/:vegetablesId' const picturesPath = itemPath + '/pictures' const picturePath = picturesPath + '/:pictureId' + const propertiesPath = itemPath + '/properties' + const propertyPath = propertiesPath + '/:propertyId' + /* Vegetables */ router.route(basePath) .get( function (req, res) { @@ -35,6 +39,7 @@ module.exports = function (passport) { } ) + /* Vegetable */ router.route(itemPath) .get( function (req, res) { @@ -74,6 +79,7 @@ module.exports = function (passport) { } ) + /* Pictures */ router.route(picturesPath) .get( function (req, res) { @@ -100,6 +106,7 @@ module.exports = function (passport) { } ) + /* Picture */ router.route(picturePath) .get( function (req, res) { @@ -126,5 +133,72 @@ module.exports = function (passport) { } ) + /* Properties */ + router.route(propertiesPath) + .get( + function (req, res) { + Properties.getAll(req, function (err, item) { + if (err) { + res.status(item || 500).send(err.message) + } else { + res.status(200).json(item) + } + }) + } + ) + .post( + passport.authenticate(['basic-auth']), + function (req, res) { + const property = new Properties() + property.createOne(req, function (err, item) { + if (err) { + res.status(item || 500).send(err.message) + } else { + res.status(201).json(item) + } + }) + } + ) + + /* Property */ + router.route(propertyPath) + .get( + function (req, res) { + Properties.getOne(req, function (err, item) { + if (err) { + res.status(item || 500).send(err.message) + } else { + res.status(200).json(item) + } + }) + } + ) + .patch( + passport.authenticate(['basic-auth']), + function (req, res) { + const property = new Properties() + property.patchOne(req, function (err, item) { + if (err) { + res.status(item || 500).send(err.message) + } else { + res.status(200).json(item) + } + }) + } + ) + .delete( + passport.authenticate(['basic-auth']), + function (req, res) { + const property = new Properties() + property.deleteOne(req, function (err, item) { + if (err) { + res.status(item || 500).send(err.message) + } else { + res.status(200).json(item) + } + }) + } + ) + return router }