import 'dart:convert';
import 'package:http/http.dart' as http;
import '../dataObjects/delivery.dart';
import 'package:project_manager/consts.dart';

/// Utility method that fetches all deliveries from the back-end.
///
/// Returns a list of deliveries in the same order as saved in the database.
Future<List<Delivery>> fetchDeliveries() async {
  const apiUrl = '$URI/deliveries';
  print(apiUrl);
  final response = await http.get(Uri.parse(apiUrl), headers: {"Content-Type": "application/json"});
  final List body = json.decode(response.body);
  return body.map((e) => Delivery.fromJson(e)).toList();}

Future<Delivery> fetchDelivery(String id) async {
  String apiUrl = '$URI/deliveries/$id';
  final response = await http.get(Uri.parse(apiUrl), headers: {"Content-Type": "application/json"});
  return Delivery.fromJson(json.decode(response.body));}

