Files
simrsx-fe/app/services/installation.service.ts

80 lines
2.5 KiB
TypeScript

import { xfetch } from '~/composables/useXfetch'
const mainUrl = '/api/v1/installation'
export async function getInstallations(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(mainUrl, 'GET')
const result: any = {}
result.success = resp.success
result.body = (resp.body as Record<string, any>) || {}
return result
} catch (error) {
console.error('Error fetching installations:', error)
throw new Error('Failed to fetch installations')
}
}
export async function getInstallationDetail(id: number | string) {
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 installation detail:', error)
throw new Error('Failed to get installation detail')
}
}
export async function postInstallation(record: any) {
try {
const resp = await xfetch(mainUrl, 'POST', record)
const result: any = {}
result.success = resp.success
result.body = (resp.body as Record<string, any>) || {}
return result
} catch (error) {
console.error('Error posting installation:', error)
throw new Error('Failed to post installation')
}
}
export async function patchInstallation(id: number | string, record: any) {
try {
const resp = await xfetch(`${mainUrl}/${id}`, 'PATCH', record)
const result: any = {}
result.success = resp.success
result.body = (resp.body as Record<string, any>) || {}
return result
} catch (error) {
console.error('Error putting installation:', error)
throw new Error('Failed to put installation')
}
}
export async function removeInstallation(id: number | string) {
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 record:', error)
throw new Error('Failed to delete installation')
}
}