api/routes/vegetableTypes.js

76 lines
1.9 KiB
JavaScript
Raw Normal View History

2018-09-08 00:00:47 +02:00
const express = require('express')
const router = express.Router()
const VegetableTypes = require('../middleware/VegetableTypes')
module.exports = function (passport) {
const basePath = '/api/types/'
const itemPath = basePath + ':vegetableTypesId'
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 {
2018-09-09 18:15:19 +02:00
res.status(201).json(item)
2018-09-08 00:00:47 +02:00
}
})
}
)
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
}