feat: Integrate ErrorService for improved error handling and add imageUrl field to Trip model

This commit is contained in:
Van Leemput Dayron
2025-11-03 11:33:25 +01:00
parent 882f7c8963
commit 83aed85fea
3 changed files with 223 additions and 174 deletions

View File

@@ -2,49 +2,51 @@ import 'dart:convert';
import 'package:cloud_firestore/cloud_firestore.dart';
/// Model representing a travel trip in the application.
///
///
/// This class encapsulates all trip-related information including dates,
/// location, participants, and budget details. It provides serialization
/// methods for Firebase operations and supports trip lifecycle management.
class Trip {
/// Unique identifier for the trip (usually Firestore document ID).
final String? id;
/// Title or name of the trip.
final String title;
/// Detailed description of the trip.
final String description;
/// Trip destination or location.
final String location;
/// Trip start date and time.
final DateTime startDate;
/// Trip end date and time.
final DateTime endDate;
/// Optional budget for the trip in the local currency.
final double? budget;
/// List of participant user IDs.
final List<String> participants;
/// User ID of the trip creator.
final String createdBy;
/// Timestamp when the trip was created.
final DateTime createdAt;
/// Timestamp when the trip was last updated.
final DateTime updatedAt;
/// Current status of the trip (e.g., 'draft', 'active', 'completed').
final String status;
final String? imageUrl;
/// Creates a new [Trip] instance.
///
///
/// Most fields are required except [id] and [budget].
/// [status] defaults to 'draft' for new trips.
Trip({
@@ -60,32 +62,33 @@ class Trip {
required this.createdAt,
required this.updatedAt,
this.status = 'draft',
this.imageUrl,
});
// NOUVELLE MÉTHODE HELPER pour convertir n'importe quel format de date
static DateTime _parseDateTime(dynamic value) {
if (value == null) return DateTime.now();
// Si c'est déjà un Timestamp Firebase
if (value is Timestamp) {
return value.toDate();
}
// Si c'est un int (millisecondes depuis epoch)
if (value is int) {
return DateTime.fromMillisecondsSinceEpoch(value);
}
// Si c'est un String (ISO 8601)
if (value is String) {
return DateTime.parse(value);
}
// Si c'est déjà un DateTime
if (value is DateTime) {
return value;
}
// Par défaut
return DateTime.now();
}
@@ -106,6 +109,7 @@ class Trip {
createdAt: _parseDateTime(map['createdAt']),
updatedAt: _parseDateTime(map['updatedAt']),
status: map['status'] as String? ?? 'draft',
imageUrl: map['imageUrl'] as String?,
);
} catch (e) {
rethrow;
@@ -126,6 +130,7 @@ class Trip {
'createdAt': Timestamp.fromDate(createdAt),
'updatedAt': Timestamp.fromDate(updatedAt),
'status': status,
'imageUrl': imageUrl,
};
}
@@ -148,6 +153,7 @@ class Trip {
DateTime? createdAt,
DateTime? updatedAt,
String? status,
String? imageUrl,
}) {
return Trip(
id: id ?? this.id,
@@ -162,6 +168,7 @@ class Trip {
createdAt: createdAt ?? this.createdAt,
updatedAt: updatedAt ?? this.updatedAt,
status: status ?? this.status,
imageUrl: imageUrl ?? this.imageUrl,
);
}
@@ -236,4 +243,4 @@ class Trip {
@override
int get hashCode => id.hashCode;
}
}