aboutsummaryrefslogtreecommitdiffstats
path: root/app
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
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')
-rw-r--r--app/Controllers/Http/RecipeController.js27
-rw-r--r--app/Controllers/Http/ServiceController.js2
-rw-r--r--app/Controllers/Http/WorkspaceController.js116
-rw-r--r--app/Models/Recipe.js9
-rw-r--r--app/Models/User.js4
-rw-r--r--app/Models/Workspace.js12
6 files changed, 169 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
3const Recipe = use('App/Models/Recipe');
4const fetch = require('node-fetch');
5
6class 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
27module.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
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
diff --git a/app/Models/Recipe.js b/app/Models/Recipe.js
new file mode 100644
index 0000000..9e3619c
--- /dev/null
+++ b/app/Models/Recipe.js
@@ -0,0 +1,9 @@
1'use strict'
2
3/** @type {typeof import('@adonisjs/lucid/src/Lucid/Model')} */
4const Model = use('Model')
5
6class Recipe extends Model {
7}
8
9module.exports = Recipe
diff --git a/app/Models/User.js b/app/Models/User.js
index 0bb1547..c9a680a 100644
--- a/app/Models/User.js
+++ b/app/Models/User.js
@@ -38,6 +38,10 @@ class User extends Model {
38 services () { 38 services () {
39 return this.hasMany('App/Models/Service', 'id', 'userId') 39 return this.hasMany('App/Models/Service', 'id', 'userId')
40 } 40 }
41
42 workspaces () {
43 return this.hasMany('App/Models/Workspace', 'id', 'userId')
44 }
41} 45}
42 46
43module.exports = User 47module.exports = User
diff --git a/app/Models/Workspace.js b/app/Models/Workspace.js
new file mode 100644
index 0000000..f78a3f9
--- /dev/null
+++ b/app/Models/Workspace.js
@@ -0,0 +1,12 @@
1'use strict'
2
3/** @type {typeof import('@adonisjs/lucid/src/Lucid/Model')} */
4const Model = use('Model')
5
6class Workspace extends Model {
7 user() {
8 return this.belongsTo('App/Models/User', 'userId', 'id')
9 }
10}
11
12module.exports = Workspace