diff options
Diffstat (limited to 'app/Controllers/Http/WorkspaceController.js')
-rw-r--r-- | app/Controllers/Http/WorkspaceController.js | 104 |
1 files changed, 51 insertions, 53 deletions
diff --git a/app/Controllers/Http/WorkspaceController.js b/app/Controllers/Http/WorkspaceController.js index b64d858..ecf79af 100644 --- a/app/Controllers/Http/WorkspaceController.js +++ b/app/Controllers/Http/WorkspaceController.js | |||
@@ -1,8 +1,7 @@ | |||
1 | 'use strict' | ||
2 | 1 | ||
3 | const Workspace = use('App/Models/Workspace'); | 2 | const Workspace = use('App/Models/Workspace'); |
4 | const { | 3 | const { |
5 | validateAll | 4 | validateAll, |
6 | } = use('Validator'); | 5 | } = use('Validator'); |
7 | 6 | ||
8 | const uuid = require('uuid/v4'); | 7 | const uuid = require('uuid/v4'); |
@@ -12,12 +11,12 @@ class WorkspaceController { | |||
12 | async create({ | 11 | async create({ |
13 | request, | 12 | request, |
14 | response, | 13 | response, |
15 | auth | 14 | auth, |
16 | }) { | 15 | }) { |
17 | try { | 16 | try { |
18 | await auth.getUser() | 17 | await auth.getUser(); |
19 | } catch (error) { | 18 | } catch (error) { |
20 | return response.send('Missing or invalid api token') | 19 | return response.send('Missing or invalid api token'); |
21 | } | 20 | } |
22 | 21 | ||
23 | // Validate user input | 22 | // Validate user input |
@@ -26,10 +25,10 @@ class WorkspaceController { | |||
26 | }); | 25 | }); |
27 | if (validation.fails()) { | 26 | if (validation.fails()) { |
28 | return response.status(401).send({ | 27 | return response.status(401).send({ |
29 | "message": "Invalid POST arguments", | 28 | message: 'Invalid POST arguments', |
30 | "messages": validation.messages(), | 29 | messages: validation.messages(), |
31 | "status": 401 | 30 | status: 401, |
32 | }) | 31 | }); |
33 | } | 32 | } |
34 | 33 | ||
35 | const data = request.all(); | 34 | const data = request.all(); |
@@ -38,7 +37,7 @@ class WorkspaceController { | |||
38 | let workspaceId; | 37 | let workspaceId; |
39 | do { | 38 | do { |
40 | workspaceId = uuid(); | 39 | workspaceId = uuid(); |
41 | } while ((await Workspace.query().where('workspaceId', workspaceId).fetch()).rows.length > 0) | 40 | } while ((await Workspace.query().where('workspaceId', workspaceId).fetch()).rows.length > 0); // eslint-disable-line no-await-in-loop |
42 | 41 | ||
43 | const order = (await auth.user.workspaces().fetch()).rows.length; | 42 | const order = (await auth.user.workspaces().fetch()).rows.length; |
44 | 43 | ||
@@ -48,7 +47,7 @@ class WorkspaceController { | |||
48 | name: data.name, | 47 | name: data.name, |
49 | order, | 48 | order, |
50 | services: JSON.stringify([]), | 49 | services: JSON.stringify([]), |
51 | data: JSON.stringify(data) | 50 | data: JSON.stringify(data), |
52 | }); | 51 | }); |
53 | 52 | ||
54 | return response.send({ | 53 | return response.send({ |
@@ -57,37 +56,37 @@ class WorkspaceController { | |||
57 | id: workspaceId, | 56 | id: workspaceId, |
58 | order, | 57 | order, |
59 | workspaces: [], | 58 | workspaces: [], |
60 | }) | 59 | }); |
61 | } | 60 | } |
62 | 61 | ||
63 | async edit({ | 62 | async edit({ |
64 | request, | 63 | request, |
65 | response, | 64 | response, |
66 | auth, | 65 | auth, |
67 | params | 66 | params, |
68 | }) { | 67 | }) { |
69 | try { | 68 | try { |
70 | await auth.getUser() | 69 | await auth.getUser(); |
71 | } catch (error) { | 70 | } catch (error) { |
72 | return response.send('Missing or invalid api token') | 71 | return response.send('Missing or invalid api token'); |
73 | } | 72 | } |
74 | 73 | ||
75 | // Validate user input | 74 | // Validate user input |
76 | const validation = await validateAll(request.all(), { | 75 | const validation = await validateAll(request.all(), { |
77 | name: 'required|alpha', | 76 | name: 'required|alpha', |
78 | services: 'required|array' | 77 | services: 'required|array', |
79 | }); | 78 | }); |
80 | if (validation.fails()) { | 79 | if (validation.fails()) { |
81 | return response.status(401).send({ | 80 | return response.status(401).send({ |
82 | "message": "Invalid POST arguments", | 81 | message: 'Invalid POST arguments', |
83 | "messages": validation.messages(), | 82 | messages: validation.messages(), |
84 | "status": 401 | 83 | status: 401, |
85 | }) | 84 | }); |
86 | } | 85 | } |
87 | 86 | ||
88 | const data = request.all(); | 87 | const data = request.all(); |
89 | const { | 88 | const { |
90 | id | 89 | id, |
91 | } = params; | 90 | } = params; |
92 | 91 | ||
93 | // Update data in database | 92 | // Update data in database |
@@ -95,7 +94,7 @@ class WorkspaceController { | |||
95 | .where('workspaceId', id) | 94 | .where('workspaceId', id) |
96 | .where('userId', auth.user.id)).update({ | 95 | .where('userId', auth.user.id)).update({ |
97 | name: data.name, | 96 | name: data.name, |
98 | services: JSON.stringify(data.services) | 97 | services: JSON.stringify(data.services), |
99 | }); | 98 | }); |
100 | 99 | ||
101 | // Get updated row | 100 | // Get updated row |
@@ -104,24 +103,24 @@ class WorkspaceController { | |||
104 | .where('userId', auth.user.id).fetch()).rows[0]; | 103 | .where('userId', auth.user.id).fetch()).rows[0]; |
105 | 104 | ||
106 | return response.send({ | 105 | return response.send({ |
107 | "id": workspace.workspaceId, | 106 | id: workspace.workspaceId, |
108 | "name": data.name, | 107 | name: data.name, |
109 | "order": workspace.order, | 108 | order: workspace.order, |
110 | "services": data.services, | 109 | services: data.services, |
111 | "userId": auth.user.id | 110 | userId: auth.user.id, |
112 | }) | 111 | }); |
113 | } | 112 | } |
114 | 113 | ||
115 | async delete({ | 114 | async delete({ |
116 | request, | 115 | request, |
117 | response, | 116 | response, |
118 | auth, | 117 | auth, |
119 | params | 118 | params, |
120 | }) { | 119 | }) { |
121 | try { | 120 | try { |
122 | await auth.getUser() | 121 | await auth.getUser(); |
123 | } catch (error) { | 122 | } catch (error) { |
124 | return response.send('Missing or invalid api token') | 123 | return response.send('Missing or invalid api token'); |
125 | } | 124 | } |
126 | 125 | ||
127 | // Validate user input | 126 | // Validate user input |
@@ -130,14 +129,14 @@ class WorkspaceController { | |||
130 | }); | 129 | }); |
131 | if (validation.fails()) { | 130 | if (validation.fails()) { |
132 | return response.status(401).send({ | 131 | return response.status(401).send({ |
133 | "message": "Invalid POST arguments", | 132 | message: 'Invalid POST arguments', |
134 | "messages": validation.messages(), | 133 | messages: validation.messages(), |
135 | "status": 401 | 134 | status: 401, |
136 | }) | 135 | }); |
137 | } | 136 | } |
138 | 137 | ||
139 | const { | 138 | const { |
140 | id | 139 | id, |
141 | } = params; | 140 | } = params; |
142 | 141 | ||
143 | // Update data in database | 142 | // Update data in database |
@@ -146,38 +145,37 @@ class WorkspaceController { | |||
146 | .where('userId', auth.user.id)).delete(); | 145 | .where('userId', auth.user.id)).delete(); |
147 | 146 | ||
148 | return response.send({ | 147 | return response.send({ |
149 | "message": "Successfully deleted workspace", | 148 | message: 'Successfully deleted workspace', |
150 | }) | 149 | }); |
151 | } | 150 | } |
152 | 151 | ||
153 | // List all workspaces a user has created | 152 | // List all workspaces a user has created |
154 | async list({ | 153 | async list({ |
155 | request, | ||
156 | response, | 154 | response, |
157 | auth | 155 | auth, |
158 | }) { | 156 | }) { |
159 | try { | 157 | try { |
160 | await auth.getUser() | 158 | await auth.getUser(); |
161 | } catch (error) { | 159 | } catch (error) { |
162 | return response.send('Missing or invalid api token') | 160 | return response.send('Missing or invalid api token'); |
163 | } | 161 | } |
164 | 162 | ||
165 | const workspaces = (await auth.user.workspaces().fetch()).rows; | 163 | const workspaces = (await auth.user.workspaces().fetch()).rows; |
166 | // Convert to array with all data Franz wants | 164 | // Convert to array with all data Franz wants |
167 | let workspacesArray = []; | 165 | let workspacesArray = []; |
168 | if(workspaces) { | 166 | if (workspaces) { |
169 | workspacesArray = workspaces.map(workspace => ({ | 167 | workspacesArray = workspaces.map((workspace) => ({ |
170 | "id": workspace.workspaceId, | 168 | id: workspace.workspaceId, |
171 | "name": workspace.name, | 169 | name: workspace.name, |
172 | "order": workspace.order, | 170 | order: workspace.order, |
173 | "services": JSON.parse(workspace.services), | 171 | services: JSON.parse(workspace.services), |
174 | "userId": auth.user.id | 172 | userId: auth.user.id, |
175 | })) | 173 | })); |
176 | } | 174 | } |
177 | |||
178 | 175 | ||
179 | return response.send(workspacesArray) | 176 | |
177 | return response.send(workspacesArray); | ||
180 | } | 178 | } |
181 | } | 179 | } |
182 | 180 | ||
183 | module.exports = WorkspaceController | 181 | module.exports = WorkspaceController; |