import 'dart:convert'; import 'package:fast_med_flutter/widgets/DataTableMySqlDemo/Horario.dart'; import 'package:fast_med_flutter/widgets/DataTableMySqlDemo/Especialista.dart'; import 'package:http/http.dart' as http; import 'Oficina.dart'; class Services { static const URL_GET_ALL = 'http://ada.uprrp.edu/~oniel.mendez2/json/getOficinas.php'; static const URL_OFICINA_GET_BY_ID = 'https://ada.uprrp.edu/~oniel.mendez2/json/getOficinaByID.php'; static const URL_HORARIO = 'https://ada.uprrp.edu/~oniel.mendez2/json/getHorario.php'; static const URL_ESPECIALISTA = 'https://ada.uprrp.edu/~oniel.mendez2/json/getEspecialista.php'; //static const _GET_ALL = "GET_ALL"; //get all oficinas static Future> getOficinas() async { try { final response = await http.get(URL_GET_ALL); if (200 == response.statusCode) { final parsed = json.decode(response.body).cast>(); List list = parsed.map((json) => Oficina.allFromJson(json)).toList(); return list; } else { final response = '[{"id":"1","name":"error."},{"id":"2","name":"el codigo"},{"id":"3","name":"!=200"}]'; final parsed = json.decode(response).cast>(); List list = parsed.map((json) => Oficina.allFromJson(json)).toList(); return list; // return an empty list on exception/error } } catch (e) { final response = '[{"id":"1","name":"oficina 1,"telephone":"787","address":"Carretera ##","email":"email@domain.com""},' '{"id":"2","name":"oficina 2","telephone":"787","address":"Carretera ##","email":"email@domain.com"},' '{"id":"3","name":"oficina 3","telephone":"787","address":"Carretera ##","email":"email@domain.com"}]'; final parsed = json.decode(response).cast>(); List list = parsed.map((json) => Oficina.allFromJson(json)).toList(); return list; // return an empty list on exception/error } } static Future> getOficina(id) async { try { final response = await http.post(URL_OFICINA_GET_BY_ID, body: {'id': id}); if (200 == response.statusCode) { final parsed = json.decode(response.body).cast>(); List list = parsed.map((json) => Oficina.allFromJson(json)).toList(); return list; } else { final response = '[{"id":"1","name":"error.","telephone":"787","address":"Carretera ##","email":"email@domain.com"}]'; final parsed = json.decode(response).cast>(); List list = parsed.map((json) => Oficina.allFromJson(json)).toList(); return list; // return an empty list on exception/error } } catch (e) { final response = '[{"id":"1","name":"oficina 1","telephone":"787","address":"Carretera ##","email":"email@domain.com"}]'; final parsed = json.decode(response).cast>(); List list = parsed.map((json) => Oficina.allFromJson(json)).toList(); return list; // return an empty list on exception/error } } //para conseguir el horario de una oficina static Future> getHorario(id) async { try { final response = await http.post(URL_HORARIO, body: {'id': id}); if (200 == response.statusCode && response != null) { final parsed = json.decode(response.body).cast>(); List list = parsed.map((json) => Horario.allFromJson(json)).toList(); return list; } else { final response = '[{"id":"1","name":"error.","telephone":"787","address":"Carretera ##","email":"email@domain.com"}]'; final parsed = json.decode(response).cast>(); List list = parsed.map((json) => Horario.allFromJson(json)).toList(); return list; // return an empty list on exception/error } } catch (e) { final response = '[{"id":"1","name":"oficina 1","telephone":"787","address":"Carretera ##","email":"email@domain.com"}]'; final parsed = json.decode(response).cast>(); List list = parsed.map((json) => Horario.allFromJson(json)).toList(); return list; // return an empty list on exception/error } } //para conseguir especialistas que trabajen en una oficina static Future> getEspecialista(id) async { try { final response = await http.post(URL_ESPECIALISTA, body: {'id': id}); if (200 == response.statusCode && response != null) { final parsed = json.decode(response.body).cast>(); List list = parsed.map((json) => Especialista.allFromJson(json)).toList(); return list; } else { final response = '[{"id":"1","name":"error.","telephone":"787","address":"Carretera ##","email":"email@domain.com"}]'; final parsed = json.decode(response).cast>(); List list = parsed.map((json) => Especialista.allFromJson(json)).toList(); return list; // return an empty list on exception/error } } catch (e) { final response = '[{"id":"1","name":"oficina 1","telephone":"787","address":"Carretera ##","email":"email@domain.com"}]'; final parsed = json.decode(response).cast>(); List list = parsed.map((json) => Especialista.allFromJson(json)).toList(); return list; // return an empty list on exception/error } } }