From 419933f6505caf4c5e685f8436b1ff735185e55a Mon Sep 17 00:00:00 2001 From: Vijay Raghavan Aravamudhan Date: Sun, 1 Aug 2021 11:07:57 +0000 Subject: Moved 'internal-server' into a sub-folder as opposed to a git submodule. (#1715) * Ignored tests in 'internal-server' folder since there are none. * Linter fixes --- .../app/Controllers/Http/WorkspaceController.js | 148 +++++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 src/internal-server/app/Controllers/Http/WorkspaceController.js (limited to 'src/internal-server/app/Controllers/Http/WorkspaceController.js') diff --git a/src/internal-server/app/Controllers/Http/WorkspaceController.js b/src/internal-server/app/Controllers/Http/WorkspaceController.js new file mode 100644 index 000000000..4189fbcdd --- /dev/null +++ b/src/internal-server/app/Controllers/Http/WorkspaceController.js @@ -0,0 +1,148 @@ +const Workspace = use('App/Models/Workspace'); +const { + validateAll, +} = use('Validator'); + +const uuid = require('uuid/v4'); + +class WorkspaceController { + // Create a new workspace for user + async create({ + request, + response, + }) { + // Validate user input + const validation = await validateAll(request.all(), { + name: 'required', + }); + if (validation.fails()) { + return response.status(401).send({ + message: 'Invalid POST arguments', + messages: validation.messages(), + status: 401, + }); + } + + const data = request.all(); + + // Get new, unused uuid + let workspaceId; + do { + workspaceId = uuid(); + } while ((await Workspace.query().where('workspaceId', workspaceId).fetch()).rows.length > 0); // eslint-disable-line no-await-in-loop + + const order = (await Workspace.all()).rows.length; + + await Workspace.create({ + workspaceId, + name: data.name, + order, + services: JSON.stringify([]), + data: JSON.stringify(data), + }); + + return response.send({ + userId: 1, + name: data.name, + id: workspaceId, + order, + workspaces: [], + }); + } + + async edit({ + request, + response, + params, + }) { + // Validate user input + const validation = await validateAll(request.all(), { + name: 'required', + services: 'required|array', + }); + if (validation.fails()) { + return response.status(401).send({ + message: 'Invalid POST arguments', + messages: validation.messages(), + status: 401, + }); + } + + const data = request.all(); + const { + id, + } = params; + + // Update data in database + await (Workspace.query() + .where('workspaceId', id)).update({ + name: data.name, + services: JSON.stringify(data.services), + }); + + // Get updated row + const workspace = (await Workspace.query() + .where('workspaceId', id).fetch()).rows[0]; + + return response.send({ + id: workspace.workspaceId, + name: data.name, + order: workspace.order, + services: data.services, + userId: 1, + }); + } + + async delete({ + // eslint-disable-next-line no-unused-vars + request, + response, + params, + }) { + // Validate user input + const validation = await validateAll(params, { + id: 'required', + }); + if (validation.fails()) { + return response.status(401).send({ + message: 'Invalid arguments', + messages: validation.messages(), + status: 401, + }); + } + + const { + id, + } = params; + + // Update data in database + await (Workspace.query() + .where('workspaceId', id)).delete(); + + return response.send({ + message: 'Successfully deleted workspace', + }); + } + + // List all workspaces a user has created + async list({ + response, + }) { + const workspaces = (await Workspace.all()).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: typeof workspace.services === 'string' ? JSON.parse(workspace.services) : workspace.services, + userId: 1, + })); + } + + return response.send(workspacesArray); + } +} + +module.exports = WorkspaceController; -- cgit v1.2.3-70-g09d2