From d4a67b9bd7b335049b430e46c40ea548a97225a8 Mon Sep 17 00:00:00 2001 From: qowevisa Date: Sun, 10 Nov 2024 08:22:13 +0200 Subject: [PATCH] Add $lib/entities.ts for fetching and controlling entities in database --- src/lib/entities.ts | 153 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 153 insertions(+) create mode 100644 src/lib/entities.ts diff --git a/src/lib/entities.ts b/src/lib/entities.ts new file mode 100644 index 0000000..d799bc8 --- /dev/null +++ b/src/lib/entities.ts @@ -0,0 +1,153 @@ + +// TYPES {{{ + +import { BASE_API_URL } from "./api"; +import type { ErrorMessage, Message } from "./api"; + +// +export interface Type { + color: string; + comment: string; + id: number; + name: string; +} + +export interface Card { + balance: number; + credit_line: number; + have_credit_line: boolean; + id: number; + name: string; +} + +// +// }}} +// + + +export async function getAll(groupName: string, session?: string): Promise { + const url = `${BASE_API_URL}/${groupName}/all` + const defaultHeaders = { + 'Content-Type': 'application/json', + }; + + const headers = session + ? { ...defaultHeaders, Cookie: `session=${session}` } + : defaultHeaders + + const config: RequestInit = { + headers + }; + + try { + const response = await fetch(url, config); + if (!response.ok) { + const body = await response.json() + throw new Error(`Failed to fetch ${groupName}: ${body.message}`); + } + return await response.json() as T[]; + } catch (err) { + const error = err as Error + return { message: error.message }; + } +} + +export async function getByID(groupName: string, id: number): Promise { + try { + const response = await fetch(`${BASE_API_URL}/${groupName}/${id}`); + if (!response.ok) { + throw new Error(`Failed to fetch ${groupName} by ID: ${response.statusText}`); + } + return await response.json() as T; + } catch (err) { + const error = err as Error + return { message: error.message }; + } +} + +export async function create(groupName: string, data: T, session?: string): Promise { + const url = `${BASE_API_URL}/${groupName}/add` + 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 = { + 'Content-Type': 'application/json', + }; + + const headers = session + ? { ...defaultHeaders, Cookie: `session=${session}` } + : defaultHeaders + + const config: RequestInit = { + method: 'PUT', + headers, + body: JSON.stringify(data) + }; + + try { + const response = await fetch(url, config); + if (!response.ok) { + const body = await response.json() + throw new Error(`Failed to update ${groupName}: ${body.message}`); + } + return await response.json() as T; + } catch (err) { + const error = err as Error + return { message: error.message }; + } +} + +export async function remove(groupName: string, id: number, session?: string): Promise { + const url = `${BASE_API_URL}/${groupName}/delete/${id}` + const defaultHeaders = { + 'Content-Type': 'application/json', + }; + + const headers = session + ? { ...defaultHeaders, Cookie: `session=${session}` } + : defaultHeaders + + const config: RequestInit = { + method: 'DELETE', + headers, + }; + + try { + const response = await fetch(url, config); + if (!response.ok) { + throw new Error(`Failed to delete ${groupName}: ${response.statusText}`); + } + return await response.json() as T; + } catch (err) { + const error = err as Error + return { message: error.message }; + } +}