fix: update device service and handler

This commit is contained in:
riefive
2025-10-06 09:51:31 +07:00
parent 45ea70d415
commit ecdc5d80d9
3 changed files with 22 additions and 78 deletions
+12 -68
View File
@@ -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')
}