fix: update device service and handler
This commit is contained in:
@@ -28,10 +28,10 @@ import {
|
||||
handleActionRemove,
|
||||
handleCancelForm,
|
||||
} from '~/handlers/device.handler'
|
||||
import { uoms, getUomList } from "~/handlers/_shared.handler"
|
||||
import { uoms, getUomList } from '~/handlers/_shared.handler'
|
||||
|
||||
// Services
|
||||
import { getDevices, getDeviceDetail } from '~/services/device.service'
|
||||
import { getList, getDetail } from '~/services/device.service'
|
||||
|
||||
const title = ref('')
|
||||
|
||||
@@ -45,7 +45,7 @@ const {
|
||||
fetchData: getToolsList,
|
||||
} = usePaginatedList({
|
||||
fetchFn: async (params: any) => {
|
||||
const result = await getDevices({ search: params.search, page: params['page-number'] || 0 })
|
||||
const result = await getList({ search: params.search, 'page-number': params['page-number'] || 0 })
|
||||
return { success: result.success || false, body: result.body || {} }
|
||||
},
|
||||
entityName: 'device',
|
||||
@@ -87,7 +87,7 @@ provide('rec_item', recItem)
|
||||
provide('table_data_loader', isLoading)
|
||||
|
||||
const getCurrentToolsDetail = async (id: number | string) => {
|
||||
const result = await getDeviceDetail(id)
|
||||
const result = await getDetail(id)
|
||||
if (result.success) {
|
||||
const currentValue = result.body?.data || {}
|
||||
recItem.value = currentValue
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { createCrudHandler } from '~/handlers/_handler'
|
||||
import { postDevice, patchDevice, removeDevice } from '~/services/device.service'
|
||||
import { genCrudHandler } from '~/handlers/_handler'
|
||||
import { create, update, remove } from '~/services/device.service'
|
||||
|
||||
export const {
|
||||
recId,
|
||||
@@ -14,8 +14,8 @@ export const {
|
||||
handleActionEdit,
|
||||
handleActionRemove,
|
||||
handleCancelForm,
|
||||
} = createCrudHandler({
|
||||
post: postDevice,
|
||||
patch: patchDevice,
|
||||
remove: removeDevice,
|
||||
} = genCrudHandler({
|
||||
create,
|
||||
update,
|
||||
remove,
|
||||
})
|
||||
|
||||
@@ -1,79 +1,23 @@
|
||||
import { xfetch } from '~/composables/useXfetch'
|
||||
import * as base from './_crud-base'
|
||||
|
||||
const mainUrl = '/api/v1/device'
|
||||
const path = '/api/v1/device'
|
||||
|
||||
export async function getDevices(params: any = null) {
|
||||
try {
|
||||
let url = mainUrl
|
||||
if (params && typeof params === 'object' && Object.keys(params).length > 0) {
|
||||
const searchParams = new URLSearchParams()
|
||||
for (const key in params) {
|
||||
if (params[key] !== null && params[key] !== undefined && params[key] !== '') {
|
||||
searchParams.append(key, params[key])
|
||||
}
|
||||
}
|
||||
const queryString = searchParams.toString()
|
||||
if (queryString) url += `?${queryString}`
|
||||
}
|
||||
const resp = await xfetch(url, 'GET')
|
||||
const result: any = {}
|
||||
result.success = resp.success
|
||||
result.body = (resp.body as Record<string, any>) || {}
|
||||
return result
|
||||
} catch (error) {
|
||||
console.error('Error fetching devices:', error)
|
||||
throw new Error('Failed to fetch devices')
|
||||
}
|
||||
export function create(data: any) {
|
||||
return base.create(path, data, 'device')
|
||||
}
|
||||
|
||||
export async function getDeviceDetail(id: string | number) {
|
||||
try {
|
||||
const resp = await xfetch(`${mainUrl}/${id}`, 'GET')
|
||||
const result: any = {}
|
||||
result.success = resp.success
|
||||
result.body = (resp.body as Record<string, any>) || {}
|
||||
return result
|
||||
} catch (error) {
|
||||
console.error('Error fetching device detail:', error)
|
||||
throw new Error('Failed to fetch device detail')
|
||||
}
|
||||
export function getList(params: any = null) {
|
||||
return base.getList(path, params, 'device')
|
||||
}
|
||||
|
||||
export async function postDevice(data: any) {
|
||||
try {
|
||||
const resp = await xfetch(mainUrl, 'POST', data)
|
||||
const result: any = {}
|
||||
result.success = resp.success
|
||||
result.body = (resp.body as Record<string, any>) || {}
|
||||
return result
|
||||
} catch (error) {
|
||||
console.error('Error creating device:', error)
|
||||
throw new Error('Failed to create device')
|
||||
}
|
||||
export function getDetail(id: number | string) {
|
||||
return base.getDetail(path, id, 'device')
|
||||
}
|
||||
|
||||
export async function patchDevice(id: string | number, data: any) {
|
||||
try {
|
||||
const resp = await xfetch(`${mainUrl}/${id}`, 'PATCH', data)
|
||||
const result: any = {}
|
||||
result.success = resp.success
|
||||
result.body = (resp.body as Record<string, any>) || {}
|
||||
return result
|
||||
} catch (error) {
|
||||
console.error('Error updating device:', error)
|
||||
throw new Error('Failed to update device')
|
||||
}
|
||||
export function update(id: number | string, data: any) {
|
||||
return base.update(path, id, data, 'device')
|
||||
}
|
||||
|
||||
export async function removeDevice(id: string | number) {
|
||||
try {
|
||||
const resp = await xfetch(`${mainUrl}/${id}`, 'DELETE')
|
||||
const result: any = {}
|
||||
result.success = resp.success
|
||||
result.body = (resp.body as Record<string, any>) || {}
|
||||
return result
|
||||
} catch (error) {
|
||||
console.error('Error deleting device:', error)
|
||||
throw new Error('Failed to delete device')
|
||||
}
|
||||
export function remove(id: number | string) {
|
||||
return base.remove(path, id, 'device')
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user