aboutsummaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authorLibravatar vantezzen <properly@protonmail.com>2019-08-23 16:50:37 +0200
committerLibravatar vantezzen <properly@protonmail.com>2019-08-23 16:50:37 +0200
commite00a1987866a804f2ae6e3206bd583df703663e8 (patch)
treea7d4ce509bc220105d5302ff1172d8556fabf47e /app
parentAdd workspaces to feature list (diff)
downloadferdium-server-e00a1987866a804f2ae6e3206bd583df703663e8.tar.gz
ferdium-server-e00a1987866a804f2ae6e3206bd583df703663e8.tar.zst
ferdium-server-e00a1987866a804f2ae6e3206bd583df703663e8.zip
Add recipe store
Diffstat (limited to 'app')
-rw-r--r--app/Controllers/Http/RecipeController.js110
-rw-r--r--app/Controllers/Http/ServiceController.js7
-rw-r--r--app/Controllers/Http/WorkspaceController.js27
3 files changed, 120 insertions, 24 deletions
diff --git a/app/Controllers/Http/RecipeController.js b/app/Controllers/Http/RecipeController.js
index 0b9d488..641f4ef 100644
--- a/app/Controllers/Http/RecipeController.js
+++ b/app/Controllers/Http/RecipeController.js
@@ -1,27 +1,103 @@
1'use strict' 1'use strict'
2 2
3const Recipe = use('App/Models/Recipe'); 3const Recipe = use('App/Models/Recipe');
4const Helpers = use('Helpers')
5const Drive = use('Drive')
4const fetch = require('node-fetch'); 6const fetch = require('node-fetch');
7const path = require('path');
5 8
6class RecipeController { 9class RecipeController {
7 async list({ 10 async list({
8 response 11 response
9 }) { 12 }) {
10 const officialRecipes = JSON.parse(await (await fetch('https://api.franzinfra.com/v1/recipes')).text()); 13 const officialRecipes = JSON.parse(await (await fetch('https://api.franzinfra.com/v1/recipes')).text());
11 const customRecipesArray = (await Recipe.all()).rows; 14 const customRecipesArray = (await Recipe.all()).rows;
12 const customRecipes = customRecipesArray.map(recipe => ({ 15 const customRecipes = customRecipesArray.map(recipe => ({
13 "id": recipe.recipeId, 16 "id": recipe.recipeId,
14 "name": recipe.name, 17 "name": recipe.name,
15 ...JSON.parse(recipe.data) 18 ...JSON.parse(recipe.data)
16 })) 19 }))
17 20
18 const recipes = [ 21 const recipes = [
19 ...officialRecipes, 22 ...officialRecipes,
20 ...customRecipes, 23 ...customRecipes,
21 ] 24 ]
22 25
23 return response.send(recipes) 26 return response.send(recipes)
27 }
28
29 async create({
30 request,
31 response
32 }) {
33 const data = request.all();
34
35 const pkg = request.file('package')
36
37 await pkg.move(path.join(Helpers.appRoot(), '/recipes/'), {
38 name: data.id + '.tar.gz',
39 overwrite: false
40 })
41
42 await Recipe.create({
43 name: data.name,
44 recipeId: data.id,
45 data: JSON.stringify({
46 "author": data.author,
47 "featured": false,
48 "version": "1.0.0",
49 "icons": {
50 "png": data.png,
51 "svg": data.svg
52 }
53 })
54 })
55
56 return response.send('Created new recipe')
57 }
58
59 async search({
60 request,
61 response
62 }) {
63 const needle = request.input('needle')
64
65 const remoteResults = JSON.parse(await (await fetch('https://api.franzinfra.com/v1/recipes/search?needle=' + needle)).text());
66 const localResultsArray = (await Recipe.query().where('name', 'LIKE', '%' + needle + '%').fetch()).toJSON();
67 const localResults = localResultsArray.map(recipe => ({
68 "id": recipe.recipeId,
69 "name": recipe.name,
70 ...JSON.parse(recipe.data)
71 }))
72
73 const results = [
74 ...localResults,
75 ...remoteResults,
76 ]
77
78 return response.send(results);
79 }
80
81 // Download a recipe
82 async download({
83 request,
84 response,
85 params
86 }) {
87 const service = params.recipe;
88
89 // Chack for invalid characters
90 if (/\.{1,}/.test(service) || /\/{1,}/.test(service)) {
91 return response.send('Invalid recipe name');
92 }
93
94 // Check if recipe exists in recipes folder
95 if (await Drive.exists(service + '.tar.gz')) {
96 response.send(await Drive.get(service + '.tar.gz'))
97 } else {
98 response.redirect('https://api.franzinfra.com/v1/recipes/download/' + service)
24 } 99 }
100 }
25} 101}
26 102
27module.exports = RecipeController 103module.exports = RecipeController
diff --git a/app/Controllers/Http/ServiceController.js b/app/Controllers/Http/ServiceController.js
index 4c908ac..d1adea3 100644
--- a/app/Controllers/Http/ServiceController.js
+++ b/app/Controllers/Http/ServiceController.js
@@ -90,13 +90,6 @@ class ServiceController {
90 90
91 return response.send(servicesArray) 91 return response.send(servicesArray)
92 } 92 }
93
94 // Download a recipe (currently simply redirects to Franz's API)
95 download({ request, response, params }) {
96 const service = params.recipe;
97
98 response.redirect('https://api.franzinfra.com/v1/recipes/download/' + service)
99 }
100} 93}
101 94
102module.exports = ServiceController 95module.exports = ServiceController
diff --git a/app/Controllers/Http/WorkspaceController.js b/app/Controllers/Http/WorkspaceController.js
index 55a0c75..5573382 100644
--- a/app/Controllers/Http/WorkspaceController.js
+++ b/app/Controllers/Http/WorkspaceController.js
@@ -83,6 +83,33 @@ class WorkspaceController {
83 }) 83 })
84 } 84 }
85 85
86 async delete({
87 request,
88 response,
89 auth,
90 params
91 }) {
92 try {
93 await auth.getUser()
94 } catch (error) {
95 return response.send('Missing or invalid api token')
96 }
97
98 const data = request.all();
99 const {
100 id
101 } = params;
102
103 // Update data in database
104 await (Workspace.query()
105 .where('workspaceId', id)
106 .where('userId', auth.user.id)).delete();
107
108 return response.send({
109 "message": "Successfully deleted workspace",
110 })
111 }
112
86 // List all workspaces a user has created 113 // List all workspaces a user has created
87 async list({ 114 async list({
88 request, 115 request,