api/middleware/VegetableTypes.js

109 lines
2.2 KiB
JavaScript
Raw Normal View History

const models = require('../models')
const vegetableTypes = models.vegetableTypes
2018-09-08 00:00:47 +02:00
class VegetableTypes {
static getAll (req, callback) {
vegetableTypes.findAndCountAll({
order: [
['name', 'ASC']
2018-09-09 18:15:19 +02:00
],
include: [{
2019-04-11 12:31:56 +02:00
model: models.Vegetables,
as: 'Vegetables',
order: [
['name', 'ASC']
]
}]
2018-09-08 00:00:47 +02:00
})
.then(items => {
if (!items) {
callback(new Error('No vegetable type found'), 204)
return false
}
callback(null, items)
})
.catch((e) => {
callback(e, null)
})
}
createOne (req, callback) {
vegetableTypes.create(req.body)
.then(item => {
callback(null, item)
})
.catch(e => {
callback(e, null)
})
}
static getOne (req, callback) {
2018-09-09 18:15:19 +02:00
vegetableTypes.findById(
req.params.vegetableTypesId,
{
include: [{
model: models.Vegetables,
as: 'Vegetables',
order: [
['name', 'ASC']
]
}]
2018-09-09 18:15:19 +02:00
})
2018-09-08 00:00:47 +02:00
.then(item => {
if (!item) {
callback(new Error('Item vegetable type not found'), 404)
return false
}
callback(null, item)
})
.catch((e) => {
callback(e, null)
})
}
patchOne (req, callback) {
VegetableTypes.getOne(req, (err, item) => {
if (err) {
callback(err, item)
return false
}
item.update(req.body)
.then(animal => {
callback(null, animal)
})
.catch(e => {
callback(e, null)
})
})
}
deleteOne (req, callback) {
VegetableTypes.getOne(req, (err, item) => {
if (err) {
callback(err, item)
return false
}
vegetableTypes.destroy({
where: {
id: req.params.vegetableTypesId
}
})
.then(deleted => {
if (deleted === 0) {
callback(new Error('Error when trying to delete item'))
return false
}
callback(null, null)
})
.catch(e => {
callback(e, null)
})
})
}
}
module.exports = VegetableTypes