feat: integrate ErrorService for consistent error display and standardize bloc error messages.
This commit is contained in:
@@ -1,33 +1,45 @@
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import '../models/trip.dart';
|
||||
import '../services/error_service.dart';
|
||||
|
||||
class TripRepository {
|
||||
final FirebaseFirestore _firestore = FirebaseFirestore.instance;
|
||||
final _errorService = ErrorService();
|
||||
|
||||
CollectionReference get _tripsCollection => _firestore.collection('trips');
|
||||
|
||||
// Récupérer tous les voyages d'un utilisateur
|
||||
Stream<List<Trip>> getTripsByUserId(String userId) {
|
||||
Stream<List<Trip>> getTripsByUserId(String userId) {
|
||||
try {
|
||||
return _tripsCollection
|
||||
.where('participants', arrayContains: userId)
|
||||
.snapshots()
|
||||
.map((snapshot) {
|
||||
final trips = snapshot.docs
|
||||
.map((doc) {
|
||||
try {
|
||||
final data = doc.data() as Map<String, dynamic>;
|
||||
return Trip.fromMap(data, doc.id);
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
})
|
||||
.whereType<Trip>()
|
||||
.toList();
|
||||
return trips;
|
||||
});
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la récupération des voyages: $e');
|
||||
.map((snapshot) {
|
||||
final trips = snapshot.docs
|
||||
.map((doc) {
|
||||
try {
|
||||
final data = doc.data() as Map<String, dynamic>;
|
||||
return Trip.fromMap(data, doc.id);
|
||||
} catch (e, stackTrace) {
|
||||
_errorService.logError(
|
||||
'TripRepository',
|
||||
'Erreur parsing trip ${doc.id}: $e',
|
||||
stackTrace,
|
||||
);
|
||||
return null;
|
||||
}
|
||||
})
|
||||
.whereType<Trip>()
|
||||
.toList();
|
||||
return trips;
|
||||
});
|
||||
} catch (e, stackTrace) {
|
||||
_errorService.logError(
|
||||
'TripRepository',
|
||||
'Erreur stream trips: $e',
|
||||
stackTrace,
|
||||
);
|
||||
throw Exception('Impossible de récupérer les voyages');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,8 +50,13 @@ class TripRepository {
|
||||
// Ne pas modifier les timestamps ici, ils sont déjà au bon format
|
||||
final docRef = await _tripsCollection.add(tripData);
|
||||
return docRef.id;
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la création du voyage: $e');
|
||||
} catch (e, stackTrace) {
|
||||
_errorService.logError(
|
||||
'TripRepository',
|
||||
'Erreur création trip: $e',
|
||||
stackTrace,
|
||||
);
|
||||
throw Exception('Impossible de créer le voyage');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,14 +64,19 @@ class TripRepository {
|
||||
Future<Trip?> getTripById(String tripId) async {
|
||||
try {
|
||||
final doc = await _tripsCollection.doc(tripId).get();
|
||||
|
||||
|
||||
if (!doc.exists) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
return Trip.fromMap(doc.data() as Map<String, dynamic>, doc.id);
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la récupération du voyage: $e');
|
||||
} catch (e, stackTrace) {
|
||||
_errorService.logError(
|
||||
'TripRepository',
|
||||
'Erreur get trip: $e',
|
||||
stackTrace,
|
||||
);
|
||||
throw Exception('Impossible de récupérer le voyage');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,10 +86,15 @@ class TripRepository {
|
||||
final tripData = trip.toMap();
|
||||
// Mettre à jour le timestamp de modification
|
||||
tripData['updatedAt'] = Timestamp.now();
|
||||
|
||||
|
||||
await _tripsCollection.doc(tripId).update(tripData);
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la mise à jour du voyage: $e');
|
||||
} catch (e, stackTrace) {
|
||||
_errorService.logError(
|
||||
'TripRepository',
|
||||
'Erreur update trip: $e',
|
||||
stackTrace,
|
||||
);
|
||||
throw Exception('Impossible de mettre à jour le voyage');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,8 +102,13 @@ class TripRepository {
|
||||
Future<void> deleteTrip(String tripId) async {
|
||||
try {
|
||||
await _tripsCollection.doc(tripId).delete();
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la suppression du voyage: $e');
|
||||
} catch (e, stackTrace) {
|
||||
_errorService.logError(
|
||||
'TripRepository',
|
||||
'Erreur delete trip: $e',
|
||||
stackTrace,
|
||||
);
|
||||
throw Exception('Impossible de supprimer le voyage');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user