diff options
Diffstat (limited to 'app/Controllers/Http/WorkspaceController.ts')
-rw-r--r-- | app/Controllers/Http/WorkspaceController.ts | 180 |
1 files changed, 180 insertions, 0 deletions
diff --git a/app/Controllers/Http/WorkspaceController.ts b/app/Controllers/Http/WorkspaceController.ts new file mode 100644 index 0000000..4dff4df --- /dev/null +++ b/app/Controllers/Http/WorkspaceController.ts | |||
@@ -0,0 +1,180 @@ | |||
1 | import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'; | ||
2 | import { validator, schema } from '@ioc:Adonis/Core/Validator'; | ||
3 | import Workspace from 'App/Models/Workspace'; | ||
4 | import { v4 as uuid } from 'uuid'; | ||
5 | |||
6 | const createSchema = schema.create({ | ||
7 | name: schema.string(), | ||
8 | }); | ||
9 | |||
10 | const editSchema = schema.create({ | ||
11 | name: schema.string(), | ||
12 | }); | ||
13 | |||
14 | const deleteSchema = schema.create({ | ||
15 | id: schema.string(), | ||
16 | }); | ||
17 | |||
18 | export default class WorkspacesController { | ||
19 | // Create a new workspace for user | ||
20 | public async create({ request, response, auth }: HttpContextContract) { | ||
21 | // @ts-expect-error Property 'user' does not exist on type 'HttpContextContract'. | ||
22 | const user = auth.user ?? request.user; | ||
23 | |||
24 | if (!user) { | ||
25 | return response.unauthorized('Missing or invalid api token'); | ||
26 | } | ||
27 | |||
28 | // Validate user input | ||
29 | let data; | ||
30 | try { | ||
31 | data = await request.validate({ schema: createSchema }); | ||
32 | } catch (error) { | ||
33 | return response.status(401).send({ | ||
34 | message: 'Invalid POST arguments', | ||
35 | messages: error.messages, | ||
36 | status: 401, | ||
37 | }); | ||
38 | } | ||
39 | |||
40 | // Get new, unused uuid | ||
41 | let workspaceId; | ||
42 | do { | ||
43 | workspaceId = uuid(); | ||
44 | } while ( | ||
45 | // eslint-disable-next-line unicorn/no-await-expression-member, no-await-in-loop | ||
46 | (await Workspace.query().where('workspaceId', workspaceId)).length > 0 | ||
47 | ); | ||
48 | |||
49 | // eslint-disable-next-line unicorn/no-await-expression-member | ||
50 | const order = (await user.related('workspaces').query()).length; | ||
51 | |||
52 | await Workspace.create({ | ||
53 | userId: user.id, | ||
54 | workspaceId, | ||
55 | name: data.name, | ||
56 | order, | ||
57 | services: JSON.stringify([]), | ||
58 | data: JSON.stringify(data), | ||
59 | }); | ||
60 | |||
61 | return response.send({ | ||
62 | userId: user.id, | ||
63 | name: data.name, | ||
64 | id: workspaceId, | ||
65 | order, | ||
66 | workspaces: [], | ||
67 | }); | ||
68 | } | ||
69 | |||
70 | public async edit({ request, response, auth, params }: HttpContextContract) { | ||
71 | // @ts-expect-error Property 'user' does not exist on type 'HttpContextContract'. | ||
72 | const user = auth.user ?? request.user; | ||
73 | |||
74 | if (!user) { | ||
75 | return response.unauthorized('Missing or invalid api token'); | ||
76 | } | ||
77 | |||
78 | // Validate user input | ||
79 | try { | ||
80 | await request.validate({ schema: editSchema }); | ||
81 | } catch (error) { | ||
82 | return response.status(401).send({ | ||
83 | message: 'Invalid POST arguments', | ||
84 | messages: error.messages, | ||
85 | status: 401, | ||
86 | }); | ||
87 | } | ||
88 | |||
89 | const data = request.all(); | ||
90 | const { id } = params; | ||
91 | |||
92 | // Update data in database | ||
93 | await Workspace.query() | ||
94 | .where('workspaceId', id) | ||
95 | .where('userId', user.id) | ||
96 | .update({ | ||
97 | name: data.name, | ||
98 | services: JSON.stringify(data.services), | ||
99 | }); | ||
100 | |||
101 | // Get updated row | ||
102 | const workspace = await Workspace.query() | ||
103 | .where('workspaceId', id) | ||
104 | .where('userId', user.id) | ||
105 | .firstOrFail(); | ||
106 | |||
107 | return response.send({ | ||
108 | id: workspace.workspaceId, | ||
109 | name: data.name, | ||
110 | order: workspace.order, | ||
111 | services: data.services, | ||
112 | userId: user.id, | ||
113 | }); | ||
114 | } | ||
115 | |||
116 | public async delete({ response, auth, params }: HttpContextContract) { | ||
117 | // @ts-expect-error Property 'user' does not exist on type 'HttpContextContract'. | ||
118 | const user = auth.user ?? request.user; | ||
119 | |||
120 | if (!user) { | ||
121 | return response.unauthorized('Missing or invalid api token'); | ||
122 | } | ||
123 | |||
124 | // Validate user input | ||
125 | let data; | ||
126 | try { | ||
127 | data = await validator.validate({ | ||
128 | data: params, | ||
129 | schema: deleteSchema, | ||
130 | }); | ||
131 | } catch (error) { | ||
132 | return response.status(401).send({ | ||
133 | message: 'Invalid arguments', | ||
134 | messages: error.messages, | ||
135 | status: 401, | ||
136 | }); | ||
137 | } | ||
138 | |||
139 | const { id } = data; | ||
140 | |||
141 | // Update data in database | ||
142 | await Workspace.query() | ||
143 | .where('workspaceId', id) | ||
144 | .where('userId', user.id) | ||
145 | .delete(); | ||
146 | |||
147 | return response.send({ | ||
148 | message: 'Successfully deleted workspace', | ||
149 | }); | ||
150 | } | ||
151 | |||
152 | // List all workspaces a user has created | ||
153 | public async list({ request, response, auth }: HttpContextContract) { | ||
154 | // @ts-expect-error Property 'user' does not exist on type 'HttpContextContract'. | ||
155 | const user = auth.user ?? request.user; | ||
156 | |||
157 | if (!user) { | ||
158 | return response.unauthorized('Missing or invalid api token'); | ||
159 | } | ||
160 | |||
161 | const workspaces = await user.related('workspaces').query(); | ||
162 | // Convert to array with all data Franz wants | ||
163 | let workspacesArray: object[] = []; | ||
164 | if (workspaces) { | ||
165 | // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
166 | workspacesArray = workspaces.map((workspace: any) => ({ | ||
167 | id: workspace.workspaceId, | ||
168 | name: workspace.name, | ||
169 | order: workspace.order, | ||
170 | services: | ||
171 | typeof workspace.services === 'string' | ||
172 | ? JSON.parse(workspace.services) | ||
173 | : workspace.services, | ||
174 | userId: user.id, | ||
175 | })); | ||
176 | } | ||
177 | |||
178 | return response.send(workspacesArray); | ||
179 | } | ||
180 | } | ||