From fa6b99c193efb043b303e37a9e47906404398f58 Mon Sep 17 00:00:00 2001 From: riefive Date: Mon, 29 Sep 2025 13:23:51 +0700 Subject: [PATCH] feat(unit): add specialist service models --- app/handlers/specialist.handler.ts | 21 ++++++++ app/models/specialist.ts | 5 ++ app/services/specialist.service.ts | 79 ++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 app/handlers/specialist.handler.ts create mode 100644 app/models/specialist.ts create mode 100644 app/services/specialist.service.ts diff --git a/app/handlers/specialist.handler.ts b/app/handlers/specialist.handler.ts new file mode 100644 index 00000000..445f8c15 --- /dev/null +++ b/app/handlers/specialist.handler.ts @@ -0,0 +1,21 @@ +import { createCrudHandler } from '~/handlers/_handler' +import { postSpecialist, patchSpecialist, removeSpecialist } from '~/services/specialist.service' + +export const { + recId, + recAction, + recItem, + isReadonly, + isProcessing, + isFormEntryDialogOpen, + isRecordConfirmationOpen, + onResetState, + handleActionSave, + handleActionEdit, + handleActionRemove, + handleCancelForm, +} = createCrudHandler({ + post: postSpecialist, + patch: patchSpecialist, + remove: removeSpecialist, +}) diff --git a/app/models/specialist.ts b/app/models/specialist.ts new file mode 100644 index 00000000..0a27f023 --- /dev/null +++ b/app/models/specialist.ts @@ -0,0 +1,5 @@ +export interface Specialist { + code: string + name: string + unit_id: number +} diff --git a/app/services/specialist.service.ts b/app/services/specialist.service.ts new file mode 100644 index 00000000..bd359918 --- /dev/null +++ b/app/services/specialist.service.ts @@ -0,0 +1,79 @@ +import { xfetch } from '~/composables/useXfetch' + +const mainUrl = '/api/v1/specialist' + +export async function getSpecialists(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) || {} + return result + } catch (error) { + console.error('Error fetching specialists:', error) + throw new Error('Failed to fetch specialists') + } +} + +export async function getSpecialistDetail(id: number | string) { + try { + const resp = await xfetch(`${mainUrl}/${id}`, 'GET') + const result: any = {} + result.success = resp.success + result.body = (resp.body as Record) || {} + return result + } catch (error) { + console.error('Error fetching specialist detail:', error) + throw new Error('Failed to get specialist detail') + } +} + +export async function postSpecialist(record: any) { + try { + const resp = await xfetch(mainUrl, 'POST', record) + const result: any = {} + result.success = resp.success + result.body = (resp.body as Record) || {} + return result + } catch (error) { + console.error('Error posting specialist:', error) + throw new Error('Failed to post specialist') + } +} + +export async function patchSpecialist(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) || {} + return result + } catch (error) { + console.error('Error putting specialist:', error) + throw new Error('Failed to put specialist') + } +} + +export async function removeSpecialist(id: number | string) { + try { + const resp = await xfetch(`${mainUrl}/${id}`, 'DELETE') + const result: any = {} + result.success = resp.success + result.body = (resp.body as Record) || {} + return result + } catch (error) { + console.error('Error deleting record:', error) + throw new Error('Failed to delete specialist') + } +}