refs #0 The root commit.
This commit is contained in:
147
front/src/lib/server/database.js
Normal file
147
front/src/lib/server/database.js
Normal file
@@ -0,0 +1,147 @@
|
||||
import { error, fail } from '@sveltejs/kit';
|
||||
import { BACKEND_URL } from '$env/static/private';
|
||||
|
||||
const module = "book"
|
||||
|
||||
export async function getItems(project) {
|
||||
const response = await fetch(`${BACKEND_URL}/${project}/`);
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export async function getItem(project, id) {
|
||||
const response = await fetch(`${BACKEND_URL}/${project}/${id}`);
|
||||
if (response.status !== 200) throw error(response.status);
|
||||
const item = await response.json();
|
||||
return item;
|
||||
}
|
||||
|
||||
export async function getEmptyItem(project) {
|
||||
const response = await fetch(`${BACKEND_URL}/_module/${project}`);
|
||||
if (response.status !== 200) throw error(response.status);
|
||||
const result = await response.json();
|
||||
const fields = Object.keys(result.fields);
|
||||
const entries = new Map(fields.map(field => [field, null]))
|
||||
return Object.fromEntries(entries);
|
||||
}
|
||||
|
||||
export async function getProjects() {
|
||||
const response = await fetch(`${BACKEND_URL}/_module/`);
|
||||
if (response.status !== 200) throw error(response.status);
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export async function getProject(project) {
|
||||
const response = await fetch(`${BACKEND_URL}/_module/${project}`);
|
||||
if (response.status !== 200) throw error(response.status);
|
||||
return response.json();
|
||||
}
|
||||
|
||||
|
||||
export async function createItem(project, data) {
|
||||
const fields = Object.fromEntries(
|
||||
Array.from(data.entries()).filter(
|
||||
([key, value]) => !(value instanceof File)
|
||||
)
|
||||
);
|
||||
|
||||
const options = {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(fields)
|
||||
}
|
||||
const response = await fetch(`${BACKEND_URL}/${project}/`, options);
|
||||
if (response.status !== 200) throw error(response.status);
|
||||
|
||||
const item = await response.json();
|
||||
|
||||
const files = Object.fromEntries(
|
||||
Array.from(data.entries()).filter(
|
||||
([key, value]) => value instanceof File && value.size !== 0
|
||||
)
|
||||
);
|
||||
|
||||
for (let key in files)
|
||||
{
|
||||
const data = new FormData()
|
||||
data.append('file', files[key]);
|
||||
await uploadFile(project, item["id"], key, data);
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
export async function updateItem(project, id, data) {
|
||||
const files = Object.fromEntries(
|
||||
Array.from(data.entries()).filter(
|
||||
([key, value]) => value instanceof File && value.size !== 0
|
||||
)
|
||||
);
|
||||
|
||||
for (let key in files)
|
||||
{
|
||||
const data = new FormData()
|
||||
data.append('file', files[key]);
|
||||
await deleteFile(project, id, key);
|
||||
await uploadFile(project, id, key, data);
|
||||
}
|
||||
|
||||
const fields = Object.fromEntries(
|
||||
Array.from(data.entries()).filter(
|
||||
([key, value]) => !(value instanceof File)
|
||||
)
|
||||
);
|
||||
|
||||
const options = {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(fields)
|
||||
}
|
||||
const response = await fetch(`${BACKEND_URL}/${project}/${id}`, options);
|
||||
if (response.status !== 200) throw error(response.status);
|
||||
|
||||
const item = response.json();
|
||||
return item;
|
||||
}
|
||||
|
||||
export async function deleteItem(project, id) {
|
||||
const options = {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
}
|
||||
const response = await fetch(`${BACKEND_URL}/${project}/${id}`, options);
|
||||
if (response.status !== 200) throw error(response.status);
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export async function readFile(project, id, field) {
|
||||
const response = await fetch(`${BACKEND_URL}/${project}/${id}/${field}`);
|
||||
return response;
|
||||
}
|
||||
|
||||
export async function uploadFile(project, id, field, data) {
|
||||
const options = {
|
||||
method: 'POST',
|
||||
body: data,
|
||||
}
|
||||
const response = await fetch(`${BACKEND_URL}/${project}/${id}/${field}`, options);
|
||||
if (response.status !== 200) throw error(response.status);
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export async function deleteFile(project, id, field) {
|
||||
const options = {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
}
|
||||
const response = await fetch(`${BACKEND_URL}/${project}/${id}/${field}`, options);
|
||||
if (response.status !== 200) throw error(response.status);
|
||||
return response.json();
|
||||
}
|
||||
Reference in New Issue
Block a user