aboutsummaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authorLibravatar vantezzen <properly@protonmail.com>2019-08-26 21:27:07 +0200
committerLibravatar vantezzen <properly@protonmail.com>2019-08-26 21:27:07 +0200
commitb402d07038e6bfe321a389ca68c1f063e723a9d6 (patch)
treeb41b073353449ffe873169037677583f1933141b /app
parentImprove recipe creation (diff)
downloadferdium-server-b402d07038e6bfe321a389ca68c1f063e723a9d6.tar.gz
ferdium-server-b402d07038e6bfe321a389ca68c1f063e723a9d6.tar.zst
ferdium-server-b402d07038e6bfe321a389ca68c1f063e723a9d6.zip
Add more ferdi routes
Diffstat (limited to 'app')
-rw-r--r--app/Controllers/Http/ServiceController.js156
-rw-r--r--app/Controllers/Http/UserController.js1
-rw-r--r--app/Controllers/Http/WorkspaceController.js3
3 files changed, 142 insertions, 18 deletions
diff --git a/app/Controllers/Http/ServiceController.js b/app/Controllers/Http/ServiceController.js
index 0d1bae2..66cf6e0 100644
--- a/app/Controllers/Http/ServiceController.js
+++ b/app/Controllers/Http/ServiceController.js
@@ -29,6 +29,7 @@ class ServiceController {
29 if (validation.fails()) { 29 if (validation.fails()) {
30 return response.status(401).send({ 30 return response.status(401).send({
31 "message": "Invalid POST arguments", 31 "message": "Invalid POST arguments",
32 "messages": validation.messages(),
32 "status": 401 33 "status": 401
33 }) 34 })
34 } 35 }
@@ -39,7 +40,7 @@ class ServiceController {
39 let serviceId; 40 let serviceId;
40 do { 41 do {
41 serviceId = uuid(); 42 serviceId = uuid();
42 } while((await Service.query().where('serviceId', serviceId).fetch()).rows.length > 0) 43 } while ((await Service.query().where('serviceId', serviceId).fetch()).rows.length > 0)
43 44
44 const service = await Service.create({ 45 const service = await Service.create({
45 userId: auth.user.id, 46 userId: auth.user.id,
@@ -81,30 +82,149 @@ class ServiceController {
81 } catch (error) { 82 } catch (error) {
82 return response.send('Missing or invalid api token') 83 return response.send('Missing or invalid api token')
83 } 84 }
84 85
85 const services = (await auth.user.services().fetch()).rows; 86 const services = (await auth.user.services().fetch()).rows;
86 // Convert to array with all data Franz wants 87 // Convert to array with all data Franz wants
87 const servicesArray = services.map(service => ({ 88 const servicesArray = services.map(service => ({
88 "customRecipe": false, 89 "customRecipe": false,
89 "hasCustomIcon": false, 90 "hasCustomIcon": false,
90 "isBadgeEnabled": true, 91 "isBadgeEnabled": true,
91 "isDarkModeEnabled": "", 92 "isDarkModeEnabled": "",
92 "isEnabled": true, 93 "isEnabled": true,
93 "isMuted": false, 94 "isMuted": false,
94 "isNotificationEnabled": true, 95 "isNotificationEnabled": true,
95 "order": 1, 96 "order": 1,
96 "spellcheckerLanguage": "", 97 "spellcheckerLanguage": "",
97 "workspaces": [], 98 "workspaces": [],
98 "iconUrl": null, 99 "iconUrl": null,
99 ...JSON.parse(service.settings), 100 ...JSON.parse(service.settings),
100 "id": service.serviceId, 101 "id": service.serviceId,
101 "name": service.name, 102 "name": service.name,
102 "recipeId": service.recipeId, 103 "recipeId": service.recipeId,
103 "userId": auth.user.id, 104 "userId": auth.user.id,
104 })) 105 }))
105 106
106 return response.send(servicesArray) 107 return response.send(servicesArray)
107 } 108 }
109
110 async edit({
111 request,
112 response,
113 auth,
114 params
115 }) {
116 try {
117 await auth.getUser()
118 } catch (error) {
119 return response.send('Missing or invalid api token')
120 }
121
122 // Validate user input
123 const validation = await validateAll(request.all(), {
124 name: 'required',
125 });
126 if (validation.fails()) {
127 return response.status(401).send({
128 "message": "Invalid POST arguments",
129 "messages": validation.messages(),
130 "status": 401
131 })
132 }
133
134 const data = request.all();
135 const {
136 id
137 } = params;
138
139 // Get current settings from db
140 const serviceData = (await Service.query()
141 .where('serviceId', id)
142 .where('userId', auth.user.id).fetch()).rows[0];
143
144 let settings = {
145 ...JSON.parse(serviceData.settings),
146 ...data,
147 };
148
149 // Update data in database
150 await (Service.query()
151 .where('serviceId', id)
152 .where('userId', auth.user.id)).update({
153 name: data.name,
154 settings: JSON.stringify(settings)
155 });
156
157 // Get updated row
158 const service = (await Service.query()
159 .where('serviceId', id)
160 .where('userId', auth.user.id).fetch()).rows[0];
161
162 return response.send({
163 "id": service.serviceId,
164 "name": data.name,
165 ...settings,
166 "userId": auth.user.id
167 })
168 }
169
170 async reorder({
171 request,
172 response,
173 auth
174 }) {
175 const data = request.all();
176
177 for (const service in data) {
178 // Get current settings from db
179 const serviceData = (await Service.query()
180 .where('serviceId', service)
181 .where('userId', auth.user.id).fetch()).rows[0];
182
183 let settings = {
184 ...JSON.parse(serviceData.settings),
185 order: data[service]
186 };
187
188 // Update data in database
189 await (Service.query()
190 .where('serviceId', service)
191 .where('userId', auth.user.id))
192 .update({
193 settings: JSON.stringify(settings)
194 });
195 }
196
197 // Get new services
198 const services = (await auth.user.services().fetch()).rows;
199 // Convert to array with all data Franz wants
200 const servicesArray = services.map(service => ({
201 "customRecipe": false,
202 "hasCustomIcon": false,
203 "isBadgeEnabled": true,
204 "isDarkModeEnabled": "",
205 "isEnabled": true,
206 "isMuted": false,
207 "isNotificationEnabled": true,
208 "order": 1,
209 "spellcheckerLanguage": "",
210 "workspaces": [],
211 "iconUrl": null,
212 ...JSON.parse(service.settings),
213 "id": service.serviceId,
214 "name": service.name,
215 "recipeId": service.recipeId,
216 "userId": auth.user.id,
217 }))
218
219 return response.send(servicesArray)
220 }
221
222 update({
223 request,
224 response
225 }) {
226 return response.send([])
227 }
108} 228}
109 229
110module.exports = ServiceController 230module.exports = ServiceController
diff --git a/app/Controllers/Http/UserController.js b/app/Controllers/Http/UserController.js
index f81a0d5..084b023 100644
--- a/app/Controllers/Http/UserController.js
+++ b/app/Controllers/Http/UserController.js
@@ -24,6 +24,7 @@ class UserController {
24 if (validation.fails()) { 24 if (validation.fails()) {
25 return response.status(401).send({ 25 return response.status(401).send({
26 "message": "Invalid POST arguments", 26 "message": "Invalid POST arguments",
27 "messages": validation.messages(),
27 "status": 401 28 "status": 401
28 }) 29 })
29 } 30 }
diff --git a/app/Controllers/Http/WorkspaceController.js b/app/Controllers/Http/WorkspaceController.js
index 3d45893..b64d858 100644
--- a/app/Controllers/Http/WorkspaceController.js
+++ b/app/Controllers/Http/WorkspaceController.js
@@ -27,6 +27,7 @@ class WorkspaceController {
27 if (validation.fails()) { 27 if (validation.fails()) {
28 return response.status(401).send({ 28 return response.status(401).send({
29 "message": "Invalid POST arguments", 29 "message": "Invalid POST arguments",
30 "messages": validation.messages(),
30 "status": 401 31 "status": 401
31 }) 32 })
32 } 33 }
@@ -79,6 +80,7 @@ class WorkspaceController {
79 if (validation.fails()) { 80 if (validation.fails()) {
80 return response.status(401).send({ 81 return response.status(401).send({
81 "message": "Invalid POST arguments", 82 "message": "Invalid POST arguments",
83 "messages": validation.messages(),
82 "status": 401 84 "status": 401
83 }) 85 })
84 } 86 }
@@ -129,6 +131,7 @@ class WorkspaceController {
129 if (validation.fails()) { 131 if (validation.fails()) {
130 return response.status(401).send({ 132 return response.status(401).send({
131 "message": "Invalid POST arguments", 133 "message": "Invalid POST arguments",
134 "messages": validation.messages(),
132 "status": 401 135 "status": 401
133 }) 136 })
134 } 137 }