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