From 45ea70d415728f166805cf3ca3a53c1518246d1d Mon Sep 17 00:00:00 2001 From: riefive Date: Mon, 6 Oct 2025 09:44:48 +0700 Subject: [PATCH] fix: update crud base --- app/services/_crud-base.ts | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/app/services/_crud-base.ts b/app/services/_crud-base.ts index f9d33744..aaedb521 100644 --- a/app/services/_crud-base.ts +++ b/app/services/_crud-base.ts @@ -1,6 +1,6 @@ import { xfetch } from '~/composables/useXfetch' -export async function create(path: string, data: any) { +export async function create(path: string, data: any, name: string = 'item') { try { const resp = await xfetch(path, 'POST', data) const result: any = {} @@ -8,12 +8,12 @@ export async function create(path: string, data: any) { result.body = (resp.body as Record) || {} return result } catch (error) { - console.error('Error posting division:', error) - throw new Error('Failed to post division') + console.error(`Error posting ${name}:`, error) + throw new Error(`Failed to post ${name}`) } } -export async function getList(path: string, params: any = null) { +export async function getList(path: string, params: any = null, name: string = 'item') { try { let url = path if (params && typeof params === 'object' && Object.keys(params).length > 0) { @@ -32,12 +32,12 @@ export async function getList(path: string, params: any = null) { result.body = (resp.body as Record) || {} return result } catch (error) { - console.error('Error fetching divisions:', error) - throw new Error('Failed to fetch divisions') + console.error(`Error fetching ${name}s:`, error) + throw new Error(`Failed to fetch ${name}s`) } } -export async function getDetail(path: string, id: number | string) { +export async function getDetail(path: string, id: number | string, name: string = 'item') { try { const resp = await xfetch(`${path}/${id}`, 'GET') const result: any = {} @@ -45,12 +45,12 @@ export async function getDetail(path: string, id: number | string) { result.body = (resp.body as Record) || {} return result } catch (error) { - console.error('Error fetching division detail:', error) - throw new Error('Failed to get division detail') + console.error(`Error fetching ${name} detail:`, error) + throw new Error(`Failed to get ${name} detail`) } } -export async function update(path: string, id: number | string, data: any) { +export async function update(path: string, id: number | string, data: any, name: string = 'item') { try { const resp = await xfetch(`${path}/${id}`, 'PATCH', data) const result: any = {} @@ -58,12 +58,12 @@ export async function update(path: string, id: number | string, data: any) { result.body = (resp.body as Record) || {} return result } catch (error) { - console.error('Error putting division:', error) - throw new Error('Failed to put division') + console.error(`Error putting ${name}:`, error) + throw new Error(`Failed to put ${name}`) } } -export async function remove(path: string, id: number | string) { +export async function remove(path: string, id: number | string, name: string = 'item') { try { const resp = await xfetch(`${path}/${id}`, 'DELETE') const result: any = {} @@ -71,7 +71,7 @@ export async function remove(path: string, id: number | string) { result.body = (resp.body as Record) || {} return result } catch (error) { - console.error('Error deleting data:', error) - throw new Error('Failed to delete division') + console.error(`Error deleting ${name}:`, error) + throw new Error(`Failed to delete ${name}`) } }