Added Properties to Vegetable
This commit is contained in:
parent
c9652c1366
commit
6d94b5f60c
9 changed files with 277 additions and 1 deletions
92
middleware/Properties.js
Normal file
92
middleware/Properties.js
Normal file
|
@ -0,0 +1,92 @@
|
|||
const properties = require('../models').properties
|
||||
|
||||
class Properties {
|
||||
static getAll (req, callback) {
|
||||
properties.findAndCountAll({
|
||||
order: [
|
||||
['name', 'ASC']
|
||||
]
|
||||
})
|
||||
.then(items => {
|
||||
if (!items) {
|
||||
callback(new Error('No property found'), 204)
|
||||
return false
|
||||
}
|
||||
callback(null, items)
|
||||
})
|
||||
.catch((e) => {
|
||||
callback(e, null)
|
||||
})
|
||||
}
|
||||
|
||||
createOne (req, callback) {
|
||||
properties.create(req.body)
|
||||
.then(item => {
|
||||
callback(null, item)
|
||||
})
|
||||
.catch(e => {
|
||||
callback(e, null)
|
||||
})
|
||||
}
|
||||
|
||||
static getOne (req, callback) {
|
||||
properties.findById(
|
||||
req.params.propertyId
|
||||
)
|
||||
.then(item => {
|
||||
if (!item) {
|
||||
callback(new Error('Property not found'), 404)
|
||||
return false
|
||||
}
|
||||
callback(null, item)
|
||||
})
|
||||
.catch((e) => {
|
||||
callback(e, null)
|
||||
})
|
||||
}
|
||||
|
||||
patchOne (req, callback) {
|
||||
Properties.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) {
|
||||
Properties.getOne(req, (err, item) => {
|
||||
if (err) {
|
||||
callback(err, item)
|
||||
return false
|
||||
}
|
||||
|
||||
properties.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 = Properties
|
|
@ -1,4 +1,5 @@
|
|||
const vegetables = require('../models').vegetables
|
||||
const models = require('../models')
|
||||
const VegetableTypes = require('./VegetableTypes')
|
||||
const uuid = require('uuid/v4')
|
||||
const multer = require('multer')
|
||||
|
@ -176,7 +177,15 @@ class Vegetables {
|
|||
vegetableTypeId: req.params.vegetableTypesId,
|
||||
id: req.params.vegetablesId
|
||||
},
|
||||
include: ['Type', 'Pictures']
|
||||
include: [
|
||||
'Type',
|
||||
'Pictures',
|
||||
{
|
||||
model: models.vegetableProperties,
|
||||
as: 'Properties',
|
||||
include: ['Property']
|
||||
}
|
||||
]
|
||||
})
|
||||
.then(item => {
|
||||
if (!item) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue