diff options
author | 2021-08-13 00:45:01 +0530 | |
---|---|---|
committer | 2021-08-13 00:45:01 +0530 | |
commit | 2d27d5e66649d4f5baf127a53ee5ae524eae3a59 (patch) | |
tree | c592ea219ac8cd987fc367f57b54034c450ab2ab /src/internal-server/app/Controllers/Http/WorkspaceController.js | |
parent | Ferdi v5.6.0 (diff) | |
parent | 5.6.1-nightly.24 [skip ci] (diff) | |
download | ferdium-app-2d27d5e66649d4f5baf127a53ee5ae524eae3a59.tar.gz ferdium-app-2d27d5e66649d4f5baf127a53ee5ae524eae3a59.tar.zst ferdium-app-2d27d5e66649d4f5baf127a53ee5ae524eae3a59.zip |
chore: merge from nightly branch into release branch in prep for next beta
Diffstat (limited to 'src/internal-server/app/Controllers/Http/WorkspaceController.js')
-rw-r--r-- | src/internal-server/app/Controllers/Http/WorkspaceController.js | 148 |
1 files changed, 148 insertions, 0 deletions
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 @@ | |||
1 | const Workspace = use('App/Models/Workspace'); | ||
2 | const { | ||
3 | validateAll, | ||
4 | } = use('Validator'); | ||
5 | |||
6 | const uuid = require('uuid/v4'); | ||
7 | |||
8 | class WorkspaceController { | ||
9 | // Create a new workspace for user | ||
10 | async create({ | ||
11 | request, | ||
12 | response, | ||
13 | }) { | ||
14 | // Validate user input | ||
15 | const validation = await validateAll(request.all(), { | ||
16 | name: 'required', | ||
17 | }); | ||
18 | if (validation.fails()) { | ||
19 | return response.status(401).send({ | ||
20 | message: 'Invalid POST arguments', | ||
21 | messages: validation.messages(), | ||
22 | status: 401, | ||
23 | }); | ||
24 | } | ||
25 | |||
26 | const data = request.all(); | ||
27 | |||
28 | // Get new, unused uuid | ||
29 | let workspaceId; | ||
30 | do { | ||
31 | workspaceId = uuid(); | ||
32 | } while ((await Workspace.query().where('workspaceId', workspaceId).fetch()).rows.length > 0); // eslint-disable-line no-await-in-loop | ||
33 | |||
34 | const order = (await Workspace.all()).rows.length; | ||
35 | |||
36 | await Workspace.create({ | ||
37 | workspaceId, | ||
38 | name: data.name, | ||
39 | order, | ||
40 | services: JSON.stringify([]), | ||
41 | data: JSON.stringify(data), | ||
42 | }); | ||
43 | |||
44 | return response.send({ | ||
45 | userId: 1, | ||
46 | name: data.name, | ||
47 | id: workspaceId, | ||
48 | order, | ||
49 | workspaces: [], | ||
50 | }); | ||
51 | } | ||
52 | |||
53 | async edit({ | ||
54 | request, | ||
55 | response, | ||
56 | params, | ||
57 | }) { | ||
58 | // Validate user input | ||
59 | const validation = await validateAll(request.all(), { | ||
60 | name: 'required', | ||
61 | services: 'required|array', | ||
62 | }); | ||
63 | if (validation.fails()) { | ||
64 | return response.status(401).send({ | ||
65 | message: 'Invalid POST arguments', | ||
66 | messages: validation.messages(), | ||
67 | status: 401, | ||
68 | }); | ||
69 | } | ||
70 | |||
71 | const data = request.all(); | ||
72 | const { | ||
73 | id, | ||
74 | } = params; | ||
75 | |||
76 | // Update data in database | ||
77 | await (Workspace.query() | ||
78 | .where('workspaceId', id)).update({ | ||
79 | name: data.name, | ||
80 | services: JSON.stringify(data.services), | ||
81 | }); | ||
82 | |||
83 | // Get updated row | ||
84 | const workspace = (await Workspace.query() | ||
85 | .where('workspaceId', id).fetch()).rows[0]; | ||
86 | |||
87 | return response.send({ | ||
88 | id: workspace.workspaceId, | ||
89 | name: data.name, | ||
90 | order: workspace.order, | ||
91 | services: data.services, | ||
92 | userId: 1, | ||
93 | }); | ||
94 | } | ||
95 | |||
96 | async delete({ | ||
97 | // eslint-disable-next-line no-unused-vars | ||
98 | request, | ||
99 | response, | ||
100 | params, | ||
101 | }) { | ||
102 | // Validate user input | ||
103 | const validation = await validateAll(params, { | ||
104 | id: 'required', | ||
105 | }); | ||
106 | if (validation.fails()) { | ||
107 | return response.status(401).send({ | ||
108 | message: 'Invalid arguments', | ||
109 | messages: validation.messages(), | ||
110 | status: 401, | ||
111 | }); | ||
112 | } | ||
113 | |||
114 | const { | ||
115 | id, | ||
116 | } = params; | ||
117 | |||
118 | // Update data in database | ||
119 | await (Workspace.query() | ||
120 | .where('workspaceId', id)).delete(); | ||
121 | |||
122 | return response.send({ | ||
123 | message: 'Successfully deleted workspace', | ||
124 | }); | ||
125 | } | ||
126 | |||
127 | // List all workspaces a user has created | ||
128 | async list({ | ||
129 | response, | ||
130 | }) { | ||
131 | const workspaces = (await Workspace.all()).rows; | ||
132 | // Convert to array with all data Franz wants | ||
133 | let workspacesArray = []; | ||
134 | if (workspaces) { | ||
135 | workspacesArray = workspaces.map(workspace => ({ | ||
136 | id: workspace.workspaceId, | ||
137 | name: workspace.name, | ||
138 | order: workspace.order, | ||
139 | services: typeof workspace.services === 'string' ? JSON.parse(workspace.services) : workspace.services, | ||
140 | userId: 1, | ||
141 | })); | ||
142 | } | ||
143 | |||
144 | return response.send(workspacesArray); | ||
145 | } | ||
146 | } | ||
147 | |||
148 | module.exports = WorkspaceController; | ||