Added vegetable search route

This commit is contained in:
Damien Broqua 2019-03-15 13:51:11 +01:00
parent b7f909ba09
commit 338eeb4562
3 changed files with 54 additions and 6 deletions

View file

@ -7,6 +7,7 @@ const path = require('path')
const fs = require('fs')
const Resize = require('../libs/resize')
const Aws = require('../libs/aws')
const Op = models.Sequelize.Op
class Vegetables {
constructor () {
@ -136,6 +137,30 @@ class Vegetables {
})
}
static searchAll (req, callback) {
vegetables.findAndCountAll({
where: {
name: {
[Op.iLike]: req.query.q
}
},
include: ['Type'],
order: [
['name', 'ASC']
]
})
.then(items => {
if (!items) {
callback(new Error('No vegetable found'), 204)
return false
}
callback(null, items)
})
.catch((e) => {
callback(e, null)
})
}
createOne (req, callback) {
VegetableTypes.getOne(req, (err, universe) => {
if (err) {
@ -158,7 +183,7 @@ class Vegetables {
let _create = () => {
if (uploaded === 2) {
this._createItem(req, callback)
fs.unlink(req.file.path, () => {})
fs.unlink(req.file.path, () => { })
}
}
@ -176,7 +201,7 @@ class Vegetables {
req.body.mainPicture = res.file
uploaded += 1
_create()
fs.unlink(file.output, () => {})
fs.unlink(file.output, () => { })
}
})
})
@ -194,7 +219,7 @@ class Vegetables {
if (!err) {
uploaded += 1
_create()
fs.unlink(file.output, () => {})
fs.unlink(file.output, () => { })
}
})
})
@ -262,7 +287,7 @@ class Vegetables {
let _patch = () => {
if (uploaded === 2) {
this._patchOne(item, values, callback)
fs.unlink(req.file.path, () => {})
fs.unlink(req.file.path, () => { })
}
}
@ -280,7 +305,7 @@ class Vegetables {
values.mainPicture = res.file
uploaded += 1
_patch()
fs.unlink(file.output, () => {})
fs.unlink(file.output, () => { })
}
})
})
@ -298,7 +323,7 @@ class Vegetables {
if (!err) {
uploaded += 1
_patch()
fs.unlink(file.output, () => {})
fs.unlink(file.output, () => { })
}
})
})