From 4db319476ca5e8dbfcd0ccfc466a7061a2d9ae5b Mon Sep 17 00:00:00 2001 From: vantezzen Date: Fri, 6 Sep 2019 12:14:27 +0200 Subject: Fix recipe search --- app/Controllers/Http/RecipeController.js | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) (limited to 'app') diff --git a/app/Controllers/Http/RecipeController.js b/app/Controllers/Http/RecipeController.js index 7109d2d..1905886 100644 --- a/app/Controllers/Http/RecipeController.js +++ b/app/Controllers/Http/RecipeController.js @@ -137,21 +137,27 @@ class RecipeController { const needle = request.input('needle'); // Get results - let remoteResults = []; - if (Env.get('CONNECT_WITH_FRANZ') == 'true' && needle !== 'ferdi:custom') { // eslint-disable-line eqeqeq - remoteResults = JSON.parse(await (await fetch(`https://api.franzinfra.com/v1/recipes/search?needle=${encodeURIComponent(needle)}`)).text()); - } - const localResultsArray = (await Recipe.query().where('name', 'LIKE', `%${needle}%`).fetch()).toJSON(); - const localResults = localResultsArray.map((recipe) => ({ - id: recipe.recipeId, - name: recipe.name, - ...JSON.parse(recipe.data), - })); - let results; + if (needle === 'ferdi:custom') { - results = localResults; + const dbResults = (await Recipe.all().fetch()).toJSON(); + results = dbResults.map((recipe) => ({ + id: recipe.recipeId, + name: recipe.name, + ...JSON.parse(recipe.data), + })); } else { + let remoteResults = []; + if (Env.get('CONNECT_WITH_FRANZ') == 'true') { // eslint-disable-line eqeqeq + remoteResults = JSON.parse(await (await fetch(`https://api.franzinfra.com/v1/recipes/search?needle=${encodeURIComponent(needle)}`)).text()); + } + const localResultsArray = (await Recipe.query().where('name', 'LIKE', `%${needle}%`).fetch()).toJSON(); + const localResults = localResultsArray.map((recipe) => ({ + id: recipe.recipeId, + name: recipe.name, + ...JSON.parse(recipe.data), + })); + results = [ ...localResults, ...remoteResults || [], -- cgit v1.2.3-54-g00ecf