diff options
author | 2024-02-10 18:37:40 -0700 | |
---|---|---|
committer | 2024-02-13 06:59:44 +0530 | |
commit | e1c47572a6235fd8fd20af888ac3a11c7ae1369d (patch) | |
tree | 2dccff36a441916d7014037cef3f7ce84a790cad /app/Controllers/Http/WorkspaceController.ts | |
parent | refactor: project maintenance (diff) | |
download | ferdium-server-e1c47572a6235fd8fd20af888ac3a11c7ae1369d.tar.gz ferdium-server-e1c47572a6235fd8fd20af888ac3a11c7ae1369d.tar.zst ferdium-server-e1c47572a6235fd8fd20af888ac3a11c7ae1369d.zip |
updates
Diffstat (limited to 'app/Controllers/Http/WorkspaceController.ts')
-rw-r--r-- | app/Controllers/Http/WorkspaceController.ts | 85 |
1 files changed, 44 insertions, 41 deletions
diff --git a/app/Controllers/Http/WorkspaceController.ts b/app/Controllers/Http/WorkspaceController.ts index a2bc54e..6cecf69 100644 --- a/app/Controllers/Http/WorkspaceController.ts +++ b/app/Controllers/Http/WorkspaceController.ts | |||
@@ -1,53 +1,53 @@ | |||
1 | import type { HttpContext } from '@adonisjs/core/http' | 1 | import type { HttpContext } from '@adonisjs/core/http'; |
2 | import { validator, schema } from '@adonisjs/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 }: HttpContext) { | 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 }: HttpContext) { | 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,54 +110,57 @@ 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({ request, response, auth, params }: HttpContext) { | 116 | public async delete({ request, response, auth, params }: HttpContext) { |
117 | // @ts-expect-error Property 'user' does not exist on type 'HttpContextContract'. | 117 | // @ts-expect-error Property 'user' does not exist on type 'HttpContextContract'. |
118 | const user = auth.user ?? request.user | 118 | const user = auth.user ?? request.user; |
119 | 119 | ||
120 | if (!user) { | 120 | if (!user) { |
121 | return response.unauthorized('Missing or invalid api token') | 121 | return response.unauthorized('Missing or invalid api token'); |
122 | } | 122 | } |
123 | 123 | ||
124 | // Validate user input | 124 | // Validate user input |
125 | let data | 125 | let data; |
126 | try { | 126 | try { |
127 | data = await validator.validate({ | 127 | data = await validator.validate({ |
128 | data: params, | 128 | data: params, |
129 | schema: deleteSchema, | 129 | schema: deleteSchema, |
130 | }) | 130 | }); |
131 | } catch (error) { | 131 | } catch (error) { |
132 | return response.status(401).send({ | 132 | return response.status(401).send({ |
133 | message: 'Invalid arguments', | 133 | message: 'Invalid arguments', |
134 | messages: error.messages, | 134 | messages: error.messages, |
135 | status: 401, | 135 | status: 401, |
136 | }) | 136 | }); |
137 | } | 137 | } |
138 | 138 | ||
139 | const { id } = data | 139 | const { id } = data; |
140 | 140 | ||
141 | // Update data in database | 141 | // Update data in database |
142 | await Workspace.query().where('workspaceId', id).where('userId', user.id).delete() | 142 | await Workspace.query() |
143 | .where('workspaceId', id) | ||
144 | .where('userId', user.id) | ||
145 | .delete(); | ||
143 | 146 | ||
144 | return response.send({ | 147 | return response.send({ |
145 | message: 'Successfully deleted workspace', | 148 | message: 'Successfully deleted workspace', |
146 | }) | 149 | }); |
147 | } | 150 | } |
148 | 151 | ||
149 | // List all workspaces a user has created | 152 | // List all workspaces a user has created |
150 | public async list({ request, response, auth }: HttpContext) { | 153 | public async list({ request, response, auth }: HttpContext) { |
151 | // @ts-expect-error Property 'user' does not exist on type 'HttpContextContract'. | 154 | // @ts-expect-error Property 'user' does not exist on type 'HttpContextContract'. |
152 | const user = auth.user ?? request.user | 155 | const user = auth.user ?? request.user; |
153 | 156 | ||
154 | if (!user) { | 157 | if (!user) { |
155 | return response.unauthorized('Missing or invalid api token') | 158 | return response.unauthorized('Missing or invalid api token'); |
156 | } | 159 | } |
157 | 160 | ||
158 | const workspaces = await user.related('workspaces').query() | 161 | const workspaces = await user.related('workspaces').query(); |
159 | // Convert to array with all data Franz wants | 162 | // Convert to array with all data Franz wants |
160 | let workspacesArray: object[] = [] | 163 | let workspacesArray: object[] = []; |
161 | if (workspaces) { | 164 | if (workspaces) { |
162 | // eslint-disable-next-line @typescript-eslint/no-explicit-any | 165 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
163 | workspacesArray = workspaces.map((workspace: any) => ({ | 166 | workspacesArray = workspaces.map((workspace: any) => ({ |
@@ -169,9 +172,9 @@ export default class WorkspaceController { | |||
169 | ? JSON.parse(workspace.services) | 172 | ? JSON.parse(workspace.services) |
170 | : workspace.services, | 173 | : workspace.services, |
171 | userId: user.id, | 174 | userId: user.id, |
172 | })) | 175 | })); |
173 | } | 176 | } |
174 | 177 | ||
175 | return response.send(workspacesArray) | 178 | return response.send(workspacesArray); |
176 | } | 179 | } |
177 | } | 180 | } |