From e00a1987866a804f2ae6e3206bd583df703663e8 Mon Sep 17 00:00:00 2001 From: vantezzen Date: Fri, 23 Aug 2019 16:50:37 +0200 Subject: Add recipe store --- app/Controllers/Http/RecipeController.js | 110 +++++++++++++++++++++++----- app/Controllers/Http/ServiceController.js | 7 -- app/Controllers/Http/WorkspaceController.js | 27 +++++++ 3 files changed, 120 insertions(+), 24 deletions(-) (limited to 'app/Controllers/Http') diff --git a/app/Controllers/Http/RecipeController.js b/app/Controllers/Http/RecipeController.js index 0b9d488..641f4ef 100644 --- a/app/Controllers/Http/RecipeController.js +++ b/app/Controllers/Http/RecipeController.js @@ -1,27 +1,103 @@ 'use strict' const Recipe = use('App/Models/Recipe'); +const Helpers = use('Helpers') +const Drive = use('Drive') const fetch = require('node-fetch'); +const path = require('path'); class RecipeController { - async list({ - response - }) { - const officialRecipes = JSON.parse(await (await fetch('https://api.franzinfra.com/v1/recipes')).text()); - const customRecipesArray = (await Recipe.all()).rows; - const customRecipes = customRecipesArray.map(recipe => ({ - "id": recipe.recipeId, - "name": recipe.name, - ...JSON.parse(recipe.data) - })) - - const recipes = [ - ...officialRecipes, - ...customRecipes, - ] - - return response.send(recipes) + async list({ + response + }) { + const officialRecipes = JSON.parse(await (await fetch('https://api.franzinfra.com/v1/recipes')).text()); + const customRecipesArray = (await Recipe.all()).rows; + const customRecipes = customRecipesArray.map(recipe => ({ + "id": recipe.recipeId, + "name": recipe.name, + ...JSON.parse(recipe.data) + })) + + const recipes = [ + ...officialRecipes, + ...customRecipes, + ] + + return response.send(recipes) + } + + async create({ + request, + response + }) { + const data = request.all(); + + const pkg = request.file('package') + + await pkg.move(path.join(Helpers.appRoot(), '/recipes/'), { + name: data.id + '.tar.gz', + overwrite: false + }) + + await Recipe.create({ + name: data.name, + recipeId: data.id, + data: JSON.stringify({ + "author": data.author, + "featured": false, + "version": "1.0.0", + "icons": { + "png": data.png, + "svg": data.svg + } + }) + }) + + return response.send('Created new recipe') + } + + async search({ + request, + response + }) { + const needle = request.input('needle') + + const remoteResults = JSON.parse(await (await fetch('https://api.franzinfra.com/v1/recipes/search?needle=' + 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) + })) + + const results = [ + ...localResults, + ...remoteResults, + ] + + return response.send(results); + } + + // Download a recipe + async download({ + request, + response, + params + }) { + const service = params.recipe; + + // Chack for invalid characters + if (/\.{1,}/.test(service) || /\/{1,}/.test(service)) { + return response.send('Invalid recipe name'); + } + + // Check if recipe exists in recipes folder + if (await Drive.exists(service + '.tar.gz')) { + response.send(await Drive.get(service + '.tar.gz')) + } else { + response.redirect('https://api.franzinfra.com/v1/recipes/download/' + service) } + } } module.exports = RecipeController diff --git a/app/Controllers/Http/ServiceController.js b/app/Controllers/Http/ServiceController.js index 4c908ac..d1adea3 100644 --- a/app/Controllers/Http/ServiceController.js +++ b/app/Controllers/Http/ServiceController.js @@ -90,13 +90,6 @@ class ServiceController { return response.send(servicesArray) } - - // Download a recipe (currently simply redirects to Franz's API) - download({ request, response, params }) { - const service = params.recipe; - - response.redirect('https://api.franzinfra.com/v1/recipes/download/' + service) - } } module.exports = ServiceController diff --git a/app/Controllers/Http/WorkspaceController.js b/app/Controllers/Http/WorkspaceController.js index 55a0c75..5573382 100644 --- a/app/Controllers/Http/WorkspaceController.js +++ b/app/Controllers/Http/WorkspaceController.js @@ -83,6 +83,33 @@ class WorkspaceController { }) } + async delete({ + request, + response, + auth, + params + }) { + try { + await auth.getUser() + } catch (error) { + return response.send('Missing or invalid api token') + } + + const data = request.all(); + const { + id + } = params; + + // Update data in database + await (Workspace.query() + .where('workspaceId', id) + .where('userId', auth.user.id)).delete(); + + return response.send({ + "message": "Successfully deleted workspace", + }) + } + // List all workspaces a user has created async list({ request, -- cgit v1.2.3-70-g09d2