diff options
Diffstat (limited to 'app/Controllers/Http/WorkspaceController.js')
-rw-r--r-- | app/Controllers/Http/WorkspaceController.js | 71 |
1 files changed, 31 insertions, 40 deletions
diff --git a/app/Controllers/Http/WorkspaceController.js b/app/Controllers/Http/WorkspaceController.js index 496912e..3734345 100644 --- a/app/Controllers/Http/WorkspaceController.js +++ b/app/Controllers/Http/WorkspaceController.js | |||
@@ -1,18 +1,11 @@ | |||
1 | |||
2 | const Workspace = use('App/Models/Workspace'); | 1 | const Workspace = use('App/Models/Workspace'); |
3 | const { | 2 | const { validateAll } = use('Validator'); |
4 | validateAll, | ||
5 | } = use('Validator'); | ||
6 | 3 | ||
7 | const uuid = require('uuid/v4'); | 4 | const { v4: uuid } = require('uuid'); |
8 | 5 | ||
9 | class WorkspaceController { | 6 | class WorkspaceController { |
10 | // Create a new workspace for user | 7 | // Create a new workspace for user |
11 | async create({ | 8 | async create({ request, response, auth }) { |
12 | request, | ||
13 | response, | ||
14 | auth, | ||
15 | }) { | ||
16 | try { | 9 | try { |
17 | await auth.getUser(); | 10 | await auth.getUser(); |
18 | } catch (error) { | 11 | } catch (error) { |
@@ -37,7 +30,10 @@ class WorkspaceController { | |||
37 | let workspaceId; | 30 | let workspaceId; |
38 | do { | 31 | do { |
39 | workspaceId = uuid(); | 32 | workspaceId = uuid(); |
40 | } while ((await Workspace.query().where('workspaceId', workspaceId).fetch()).rows.length > 0); // eslint-disable-line no-await-in-loop | 33 | } while ( |
34 | (await Workspace.query().where('workspaceId', workspaceId).fetch()).rows | ||
35 | .length > 0 | ||
36 | ); // eslint-disable-line no-await-in-loop | ||
41 | 37 | ||
42 | const order = (await auth.user.workspaces().fetch()).rows.length; | 38 | const order = (await auth.user.workspaces().fetch()).rows.length; |
43 | 39 | ||
@@ -59,12 +55,7 @@ class WorkspaceController { | |||
59 | }); | 55 | }); |
60 | } | 56 | } |
61 | 57 | ||
62 | async edit({ | 58 | async edit({ request, response, auth, params }) { |
63 | request, | ||
64 | response, | ||
65 | auth, | ||
66 | params, | ||
67 | }) { | ||
68 | try { | 59 | try { |
69 | await auth.getUser(); | 60 | await auth.getUser(); |
70 | } catch (error) { | 61 | } catch (error) { |
@@ -85,22 +76,24 @@ class WorkspaceController { | |||
85 | } | 76 | } |
86 | 77 | ||
87 | const data = request.all(); | 78 | const data = request.all(); |
88 | const { | 79 | const { id } = params; |
89 | id, | ||
90 | } = params; | ||
91 | 80 | ||
92 | // Update data in database | 81 | // Update data in database |
93 | await (Workspace.query() | 82 | await Workspace.query() |
94 | .where('workspaceId', id) | 83 | .where('workspaceId', id) |
95 | .where('userId', auth.user.id)).update({ | 84 | .where('userId', auth.user.id) |
96 | name: data.name, | 85 | .update({ |
97 | services: JSON.stringify(data.services), | 86 | name: data.name, |
98 | }); | 87 | services: JSON.stringify(data.services), |
88 | }); | ||
99 | 89 | ||
100 | // Get updated row | 90 | // Get updated row |
101 | const workspace = (await Workspace.query() | 91 | const workspace = ( |
102 | .where('workspaceId', id) | 92 | await Workspace.query() |
103 | .where('userId', auth.user.id).fetch()).rows[0]; | 93 | .where('workspaceId', id) |
94 | .where('userId', auth.user.id) | ||
95 | .fetch() | ||
96 | ).rows[0]; | ||
104 | 97 | ||
105 | return response.send({ | 98 | return response.send({ |
106 | id: workspace.workspaceId, | 99 | id: workspace.workspaceId, |
@@ -136,14 +129,13 @@ class WorkspaceController { | |||
136 | }); | 129 | }); |
137 | } | 130 | } |
138 | 131 | ||
139 | const { | 132 | const { id } = params; |
140 | id, | ||
141 | } = params; | ||
142 | 133 | ||
143 | // Update data in database | 134 | // Update data in database |
144 | await (Workspace.query() | 135 | await Workspace.query() |
145 | .where('workspaceId', id) | 136 | .where('workspaceId', id) |
146 | .where('userId', auth.user.id)).delete(); | 137 | .where('userId', auth.user.id) |
138 | .delete(); | ||
147 | 139 | ||
148 | return response.send({ | 140 | return response.send({ |
149 | message: 'Successfully deleted workspace', | 141 | message: 'Successfully deleted workspace', |
@@ -151,10 +143,7 @@ class WorkspaceController { | |||
151 | } | 143 | } |
152 | 144 | ||
153 | // List all workspaces a user has created | 145 | // List all workspaces a user has created |
154 | async list({ | 146 | async list({ response, auth }) { |
155 | response, | ||
156 | auth, | ||
157 | }) { | ||
158 | try { | 147 | try { |
159 | await auth.getUser(); | 148 | await auth.getUser(); |
160 | } catch (error) { | 149 | } catch (error) { |
@@ -165,16 +154,18 @@ class WorkspaceController { | |||
165 | // Convert to array with all data Franz wants | 154 | // Convert to array with all data Franz wants |
166 | let workspacesArray = []; | 155 | let workspacesArray = []; |
167 | if (workspaces) { | 156 | if (workspaces) { |
168 | workspacesArray = workspaces.map((workspace) => ({ | 157 | workspacesArray = workspaces.map(workspace => ({ |
169 | id: workspace.workspaceId, | 158 | id: workspace.workspaceId, |
170 | name: workspace.name, | 159 | name: workspace.name, |
171 | order: workspace.order, | 160 | order: workspace.order, |
172 | services: typeof workspace.services === 'string' ? JSON.parse(workspace.services) : workspace.services, | 161 | services: |
162 | typeof workspace.services === 'string' | ||
163 | ? JSON.parse(workspace.services) | ||
164 | : workspace.services, | ||
173 | userId: auth.user.id, | 165 | userId: auth.user.id, |
174 | })); | 166 | })); |
175 | } | 167 | } |
176 | 168 | ||
177 | |||
178 | return response.send(workspacesArray); | 169 | return response.send(workspacesArray); |
179 | } | 170 | } |
180 | } | 171 | } |