diff options
Diffstat (limited to 'src/server/app/Controllers/Http/ServiceController.js')
-rw-r--r-- | src/server/app/Controllers/Http/ServiceController.js | 211 |
1 files changed, 0 insertions, 211 deletions
diff --git a/src/server/app/Controllers/Http/ServiceController.js b/src/server/app/Controllers/Http/ServiceController.js deleted file mode 100644 index ea7035ca1..000000000 --- a/src/server/app/Controllers/Http/ServiceController.js +++ /dev/null | |||
@@ -1,211 +0,0 @@ | |||
1 | const Service = use('App/Models/Service'); | ||
2 | const { | ||
3 | validateAll, | ||
4 | } = use('Validator'); | ||
5 | |||
6 | const uuid = require('uuid/v4'); | ||
7 | |||
8 | class ServiceController { | ||
9 | // Create a new service for user | ||
10 | async create({ | ||
11 | request, | ||
12 | response, | ||
13 | }) { | ||
14 | // Validate user input | ||
15 | const validation = await validateAll(request.all(), { | ||
16 | name: 'required|string', | ||
17 | recipeId: 'required', | ||
18 | }); | ||
19 | if (validation.fails()) { | ||
20 | return response.status(401).send({ | ||
21 | message: 'Invalid POST arguments', | ||
22 | messages: validation.messages(), | ||
23 | status: 401, | ||
24 | }); | ||
25 | } | ||
26 | |||
27 | const data = request.all(); | ||
28 | |||
29 | // Get new, unused uuid | ||
30 | let serviceId; | ||
31 | do { | ||
32 | serviceId = uuid(); | ||
33 | } while ((await Service.query().where('serviceId', serviceId).fetch()).rows.length > 0); // eslint-disable-line no-await-in-loop | ||
34 | |||
35 | await Service.create({ | ||
36 | serviceId, | ||
37 | name: data.name, | ||
38 | recipeId: data.recipeId, | ||
39 | settings: JSON.stringify(data), | ||
40 | }); | ||
41 | |||
42 | return response.send({ | ||
43 | data: { | ||
44 | userId: 1, | ||
45 | id: serviceId, | ||
46 | isEnabled: true, | ||
47 | isNotificationEnabled: true, | ||
48 | isBadgeEnabled: true, | ||
49 | isMuted: false, | ||
50 | isDarkModeEnabled: '', | ||
51 | spellcheckerLanguage: '', | ||
52 | order: 1, | ||
53 | customRecipe: false, | ||
54 | hasCustomIcon: false, | ||
55 | workspaces: [], | ||
56 | iconUrl: null, | ||
57 | ...data, | ||
58 | }, | ||
59 | status: ['created'], | ||
60 | }); | ||
61 | } | ||
62 | |||
63 | // List all services a user has created | ||
64 | async list({ | ||
65 | response, | ||
66 | }) { | ||
67 | const services = (await Service.all()).rows; | ||
68 | // Convert to array with all data Franz wants | ||
69 | const servicesArray = services.map(service => ({ | ||
70 | customRecipe: false, | ||
71 | hasCustomIcon: false, | ||
72 | isBadgeEnabled: true, | ||
73 | isDarkModeEnabled: '', | ||
74 | isEnabled: true, | ||
75 | isMuted: false, | ||
76 | isNotificationEnabled: true, | ||
77 | order: 1, | ||
78 | spellcheckerLanguage: '', | ||
79 | workspaces: [], | ||
80 | iconUrl: null, | ||
81 | ...JSON.parse(service.settings), | ||
82 | id: service.serviceId, | ||
83 | name: service.name, | ||
84 | recipeId: service.recipeId, | ||
85 | userId: 1, | ||
86 | })); | ||
87 | |||
88 | return response.send(servicesArray); | ||
89 | } | ||
90 | |||
91 | async edit({ | ||
92 | request, | ||
93 | response, | ||
94 | params, | ||
95 | }) { | ||
96 | // Validate user input | ||
97 | const validation = await validateAll(request.all(), { | ||
98 | name: 'required', | ||
99 | }); | ||
100 | if (validation.fails()) { | ||
101 | return response.status(401).send({ | ||
102 | message: 'Invalid POST arguments', | ||
103 | messages: validation.messages(), | ||
104 | status: 401, | ||
105 | }); | ||
106 | } | ||
107 | |||
108 | const data = request.all(); | ||
109 | const { | ||
110 | id, | ||
111 | } = params; | ||
112 | |||
113 | // Get current settings from db | ||
114 | const serviceData = (await Service.query() | ||
115 | .where('serviceId', id).fetch()).rows[0]; | ||
116 | |||
117 | const settings = { | ||
118 | ...JSON.parse(serviceData.settings), | ||
119 | ...data, | ||
120 | }; | ||
121 | |||
122 | // Update data in database | ||
123 | await (Service.query() | ||
124 | .where('serviceId', id)).update({ | ||
125 | name: data.name, | ||
126 | settings: JSON.stringify(settings), | ||
127 | }); | ||
128 | |||
129 | // Get updated row | ||
130 | const service = (await Service.query() | ||
131 | .where('serviceId', id).fetch()).rows[0]; | ||
132 | |||
133 | return response.send({ | ||
134 | id: service.serviceId, | ||
135 | name: data.name, | ||
136 | ...settings, | ||
137 | userId: 1, | ||
138 | }); | ||
139 | } | ||
140 | |||
141 | async reorder({ | ||
142 | request, | ||
143 | response, | ||
144 | }) { | ||
145 | const data = request.all(); | ||
146 | |||
147 | for (const service of Object.keys(data)) { | ||
148 | // Get current settings from db | ||
149 | const serviceData = (await Service.query() // eslint-disable-line no-await-in-loop | ||
150 | .where('serviceId', service).fetch()).rows[0]; | ||
151 | |||
152 | const settings = { | ||
153 | ...JSON.parse(serviceData.settings), | ||
154 | order: data[service], | ||
155 | }; | ||
156 | |||
157 | // Update data in database | ||
158 | await (Service.query() // eslint-disable-line no-await-in-loop | ||
159 | .where('serviceId', service)) | ||
160 | .update({ | ||
161 | settings: JSON.stringify(settings), | ||
162 | }); | ||
163 | } | ||
164 | |||
165 | // Get new services | ||
166 | const services = (await Service.all()).rows; | ||
167 | // Convert to array with all data Franz wants | ||
168 | const servicesArray = services.map(service => ({ | ||
169 | customRecipe: false, | ||
170 | hasCustomIcon: false, | ||
171 | isBadgeEnabled: true, | ||
172 | isDarkModeEnabled: '', | ||
173 | isEnabled: true, | ||
174 | isMuted: false, | ||
175 | isNotificationEnabled: true, | ||
176 | order: 1, | ||
177 | spellcheckerLanguage: '', | ||
178 | workspaces: [], | ||
179 | iconUrl: null, | ||
180 | ...JSON.parse(service.settings), | ||
181 | id: service.serviceId, | ||
182 | name: service.name, | ||
183 | recipeId: service.recipeId, | ||
184 | userId: 1, | ||
185 | })); | ||
186 | |||
187 | return response.send(servicesArray); | ||
188 | } | ||
189 | |||
190 | update({ | ||
191 | response, | ||
192 | }) { | ||
193 | return response.send([]); | ||
194 | } | ||
195 | |||
196 | async delete({ | ||
197 | params, | ||
198 | response, | ||
199 | }) { | ||
200 | // Update data in database | ||
201 | await (Service.query() | ||
202 | .where('serviceId', params.id)).delete(); | ||
203 | |||
204 | return response.send({ | ||
205 | message: 'Sucessfully deleted service', | ||
206 | status: 200, | ||
207 | }); | ||
208 | } | ||
209 | } | ||
210 | |||
211 | module.exports = ServiceController; | ||