categoty_controller.dart 2.37 KB
Newer Older
yenisleydi committed
1
import 'dart:convert'; // Para convertir datos a JSON
yenisleydi committed
2 3 4 5 6
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:primer_practica/src/http_api/category_service.dart';
import 'package:primer_practica/src/models/categoryModel.dart';

class CategoryController {
yenisleydi committed
7
  final Connectivity _connectivity = Connectivity();
yenisleydi committed
8

yenisleydi committed
9
  // Método para obtener las categorías
yenisleydi committed
10 11 12 13 14 15 16
  Future<Map<String, dynamic>> getCategories() async {
    Map<String, dynamic> mapResp = {
      'ok': false,
      'message': 'No hay categorias',
      'data': null
    };

yenisleydi committed
17
    ConnectivityResult connectivityResult = (await _connectivity.checkConnectivity());
yenisleydi committed
18 19 20

    if (connectivityResult != ConnectivityResult.none) {
      if (connectivityResult == ConnectivityResult.wifi || connectivityResult == ConnectivityResult.mobile) {
yenisleydi committed
21 22
        CategoryService categoryApi = CategoryService();
        Map<String, dynamic> respGet = await categoryApi.getCategories();
yenisleydi committed
23 24 25

        if (respGet['statusCode'] == 200) {
          try {
yenisleydi committed
26 27 28 29 30
            var decodeResp = json.decode(respGet['body']);
            List<CategoryModel> listCategories = CategoryModel.fromJsonArray(decodeResp['data']);
            mapResp['ok'] = true;
            mapResp['message'] = "${listCategories.length} categorías encontradas";
            mapResp['data'] = listCategories;
yenisleydi committed
31
          } catch (e) {
yenisleydi committed
32
            mapResp['message'] = "Error en decodificación: $e";
yenisleydi committed
33 34
          }
        } else {
yenisleydi committed
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
          mapResp['message'] = "${respGet['body']}";
        }
      }
    }

    return mapResp;
  }

  // Método para agregar una categoría
  Future<Map<String, dynamic>> addCategory(CategoryModel category) async {
    Map<String, dynamic> mapResp = {
      'ok': false,
      'message': 'No se pudo agregar la categoría'
    };

    ConnectivityResult connectivityResult = (await _connectivity.checkConnectivity());

    if (connectivityResult != ConnectivityResult.none) {
      if (connectivityResult == ConnectivityResult.wifi || connectivityResult == ConnectivityResult.mobile) {
        CategoryService categoryApi = CategoryService();
        Map<String, dynamic> respPost = await categoryApi.postCategory(category);

        if (respPost['statusCode'] == 201) {
          mapResp['ok'] = true;
          mapResp['message'] = 'Categoría agregada exitosamente';
        } else {
          mapResp['message'] = "${respPost['body']}";
yenisleydi committed
62 63 64 65
        }
      }
    }

yenisleydi committed
66
    return mapResp;
yenisleydi committed
67 68
  }
}