fix: update crud base
This commit is contained in:
+15
-15
@@ -1,6 +1,6 @@
|
|||||||
import { xfetch } from '~/composables/useXfetch'
|
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 {
|
try {
|
||||||
const resp = await xfetch(path, 'POST', data)
|
const resp = await xfetch(path, 'POST', data)
|
||||||
const result: any = {}
|
const result: any = {}
|
||||||
@@ -8,12 +8,12 @@ export async function create(path: string, data: any) {
|
|||||||
result.body = (resp.body as Record<string, any>) || {}
|
result.body = (resp.body as Record<string, any>) || {}
|
||||||
return result
|
return result
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error posting division:', error)
|
console.error(`Error posting ${name}:`, error)
|
||||||
throw new Error('Failed to post division')
|
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 {
|
try {
|
||||||
let url = path
|
let url = path
|
||||||
if (params && typeof params === 'object' && Object.keys(params).length > 0) {
|
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<string, any>) || {}
|
result.body = (resp.body as Record<string, any>) || {}
|
||||||
return result
|
return result
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching divisions:', error)
|
console.error(`Error fetching ${name}s:`, error)
|
||||||
throw new Error('Failed to fetch divisions')
|
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 {
|
try {
|
||||||
const resp = await xfetch(`${path}/${id}`, 'GET')
|
const resp = await xfetch(`${path}/${id}`, 'GET')
|
||||||
const result: any = {}
|
const result: any = {}
|
||||||
@@ -45,12 +45,12 @@ export async function getDetail(path: string, id: number | string) {
|
|||||||
result.body = (resp.body as Record<string, any>) || {}
|
result.body = (resp.body as Record<string, any>) || {}
|
||||||
return result
|
return result
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching division detail:', error)
|
console.error(`Error fetching ${name} detail:`, error)
|
||||||
throw new Error('Failed to get division detail')
|
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 {
|
try {
|
||||||
const resp = await xfetch(`${path}/${id}`, 'PATCH', data)
|
const resp = await xfetch(`${path}/${id}`, 'PATCH', data)
|
||||||
const result: any = {}
|
const result: any = {}
|
||||||
@@ -58,12 +58,12 @@ export async function update(path: string, id: number | string, data: any) {
|
|||||||
result.body = (resp.body as Record<string, any>) || {}
|
result.body = (resp.body as Record<string, any>) || {}
|
||||||
return result
|
return result
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error putting division:', error)
|
console.error(`Error putting ${name}:`, error)
|
||||||
throw new Error('Failed to put division')
|
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 {
|
try {
|
||||||
const resp = await xfetch(`${path}/${id}`, 'DELETE')
|
const resp = await xfetch(`${path}/${id}`, 'DELETE')
|
||||||
const result: any = {}
|
const result: any = {}
|
||||||
@@ -71,7 +71,7 @@ export async function remove(path: string, id: number | string) {
|
|||||||
result.body = (resp.body as Record<string, any>) || {}
|
result.body = (resp.body as Record<string, any>) || {}
|
||||||
return result
|
return result
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error deleting data:', error)
|
console.error(`Error deleting ${name}:`, error)
|
||||||
throw new Error('Failed to delete division')
|
throw new Error(`Failed to delete ${name}`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user