Added vegetable search route

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

1
app.js
View file

@ -36,6 +36,7 @@ app.use('/', require('./routes/vegetableTypes')(passport))
app.use('/', require('./routes/vegetables')(passport)) app.use('/', require('./routes/vegetables')(passport))
app.use('/', require('./routes/properties')(passport)) app.use('/', require('./routes/properties')(passport))
app.use('/', require('./routes/user')(passport)) app.use('/', require('./routes/user')(passport))
app.use('/', require('./routes/search')(passport))
app.use(function (req, res, next) { app.use(function (req, res, next) {
next(createError(404)) next(createError(404))

View file

@ -7,6 +7,7 @@ const path = require('path')
const fs = require('fs') const fs = require('fs')
const Resize = require('../libs/resize') const Resize = require('../libs/resize')
const Aws = require('../libs/aws') const Aws = require('../libs/aws')
const Op = models.Sequelize.Op
class Vegetables { class Vegetables {
constructor () { 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) { createOne (req, callback) {
VegetableTypes.getOne(req, (err, universe) => { VegetableTypes.getOne(req, (err, universe) => {
if (err) { if (err) {
@ -158,7 +183,7 @@ class Vegetables {
let _create = () => { let _create = () => {
if (uploaded === 2) { if (uploaded === 2) {
this._createItem(req, callback) 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 req.body.mainPicture = res.file
uploaded += 1 uploaded += 1
_create() _create()
fs.unlink(file.output, () => {}) fs.unlink(file.output, () => { })
} }
}) })
}) })
@ -194,7 +219,7 @@ class Vegetables {
if (!err) { if (!err) {
uploaded += 1 uploaded += 1
_create() _create()
fs.unlink(file.output, () => {}) fs.unlink(file.output, () => { })
} }
}) })
}) })
@ -262,7 +287,7 @@ class Vegetables {
let _patch = () => { let _patch = () => {
if (uploaded === 2) { if (uploaded === 2) {
this._patchOne(item, values, callback) 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 values.mainPicture = res.file
uploaded += 1 uploaded += 1
_patch() _patch()
fs.unlink(file.output, () => {}) fs.unlink(file.output, () => { })
} }
}) })
}) })
@ -298,7 +323,7 @@ class Vegetables {
if (!err) { if (!err) {
uploaded += 1 uploaded += 1
_patch() _patch()
fs.unlink(file.output, () => {}) fs.unlink(file.output, () => { })
} }
}) })
}) })

22
routes/search.js Normal file
View file

@ -0,0 +1,22 @@
const express = require('express')
const router = express.Router()
const Vegetables = require('../middleware/Vegetables')
module.exports = function () {
const basePath = '/api/search/vegetables'
router.route(basePath)
.get(
function (req, res) {
Vegetables.searchAll(req, function (err, items) {
if (err) {
res.status(items || 500).send(err.message)
} else {
res.status(200).json(items)
}
})
}
)
return router
}