103 lines
3.6 KiB
Svelte
103 lines
3.6 KiB
Svelte
<script context="module">
|
|
const MyFetch = {
|
|
'post': async (rest_url, request_body) => {
|
|
return MyFetch._fetch(rest_url, MyFetch._buildRequestOptions(request_body), {})
|
|
},
|
|
'put': async (rest_url, http_body) => {
|
|
return MyFetch._fetch(rest_url, MyFetch._buildRequestOptions(http_body, true), {})
|
|
},
|
|
'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 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
|
|
})
|
|
}
|
|
}
|
|
|
|
export const WorkingHoursRepository = {
|
|
'browse': () => {
|
|
return MyFetch.get('/working-hours')
|
|
},
|
|
|
|
'read': 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) => {
|
|
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))
|
|
},
|
|
|
|
'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()
|
|
}
|
|
}
|
|
|
|
export const 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> |