fix: update device service and handler
This commit is contained in:
@@ -28,10 +28,10 @@ import {
|
|||||||
handleActionRemove,
|
handleActionRemove,
|
||||||
handleCancelForm,
|
handleCancelForm,
|
||||||
} from '~/handlers/device.handler'
|
} from '~/handlers/device.handler'
|
||||||
import { uoms, getUomList } from "~/handlers/_shared.handler"
|
import { uoms, getUomList } from '~/handlers/_shared.handler'
|
||||||
|
|
||||||
// Services
|
// Services
|
||||||
import { getDevices, getDeviceDetail } from '~/services/device.service'
|
import { getList, getDetail } from '~/services/device.service'
|
||||||
|
|
||||||
const title = ref('')
|
const title = ref('')
|
||||||
|
|
||||||
@@ -45,7 +45,7 @@ const {
|
|||||||
fetchData: getToolsList,
|
fetchData: getToolsList,
|
||||||
} = usePaginatedList({
|
} = usePaginatedList({
|
||||||
fetchFn: async (params: any) => {
|
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 || {} }
|
return { success: result.success || false, body: result.body || {} }
|
||||||
},
|
},
|
||||||
entityName: 'device',
|
entityName: 'device',
|
||||||
@@ -87,7 +87,7 @@ provide('rec_item', recItem)
|
|||||||
provide('table_data_loader', isLoading)
|
provide('table_data_loader', isLoading)
|
||||||
|
|
||||||
const getCurrentToolsDetail = async (id: number | string) => {
|
const getCurrentToolsDetail = async (id: number | string) => {
|
||||||
const result = await getDeviceDetail(id)
|
const result = await getDetail(id)
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
const currentValue = result.body?.data || {}
|
const currentValue = result.body?.data || {}
|
||||||
recItem.value = currentValue
|
recItem.value = currentValue
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { createCrudHandler } from '~/handlers/_handler'
|
import { genCrudHandler } from '~/handlers/_handler'
|
||||||
import { postDevice, patchDevice, removeDevice } from '~/services/device.service'
|
import { create, update, remove } from '~/services/device.service'
|
||||||
|
|
||||||
export const {
|
export const {
|
||||||
recId,
|
recId,
|
||||||
@@ -14,8 +14,8 @@ export const {
|
|||||||
handleActionEdit,
|
handleActionEdit,
|
||||||
handleActionRemove,
|
handleActionRemove,
|
||||||
handleCancelForm,
|
handleCancelForm,
|
||||||
} = createCrudHandler({
|
} = genCrudHandler({
|
||||||
post: postDevice,
|
create,
|
||||||
patch: patchDevice,
|
update,
|
||||||
remove: removeDevice,
|
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) {
|
export function create(data: any) {
|
||||||
try {
|
return base.create(path, data, 'device')
|
||||||
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 async function getDeviceDetail(id: string | number) {
|
export function getList(params: any = null) {
|
||||||
try {
|
return base.getList(path, params, 'device')
|
||||||
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 async function postDevice(data: any) {
|
export function getDetail(id: number | string) {
|
||||||
try {
|
return base.getDetail(path, id, 'device')
|
||||||
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 async function patchDevice(id: string | number, data: any) {
|
export function update(id: number | string, data: any) {
|
||||||
try {
|
return base.update(path, id, data, 'device')
|
||||||
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 async function removeDevice(id: string | number) {
|
export function remove(id: number | string) {
|
||||||
try {
|
return base.remove(path, id, 'device')
|
||||||
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')
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user