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"> <script context="module">
const MyFetch = { const MyFetch = {
'post': async (rest_url, request_body) => { 'post': async (rest_url, request_body) => {
return MyFetch._fetch(rest_url, { return MyFetch._fetch(rest_url, MyFetch._buildRequestOptions(request_body), {})
method: 'POST',
mode: 'cors',
cache: 'no-cache',
credentials: 'omit',
headers: {
'Content-Type': 'application/json',
},
body: request_body,
}, {})
}, },
'put': async (rest_url, http_body) => { 'put': async (rest_url, http_body) => {
return MyFetch._fetch(rest_url, { return MyFetch._fetch(rest_url, MyFetch._buildRequestOptions(http_body, true), {})
method: 'PUT',
mode: 'cors',
cache: 'no-cache',
credentials: 'omit',
headers: {
'Content-Type': 'application/json',
},
body: http_body,
}, {})
}, },
'get': async (rest_url, query_parameters) => { 'get': async (rest_url, query_parameters) => {
if (query_parameters !== null) { if (query_parameters !== null) {
@@ -38,14 +20,30 @@
}, },
'_fetch': async (rest_url, request_options) => { '_fetch': async (rest_url, request_options) => {
// noinspection JSUnresolvedVariable // noinspection JSUnresolvedVariable
let response = await fetch(env.API_URL + rest_url, request_options).catch(err => { let response = await MyFetch._pureFetch(rest_url, request_options)
console.error(err)
return null
})
if (!response.ok) { if (!response.ok) {
throw new Error('Fail!') throw new Error('Fail!')
} }
return response.json() 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) 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) => { 'add': async (record) => {
console.debug(record); console.debug(record);
return MyFetch.post('/working-hours', JSON.stringify(record)) return MyFetch.post('/working-hours', JSON.stringify(record))
@@ -66,6 +74,18 @@
'update': async (date, record) => { 'update': async (date, record) => {
console.debug(record); console.debug(record);
return MyFetch.put('/working-hours/' + date, JSON.stringify(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()
} }
} }
+2 -8
View File
@@ -9,20 +9,14 @@
export let newRecord = false export let newRecord = false
const Controller = { const Controller = {
'saveRecord': () => { 'saveRecord': () => {
if (newRecord === true) { WorkingHoursRepository.addOrUpdate(activeRecord.workingDay, activeRecord).then(() => {
WorkingHoursRepository.add(activeRecord).then(() => {
Controller.nextDay() Controller.nextDay()
}); });
} else {
WorkingHoursRepository.update(activeRecord.workingDay, activeRecord).then(() => {
Controller.nextDay()
});
}
}, },
'nextDay': () => { 'nextDay': () => {
let actDate = new TimekeepingDate(activeRecord.workingDay) let actDate = new TimekeepingDate(activeRecord.workingDay)
let nextDay = actDate.getNextDay() let nextDay = actDate.getNextDay()
activeRecord = {workingTime: '00:00:00', workingDay: nextDay.getDateString()} activeRecord = WorkingHoursRepository.readOrNew(nextDay.getDateString())
}, },
'cancelActiveRecord': () => { 'cancelActiveRecord': () => {
activeRecord = null; activeRecord = null;
+1 -2
View File
@@ -21,8 +21,7 @@
}, },
'newRecord': () => { 'newRecord': () => {
const actDate = new TimekeepingDate() const actDate = new TimekeepingDate()
activeRecord = {workingTime: '00:00:00', workingDay: actDate.getDateString()} activeRecord = WorkingHoursRepository.readOrNew(actDate.getDateString())
newRecord = true;
}, },
'selectActiveRecord': record => WorkingHoursRepository.read(record.workingDay).then(data => { 'selectActiveRecord': record => WorkingHoursRepository.read(record.workingDay).then(data => {
if (data === null) { if (data === null) {