refactor: Abfrage in ein eigenes Modul ausgelagert.

This commit is contained in:
2021-12-15 17:47:02 +01:00
parent 3dd0f451fc
commit 45650a0f9a
5 changed files with 117 additions and 140 deletions
+83
View File
@@ -0,0 +1,83 @@
<script context="module">
const MyFetch = {
'post': async (rest_url, request_body) => {
return MyFetch._fetch(rest_url, {
method: 'POST',
mode: 'cors',
cache: 'no-cache',
credentials: 'omit',
headers: {
'Content-Type': 'application/json',
},
body: request_body,
}, {})
},
'put': async (rest_url, http_body) => {
return MyFetch._fetch(rest_url, {
method: 'PUT',
mode: 'cors',
cache: 'no-cache',
credentials: 'omit',
headers: {
'Content-Type': 'application/json',
},
body: http_body,
}, {})
},
'get': async (rest_url, query_parameters) => {
if (query_parameters !== null) {
let filter = []
for (let parameter in query_parameters) {
filter.push(parameter + '=' + query_parameters[parameter])
}
if (filter.length > 0) {
rest_url += '?' + filter.join('&')
}
}
return MyFetch._fetch(rest_url, {})
},
'_fetch': async (rest_url, request_options) => {
// noinspection JSUnresolvedVariable
let response = await fetch(env.API_URL + rest_url, request_options).catch(err => {
console.error(err)
return null
})
if (!response.ok) {
throw new Error('Fail!')
}
return response.json()
}
}
export let WorkingHoursRepository = {
'browse': () => {
return MyFetch.get('/working-hours')
},
'read': date => {
return MyFetch.get('/working-hours/' + date)
},
'add': async (record) => {
console.debug(record);
return MyFetch.post('/working-hours', JSON.stringify(record))
},
'update': async (date, record) => {
console.debug(record);
return MyFetch.put('/working-hours/' + date, JSON.stringify(record))
}
}
export let WorkingHoursViewsRepository = {
'weekly': async () => {
return MyFetch.get('/views/working-hours/weekly')
},
'monthly': async () => {
return MyFetch.get('/views/working-hours/monthly')
},
'yearly': async () => {
return MyFetch.get('/views/working-hours/yearly')
},
}
</script>