Feature: Es wird beachtet, dass es schon vorhandene Daten geben kann

This commit is contained in:
2022-01-10 14:16:41 +01:00
parent d133cb017b
commit cf50607f43
3 changed files with 49 additions and 36 deletions
+44 -24
View File
@@ -1,28 +1,10 @@
<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,
}, {})
return MyFetch._fetch(rest_url, MyFetch._buildRequestOptions(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,
}, {})
return MyFetch._fetch(rest_url, MyFetch._buildRequestOptions(http_body, true), {})
},
'get': async (rest_url, query_parameters) => {
if (query_parameters !== null) {
@@ -38,14 +20,30 @@
},
'_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
})
let response = await MyFetch._pureFetch(rest_url, request_options)
if (!response.ok) {
throw new Error('Fail!')
}
return response.json()
},
'_buildRequestOptions': (request_body, needPut) => {
return {
method: (needPut === true) ? 'PUT' : 'POST',
mode: 'cors',
cache: 'no-cache',
credentials: 'omit',
headers: {
'Content-Type': 'application/json',
},
body: request_body,
}
},
'_pureFetch': async (rest_url, request_options) => {
// noinspection JSUnresolvedVariable
return await fetch(env.API_URL + rest_url, request_options).catch(err => {
console.error(err)
return null
})
}
}
@@ -58,6 +56,16 @@
return MyFetch.get('/working-hours/' + date)
},
'readOrNew': async date => {
let response = await MyFetch._pureFetch('/working-hours/' + date)
if (response.status === 404) {
return {workingTime: '00:00:00', workingDay: date}
} else if (!response.ok) {
throw new Error('Fail!')
}
return response.json()
},
'add': async (record) => {
console.debug(record);
return MyFetch.post('/working-hours', JSON.stringify(record))
@@ -66,6 +74,18 @@
'update': async (date, record) => {
console.debug(record);
return MyFetch.put('/working-hours/' + date, JSON.stringify(record))
},
'addOrUpdate': async (date, record) => {
console.debug(record);
let request_options = MyFetch._buildRequestOptions(JSON.stringify(record))
let response = await MyFetch._pureFetch('/working-hours', request_options)
if (response.status === 409) {
return WorkingHoursRepository.update(date, record)
} else if (!response.ok) {
throw new Error('Fail!')
}
return response.json()
}
}