From 338eeb4562c3c236957962fb8520d5cdd016c909 Mon Sep 17 00:00:00 2001 From: dbroqua Date: Fri, 15 Mar 2019 13:51:11 +0100 Subject: [PATCH] Added vegetable search route --- app.js | 1 + middleware/Vegetables.js | 37 +++++++++++++++++++++++++++++++------ routes/search.js | 22 ++++++++++++++++++++++ 3 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 routes/search.js diff --git a/app.js b/app.js index 2cd3c09..675db28 100644 --- a/app.js +++ b/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)) diff --git a/middleware/Vegetables.js b/middleware/Vegetables.js index f9adbf1..abbc9f9 100644 --- a/middleware/Vegetables.js +++ b/middleware/Vegetables.js @@ -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, () => { }) } }) }) diff --git a/routes/search.js b/routes/search.js new file mode 100644 index 0000000..ef2d262 --- /dev/null +++ b/routes/search.js @@ -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 +}