api/middleware/VegetableProperties.js
2018-10-06 16:17:27 +02:00

95 lines
2 KiB
JavaScript

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 => {
console.log('this case?')
callback(e, null)
})
})
}
}
module.exports = VegetableProperties