Skip to content

Commit 671fe4b

Browse files
committed
reworked services to prevent code duplication
1 parent 5eafeb0 commit 671fe4b

31 files changed

Lines changed: 499 additions & 242 deletions

src/locales/en.json

Lines changed: 104 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -159,102 +159,108 @@
159159
},
160160
"notifications": {
161161
"tags": {
162-
"get": {
163-
"failed": {
164-
"title": "Error",
165-
"message": "Loading the Tags failed"
166-
}
167-
}
168-
},
169-
"tag": {
170-
"post": {
171-
"failed": {
172-
"title": "Error",
173-
"message": "Creating the Tag failed"
162+
"all": {
163+
"get": {
164+
"failed": {
165+
"title": "Error",
166+
"message": "Loading the Tags failed"
167+
}
168+
},
169+
"post": {
170+
"failed": {
171+
"title": "Error",
172+
"message": "Creating the Tag failed"
173+
}
174174
}
175175
},
176-
"delete": {
177-
"failed": {
178-
"title": "Error",
179-
"message": "Deleting the Tag failed"
176+
"single": {
177+
"delete": {
178+
"failed": {
179+
"title": "Error",
180+
"message": "Deleting the Tag failed"
181+
}
180182
}
181183
}
182184
},
183185
"context_factors": {
184-
"get": {
185-
"failed": {
186-
"title": "Error",
187-
"message": "Loading the Context Factors failed"
188-
}
189-
}
190-
},
191-
"context_factor": {
192-
"get": {
193-
"failed": {
194-
"title": "Error",
195-
"message": "Loading the Context Factor failed"
196-
}
197-
},
198-
"put": {
199-
"failed": {
200-
"title": "Error",
201-
"message": "Updating the Context Factor failed"
186+
"all": {
187+
"get": {
188+
"failed": {
189+
"title": "Error",
190+
"message": "Loading the Context Factors failed"
191+
}
202192
},
203-
"success": {
204-
"title": "Success",
205-
"message": "The Context Factor got updated"
193+
"post": {
194+
"failed": {
195+
"title": "Error",
196+
"message": "Creating the Context Factor failed"
197+
},
198+
"success": {
199+
"title": "Success",
200+
"message": "The Context Factor got created"
201+
}
206202
}
207203
},
208-
"post": {
209-
"failed": {
210-
"title": "Error",
211-
"message": "Creating the Context Factor failed"
204+
"single": {
205+
"get": {
206+
"failed": {
207+
"title": "Error",
208+
"message": "Loading the Context Factor failed"
209+
}
212210
},
213-
"success": {
214-
"title": "Success",
215-
"message": "The Context Factor got created"
211+
"put": {
212+
"failed": {
213+
"title": "Error",
214+
"message": "Updating the Context Factor failed"
215+
},
216+
"success": {
217+
"title": "Success",
218+
"message": "The Context Factor got updated"
219+
}
216220
}
217221
}
218222
},
219223
"processes": {
220-
"get": {
221-
"failed": {
222-
"title": "Error",
223-
"message": "Loading the Processes failed"
224-
}
225-
}
226-
},
227-
"process": {
228-
"get": {
229-
"failed": {
230-
"title": "Error",
231-
"message": "Loading the Process failed"
232-
}
233-
},
234-
"put": {
235-
"failed": {
236-
"title": "Error",
237-
"message": "Updating the Process failed"
224+
"all": {
225+
"get": {
226+
"failed": {
227+
"title": "Error",
228+
"message": "Loading the Processes failed"
229+
}
238230
},
239-
"success": {
240-
"title": "Success",
241-
"message": "The Process got updated"
231+
"post": {
232+
"failed": {
233+
"title": "Error",
234+
"message": "Creating the Process failed"
235+
},
236+
"success": {
237+
"title": "Success",
238+
"message": "The Process got created"
239+
}
242240
}
243241
},
244-
"post": {
245-
"failed": {
246-
"title": "Error",
247-
"message": "Creating the Process failed"
242+
"single": {
243+
"get": {
244+
"failed": {
245+
"title": "Error",
246+
"message": "Loading the Process failed"
247+
}
248248
},
249-
"success": {
250-
"title": "Success",
251-
"message": "The Process got created"
252-
}
253-
},
254-
"delete": {
255-
"failed": {
256-
"title": "Error",
257-
"message": "Deleting the Process failed"
249+
"put": {
250+
"failed": {
251+
"title": "Error",
252+
"message": "Updating the Process failed"
253+
},
254+
"success": {
255+
"title": "Success",
256+
"message": "The Process got updated"
257+
}
258+
},
259+
"delete": {
260+
"failed": {
261+
"title": "Error",
262+
"message": "Deleting the Process failed"
263+
}
258264
}
259265
},
260266
"tags": {
@@ -287,26 +293,28 @@
287293
}
288294
},
289295
"context_types": {
290-
"get": {
291-
"failed": {
292-
"title": "Error",
293-
"message": "Loading the Context Types failed"
294-
}
295-
}
296-
},
297-
"context_type": {
298-
"post": {
299-
"failed": {
300-
"title": "Error",
301-
"message": "Creating the Context Type failed"
296+
"all": {
297+
"get": {
298+
"failed": {
299+
"title": "Error",
300+
"message": "Loading the Context Types failed"
301+
}
302+
},
303+
"post": {
304+
"failed": {
305+
"title": "Error",
306+
"message": "Creating the Context Type failed"
307+
}
302308
}
303309
},
304-
"delete": {
305-
"failed": {
306-
"title": "Error",
307-
"message": "Deleting the Context Type failed"
308-
},
309-
"not_allowed": "This Context is in use"
310+
"single": {
311+
"delete": {
312+
"failed": {
313+
"title": "Error",
314+
"message": "Deleting the Context Type failed"
315+
},
316+
"not_allowed": "This Context is in use"
317+
}
310318
}
311319
}
312320
},

src/services/base.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import Vue from 'vue'
2+
import getRoute from './routes'
23

34
export default class Service {
45
constructor({
5-
method, endpoint, data = undefined, name, success = () => {}, failed = () => {}
6+
method, endpoint, routeOverrides, data = undefined, name, success = () => {}, failed = () => {}
67
}) {
78
this.method = method
8-
this.endpoint = endpoint
9+
this.endpoint = endpoint || getRoute(name, routeOverrides)
910
this.data = data
1011
this.name = name
1112
this.success = success
@@ -40,4 +41,25 @@ export default class Service {
4041
showSuccessNotification() {
4142
this.showNotification({ key: 'success', type: 'success' })
4243
}
44+
45+
static builder({ name, success, failed }) {
46+
function build(method, prefix, data = undefined) {
47+
return new this({
48+
name: name + '.' + prefix,
49+
success,
50+
failed,
51+
data,
52+
method,
53+
routeOverrides: (prefix === 'single' && data) ? [data] : []
54+
})
55+
}
56+
57+
return {
58+
getAll: build.bind(this, 'get', 'all'),
59+
get: build.bind(this, 'get', 'single'),
60+
create: build.bind(this, 'post', 'all'),
61+
update: build.bind(this, 'put', 'single'),
62+
remove: build.bind(this, 'delete', 'single')
63+
}
64+
}
4365
}

src/services/contextFactor/create.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ function success(contextFactor) {
88
this.showSuccessNotification()
99
}
1010

11-
export default (contextFactor) => new Service({
12-
method: 'post',
13-
endpoint: '/context_factors',
14-
name: 'context_factor',
15-
data: contextFactor,
11+
export default (contextFactor) => Service.builder({
12+
name: 'context_factors',
1613
success
17-
})
14+
}).create(contextFactor)

src/services/contextFactor/get.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ function success(contextFactor) {
88
updateAndSetActive(store, contextFactor, ContextFactor, 'contextFactor')
99
}
1010

11-
export default (contextFactor) => new Service({
12-
method: 'get',
13-
endpoint: `/context_factors/${contextFactor.id}`,
14-
name: 'context_factor',
11+
export default (contextFactor) => Service.builder({
12+
name: 'context_factors',
1513
success
16-
})
14+
}).get(contextFactor)

src/services/contextFactor/getAll.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ function success(contextFactors) {
77
store.dispatch('contextFactor/store', contextFactors.map(p => new ContextFactor(p)))
88
}
99

10-
export default () => new Service({
11-
method: 'get',
12-
endpoint: '/context_factors',
10+
export default () => Service.builder({
1311
name: 'context_factors',
1412
success
15-
})
13+
}).getAll()

src/services/contextFactor/update.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ function success(contextFactor) {
88
this.showSuccessNotification()
99
}
1010

11-
export default (contextFactor) => new Service({
12-
method: 'put',
13-
endpoint: `/context_factors/${contextFactor.id}`,
14-
name: 'context_factor',
15-
data: contextFactor,
11+
export default (contextFactor) => Service.builder({
12+
name: 'context_factors',
1613
success
17-
})
14+
}).update(contextFactor)

src/services/contextType/create.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ const success = (contextType) => {
77
store.dispatch('contextType/add', new ContextType(contextType))
88
}
99

10-
export default (contextType) => new Service({
11-
method: 'post',
12-
endpoint: '/context_types',
13-
data: contextType,
14-
name: 'context_type',
10+
export default (contextType) => Service.builder({
11+
name: 'context_types',
1512
success
16-
})
13+
}).create(contextType)

src/services/contextType/getAll.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ const success = (contextTypes) => {
77
store.dispatch('contextType/store', contextTypes.map(p => new ContextType(p)))
88
}
99

10-
export default () => new Service({
11-
method: 'get',
12-
endpoint: '/context_types',
10+
export default () => Service.builder({
1311
name: 'context_types',
1412
success
15-
})
13+
}).getAll()

src/services/contextType/remove.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,12 @@ const success = (response, { data }) => {
99

1010
const failed = (response) => {
1111
if (response.response.status === 418) {
12-
Vue.$message.warning(Vue.i18n.t('notifications.context_type.delete.not_allowed'))
12+
Vue.$message.warning(Vue.i18n.t('notifications.context_types.single.delete.not_allowed'))
1313
}
1414
}
1515

16-
export default (contextType) => new Service({
17-
method: 'delete',
18-
endpoint: `/context_types/${contextType.id}`,
19-
data: contextType,
20-
name: 'context_type',
16+
export default (contextType) => Service.builder({
17+
name: 'context_types',
2118
success,
2219
failed
23-
})
20+
}).remove(contextType)

src/services/process/addTag.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ function success(process) {
1010

1111
export default (process, tag) => new Service({
1212
method: 'post',
13-
endpoint: `/processes/${process.id}/tags`,
14-
name: 'process.tags',
13+
name: 'processes.tags',
14+
routeOverrides: [process],
1515
data: tag,
1616
success
1717
})

0 commit comments

Comments
 (0)