説明なし

Services.dart 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import 'dart:convert';
  2. import 'package:fast_med_flutter/widgets/DataTableMySqlDemo/Horario.dart';
  3. import 'package:fast_med_flutter/widgets/DataTableMySqlDemo/Especialista.dart';
  4. import 'package:http/http.dart'
  5. as http;
  6. import 'Oficina.dart';
  7. class Services {
  8. static const URL_GET_ALL = 'http://ada.uprrp.edu/~oniel.mendez2/json/getOficinas.php';
  9. static const URL_OFICINA_GET_BY_ID = 'https://ada.uprrp.edu/~oniel.mendez2/json/getOficinaByID.php';
  10. static const URL_HORARIO = 'https://ada.uprrp.edu/~oniel.mendez2/json/getHorario.php';
  11. static const URL_ESPECIALISTA = 'https://ada.uprrp.edu/~oniel.mendez2/json/getEspecialista.php';
  12. //static const _GET_ALL = "GET_ALL";
  13. //get all oficinas
  14. static Future<List<Oficina>> getOficinas() async {
  15. try {
  16. final response = await http.get(URL_GET_ALL);
  17. if (200 == response.statusCode) {
  18. final parsed = json.decode(response.body).cast<Map<String, dynamic>>();
  19. List<Oficina> list = parsed.map<Oficina>((json) => Oficina.allFromJson(json)).toList();
  20. return list;
  21. } else {
  22. final response = '[{"id":"1","name":"error."},{"id":"2","name":"el codigo"},{"id":"3","name":"!=200"}]';
  23. final parsed = json.decode(response).cast<Map<String, dynamic>>();
  24. List<Oficina> list = parsed.map<Oficina>((json) => Oficina.allFromJson(json)).toList();
  25. return list; // return an empty list on exception/error
  26. }
  27. } catch (e) {
  28. final response = '[{"id":"1","name":"oficina 1,"telephone":"787","address":"Carretera ##","email":"email@domain.com""},'
  29. '{"id":"2","name":"oficina 2","telephone":"787","address":"Carretera ##","email":"email@domain.com"},'
  30. '{"id":"3","name":"oficina 3","telephone":"787","address":"Carretera ##","email":"email@domain.com"}]';
  31. final parsed = json.decode(response).cast<Map<String, dynamic>>();
  32. List<Oficina> list = parsed.map<Oficina>((json) => Oficina.allFromJson(json)).toList();
  33. return list; // return an empty list on exception/error
  34. }
  35. }
  36. static Future<List<Oficina>> getOficina(id) async {
  37. try {
  38. final response = await http.post(URL_OFICINA_GET_BY_ID, body: {'id': id});
  39. if (200 == response.statusCode) {
  40. final parsed = json.decode(response.body).cast<Map<String, dynamic>>();
  41. List<Oficina> list = parsed.map<Oficina>((json) => Oficina.allFromJson(json)).toList();
  42. return list;
  43. } else {
  44. final response = '[{"id":"1","name":"error.","telephone":"787","address":"Carretera ##","email":"email@domain.com"}]';
  45. final parsed = json.decode(response).cast<Map<String, dynamic>>();
  46. List<Oficina> list = parsed.map<Oficina>((json) => Oficina.allFromJson(json)).toList();
  47. return list; // return an empty list on exception/error
  48. }
  49. } catch (e) {
  50. final response = '[{"id":"1","name":"oficina 1","telephone":"787","address":"Carretera ##","email":"email@domain.com"}]';
  51. final parsed = json.decode(response).cast<Map<String, dynamic>>();
  52. List<Oficina> list = parsed.map<Oficina>((json) => Oficina.allFromJson(json)).toList();
  53. return list; // return an empty list on exception/error
  54. }
  55. }
  56. //para conseguir el horario de una oficina
  57. static Future<List<Horario>> getHorario(id) async {
  58. try {
  59. final response = await http.post(URL_HORARIO, body: {'id': id});
  60. if (200 == response.statusCode && response != null) {
  61. final parsed = json.decode(response.body).cast<Map<String, dynamic>>();
  62. List<Horario> list = parsed.map<Horario>((json) => Horario.allFromJson(json)).toList();
  63. return list;
  64. } else {
  65. final response = '[{"id":"1","name":"error.","telephone":"787","address":"Carretera ##","email":"email@domain.com"}]';
  66. final parsed = json.decode(response).cast<Map<String, dynamic>>();
  67. List<Horario> list = parsed.map<Horario>((json) => Horario.allFromJson(json)).toList();
  68. return list; // return an empty list on exception/error
  69. }
  70. } catch (e) {
  71. final response = '[{"id":"1","name":"oficina 1","telephone":"787","address":"Carretera ##","email":"email@domain.com"}]';
  72. final parsed = json.decode(response).cast<Map<String, dynamic>>();
  73. List<Horario> list = parsed.map<Horario>((json) => Horario.allFromJson(json)).toList();
  74. return list; // return an empty list on exception/error
  75. }
  76. }
  77. //para conseguir especialistas que trabajen en una oficina
  78. static Future<List<Especialista>> getEspecialista(id) async {
  79. try {
  80. final response = await http.post(URL_ESPECIALISTA, body: {'id': id});
  81. if (200 == response.statusCode && response != null) {
  82. final parsed = json.decode(response.body).cast<Map<String, dynamic>>();
  83. List<Especialista> list = parsed.map<Especialista>((json) => Especialista.allFromJson(json)).toList();
  84. return list;
  85. } else {
  86. final response = '[{"id":"1","name":"error.","telephone":"787","address":"Carretera ##","email":"email@domain.com"}]';
  87. final parsed = json.decode(response).cast<Map<String, dynamic>>();
  88. List<Especialista> list = parsed.map<Especialista>((json) => Especialista.allFromJson(json)).toList();
  89. return list; // return an empty list on exception/error
  90. }
  91. } catch (e) {
  92. final response = '[{"id":"1","name":"oficina 1","telephone":"787","address":"Carretera ##","email":"email@domain.com"}]';
  93. final parsed = json.decode(response).cast<Map<String, dynamic>>();
  94. List<Especialista> list = parsed.map<Especialista>((json) => Especialista.allFromJson(json)).toList();
  95. return list; // return an empty list on exception/error
  96. }
  97. }
  98. }