From 5970b8e5bbf993c88c1f901708a7c5075a916770 Mon Sep 17 00:00:00 2001 From: vantezzen Date: Fri, 23 Aug 2019 14:04:22 +0200 Subject: Add support for workspaces --- app/Controllers/Http/RecipeController.js | 27 +++++++ app/Controllers/Http/ServiceController.js | 2 +- app/Controllers/Http/WorkspaceController.js | 116 ++++++++++++++++++++++++++++ 3 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 app/Controllers/Http/RecipeController.js create mode 100644 app/Controllers/Http/WorkspaceController.js (limited to 'app/Controllers/Http') diff --git a/app/Controllers/Http/RecipeController.js b/app/Controllers/Http/RecipeController.js new file mode 100644 index 0000000..0b9d488 --- /dev/null +++ b/app/Controllers/Http/RecipeController.js @@ -0,0 +1,27 @@ +'use strict' + +const Recipe = use('App/Models/Recipe'); +const fetch = require('node-fetch'); + +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) + } +} + +module.exports = RecipeController diff --git a/app/Controllers/Http/ServiceController.js b/app/Controllers/Http/ServiceController.js index 0fcbec0..4c908ac 100644 --- a/app/Controllers/Http/ServiceController.js +++ b/app/Controllers/Http/ServiceController.js @@ -23,7 +23,7 @@ class ServiceController { let serviceId; do { serviceId = uuid(); - } while((await Service.all()).rows.length > 0) + } while((await Service.query().where('serviceId', serviceId).fetch()).rows.length > 0) const service = await Service.create({ userId: auth.user.id, diff --git a/app/Controllers/Http/WorkspaceController.js b/app/Controllers/Http/WorkspaceController.js new file mode 100644 index 0000000..55a0c75 --- /dev/null +++ b/app/Controllers/Http/WorkspaceController.js @@ -0,0 +1,116 @@ +'use strict' + +const Workspace = use('App/Models/Workspace'); +const uuid = require('uuid/v4'); + +class WorkspaceController { + // Create a new workspace for user + async create({ + request, + response, + auth + }) { + try { + await auth.getUser() + } catch (error) { + return response.send('Missing or invalid api token') + } + + const data = request.all(); + + // Get new, unused uuid + let workspaceId; + do { + workspaceId = uuid(); + } while ((await Workspace.query().where('workspaceId', workspaceId).fetch()).rows.length > 0) + + const order = (await auth.user.workspaces().fetch()).rows.length; + + await Workspace.create({ + userId: auth.user.id, + workspaceId, + name: data.name, + order, + services: JSON.stringify([]), + data: JSON.stringify(data) + }); + + return response.send({ + userId: auth.user.id, + name: data.name, + id: workspaceId, + order, + workspaces: [], + }) + } + + async edit({ + 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)).update({ + name: data.name, + services: JSON.stringify(data.services) + }); + + // Get updated row + const workspace = (await Workspace.query() + .where('workspaceId', id) + .where('userId', auth.user.id).fetch()).rows[0]; + + return response.send({ + "id": workspace.workspaceId, + "name": data.name, + "order": workspace.order, + "services": data.services, + "userId": auth.user.id + }) + } + + // List all workspaces a user has created + async list({ + request, + response, + auth + }) { + try { + await auth.getUser() + } catch (error) { + return response.send('Missing or invalid api token') + } + + const workspaces = (await auth.user.workspaces().fetch()).rows; + // Convert to array with all data Franz wants + let workspacesArray = []; + if(workspaces) { + workspacesArray = workspaces.map(workspace => ({ + "id": workspace.workspaceId, + "name": workspace.name, + "order": workspace.order, + "services": JSON.parse(workspace.services), + "userId": auth.user.id + })) + } + + + return response.send(workspacesArray) + } +} + +module.exports = WorkspaceController -- cgit v1.2.3-70-g09d2