Enhance model and service documentation with detailed comments and descriptions

- Updated Group, Trip, User, and other model classes to include comprehensive documentation for better understanding and maintainability.
- Improved error handling and logging in services, including AuthService, ErrorService, and StorageService.
- Added validation and business logic explanations in ExpenseService and TripService.
- Refactored method comments to follow a consistent format across the codebase.
- Translated error messages and comments from French to English for consistency.
This commit is contained in:
Dayron
2025-10-30 15:56:17 +01:00
parent 1eeea6997e
commit 2faf37f145
46 changed files with 2656 additions and 220 deletions

View File

@@ -3,23 +3,49 @@ import 'package:cloud_firestore/cloud_firestore.dart';
import '../models/user.dart';
import '../services/auth_service.dart';
/// Repository for authentication operations and user data management.
///
/// This repository handles authentication logic and user data persistence
/// by coordinating between Firebase Auth and Firestore. It provides a
/// clean interface for authentication operations while managing the
/// relationship between Firebase users and application user data.
class AuthRepository {
/// Authentication service for Firebase Auth operations.
final AuthService _authService;
/// Firestore instance for user data operations.
final FirebaseFirestore _firestore;
/// Creates a new [AuthRepository] with optional service dependencies.
///
/// If [authService] or [firestore] are not provided, default instances will be used.
AuthRepository({
AuthService? authService,
FirebaseFirestore? firestore,
}) : _authService = authService ?? AuthService(),
_firestore = firestore ?? FirebaseFirestore.instance;
// Vérifier l'état de connexion actuel
/// Stream of authentication state changes.
///
/// Emits Firebase user objects when authentication state changes.
Stream<firebase_auth.User?> get authStateChanges =>
_authService.authStateChanges;
/// Gets the currently authenticated Firebase user.
///
/// Returns null if no user is currently authenticated.
firebase_auth.User? get currentUser => _authService.currentUser;
// Connexion avec email/mot de passe
/// Signs in a user with email and password.
///
/// Authenticates with Firebase Auth and retrieves the corresponding
/// user data from Firestore.
///
/// [email] - User's email address
/// [password] - User's password
///
/// Returns the [User] model if successful, null if user data not found.
/// Throws an exception if authentication fails.
Future<User?> signInWithEmailAndPassword({
required String email,
required String password,
@@ -31,11 +57,22 @@ class AuthRepository {
);
return await getUserFromFirestore(firebaseUser.user!.uid);
} catch (e) {
throw Exception('Erreur de connexion: $e');
throw Exception('Sign-in error: $e');
}
}
// Inscription avec email/mot de passe
/// Creates a new user account with email and password.
///
/// Creates a Firebase Auth account and stores additional user information
/// in Firestore.
///
/// [email] - User's email address
/// [password] - User's password
/// [nom] - User's last name
/// [prenom] - User's first name
///
/// Returns the created [User] model if successful.
/// Throws an exception if account creation fails.
Future<User?> signUpWithEmailAndPassword({
required String email,
required String password,
@@ -48,7 +85,7 @@ class AuthRepository {
password: password,
);
// Créer le document utilisateur dans Firestore
// Create user document in Firestore with additional information
final user = User(
id: firebaseUser.user!.uid,
email: email,
@@ -60,29 +97,35 @@ class AuthRepository {
return user;
} catch (e) {
throw Exception('Erreur d\'inscription: $e');
throw Exception('Registration error: $e');
}
}
// Connexion avec Google
/// Signs in a user using Google authentication.
///
/// Handles Google sign-in flow and creates/retrieves user data from Firestore.
/// If the user doesn't exist, creates a new user document.
///
/// Returns the [User] model if successful, null if Google sign-in was cancelled.
/// Throws an exception if authentication fails.
Future<User?> signInWithGoogle() async {
try {
final firebaseUser = await _authService.signInWithGoogle();
if (firebaseUser.user != null) {
// Vérifier si l'utilisateur existe déjà
// Check if user already exists in Firestore
final existingUser = await getUserFromFirestore(firebaseUser.user!.uid);
if (existingUser != null) {
return existingUser;
}
// Créer un nouvel utilisateur
// Create new user document for first-time Google sign-in
final user = User(
id: firebaseUser.user!.uid,
email: firebaseUser.user!.email ?? '',
nom: '',
prenom: firebaseUser.user!.displayName ?? 'Utilisateur',
prenom: firebaseUser.user!.displayName ?? 'User',
);
await _firestore.collection('users').doc(user.id).set(user.toMap());
@@ -90,11 +133,17 @@ class AuthRepository {
}
return null;
} catch (e) {
throw Exception('Erreur de connexion Google: $e');
throw Exception('Google sign-in error: $e');
}
}
// Connexion avec Apple
/// Signs in a user using Apple authentication.
///
/// Handles Apple sign-in flow and creates/retrieves user data from Firestore.
/// If the user doesn't exist, creates a new user document.
///
/// Returns the [User] model if successful, null if Apple sign-in was cancelled.
/// Throws an exception if authentication fails.
Future<User?> signInWithApple() async {
try {
final firebaseUser = await _authService.signInWithApple();
@@ -110,7 +159,7 @@ class AuthRepository {
id: firebaseUser.user!.uid,
email: firebaseUser.user!.email ?? '',
nom: '',
prenom: firebaseUser.user!.displayName ?? 'Utilisateur',
prenom: firebaseUser.user!.displayName ?? 'User',
);
await _firestore.collection('users').doc(user.id).set(user.toMap());
@@ -118,21 +167,34 @@ class AuthRepository {
}
return null;
} catch (e) {
throw Exception('Erreur de connexion Apple: $e');
throw Exception('Apple sign-in error: $e');
}
}
// Déconnexion
/// Signs out the current user.
///
/// Clears the authentication state and signs out from Firebase Auth.
Future<void> signOut() async {
await _authService.signOut();
}
// Réinitialisation du mot de passe
/// Sends a password reset email to the specified email address.
///
/// [email] - The email address to send the reset link to
///
/// Throws an exception if the operation fails.
Future<void> resetPassword(String email) async {
await _authService.resetPassword(email);
}
// Récupérer les données utilisateur depuis Firestore
/// Retrieves user data from Firestore by user ID.
///
/// Fetches the user document from the 'users' collection and converts
/// it to a [User] model.
///
/// [uid] - The Firebase user ID to look up
///
/// Returns the [User] model if found, null otherwise.
Future<User?> getUserFromFirestore(String uid) async {
try {
final doc = await _firestore.collection('users').doc(uid).get();

View File

@@ -2,17 +2,35 @@ import 'package:cloud_firestore/cloud_firestore.dart';
import '../models/user.dart';
import '../services/auth_service.dart';
/// Repository for user data operations in Firestore.
///
/// This repository provides methods for CRUD operations on user data,
/// including retrieving users by ID or email, updating user information,
/// and managing user profiles in the Firestore database.
class UserRepository {
/// Firestore instance for database operations.
final FirebaseFirestore _firestore;
/// Authentication service for user-related operations.
final AuthService _authService;
/// Creates a new [UserRepository] with optional dependencies.
///
/// If [firestore] or [authService] are not provided, default instances will be used.
UserRepository({
FirebaseFirestore? firestore,
AuthService? authService,
}) : _firestore = firestore ?? FirebaseFirestore.instance,
_authService = authService ?? AuthService();
// Récupérer un utilisateur par ID
/// Retrieves a user by their unique ID.
///
/// Fetches the user document from Firestore and converts it to a [User] model.
///
/// [uid] - The unique user identifier
///
/// Returns the [User] model if found, null otherwise.
/// Throws an exception if the operation fails.
Future<User?> getUserById(String uid) async {
try {
final doc = await _firestore.collection('users').doc(uid).get();
@@ -22,11 +40,19 @@ class UserRepository {
}
return null;
} catch (e) {
throw Exception('Erreur lors de la récupération de l\'utilisateur: $e');
throw Exception('Error retrieving user: $e');
}
}
// Récupérer un utilisateur par email
/// Retrieves a user by their email address.
///
/// Searches the users collection for a matching email address.
/// Email comparison is case-insensitive after trimming whitespace.
///
/// [email] - The email address to search for
///
/// Returns the first [User] found with the matching email, null if not found.
/// Throws an exception if the operation fails.
Future<User?> getUserByEmail(String email) async {
try {
final querySnapshot = await _firestore
@@ -42,11 +68,17 @@ class UserRepository {
}
return null;
} catch (e) {
throw Exception('Erreur lors de la recherche de l\'utilisateur: $e');
throw Exception('Error searching for user: $e');
}
}
// Mettre à jour un utilisateur
/// Updates an existing user in Firestore.
///
/// Updates the user document with the provided user data.
///
/// [user] - The user object containing updated information
///
/// Returns true if the update was successful, false otherwise.
Future<bool> updateUser(User user) async {
try {
await _firestore.collection('users').doc(user.id).update(user.toMap());