2018-09-08 00:00:47 +02:00
|
|
|
const express = require('express')
|
|
|
|
const router = express.Router()
|
2018-09-16 23:46:39 +02:00
|
|
|
const Vegetables = require('../middleware/Vegetables')
|
|
|
|
const Pictures = require('../middleware/Pictures')
|
2018-09-08 00:00:47 +02:00
|
|
|
|
|
|
|
module.exports = function (passport) {
|
2018-09-16 23:46:39 +02:00
|
|
|
const basePath = '/api/types/:vegetableTypesId/vegetables'
|
|
|
|
const itemPath = basePath + '/:vegetablesId'
|
|
|
|
const picturesPath = itemPath + '/pictures'
|
|
|
|
const picturePath = picturesPath + '/:pictureId'
|
2018-09-08 00:00:47 +02:00
|
|
|
|
|
|
|
router.route(basePath)
|
|
|
|
.get(
|
|
|
|
function (req, res) {
|
2018-09-16 23:46:39 +02:00
|
|
|
Vegetables.getAll(req, function (err, items) {
|
2018-09-08 00:00:47 +02:00
|
|
|
if (err) {
|
|
|
|
res.status(items || 500).send(err.message)
|
|
|
|
} else {
|
|
|
|
res.status(200).json(items)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.post(
|
|
|
|
passport.authenticate(['basic-auth']),
|
|
|
|
function (req, res) {
|
2018-09-16 23:46:39 +02:00
|
|
|
const vegetable = new Vegetables()
|
|
|
|
vegetable.createOne(req, function (err, item) {
|
2018-09-08 00:00:47 +02:00
|
|
|
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) {
|
2018-09-16 23:46:39 +02:00
|
|
|
Vegetables.getOne(req, function (err, item) {
|
2018-09-08 00:00:47 +02:00
|
|
|
if (err) {
|
|
|
|
res.status(item || 500).send(err.message)
|
|
|
|
} else {
|
|
|
|
res.status(200).json(item)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.patch(
|
|
|
|
passport.authenticate(['basic-auth']),
|
|
|
|
function (req, res) {
|
2018-09-16 23:46:39 +02:00
|
|
|
const vegetable = new Vegetables()
|
|
|
|
vegetable.patchOne(req, function (err, item) {
|
2018-09-08 00:00:47 +02:00
|
|
|
if (err) {
|
|
|
|
res.status(item || 500).send(err.message)
|
|
|
|
} else {
|
|
|
|
res.status(200).json(item)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.delete(
|
|
|
|
passport.authenticate(['basic-auth']),
|
|
|
|
function (req, res) {
|
2018-09-16 23:46:39 +02:00
|
|
|
const vegetable = new Vegetables()
|
|
|
|
vegetable.deleteOne(req, function (err, item) {
|
|
|
|
if (err) {
|
|
|
|
res.status(item || 500).send(err.message)
|
|
|
|
} else {
|
|
|
|
res.status(200).json(item)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
router.route(picturesPath)
|
|
|
|
.get(
|
|
|
|
function (req, res) {
|
|
|
|
Pictures.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 picture = new Pictures()
|
|
|
|
picture.createOne(req, function (err, item) {
|
|
|
|
if (err) {
|
|
|
|
res.status(item || 500).send(err.message)
|
|
|
|
} else {
|
|
|
|
res.status(201).json(item)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
router.route(picturePath)
|
|
|
|
.get(
|
|
|
|
function (req, res) {
|
|
|
|
Pictures.getOne(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 picture = new Pictures()
|
|
|
|
picture.deleteOne(req, function (err, item) {
|
2018-09-08 00:00:47 +02:00
|
|
|
if (err) {
|
|
|
|
res.status(item || 500).send(err.message)
|
|
|
|
} else {
|
|
|
|
res.status(200).json(item)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
return router
|
|
|
|
}
|