CategoryApi.dart 1.59 KB
Newer Older
nayeli92433 committed
1 2 3 4
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:flutter/foundation.dart';
import 'package:miapp_flutter/environments/archivo.dart';
nayeli92433 committed
5
import 'package:miapp_flutter/src/models/CategoriaModel.dart';
nayeli92433 committed
6 7

class CategoryApi {
nayeli92433 committed
8
  final String apiUrl = 'categoria'; // Asegúrate de que esta URL sea correcta
nayeli92433 committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

  Future<Map<String, dynamic>> getCategories() async {
    String url = '${apiApp}/$apiUrl?offset=0&max=100';
    if (kDebugMode) {
      print('Url -> $url');
    }
    try {
      final response = await http.get(Uri.parse(url));

      if (response.statusCode == 200) {
        return {
          'statusCode': response.statusCode,
          'body': response.body,
        };
      } else {
        return {
          'statusCode': response.statusCode,
          'body': 'Error: ${response.reasonPhrase}',
        };
      }
    } catch (e) {
      return {
        'statusCode': 501,
        'body': 'Error: $e',
      };
    }
  }
nayeli92433 committed
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


  Future<Map<String, dynamic>> postCategory(CategoriaModel categoria) async {
    String url = '$apiApp/$apiUrl';
    if (kDebugMode) {
      print('Url -> $url');
    }

    try {
      final response = await http.post(
        Uri.parse(url),
        headers: {'Content-Type': 'application/json'},
        body: json.encode(categoria.toJson()),
      );

      print('Status code: ${response.statusCode}');
      print('Response body: ${response.body}');

      return {
        "statusCode": response.statusCode,
        "body": response.body,
      };
    } catch (e) {
      return {
        "statusCode": 501,
        "body": '$e',
      };
    }
  }

nayeli92433 committed
66
}