Files
TravelMate/lib/repositories/trip_repository.dart
Dayron c4588a65c0 Refactor signup page to use BLoC pattern and implement authentication repository
- Updated signup.dart to replace Provider with BLoC for state management.
- Created AuthRepository to handle authentication logic and Firestore user management.
- Added TripRepository and UserRepository for trip and user data management.
- Implemented methods for user sign-in, sign-up, and data retrieval in repositories.
- Enhanced trip management with create, update, delete, and participant management functionalities.
- Updated AuthService to include new methods for sign-in and sign-up.
- Removed unnecessary print statements from TripService for cleaner code.
- Added dependencies for flutter_bloc and equatable in pubspec.yaml.

Not tested yet
2025-10-14 10:53:28 +02:00

113 lines
3.2 KiB
Dart

import 'package:cloud_firestore/cloud_firestore.dart';
import '../data/models/trip.dart';
class TripRepository {
final FirebaseFirestore _firestore;
TripRepository({FirebaseFirestore? firestore})
: _firestore = firestore ?? FirebaseFirestore.instance;
// Créer un voyage
Future<Trip> createTrip(Trip trip) async {
try {
final docRef = await _firestore.collection('trips').add(trip.toMap());
final createdTrip = trip.copyWith(id: docRef.id);
// Mettre à jour avec l'ID généré
await docRef.update({'id': docRef.id});
return createdTrip;
} catch (e) {
throw Exception('Erreur lors de la création du voyage: $e');
}
}
// Récupérer les voyages d'un utilisateur
Stream<List<Trip>> getUserTrips(String userId) {
return _firestore
.collection('trips')
.where('createdBy', isEqualTo: userId)
.snapshots()
.map((snapshot) {
return snapshot.docs.map((doc) {
final data = doc.data();
return Trip.fromMap({...data, 'id': doc.id});
}).toList();
});
}
// Récupérer les voyages où l'utilisateur est participant
Stream<List<Trip>> getSharedTrips(String userId) {
return _firestore
.collection('trips')
.where('participants', arrayContains: userId)
.snapshots()
.map((snapshot) {
return snapshot.docs.map((doc) {
final data = doc.data();
return Trip.fromMap({...data, 'id': doc.id});
}).toList();
});
}
// Récupérer un voyage par ID
Future<Trip?> getTripById(String tripId) async {
try {
final doc = await _firestore.collection('trips').doc(tripId).get();
if (doc.exists) {
final data = doc.data() as Map<String, dynamic>;
return Trip.fromMap({...data, 'id': doc.id});
}
return null;
} catch (e) {
throw Exception('Erreur lors de la récupération du voyage: $e');
}
}
// Mettre à jour un voyage
Future<bool> updateTrip(Trip trip) async {
try {
await _firestore
.collection('trips')
.doc(trip.id)
.update(trip.toMap());
return true;
} catch (e) {
throw Exception('Erreur lors de la mise à jour du voyage: $e');
}
}
// Supprimer un voyage
Future<bool> deleteTrip(String tripId) async {
try {
await _firestore.collection('trips').doc(tripId).delete();
return true;
} catch (e) {
throw Exception('Erreur lors de la suppression du voyage: $e');
}
}
// Ajouter un participant
Future<bool> addParticipant(String tripId, String participantEmail) async {
try {
await _firestore.collection('trips').doc(tripId).update({
'participants': FieldValue.arrayUnion([participantEmail])
});
return true;
} catch (e) {
throw Exception('Erreur lors de l\'ajout du participant: $e');
}
}
// Retirer un participant
Future<bool> removeParticipant(String tripId, String participantEmail) async {
try {
await _firestore.collection('trips').doc(tripId).update({
'participants': FieldValue.arrayRemove([participantEmail])
});
return true;
} catch (e) {
throw Exception('Erreur lors du retrait du participant: $e');
}
}
}