46 lines
1.7 KiB
TypeScript
46 lines
1.7 KiB
TypeScript
|
|
import apiClient from './apiClient';
|
||
|
|
import type { PlantaDto } from '../models/dtos/Impresion/PlantaDto';
|
||
|
|
import type { CreatePlantaDto } from '../models/dtos/Impresion/CreatePlantaDto';
|
||
|
|
import type { UpdatePlantaDto } from '../models/dtos/Impresion/UpdatePlantaDto';
|
||
|
|
|
||
|
|
const getAllPlantas = async (nombreFilter?: string, detalleFilter?: string): Promise<PlantaDto[]> => {
|
||
|
|
const params: Record<string, string> = {};
|
||
|
|
if (nombreFilter) params.nombre = nombreFilter;
|
||
|
|
if (detalleFilter) params.detalle = detalleFilter; // La API debe soportar esto
|
||
|
|
|
||
|
|
// Llama a GET /api/plantas
|
||
|
|
const response = await apiClient.get<PlantaDto[]>('/plantas', { params });
|
||
|
|
return response.data;
|
||
|
|
};
|
||
|
|
|
||
|
|
const getPlantaById = async (id: number): Promise<PlantaDto> => {
|
||
|
|
// Llama a GET /api/plantas/{id}
|
||
|
|
const response = await apiClient.get<PlantaDto>(`/plantas/${id}`);
|
||
|
|
return response.data;
|
||
|
|
};
|
||
|
|
|
||
|
|
const createPlanta = async (data: CreatePlantaDto): Promise<PlantaDto> => {
|
||
|
|
// Llama a POST /api/plantas
|
||
|
|
const response = await apiClient.post<PlantaDto>('/plantas', data);
|
||
|
|
return response.data; // La API devuelve el objeto creado (201 Created)
|
||
|
|
};
|
||
|
|
|
||
|
|
const updatePlanta = async (id: number, data: UpdatePlantaDto): Promise<void> => {
|
||
|
|
// Llama a PUT /api/plantas/{id} (204 No Content en éxito)
|
||
|
|
await apiClient.put(`/plantas/${id}`, data);
|
||
|
|
};
|
||
|
|
|
||
|
|
const deletePlanta = async (id: number): Promise<void> => {
|
||
|
|
// Llama a DELETE /api/plantas/{id} (204 No Content en éxito)
|
||
|
|
await apiClient.delete(`/plantas/${id}`);
|
||
|
|
};
|
||
|
|
|
||
|
|
const plantaService = {
|
||
|
|
getAllPlantas,
|
||
|
|
getPlantaById,
|
||
|
|
createPlanta,
|
||
|
|
updatePlanta,
|
||
|
|
deletePlanta,
|
||
|
|
};
|
||
|
|
|
||
|
|
export default plantaService;
|