CategoriaController.dart 3.3 KB
Newer Older
nayeli92433 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
import 'dart:convert';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:miapp_flutter/src/models/CategoriaModel.dart';

import '../http_api/CategoryApi.dart';

class CategoryController {
  final Connectivity _connectivity = Connectivity();
  final CategoryApi _categoryApi = CategoryApi();

  Future<Map<String, dynamic>> getCategories() async {
    Map<String, dynamic> mapResp = {
      'ok': false,
      'message': 'No hay categorias',
      'data': null
    };

    List<ConnectivityResult> connectivityResult = await _connectivity.checkConnectivity();
    if (!connectivityResult.contains(ConnectivityResult.none)) {
      if (connectivityResult.contains(ConnectivityResult.wifi) || connectivityResult.contains(ConnectivityResult.mobile)) {
        try {
          Map<String, dynamic> respGet = await _categoryApi.getCategories();
          if (respGet['statusCode'] == 200) {
            try {
              var decodeResp = json.decode(respGet['body']);
nayeli92433 committed
26
              List<CategoriaModel> listCategories = CategoriaModel.fromJsonArray(decodeResp['data']);
nayeli92433 committed
27
              mapResp['ok'] = true;
nayeli92433 committed
28
              mapResp['message'] = "${listCategories.length} categorías encontradas";
nayeli92433 committed
29 30 31 32 33
              mapResp['data'] = listCategories;
            } catch (e) {
              mapResp['message'] = "Error en procesamiento de datos: $e";
            }
          } else {
nayeli92433 committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
            mapResp['message'] = "Error en la respuesta de la API: ${respGet['body']}";
          }
        } catch (e) {
          mapResp['message'] = "Error en la solicitud a la API: $e";
        }
      } else {
        mapResp['message'] = 'No hay conexión a internet';
      }
    } else {
      mapResp['message'] = 'No hay conexión a internet';
    }

    return mapResp;
  }

  // Método para crear una categoría
  Future<Map<String, dynamic>> createCategory(CategoriaModel categoria) async {
    Map<String, dynamic> mapResp = {
      'ok': false,
      'message': 'No se pudo crear la categoría',
      'data': null
    };

    List<ConnectivityResult> connectivityResult = await _connectivity.checkConnectivity();
    if (!connectivityResult.contains(ConnectivityResult.none)) {
      if (connectivityResult.contains(ConnectivityResult.wifi) || connectivityResult.contains(ConnectivityResult.mobile)) {

        try {

          Map<String, dynamic> respPost = await _categoryApi.postCategory(categoria);


          if (respPost['statusCode'] == 200 || respPost['statusCode'] == 201) {
            print('Regresa datos ');
            try {
              var decodeResp = json.decode(respPost['body']);
              CategoriaModel newCategory = CategoriaModel.fromJson(decodeResp['data']);
              mapResp['ok'] = true;
              mapResp['message'] = "Categoría creada exitosamente";
              mapResp['data'] = newCategory;
            } catch (e) {
              mapResp['message'] = "Error en procesamiento de datos: $e";
            }
          } else {
            mapResp['message'] = "Error en la respuesta de la API: ${respPost['body']}";
nayeli92433 committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92
          }
        } catch (e) {
          mapResp['message'] = "Error en la solicitud a la API: $e";
        }
      } else {
        mapResp['message'] = 'No hay conexión a internet';
      }
    } else {
      mapResp['message'] = 'No hay conexión a internet';
    }

    return mapResp;
  }
}