Added vegetable search route
This commit is contained in:
parent
b7f909ba09
commit
338eeb4562
3 changed files with 54 additions and 6 deletions
1
app.js
1
app.js
|
@ -36,6 +36,7 @@ app.use('/', require('./routes/vegetableTypes')(passport))
|
|||
app.use('/', require('./routes/vegetables')(passport))
|
||||
app.use('/', require('./routes/properties')(passport))
|
||||
app.use('/', require('./routes/user')(passport))
|
||||
app.use('/', require('./routes/search')(passport))
|
||||
|
||||
app.use(function (req, res, next) {
|
||||
next(createError(404))
|
||||
|
|
|
@ -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) {
|
||||
|
|
22
routes/search.js
Normal file
22
routes/search.js
Normal 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
|
||||
}
|
Loading…
Reference in a new issue