first commit
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
class PatientSearchItemModel {
|
||||
final int id;
|
||||
final String nombre;
|
||||
final String apellido;
|
||||
final String cedula;
|
||||
final String telefono;
|
||||
final String edad;
|
||||
final String sexo;
|
||||
final String fecUltcon;
|
||||
final String seguro;
|
||||
final String motCons;
|
||||
|
||||
const PatientSearchItemModel({
|
||||
required this.id,
|
||||
required this.nombre,
|
||||
required this.apellido,
|
||||
required this.cedula,
|
||||
required this.telefono,
|
||||
required this.edad,
|
||||
required this.sexo,
|
||||
required this.fecUltcon,
|
||||
required this.seguro,
|
||||
required this.motCons,
|
||||
});
|
||||
|
||||
String get fullName => '$nombre $apellido'.trim();
|
||||
|
||||
factory PatientSearchItemModel.fromJson(Map<String, dynamic> json) {
|
||||
int parsedId = 0;
|
||||
if (json['id'] is int) {
|
||||
parsedId = json['id'] as int;
|
||||
} else if (json['id'] != null) {
|
||||
parsedId = int.tryParse(json['id'].toString()) ?? 0;
|
||||
}
|
||||
|
||||
return PatientSearchItemModel(
|
||||
id: parsedId,
|
||||
nombre: json['nombre']?.toString().trim() ?? '',
|
||||
apellido: json['apellido']?.toString().trim() ?? '',
|
||||
cedula: json['cedula']?.toString().trim() ?? '',
|
||||
telefono: json['telefono']?.toString().trim() ?? '',
|
||||
edad: json['edad']?.toString().trim() ?? '',
|
||||
sexo: json['sexo']?.toString().trim() ?? '',
|
||||
fecUltcon: json['fec_ultcon']?.toString().trim() ?? '',
|
||||
seguro: json['seguro']?.toString().trim() ?? '',
|
||||
motCons: json['mot_cons']?.toString().trim() ?? '',
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'id': id,
|
||||
'nombre': nombre,
|
||||
'apellido': apellido,
|
||||
'cedula': cedula,
|
||||
'telefono': telefono,
|
||||
'edad': edad,
|
||||
'sexo': sexo,
|
||||
'fec_ultcon': fecUltcon,
|
||||
'seguro': seguro,
|
||||
'mot_cons': motCons,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
import '../../../../core/network/api_client.dart';
|
||||
import '../models/patient_search_item_model.dart';
|
||||
|
||||
class PatientSearchService {
|
||||
final ApiClient apiClient;
|
||||
|
||||
PatientSearchService({ApiClient? client}) : apiClient = client ?? ApiClient();
|
||||
|
||||
Future<List<PatientSearchItemModel>> searchPatients(String rawQuery, {int limit = 25}) async {
|
||||
final cleanQuery = rawQuery.trim();
|
||||
if (cleanQuery.isEmpty) return [];
|
||||
|
||||
try {
|
||||
final words = cleanQuery.split(RegExp(r'\s+')).where((w) => w.isNotEmpty).toList();
|
||||
final queryParams = <String>[
|
||||
'limit=$limit',
|
||||
'sort=-id',
|
||||
'fields=id,nombre,apellido,cedula,telefono,edad,sexo,fec_ultcon,seguro,mot_cons',
|
||||
];
|
||||
|
||||
if (words.length == 1) {
|
||||
final w = Uri.encodeQueryComponent(words.first);
|
||||
queryParams.add('filter[_or][0][nombre][_icontains]=$w');
|
||||
queryParams.add('filter[_or][1][apellido][_icontains]=$w');
|
||||
queryParams.add('filter[_or][2][cedula][_icontains]=$w');
|
||||
queryParams.add('filter[_or][3][telefono][_icontains]=$w');
|
||||
if (int.tryParse(words.first) != null) {
|
||||
queryParams.add('filter[_or][4][id][_eq]=${words.first}');
|
||||
}
|
||||
} else {
|
||||
for (var i = 0; i < words.length; i++) {
|
||||
final w = Uri.encodeQueryComponent(words[i]);
|
||||
queryParams.add('filter[_and][$i][_or][0][nombre][_icontains]=$w');
|
||||
queryParams.add('filter[_and][$i][_or][1][apellido][_icontains]=$w');
|
||||
}
|
||||
}
|
||||
|
||||
final url = '/items/pacientes?${queryParams.join('&')}';
|
||||
final response = await apiClient.dio.get(url);
|
||||
|
||||
if (response.statusCode == 200 && response.data != null) {
|
||||
final data = response.data['data'] as List<dynamic>? ?? [];
|
||||
return data
|
||||
.map((item) => PatientSearchItemModel.fromJson(item as Map<String, dynamic>))
|
||||
.toList();
|
||||
}
|
||||
return [];
|
||||
} catch (e) {
|
||||
throw Exception('Error al consultar pacientes en Directus: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user