diff options
author | vantezzen <properly@protonmail.com> | 2019-08-23 14:04:22 +0200 |
---|---|---|
committer | vantezzen <properly@protonmail.com> | 2019-08-23 14:04:22 +0200 |
commit | 5970b8e5bbf993c88c1f901708a7c5075a916770 (patch) | |
tree | 11636435cba3414a930b4a81f9bf7ca8d4de31e1 /app/Controllers/Http | |
parent | Fix user login (diff) | |
download | ferdium-server-5970b8e5bbf993c88c1f901708a7c5075a916770.tar.gz ferdium-server-5970b8e5bbf993c88c1f901708a7c5075a916770.tar.zst ferdium-server-5970b8e5bbf993c88c1f901708a7c5075a916770.zip |
Add support for workspaces
Diffstat (limited to 'app/Controllers/Http')
-rw-r--r-- | app/Controllers/Http/RecipeController.js | 27 | ||||
-rw-r--r-- | app/Controllers/Http/ServiceController.js | 2 | ||||
-rw-r--r-- | app/Controllers/Http/WorkspaceController.js | 116 |
3 files changed, 144 insertions, 1 deletions
diff --git a/app/Controllers/Http/RecipeController.js b/app/Controllers/Http/RecipeController.js new file mode 100644 index 0000000..0b9d488 --- /dev/null +++ b/app/Controllers/Http/RecipeController.js | |||
@@ -0,0 +1,27 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const Recipe = use('App/Models/Recipe'); | ||
4 | const fetch = require('node-fetch'); | ||
5 | |||
6 | class RecipeController { | ||
7 | async list({ | ||
8 | response | ||
9 | }) { | ||
10 | const officialRecipes = JSON.parse(await (await fetch('https://api.franzinfra.com/v1/recipes')).text()); | ||
11 | const customRecipesArray = (await Recipe.all()).rows; | ||
12 | const customRecipes = customRecipesArray.map(recipe => ({ | ||
13 | "id": recipe.recipeId, | ||
14 | "name": recipe.name, | ||
15 | ...JSON.parse(recipe.data) | ||
16 | })) | ||
17 | |||
18 | const recipes = [ | ||
19 | ...officialRecipes, | ||
20 | ...customRecipes, | ||
21 | ] | ||
22 | |||
23 | return response.send(recipes) | ||
24 | } | ||
25 | } | ||
26 | |||
27 | module.exports = RecipeController | ||
diff --git a/app/Controllers/Http/ServiceController.js b/app/Controllers/Http/ServiceController.js index 0fcbec0..4c908ac 100644 --- a/app/Controllers/Http/ServiceController.js +++ b/app/Controllers/Http/ServiceController.js | |||
@@ -23,7 +23,7 @@ class ServiceController { | |||
23 | let serviceId; | 23 | let serviceId; |
24 | do { | 24 | do { |
25 | serviceId = uuid(); | 25 | serviceId = uuid(); |
26 | } while((await Service.all()).rows.length > 0) | 26 | } while((await Service.query().where('serviceId', serviceId).fetch()).rows.length > 0) |
27 | 27 | ||
28 | const service = await Service.create({ | 28 | const service = await Service.create({ |
29 | userId: auth.user.id, | 29 | userId: auth.user.id, |
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 | |||
3 | const Workspace = use('App/Models/Workspace'); | ||
4 | const uuid = require('uuid/v4'); | ||
5 | |||
6 | class 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 | |||
116 | module.exports = WorkspaceController | ||