aboutsummaryrefslogtreecommitdiffstats
path: root/app/Controllers/Http/WorkspaceController.js
diff options
context:
space:
mode:
authorLibravatar vantezzen <properly@protonmail.com>2019-08-23 14:04:22 +0200
committerLibravatar vantezzen <properly@protonmail.com>2019-08-23 14:04:22 +0200
commit5970b8e5bbf993c88c1f901708a7c5075a916770 (patch)
tree11636435cba3414a930b4a81f9bf7ca8d4de31e1 /app/Controllers/Http/WorkspaceController.js
parentFix user login (diff)
downloadferdium-server-5970b8e5bbf993c88c1f901708a7c5075a916770.tar.gz
ferdium-server-5970b8e5bbf993c88c1f901708a7c5075a916770.tar.zst
ferdium-server-5970b8e5bbf993c88c1f901708a7c5075a916770.zip
Add support for workspaces
Diffstat (limited to 'app/Controllers/Http/WorkspaceController.js')
-rw-r--r--app/Controllers/Http/WorkspaceController.js116
1 files changed, 116 insertions, 0 deletions
diff --git a/app/Controllers/Http/WorkspaceController.js b/app/Controllers/Http/WorkspaceController.js
new file mode 100644
index 0000000..55a0c75
--- /dev/null
+++ b/app/Controllers/Http/WorkspaceController.js
@@ -0,0 +1,116 @@
1'use strict'
2
3const Workspace = use('App/Models/Workspace');
4const uuid = require('uuid/v4');
5
6class WorkspaceController {
7 // Create a new workspace for user
8 async create({
9 request,
10 response,
11 auth
12 }) {
13 try {
14 await auth.getUser()
15 } catch (error) {
16 return response.send('Missing or invalid api token')
17 }
18
19 const data = request.all();
20
21 // Get new, unused uuid
22 let workspaceId;
23 do {
24 workspaceId = uuid();
25 } while ((await Workspace.query().where('workspaceId', workspaceId).fetch()).rows.length > 0)
26
27 const order = (await auth.user.workspaces().fetch()).rows.length;
28
29 await Workspace.create({
30 userId: auth.user.id,
31 workspaceId,
32 name: data.name,
33 order,
34 services: JSON.stringify([]),
35 data: JSON.stringify(data)
36 });
37
38 return response.send({
39 userId: auth.user.id,
40 name: data.name,
41 id: workspaceId,
42 order,
43 workspaces: [],
44 })
45 }
46
47 async edit({
48 request,
49 response,
50 auth,
51 params
52 }) {
53 try {
54 await auth.getUser()
55 } catch (error) {
56 return response.send('Missing or invalid api token')
57 }
58
59 const data = request.all();
60 const {
61 id
62 } = params;
63
64 // Update data in database
65 await (Workspace.query()
66 .where('workspaceId', id)
67 .where('userId', auth.user.id)).update({
68 name: data.name,
69 services: JSON.stringify(data.services)
70 });
71
72 // Get updated row
73 const workspace = (await Workspace.query()
74 .where('workspaceId', id)
75 .where('userId', auth.user.id).fetch()).rows[0];
76
77 return response.send({
78 "id": workspace.workspaceId,
79 "name": data.name,
80 "order": workspace.order,
81 "services": data.services,
82 "userId": auth.user.id
83 })
84 }
85
86 // List all workspaces a user has created
87 async list({
88 request,
89 response,
90 auth
91 }) {
92 try {
93 await auth.getUser()
94 } catch (error) {
95 return response.send('Missing or invalid api token')
96 }
97
98 const workspaces = (await auth.user.workspaces().fetch()).rows;
99 // Convert to array with all data Franz wants
100 let workspacesArray = [];
101 if(workspaces) {
102 workspacesArray = workspaces.map(workspace => ({
103 "id": workspace.workspaceId,
104 "name": workspace.name,
105 "order": workspace.order,
106 "services": JSON.parse(workspace.services),
107 "userId": auth.user.id
108 }))
109 }
110
111
112 return response.send(workspacesArray)
113 }
114}
115
116module.exports = WorkspaceController