|
| 1 | +// ignore_for_file: avoid_print |
| 2 | + |
| 3 | +import 'package:cloud_firestore/cloud_firestore.dart'; |
| 4 | +import 'package:eco_bites/core/utils/distance.dart'; |
| 5 | +import 'package:eco_bites/features/address/domain/models/address.dart'; |
| 6 | +import 'package:eco_bites/features/food/domain/models/cuisine_type.dart'; |
| 7 | +import 'package:eco_bites/features/food/domain/models/food_business.dart'; |
| 8 | +import 'package:eco_bites/features/food/domain/models/offer.dart'; |
| 9 | + |
| 10 | +class FoodBusinessRepository { |
| 11 | + final FirebaseFirestore _firestore = FirebaseFirestore.instance; |
| 12 | + |
| 13 | + Future<List<FoodBusiness>> fetchNearbySurplusFoodBusinesses({ |
| 14 | + required Address userLocation, |
| 15 | + required CuisineType favoriteCuisine, |
| 16 | + double distanceInKm = 5.0, // Default search radius |
| 17 | + }) async { |
| 18 | + try { |
| 19 | + // Fetch restaurants with the favorite cuisine |
| 20 | + print('==> favoriteCuisine: $favoriteCuisine'); |
| 21 | + // favorite cuisine name |
| 22 | + print('==> favoriteCuisine: ${favoriteCuisine.name}'); |
| 23 | + print('==> userLocation: $userLocation'); |
| 24 | + print('==> distanceInKm: $distanceInKm'); |
| 25 | + print('==> fetching nearby surplus food businesses'); |
| 26 | + final QuerySnapshot<Map<String, dynamic>> foodBusinessSnapshot = |
| 27 | + await _firestore |
| 28 | + .collection('foddBusiness') |
| 29 | + .where('cuisineType', isEqualTo: favoriteCuisine.name) |
| 30 | + .get(); |
| 31 | + |
| 32 | + print('==> foodBusinessSnapshot: ${foodBusinessSnapshot.docs}'); |
| 33 | + |
| 34 | + final List<FoodBusiness> nearbyFoodBusinesses = <FoodBusiness>[]; |
| 35 | + |
| 36 | + for (final QueryDocumentSnapshot<Map<String, dynamic>> doc |
| 37 | + in foodBusinessSnapshot.docs) { |
| 38 | + final QuerySnapshot<Map<String, dynamic>> offersSnapshot = |
| 39 | + await _firestore |
| 40 | + .collection('foddBusiness') |
| 41 | + .doc(doc.id) |
| 42 | + .collection('offers') |
| 43 | + .where('availableQuantity', isGreaterThan: 0) |
| 44 | + .where('validUntil', isGreaterThanOrEqualTo: Timestamp.now()) |
| 45 | + .get(); |
| 46 | + |
| 47 | + print('==> query snapshot: $offersSnapshot'); |
| 48 | + print('==> offersSnapshot.docs: ${offersSnapshot.docs}'); |
| 49 | + |
| 50 | + final List<Offer> surplusOffers = offersSnapshot.docs |
| 51 | + .map((QueryDocumentSnapshot<Map<String, dynamic>> offerDoc) { |
| 52 | + return Offer.fromMap(offerDoc.data(), offerDoc.id, doc.id); |
| 53 | + }).toList(); |
| 54 | + |
| 55 | + if (surplusOffers.isNotEmpty) { |
| 56 | + final double distance = calculateDistance( |
| 57 | + userLocation.latitude, |
| 58 | + userLocation.longitude, |
| 59 | + (doc.data()['latitude'] as num?)?.toDouble() ?? 0.0, |
| 60 | + (doc.data()['longitude'] as num?)?.toDouble() ?? 0.0, |
| 61 | + ); |
| 62 | + |
| 63 | + if (distance <= distanceInKm) { |
| 64 | + final FoodBusiness foodBusiness = FoodBusiness( |
| 65 | + id: doc.id, |
| 66 | + name: doc.data()['name'] ?? '', |
| 67 | + cuisineType: CuisineTypeExtension.fromString( |
| 68 | + doc.data()['cuisineType'] ?? 'local', |
| 69 | + ), |
| 70 | + latitude: (doc.data()['latitude'] as num?)?.toDouble() ?? 0.0, |
| 71 | + longitude: (doc.data()['longitude'] as num?)?.toDouble() ?? 0.0, |
| 72 | + offers: surplusOffers, |
| 73 | + ); |
| 74 | + |
| 75 | + nearbyFoodBusinesses.add(foodBusiness); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return nearbyFoodBusinesses; |
| 81 | + } catch (e) { |
| 82 | + print('Error fetching nearby surplus food businesses: $e'); |
| 83 | + return <FoodBusiness>[]; |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments