2026-09-21 16:33:51 -04:00
|
|
|
import 'package:dio/dio.dart';
|
|
|
|
|
import '../../../../core/network/api_client.dart';
|
|
|
|
|
import '../models/patient_model.dart';
|
|
|
|
|
|
|
|
|
|
class PatientService {
|
|
|
|
|
final ApiClient apiClient;
|
|
|
|
|
|
|
|
|
|
PatientService({ApiClient? client}) : apiClient = client ?? ApiClient();
|
|
|
|
|
|
|
|
|
|
Future<PatientModel> getPatientById(int id) async {
|
|
|
|
|
try {
|
|
|
|
|
final response = await apiClient.dio.get('/items/pacientes/$id');
|
|
|
|
|
if (response.statusCode == 200 && response.data != null) {
|
|
|
|
|
final data = response.data['data'] as Map<String, dynamic>;
|
|
|
|
|
return PatientModel.fromJson(data);
|
|
|
|
|
}
|
|
|
|
|
throw Exception('No se encontró el paciente con ID $id');
|
|
|
|
|
} on DioException catch (e) {
|
|
|
|
|
final serverMessage = e.response?.data?['errors']?[0]?['message'] ?? e.message;
|
|
|
|
|
throw Exception('Error al obtener datos del paciente: $serverMessage');
|
|
|
|
|
} catch (e) {
|
|
|
|
|
throw Exception('Error al obtener datos del paciente: $e');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<PatientModel> createPatient(PatientModel model) async {
|
|
|
|
|
try {
|
|
|
|
|
final payload = model.toJson(includeId: false);
|
|
|
|
|
final response = await apiClient.dio.post(
|
|
|
|
|
'/items/pacientes',
|
|
|
|
|
data: payload,
|
|
|
|
|
);
|
|
|
|
|
if ((response.statusCode == 200 || response.statusCode == 201) && response.data != null) {
|
|
|
|
|
final data = response.data['data'] as Map<String, dynamic>;
|
|
|
|
|
return PatientModel.fromJson(data);
|
|
|
|
|
}
|
|
|
|
|
throw Exception('Error en el servidor al registrar el paciente');
|
|
|
|
|
} on DioException catch (e) {
|
|
|
|
|
final serverMessage = e.response?.data?['errors']?[0]?['message'] ?? e.message;
|
|
|
|
|
throw Exception('Error al registrar paciente: $serverMessage');
|
|
|
|
|
} catch (e) {
|
|
|
|
|
throw Exception('Error al registrar paciente: $e');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<PatientModel> updatePatient(int id, Map<String, dynamic> updates) async {
|
|
|
|
|
try {
|
|
|
|
|
final response = await apiClient.dio.patch(
|
|
|
|
|
'/items/pacientes/$id',
|
|
|
|
|
data: updates,
|
|
|
|
|
);
|
|
|
|
|
if (response.statusCode == 200 && response.data != null) {
|
|
|
|
|
final data = response.data['data'] as Map<String, dynamic>;
|
|
|
|
|
return PatientModel.fromJson(data);
|
|
|
|
|
}
|
|
|
|
|
throw Exception('Error en el servidor al actualizar el paciente');
|
|
|
|
|
} on DioException catch (e) {
|
|
|
|
|
final serverMessage = e.response?.data?['errors']?[0]?['message'] ?? e.message;
|
|
|
|
|
throw Exception('Error al actualizar paciente: $serverMessage');
|
|
|
|
|
} catch (e) {
|
|
|
|
|
throw Exception('Error al actualizar paciente: $e');
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-09-22 13:41:14 -04:00
|
|
|
|
|
|
|
|
Future<void> deletePatient(int id) async {
|
|
|
|
|
try {
|
|
|
|
|
final response = await apiClient.dio.delete('/items/pacientes/$id');
|
|
|
|
|
if (response.statusCode != 200 && response.statusCode != 204) {
|
|
|
|
|
throw Exception('Error en el servidor al eliminar el paciente');
|
|
|
|
|
}
|
|
|
|
|
} on DioException catch (e) {
|
|
|
|
|
final serverMessage = e.response?.data?['errors']?[0]?['message'] ?? e.message;
|
|
|
|
|
throw Exception('Error al eliminar paciente: $serverMessage');
|
|
|
|
|
} catch (e) {
|
|
|
|
|
throw Exception('Error al eliminar paciente: $e');
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-09-21 16:33:51 -04:00
|
|
|
}
|