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) {

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
}