54 lines
1.9 KiB
Dart
54 lines
1.9 KiB
Dart
import '../../../../core/network/api_client.dart';
|
|
import '../../../dashboard/data/models/doctor_info_model.dart';
|
|
|
|
class ConfigService {
|
|
final ApiClient apiClient;
|
|
|
|
ConfigService({ApiClient? client}) : apiClient = client ?? ApiClient();
|
|
|
|
/// Obtiene la configuración del consultorio desde Directus.
|
|
Future<DoctorInfoModel> getConfig() async {
|
|
try {
|
|
final response = await apiClient.dio.get('/items/configuracion');
|
|
if (response.statusCode == 200 && response.data != null) {
|
|
final data = response.data['data'];
|
|
if (data is Map<String, dynamic>) {
|
|
return DoctorInfoModel.fromJson(data);
|
|
} else if (data is List && data.isNotEmpty && data.first is Map<String, dynamic>) {
|
|
return DoctorInfoModel.fromJson(data.first as Map<String, dynamic>);
|
|
}
|
|
}
|
|
return const DoctorInfoModel(
|
|
nombreDoctor: 'Dr. Luis A. Castro Sosa',
|
|
especialidad: 'Cirujano Urólogo',
|
|
descripEspecialidad: '',
|
|
descripGeneral: '',
|
|
);
|
|
} catch (e) {
|
|
throw Exception('Error al cargar la configuración del consultorio: $e');
|
|
}
|
|
}
|
|
|
|
/// Actualiza la configuración del consultorio en Directus mediante PATCH.
|
|
Future<DoctorInfoModel> updateConfig(DoctorInfoModel model) async {
|
|
try {
|
|
final response = await apiClient.dio.patch(
|
|
'/items/configuracion',
|
|
data: model.toJson(includeId: false),
|
|
);
|
|
|
|
if (response.statusCode == 200 && response.data != null) {
|
|
final data = response.data['data'];
|
|
if (data is Map<String, dynamic>) {
|
|
return DoctorInfoModel.fromJson(data);
|
|
} else if (data is List && data.isNotEmpty && data.first is Map<String, dynamic>) {
|
|
return DoctorInfoModel.fromJson(data.first as Map<String, dynamic>);
|
|
}
|
|
}
|
|
return model;
|
|
} catch (e) {
|
|
throw Exception('Error al guardar la configuración: $e');
|
|
}
|
|
}
|
|
}
|