Added routes for Types and Vegetables
This commit is contained in:
parent
a8dc7f8323
commit
e738d80579
17 changed files with 3938 additions and 304 deletions
90
middleware/VegetableTypes.js
Normal file
90
middleware/VegetableTypes.js
Normal file
|
@ -0,0 +1,90 @@
|
|||
const vegetableTypes = require('../models').vegetableTypes
|
||||
|
||||
class VegetableTypes {
|
||||
static getAll (req, callback) {
|
||||
vegetableTypes.findAndCountAll({
|
||||
order: [
|
||||
['name', 'ASC']
|
||||
]
|
||||
})
|
||||
.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) {
|
||||
vegetableTypes.findById(Number(req.params.vegetableTypesId))
|
||||
.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
|
Loading…
Add table
Add a link
Reference in a new issue