From afc7f1b8ff337f825fc31b44bc7200694c2b15be Mon Sep 17 00:00:00 2001 From: qowevisa Date: Wed, 27 Nov 2024 07:50:32 +0200 Subject: [PATCH] Add bulk_create function for entities --- src/lib/entities.ts | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/lib/entities.ts b/src/lib/entities.ts index 4b8c4fc..2e48dca 100644 --- a/src/lib/entities.ts +++ b/src/lib/entities.ts @@ -239,6 +239,38 @@ export async function create(groupName: string, data: T, session?: string): P } } +export async function bulk_create(groupName: string, data: T, session?: string): Promise { + const url = `${BASE_API_URL}/${groupName}/bulk_create` + const defaultHeaders = { + 'Content-Type': 'application/json', + }; + + const headers = session + ? { ...defaultHeaders, Cookie: `session=${session}` } + : defaultHeaders + + const config: RequestInit = { + method: 'POST', + headers, + body: JSON.stringify(data) + }; + + console.log(data) + + try { + const response = await fetch(url, config); + if (!response.ok) { + const body = await response.json() + throw new Error(`Failed to create ${groupName}: ${body.message}`); + } + return await response.json() as Message; + } catch (err) { + const error = err as Error + return { message: error.message }; + } +} + + export async function update(groupName: string, id: number, data: T, session?: string): Promise { const url = `${BASE_API_URL}/${groupName}/edit/${id}` const defaultHeaders = {