const express = require('express') const router = express.Router() const VegetableTypes = require('../middleware/Vegetables') module.exports = function (passport) { const basePath = '/api/types/:vegetableTypesId/vegetables/' const itemPath = basePath + ':vegetablesId' router.route(basePath) .get( function (req, res) { VegetableTypes.getAll(req, function (err, items) { if (err) { res.status(items || 500).send(err.message) } else { res.status(200).json(items) } }) } ) .post( passport.authenticate(['basic-auth']), function (req, res) { const vegetableTypes = new VegetableTypes() vegetableTypes.createOne(req, function (err, item) { if (err) { res.status(item || 500).send(err.message) } else { res.status(201).json(item) } }) } ) router.route(itemPath) .get( function (req, res) { VegetableTypes.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 vegetableTypes = new VegetableTypes() vegetableTypes.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 vegetableTypes = new VegetableTypes() vegetableTypes.deleteOne(req, function (err, item) { if (err) { res.status(item || 500).send(err.message) } else { res.status(200).json(item) } }) } ) return router }