producto_service.dart 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import 'package:yoshi_papas_app/data/api_response.dart';
  2. import 'package:yoshi_papas_app/models/models.dart';
  3. import '../services/base_service.dart';
  4. class ProductoService extends BaseService {
  5. String endPoint = '/admin/categoria';
  6. //Consulta de la lista de Sala
  7. Map<String, String> defaultQueryParameters = {
  8. "limite": "10",
  9. "ordenar": "id-desc",
  10. "pagina": "1"
  11. };
  12. Future<List<Producto>> fetchList() async {
  13. final response =
  14. await get(endPoint, queryParameters: defaultQueryParameters);
  15. final respuesta = ApiResponse(response);
  16. List<Producto> productos = [];
  17. if (respuesta.isOk && respuesta.resultados!.isNotEmpty) {
  18. for (var x in respuesta.resultados!) {
  19. final producto = Producto.fromJson(x);
  20. productos.add(producto);
  21. }
  22. }
  23. return productos;
  24. }
  25. Future<ApiResponse> postProducto({
  26. required String nombre,
  27. required String descripcion,
  28. String? descuento,
  29. required int existencia,
  30. required int idCategoria,
  31. required String precio,
  32. String? venceDescuento,
  33. }) async {
  34. var response = await post(
  35. '$endPoint/guardar',
  36. body: {
  37. 'descripcion': descripcion,
  38. 'nombre': nombre,
  39. 'descuento': descuento,
  40. 'existencia': existencia,
  41. 'idCategoria': idCategoria,
  42. 'precio': precio,
  43. 'venceDescuento': venceDescuento,
  44. },
  45. withAuth: true,
  46. );
  47. return ApiResponse(response);
  48. }
  49. Future<ApiResponse> editProducto({
  50. required int id,
  51. required String nombre,
  52. required String descripcion,
  53. String? descuento,
  54. required int existencia,
  55. required int idCategoria,
  56. required String precio,
  57. String? venceDescuento,
  58. }) async {
  59. var response = await post(
  60. endPoint,
  61. body: {
  62. 'id': id,
  63. 'descripcion': descripcion,
  64. 'nombre': nombre,
  65. 'descuento': descuento,
  66. 'existencia': existencia,
  67. 'idCategoria': idCategoria,
  68. 'precio': precio,
  69. 'venceDescuento': venceDescuento,
  70. },
  71. withAuth: true,
  72. );
  73. return ApiResponse(response);
  74. }
  75. Future<ApiResponse> deleteProducto({required int id}) async {
  76. var response = await delete(
  77. '$endPoint/eliminar',
  78. body: {
  79. 'id': id,
  80. },
  81. withAuth: true,
  82. );
  83. return ApiResponse(response);
  84. }
  85. }