09a641e5cf
| Numéro du ticket | Titre du ticket | |------------------|-----------------| | #356 | modification front page admin bovin | ## Description de la PR ## Modification du .env ## Check list - [x] Pas de régression - [ ] TU/TI/TF rédigée - [x] TU/TI/TF OK - [x] CHANGELOG modifié Reviewed-on: https://gitea.malio.fr/MALIO-DEV/Ferme/pulls/37 Reviewed-by: Autin <tristan@yuno.malio.fr> Co-authored-by: sroy <sebastien@yuno.malio.fr> Co-committed-by: sroy <sebastien@yuno.malio.fr>
61 lines
2.0 KiB
TypeScript
61 lines
2.0 KiB
TypeScript
import { useApi } from '~/composables/useApi'
|
|
import type { BovineTypeData, BovinPayload } from "~/services/dto/bovine-type-data";
|
|
|
|
export type BovineTypeListResponse =
|
|
| BovineTypeData[]
|
|
| { 'hydra:member'?: BovineTypeData[] }
|
|
|
|
export async function getBovineTypeList(): Promise<BovineTypeData[]> {
|
|
const api = useApi()
|
|
const response = await api.get<BovineTypeListResponse>('bovine_types', {}, {
|
|
toastErrorKey: 'errors.bovin.list'
|
|
})
|
|
|
|
if (Array.isArray(response)) {
|
|
return response.map(mapToBovineTypeData)
|
|
}
|
|
|
|
if (response && typeof response === 'object' && Array.isArray(response['hydra:member'])) {
|
|
return response['hydra:member'].map(mapToBovineTypeData)
|
|
}
|
|
|
|
return []
|
|
}
|
|
|
|
export async function getBovin(id: number): Promise<BovineTypeData> {
|
|
const api = useApi()
|
|
const response = await api.get<BovineTypeData>(`bovine_types/${id}`, {}, {
|
|
toastErrorKey: 'errors.bovin.fetch'
|
|
})
|
|
return mapToBovineTypeData(response)
|
|
}
|
|
|
|
export async function createBovin(payload: BovinPayload = {}): Promise<BovineTypeData> {
|
|
const api = useApi()
|
|
const response = await api.post<BovineTypeData>('bovine_types', toBovineTypePayload(payload), {
|
|
toastErrorKey: 'errors.bovin.create',
|
|
toastSuccessKey: 'success.bovin.create'
|
|
})
|
|
return mapToBovineTypeData(response)
|
|
}
|
|
|
|
export async function updateBovin(id: number, payload: BovinPayload = {}): Promise<BovineTypeData> {
|
|
const api = useApi()
|
|
const response = await api.patch<BovineTypeData>(`bovine_types/${id}`, toBovineTypePayload(payload), {
|
|
toastErrorKey: 'errors.bovin.update',
|
|
toastSuccessKey: 'success.bovin.update'
|
|
})
|
|
return mapToBovineTypeData(response)
|
|
}
|
|
|
|
const mapToBovineTypeData = (item: BovineTypeData): BovineTypeData => ({
|
|
id: item.id,
|
|
label: item.label,
|
|
code: item.code
|
|
})
|
|
|
|
const toBovineTypePayload = (payload: BovinPayload): Partial<BovineTypeData> => ({
|
|
label: payload.label ?? undefined,
|
|
code: payload.code ?? undefined
|
|
})
|