first commit
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
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');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user