api/middleware/VegetableProperties.js

95 lines
2 KiB
JavaScript
Raw Permalink Normal View History

2018-10-06 16:17:27 +02:00
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 => {
callback(e, null)
})
})
}
}
module.exports = VegetableProperties