2019-04-11 12:28:09 +02:00
|
|
|
const models = require('../models')
|
|
|
|
const vegetableTypes = models.vegetableTypes
|
2018-09-08 00:00:47 +02:00
|
|
|
|
|
|
|
class VegetableTypes {
|
2019-04-11 13:17:39 +02:00
|
|
|
static getAll (req, callback) {
|
2018-09-08 00:00:47 +02:00
|
|
|
vegetableTypes.findAndCountAll({
|
|
|
|
order: [
|
|
|
|
['name', 'ASC']
|
2018-09-09 18:15:19 +02:00
|
|
|
],
|
2019-04-11 12:28:09 +02:00
|
|
|
include: [{
|
2019-04-11 12:34:20 +02:00
|
|
|
model: models.vegetables,
|
2019-04-11 12:31:56 +02:00
|
|
|
as: 'Vegetables',
|
|
|
|
order: [
|
|
|
|
['name', 'ASC']
|
|
|
|
]
|
2019-04-11 12:28:09 +02:00
|
|
|
}]
|
2018-09-08 00:00:47 +02:00
|
|
|
})
|
|
|
|
.then(items => {
|
|
|
|
if (!items) {
|
|
|
|
callback(new Error('No vegetable type found'), 204)
|
|
|
|
return false
|
|
|
|
}
|
2019-04-11 12:47:20 +02:00
|
|
|
|
|
|
|
let res = []
|
|
|
|
|
|
|
|
for (let i = 0; i < items.rows.length; i += 1) {
|
2019-04-11 12:57:48 +02:00
|
|
|
let type = items.rows[i].toJSON()
|
2019-04-11 12:47:20 +02:00
|
|
|
let vegetables = []
|
|
|
|
|
|
|
|
for (let j = 0; j < type.Vegetables.length; j += 1) {
|
|
|
|
let k = 0
|
|
|
|
for (k = 0; k < vegetables.length; k += 1) {
|
2019-04-11 13:17:39 +02:00
|
|
|
if (type.Vegetables[j].name < vegetables[k].name) {
|
2019-04-11 13:13:01 +02:00
|
|
|
console.log('Break at:', k)
|
2019-04-11 12:47:20 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vegetables.splice(k, 0, type.Vegetables[j])
|
|
|
|
}
|
|
|
|
type.Vegetables = vegetables
|
|
|
|
res.push(type)
|
|
|
|
}
|
|
|
|
|
2019-04-11 13:13:01 +02:00
|
|
|
callback(null, { count: res.length, rows: res })
|
2018-09-08 00:00:47 +02:00
|
|
|
})
|
|
|
|
.catch((e) => {
|
2019-04-11 12:55:16 +02:00
|
|
|
console.log('ERR:', e)
|
2018-09-08 00:00:47 +02:00
|
|
|
callback(e, null)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-11 13:17:39 +02:00
|
|
|
createOne (req, callback) {
|
2018-09-08 00:00:47 +02:00
|
|
|
vegetableTypes.create(req.body)
|
|
|
|
.then(item => {
|
|
|
|
callback(null, item)
|
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
callback(e, null)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-11 13:17:39 +02:00
|
|
|
static getOne (req, callback) {
|
2018-09-09 18:15:19 +02:00
|
|
|
vegetableTypes.findById(
|
|
|
|
req.params.vegetableTypesId,
|
|
|
|
{
|
2019-04-11 12:28:09 +02:00
|
|
|
include: [{
|
2019-04-11 12:34:20 +02:00
|
|
|
model: models.vegetables,
|
2019-04-11 12:28:09 +02:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-11 13:17:39 +02:00
|
|
|
patchOne (req, callback) {
|
2018-09-08 00:00:47 +02:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-11 13:17:39 +02:00
|
|
|
deleteOne (req, callback) {
|
2018-09-08 00:00:47 +02:00
|
|
|
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
|