Refactoring
This commit is contained in:
@@ -0,0 +1,561 @@
|
||||
import 'package:dartz/dartz.dart';
|
||||
import '../../../../core/error/failures.dart';
|
||||
import '../../../../core/error/exceptions.dart';
|
||||
import '../../../../core/network/network_info.dart';
|
||||
import '../../domain/entities/demande_aide.dart';
|
||||
import '../../domain/entities/proposition_aide.dart';
|
||||
import '../../domain/entities/evaluation_aide.dart';
|
||||
import '../../domain/repositories/solidarite_repository.dart';
|
||||
import '../datasources/solidarite_remote_data_source.dart';
|
||||
import '../datasources/solidarite_local_data_source.dart';
|
||||
import '../models/demande_aide_model.dart';
|
||||
import '../models/proposition_aide_model.dart';
|
||||
import '../models/evaluation_aide_model.dart';
|
||||
|
||||
/// Implémentation du repository de solidarité
|
||||
///
|
||||
/// Cette classe implémente le contrat défini dans le domaine
|
||||
/// en combinant les sources de données locale et distante.
|
||||
class SolidariteRepositoryImpl implements SolidariteRepository {
|
||||
final SolidariteRemoteDataSource remoteDataSource;
|
||||
final SolidariteLocalDataSource localDataSource;
|
||||
final NetworkInfo networkInfo;
|
||||
|
||||
SolidariteRepositoryImpl({
|
||||
required this.remoteDataSource,
|
||||
required this.localDataSource,
|
||||
required this.networkInfo,
|
||||
});
|
||||
|
||||
// Demandes d'aide
|
||||
@override
|
||||
Future<Either<Failure, DemandeAide>> creerDemandeAide(DemandeAide demande) async {
|
||||
try {
|
||||
if (await networkInfo.isConnected) {
|
||||
final demandeModel = DemandeAideModel.fromEntity(demande);
|
||||
final result = await remoteDataSource.creerDemandeAide(demandeModel);
|
||||
|
||||
// Mettre en cache le résultat
|
||||
await localDataSource.cacherDemandeAide(result);
|
||||
|
||||
return Right(result.toEntity());
|
||||
} else {
|
||||
return Left(NetworkFailure('Aucune connexion internet disponible'));
|
||||
}
|
||||
} on ServerException catch (e) {
|
||||
return Left(ServerFailure(e.message));
|
||||
} on CacheException catch (e) {
|
||||
// Continuer même si la mise en cache échoue
|
||||
return Left(CacheFailure(e.message));
|
||||
} catch (e) {
|
||||
return Left(UnexpectedFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Either<Failure, DemandeAide>> mettreAJourDemandeAide(DemandeAide demande) async {
|
||||
try {
|
||||
if (await networkInfo.isConnected) {
|
||||
final demandeModel = DemandeAideModel.fromEntity(demande);
|
||||
final result = await remoteDataSource.mettreAJourDemandeAide(demandeModel);
|
||||
|
||||
// Mettre à jour le cache
|
||||
await localDataSource.cacherDemandeAide(result);
|
||||
|
||||
return Right(result.toEntity());
|
||||
} else {
|
||||
return Left(NetworkFailure('Aucune connexion internet disponible'));
|
||||
}
|
||||
} on ServerException catch (e) {
|
||||
return Left(ServerFailure(e.message));
|
||||
} on CacheException catch (e) {
|
||||
return Left(CacheFailure(e.message));
|
||||
} catch (e) {
|
||||
return Left(UnexpectedFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Either<Failure, DemandeAide>> obtenirDemandeAide(String id) async {
|
||||
try {
|
||||
// Essayer d'abord le cache local
|
||||
final cachedDemande = await localDataSource.obtenirDemandeAideCachee(id);
|
||||
if (cachedDemande != null && await _estCacheValide()) {
|
||||
return Right(cachedDemande.toEntity());
|
||||
}
|
||||
|
||||
// Si pas en cache ou cache expiré, aller chercher sur le serveur
|
||||
if (await networkInfo.isConnected) {
|
||||
final result = await remoteDataSource.obtenirDemandeAide(id);
|
||||
|
||||
// Mettre en cache le résultat
|
||||
await localDataSource.cacherDemandeAide(result);
|
||||
|
||||
return Right(result.toEntity());
|
||||
} else {
|
||||
// Si pas de connexion, utiliser le cache même s'il est expiré
|
||||
if (cachedDemande != null) {
|
||||
return Right(cachedDemande.toEntity());
|
||||
}
|
||||
return Left(NetworkFailure('Aucune connexion internet et aucune donnée en cache'));
|
||||
}
|
||||
} on ServerException catch (e) {
|
||||
return Left(ServerFailure(e.message));
|
||||
} on NotFoundException catch (e) {
|
||||
return Left(NotFoundFailure(e.message));
|
||||
} on CacheException catch (e) {
|
||||
return Left(CacheFailure(e.message));
|
||||
} catch (e) {
|
||||
return Left(UnexpectedFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Either<Failure, DemandeAide>> soumettreDemande(String demandeId) async {
|
||||
try {
|
||||
if (await networkInfo.isConnected) {
|
||||
final result = await remoteDataSource.soumettreDemande(demandeId);
|
||||
|
||||
// Mettre à jour le cache
|
||||
await localDataSource.cacherDemandeAide(result);
|
||||
|
||||
return Right(result.toEntity());
|
||||
} else {
|
||||
return Left(NetworkFailure('Aucune connexion internet disponible'));
|
||||
}
|
||||
} on ServerException catch (e) {
|
||||
return Left(ServerFailure(e.message));
|
||||
} on CacheException catch (e) {
|
||||
return Left(CacheFailure(e.message));
|
||||
} catch (e) {
|
||||
return Left(UnexpectedFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Either<Failure, DemandeAide>> evaluerDemande({
|
||||
required String demandeId,
|
||||
required String evaluateurId,
|
||||
required StatutAide decision,
|
||||
String? commentaire,
|
||||
double? montantApprouve,
|
||||
}) async {
|
||||
try {
|
||||
if (await networkInfo.isConnected) {
|
||||
final result = await remoteDataSource.evaluerDemande(
|
||||
demandeId: demandeId,
|
||||
evaluateurId: evaluateurId,
|
||||
decision: decision.name,
|
||||
commentaire: commentaire,
|
||||
montantApprouve: montantApprouve,
|
||||
);
|
||||
|
||||
// Mettre à jour le cache
|
||||
await localDataSource.cacherDemandeAide(result);
|
||||
|
||||
return Right(result.toEntity());
|
||||
} else {
|
||||
return Left(NetworkFailure('Aucune connexion internet disponible'));
|
||||
}
|
||||
} on ServerException catch (e) {
|
||||
return Left(ServerFailure(e.message));
|
||||
} on CacheException catch (e) {
|
||||
return Left(CacheFailure(e.message));
|
||||
} catch (e) {
|
||||
return Left(UnexpectedFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Either<Failure, List<DemandeAide>>> rechercherDemandes({
|
||||
String? organisationId,
|
||||
TypeAide? typeAide,
|
||||
StatutAide? statut,
|
||||
String? demandeurId,
|
||||
bool? urgente,
|
||||
int page = 0,
|
||||
int taille = 20,
|
||||
}) async {
|
||||
try {
|
||||
if (await networkInfo.isConnected) {
|
||||
final result = await remoteDataSource.rechercherDemandes(
|
||||
organisationId: organisationId,
|
||||
typeAide: typeAide?.name,
|
||||
statut: statut?.name,
|
||||
demandeurId: demandeurId,
|
||||
urgente: urgente,
|
||||
page: page,
|
||||
taille: taille,
|
||||
);
|
||||
|
||||
// Mettre en cache les résultats
|
||||
for (final demande in result) {
|
||||
await localDataSource.cacherDemandeAide(demande);
|
||||
}
|
||||
|
||||
return Right(result.map((model) => model.toEntity()).toList());
|
||||
} else {
|
||||
// Mode hors ligne : rechercher dans le cache local
|
||||
final cachedDemandes = await localDataSource.obtenirDemandesAideCachees();
|
||||
var filteredDemandes = cachedDemandes.where((demande) {
|
||||
if (organisationId != null && demande.organisationId != organisationId) return false;
|
||||
if (typeAide != null && demande.typeAide != typeAide) return false;
|
||||
if (statut != null && demande.statut != statut) return false;
|
||||
if (demandeurId != null && demande.demandeurId != demandeurId) return false;
|
||||
if (urgente != null && demande.estUrgente != urgente) return false;
|
||||
return true;
|
||||
}).toList();
|
||||
|
||||
// Pagination locale
|
||||
final startIndex = page * taille;
|
||||
final endIndex = (startIndex + taille).clamp(0, filteredDemandes.length);
|
||||
|
||||
if (startIndex < filteredDemandes.length) {
|
||||
filteredDemandes = filteredDemandes.sublist(startIndex, endIndex);
|
||||
} else {
|
||||
filteredDemandes = [];
|
||||
}
|
||||
|
||||
return Right(filteredDemandes.map((model) => model.toEntity()).toList());
|
||||
}
|
||||
} on ServerException catch (e) {
|
||||
return Left(ServerFailure(e.message));
|
||||
} on CacheException catch (e) {
|
||||
return Left(CacheFailure(e.message));
|
||||
} catch (e) {
|
||||
return Left(UnexpectedFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Either<Failure, List<DemandeAide>>> obtenirDemandesUrgentes(String organisationId) async {
|
||||
try {
|
||||
if (await networkInfo.isConnected) {
|
||||
final result = await remoteDataSource.obtenirDemandesUrgentes(organisationId);
|
||||
|
||||
// Mettre en cache les résultats
|
||||
for (final demande in result) {
|
||||
await localDataSource.cacherDemandeAide(demande);
|
||||
}
|
||||
|
||||
return Right(result.map((model) => model.toEntity()).toList());
|
||||
} else {
|
||||
// Mode hors ligne : filtrer le cache local
|
||||
final cachedDemandes = await localDataSource.obtenirDemandesAideCachees();
|
||||
final demandesUrgentes = cachedDemandes
|
||||
.where((demande) => demande.organisationId == organisationId && demande.estUrgente)
|
||||
.toList();
|
||||
|
||||
return Right(demandesUrgentes.map((model) => model.toEntity()).toList());
|
||||
}
|
||||
} on ServerException catch (e) {
|
||||
return Left(ServerFailure(e.message));
|
||||
} on CacheException catch (e) {
|
||||
return Left(CacheFailure(e.message));
|
||||
} catch (e) {
|
||||
return Left(UnexpectedFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Either<Failure, List<DemandeAide>>> obtenirMesdemandes(String utilisateurId) async {
|
||||
try {
|
||||
if (await networkInfo.isConnected) {
|
||||
final result = await remoteDataSource.obtenirMesdemandes(utilisateurId);
|
||||
|
||||
// Mettre en cache les résultats
|
||||
for (final demande in result) {
|
||||
await localDataSource.cacherDemandeAide(demande);
|
||||
}
|
||||
|
||||
return Right(result.map((model) => model.toEntity()).toList());
|
||||
} else {
|
||||
// Mode hors ligne : filtrer le cache local
|
||||
final cachedDemandes = await localDataSource.obtenirDemandesAideCachees();
|
||||
final mesdemandes = cachedDemandes
|
||||
.where((demande) => demande.demandeurId == utilisateurId)
|
||||
.toList();
|
||||
|
||||
return Right(mesdemandes.map((model) => model.toEntity()).toList());
|
||||
}
|
||||
} on ServerException catch (e) {
|
||||
return Left(ServerFailure(e.message));
|
||||
} on CacheException catch (e) {
|
||||
return Left(CacheFailure(e.message));
|
||||
} catch (e) {
|
||||
return Left(UnexpectedFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
// Propositions d'aide
|
||||
@override
|
||||
Future<Either<Failure, PropositionAide>> creerPropositionAide(PropositionAide proposition) async {
|
||||
try {
|
||||
if (await networkInfo.isConnected) {
|
||||
final propositionModel = PropositionAideModel.fromEntity(proposition);
|
||||
final result = await remoteDataSource.creerPropositionAide(propositionModel);
|
||||
|
||||
// Mettre en cache le résultat
|
||||
await localDataSource.cacherPropositionAide(result);
|
||||
|
||||
return Right(result.toEntity());
|
||||
} else {
|
||||
return Left(NetworkFailure('Aucune connexion internet disponible'));
|
||||
}
|
||||
} on ServerException catch (e) {
|
||||
return Left(ServerFailure(e.message));
|
||||
} on CacheException catch (e) {
|
||||
return Left(CacheFailure(e.message));
|
||||
} catch (e) {
|
||||
return Left(UnexpectedFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Either<Failure, PropositionAide>> mettreAJourPropositionAide(PropositionAide proposition) async {
|
||||
try {
|
||||
if (await networkInfo.isConnected) {
|
||||
final propositionModel = PropositionAideModel.fromEntity(proposition);
|
||||
final result = await remoteDataSource.mettreAJourPropositionAide(propositionModel);
|
||||
|
||||
// Mettre à jour le cache
|
||||
await localDataSource.cacherPropositionAide(result);
|
||||
|
||||
return Right(result.toEntity());
|
||||
} else {
|
||||
return Left(NetworkFailure('Aucune connexion internet disponible'));
|
||||
}
|
||||
} on ServerException catch (e) {
|
||||
return Left(ServerFailure(e.message));
|
||||
} on CacheException catch (e) {
|
||||
return Left(CacheFailure(e.message));
|
||||
} catch (e) {
|
||||
return Left(UnexpectedFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Either<Failure, PropositionAide>> obtenirPropositionAide(String id) async {
|
||||
try {
|
||||
// Essayer d'abord le cache local
|
||||
final cachedProposition = await localDataSource.obtenirPropositionAideCachee(id);
|
||||
if (cachedProposition != null && await _estCacheValide()) {
|
||||
return Right(cachedProposition.toEntity());
|
||||
}
|
||||
|
||||
// Si pas en cache ou cache expiré, aller chercher sur le serveur
|
||||
if (await networkInfo.isConnected) {
|
||||
final result = await remoteDataSource.obtenirPropositionAide(id);
|
||||
|
||||
// Mettre en cache le résultat
|
||||
await localDataSource.cacherPropositionAide(result);
|
||||
|
||||
return Right(result.toEntity());
|
||||
} else {
|
||||
// Si pas de connexion, utiliser le cache même s'il est expiré
|
||||
if (cachedProposition != null) {
|
||||
return Right(cachedProposition.toEntity());
|
||||
}
|
||||
return Left(NetworkFailure('Aucune connexion internet et aucune donnée en cache'));
|
||||
}
|
||||
} on ServerException catch (e) {
|
||||
return Left(ServerFailure(e.message));
|
||||
} on NotFoundException catch (e) {
|
||||
return Left(NotFoundFailure(e.message));
|
||||
} on CacheException catch (e) {
|
||||
return Left(CacheFailure(e.message));
|
||||
} catch (e) {
|
||||
return Left(UnexpectedFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Either<Failure, PropositionAide>> changerStatutProposition({
|
||||
required String propositionId,
|
||||
required bool activer,
|
||||
}) async {
|
||||
try {
|
||||
if (await networkInfo.isConnected) {
|
||||
final result = await remoteDataSource.changerStatutProposition(
|
||||
propositionId: propositionId,
|
||||
activer: activer,
|
||||
);
|
||||
|
||||
// Mettre à jour le cache
|
||||
await localDataSource.cacherPropositionAide(result);
|
||||
|
||||
return Right(result.toEntity());
|
||||
} else {
|
||||
return Left(NetworkFailure('Aucune connexion internet disponible'));
|
||||
}
|
||||
} on ServerException catch (e) {
|
||||
return Left(ServerFailure(e.message));
|
||||
} on CacheException catch (e) {
|
||||
return Left(CacheFailure(e.message));
|
||||
} catch (e) {
|
||||
return Left(UnexpectedFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Either<Failure, List<PropositionAide>>> rechercherPropositions({
|
||||
String? organisationId,
|
||||
TypeAide? typeAide,
|
||||
String? proposantId,
|
||||
bool? actives,
|
||||
int page = 0,
|
||||
int taille = 20,
|
||||
}) async {
|
||||
try {
|
||||
if (await networkInfo.isConnected) {
|
||||
final result = await remoteDataSource.rechercherPropositions(
|
||||
organisationId: organisationId,
|
||||
typeAide: typeAide?.name,
|
||||
proposantId: proposantId,
|
||||
actives: actives,
|
||||
page: page,
|
||||
taille: taille,
|
||||
);
|
||||
|
||||
// Mettre en cache les résultats
|
||||
for (final proposition in result) {
|
||||
await localDataSource.cacherPropositionAide(proposition);
|
||||
}
|
||||
|
||||
return Right(result.map((model) => model.toEntity()).toList());
|
||||
} else {
|
||||
// Mode hors ligne : rechercher dans le cache local
|
||||
final cachedPropositions = await localDataSource.obtenirPropositionsAideCachees();
|
||||
var filteredPropositions = cachedPropositions.where((proposition) {
|
||||
if (organisationId != null && proposition.organisationId != organisationId) return false;
|
||||
if (typeAide != null && proposition.typeAide != typeAide) return false;
|
||||
if (proposantId != null && proposition.proposantId != proposantId) return false;
|
||||
if (actives != null && proposition.isActiveEtDisponible != actives) return false;
|
||||
return true;
|
||||
}).toList();
|
||||
|
||||
// Pagination locale
|
||||
final startIndex = page * taille;
|
||||
final endIndex = (startIndex + taille).clamp(0, filteredPropositions.length);
|
||||
|
||||
if (startIndex < filteredPropositions.length) {
|
||||
filteredPropositions = filteredPropositions.sublist(startIndex, endIndex);
|
||||
} else {
|
||||
filteredPropositions = [];
|
||||
}
|
||||
|
||||
return Right(filteredPropositions.map((model) => model.toEntity()).toList());
|
||||
}
|
||||
} on ServerException catch (e) {
|
||||
return Left(ServerFailure(e.message));
|
||||
} on CacheException catch (e) {
|
||||
return Left(CacheFailure(e.message));
|
||||
} catch (e) {
|
||||
return Left(UnexpectedFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Either<Failure, List<PropositionAide>>> obtenirPropositionsActives(TypeAide typeAide) async {
|
||||
try {
|
||||
if (await networkInfo.isConnected) {
|
||||
final result = await remoteDataSource.obtenirPropositionsActives(typeAide.name);
|
||||
|
||||
// Mettre en cache les résultats
|
||||
for (final proposition in result) {
|
||||
await localDataSource.cacherPropositionAide(proposition);
|
||||
}
|
||||
|
||||
return Right(result.map((model) => model.toEntity()).toList());
|
||||
} else {
|
||||
// Mode hors ligne : filtrer le cache local
|
||||
final cachedPropositions = await localDataSource.obtenirPropositionsAideCachees();
|
||||
final propositionsActives = cachedPropositions
|
||||
.where((proposition) => proposition.typeAide == typeAide && proposition.isActiveEtDisponible)
|
||||
.toList();
|
||||
|
||||
return Right(propositionsActives.map((model) => model.toEntity()).toList());
|
||||
}
|
||||
} on ServerException catch (e) {
|
||||
return Left(ServerFailure(e.message));
|
||||
} on CacheException catch (e) {
|
||||
return Left(CacheFailure(e.message));
|
||||
} catch (e) {
|
||||
return Left(UnexpectedFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Either<Failure, List<PropositionAide>>> obtenirMeilleuresPropositions(int limite) async {
|
||||
try {
|
||||
if (await networkInfo.isConnected) {
|
||||
final result = await remoteDataSource.obtenirMeilleuresPropositions(limite);
|
||||
|
||||
// Mettre en cache les résultats
|
||||
for (final proposition in result) {
|
||||
await localDataSource.cacherPropositionAide(proposition);
|
||||
}
|
||||
|
||||
return Right(result.map((model) => model.toEntity()).toList());
|
||||
} else {
|
||||
// Mode hors ligne : trier le cache local par note moyenne
|
||||
final cachedPropositions = await localDataSource.obtenirPropositionsAideCachees();
|
||||
cachedPropositions.sort((a, b) {
|
||||
final noteA = a.noteMoyenne ?? 0.0;
|
||||
final noteB = b.noteMoyenne ?? 0.0;
|
||||
return noteB.compareTo(noteA);
|
||||
});
|
||||
|
||||
final meilleuresPropositions = cachedPropositions.take(limite).toList();
|
||||
return Right(meilleuresPropositions.map((model) => model.toEntity()).toList());
|
||||
}
|
||||
} on ServerException catch (e) {
|
||||
return Left(ServerFailure(e.message));
|
||||
} on CacheException catch (e) {
|
||||
return Left(CacheFailure(e.message));
|
||||
} catch (e) {
|
||||
return Left(UnexpectedFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Either<Failure, List<PropositionAide>>> obtenirMesPropositions(String utilisateurId) async {
|
||||
try {
|
||||
if (await networkInfo.isConnected) {
|
||||
final result = await remoteDataSource.obtenirMesPropositions(utilisateurId);
|
||||
|
||||
// Mettre en cache les résultats
|
||||
for (final proposition in result) {
|
||||
await localDataSource.cacherPropositionAide(proposition);
|
||||
}
|
||||
|
||||
return Right(result.map((model) => model.toEntity()).toList());
|
||||
} else {
|
||||
// Mode hors ligne : filtrer le cache local
|
||||
final cachedPropositions = await localDataSource.obtenirPropositionsAideCachees();
|
||||
final mesPropositions = cachedPropositions
|
||||
.where((proposition) => proposition.proposantId == utilisateurId)
|
||||
.toList();
|
||||
|
||||
return Right(mesPropositions.map((model) => model.toEntity()).toList());
|
||||
}
|
||||
} on ServerException catch (e) {
|
||||
return Left(ServerFailure(e.message));
|
||||
} on CacheException catch (e) {
|
||||
return Left(CacheFailure(e.message));
|
||||
} catch (e) {
|
||||
return Left(UnexpectedFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
// Méthodes utilitaires privées
|
||||
Future<bool> _estCacheValide() async {
|
||||
try {
|
||||
final localDataSourceImpl = localDataSource as SolidariteLocalDataSourceImpl;
|
||||
return await localDataSourceImpl.estCacheDemandesValide() &&
|
||||
await localDataSourceImpl.estCachePropositionsValide();
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,338 @@
|
||||
// Partie 2 de l'implémentation du repository de solidarité
|
||||
// Cette partie contient les méthodes pour le matching, les évaluations et les statistiques
|
||||
|
||||
import 'package:dartz/dartz.dart';
|
||||
import '../../../../core/error/failures.dart';
|
||||
import '../../../../core/error/exceptions.dart';
|
||||
import '../../domain/entities/demande_aide.dart';
|
||||
import '../../domain/entities/proposition_aide.dart';
|
||||
import '../../domain/entities/evaluation_aide.dart';
|
||||
import '../datasources/solidarite_remote_data_source.dart';
|
||||
import '../datasources/solidarite_local_data_source.dart';
|
||||
import '../models/demande_aide_model.dart';
|
||||
import '../models/proposition_aide_model.dart';
|
||||
import '../models/evaluation_aide_model.dart';
|
||||
|
||||
/// Extension de l'implémentation du repository de solidarité
|
||||
/// Cette partie sera intégrée dans la classe principale
|
||||
mixin SolidariteRepositoryImplPart2 {
|
||||
SolidariteRemoteDataSource get remoteDataSource;
|
||||
SolidariteLocalDataSource get localDataSource;
|
||||
bool Function() get isConnected;
|
||||
|
||||
// Matching
|
||||
Future<Either<Failure, List<PropositionAide>>> trouverPropositionsCompatibles(String demandeId) async {
|
||||
try {
|
||||
if (await isConnected()) {
|
||||
final result = await remoteDataSource.trouverPropositionsCompatibles(demandeId);
|
||||
|
||||
// Mettre en cache les résultats
|
||||
for (final proposition in result) {
|
||||
await localDataSource.cacherPropositionAide(proposition);
|
||||
}
|
||||
|
||||
return Right(result.map((model) => model.toEntity()).toList());
|
||||
} else {
|
||||
return Left(NetworkFailure('Aucune connexion internet disponible'));
|
||||
}
|
||||
} on ServerException catch (e) {
|
||||
return Left(ServerFailure(e.message));
|
||||
} on CacheException catch (e) {
|
||||
return Left(CacheFailure(e.message));
|
||||
} catch (e) {
|
||||
return Left(UnexpectedFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
Future<Either<Failure, List<DemandeAide>>> trouverDemandesCompatibles(String propositionId) async {
|
||||
try {
|
||||
if (await isConnected()) {
|
||||
final result = await remoteDataSource.trouverDemandesCompatibles(propositionId);
|
||||
|
||||
// Mettre en cache les résultats
|
||||
for (final demande in result) {
|
||||
await localDataSource.cacherDemandeAide(demande);
|
||||
}
|
||||
|
||||
return Right(result.map((model) => model.toEntity()).toList());
|
||||
} else {
|
||||
return Left(NetworkFailure('Aucune connexion internet disponible'));
|
||||
}
|
||||
} on ServerException catch (e) {
|
||||
return Left(ServerFailure(e.message));
|
||||
} on CacheException catch (e) {
|
||||
return Left(CacheFailure(e.message));
|
||||
} catch (e) {
|
||||
return Left(UnexpectedFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
Future<Either<Failure, List<PropositionAide>>> rechercherProposantsFinanciers(String demandeId) async {
|
||||
try {
|
||||
if (await isConnected()) {
|
||||
final result = await remoteDataSource.rechercherProposantsFinanciers(demandeId);
|
||||
|
||||
// Mettre en cache les résultats
|
||||
for (final proposition in result) {
|
||||
await localDataSource.cacherPropositionAide(proposition);
|
||||
}
|
||||
|
||||
return Right(result.map((model) => model.toEntity()).toList());
|
||||
} else {
|
||||
return Left(NetworkFailure('Aucune connexion internet disponible'));
|
||||
}
|
||||
} on ServerException catch (e) {
|
||||
return Left(ServerFailure(e.message));
|
||||
} on CacheException catch (e) {
|
||||
return Left(CacheFailure(e.message));
|
||||
} catch (e) {
|
||||
return Left(UnexpectedFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
// Évaluations
|
||||
Future<Either<Failure, EvaluationAide>> creerEvaluation(EvaluationAide evaluation) async {
|
||||
try {
|
||||
if (await isConnected()) {
|
||||
final evaluationModel = EvaluationAideModel.fromEntity(evaluation);
|
||||
final result = await remoteDataSource.creerEvaluation(evaluationModel);
|
||||
|
||||
// Mettre en cache le résultat
|
||||
await localDataSource.cacherEvaluation(result);
|
||||
|
||||
return Right(result.toEntity());
|
||||
} else {
|
||||
return Left(NetworkFailure('Aucune connexion internet disponible'));
|
||||
}
|
||||
} on ServerException catch (e) {
|
||||
return Left(ServerFailure(e.message));
|
||||
} on CacheException catch (e) {
|
||||
return Left(CacheFailure(e.message));
|
||||
} catch (e) {
|
||||
return Left(UnexpectedFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
Future<Either<Failure, EvaluationAide>> mettreAJourEvaluation(EvaluationAide evaluation) async {
|
||||
try {
|
||||
if (await isConnected()) {
|
||||
final evaluationModel = EvaluationAideModel.fromEntity(evaluation);
|
||||
final result = await remoteDataSource.mettreAJourEvaluation(evaluationModel);
|
||||
|
||||
// Mettre à jour le cache
|
||||
await localDataSource.cacherEvaluation(result);
|
||||
|
||||
return Right(result.toEntity());
|
||||
} else {
|
||||
return Left(NetworkFailure('Aucune connexion internet disponible'));
|
||||
}
|
||||
} on ServerException catch (e) {
|
||||
return Left(ServerFailure(e.message));
|
||||
} on CacheException catch (e) {
|
||||
return Left(CacheFailure(e.message));
|
||||
} catch (e) {
|
||||
return Left(UnexpectedFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
Future<Either<Failure, EvaluationAide>> obtenirEvaluation(String id) async {
|
||||
try {
|
||||
// Essayer d'abord le cache local
|
||||
final cachedEvaluation = await localDataSource.obtenirEvaluationCachee(id);
|
||||
if (cachedEvaluation != null && await _estCacheEvaluationsValide()) {
|
||||
return Right(cachedEvaluation.toEntity());
|
||||
}
|
||||
|
||||
// Si pas en cache ou cache expiré, aller chercher sur le serveur
|
||||
if (await isConnected()) {
|
||||
final result = await remoteDataSource.obtenirEvaluation(id);
|
||||
|
||||
// Mettre en cache le résultat
|
||||
await localDataSource.cacherEvaluation(result);
|
||||
|
||||
return Right(result.toEntity());
|
||||
} else {
|
||||
// Si pas de connexion, utiliser le cache même s'il est expiré
|
||||
if (cachedEvaluation != null) {
|
||||
return Right(cachedEvaluation.toEntity());
|
||||
}
|
||||
return Left(NetworkFailure('Aucune connexion internet et aucune donnée en cache'));
|
||||
}
|
||||
} on ServerException catch (e) {
|
||||
return Left(ServerFailure(e.message));
|
||||
} on NotFoundException catch (e) {
|
||||
return Left(NotFoundFailure(e.message));
|
||||
} on CacheException catch (e) {
|
||||
return Left(CacheFailure(e.message));
|
||||
} catch (e) {
|
||||
return Left(UnexpectedFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
Future<Either<Failure, List<EvaluationAide>>> obtenirEvaluationsDemande(String demandeId) async {
|
||||
try {
|
||||
if (await isConnected()) {
|
||||
final result = await remoteDataSource.obtenirEvaluationsDemande(demandeId);
|
||||
|
||||
// Mettre en cache les résultats
|
||||
for (final evaluation in result) {
|
||||
await localDataSource.cacherEvaluation(evaluation);
|
||||
}
|
||||
|
||||
return Right(result.map((model) => model.toEntity()).toList());
|
||||
} else {
|
||||
// Mode hors ligne : filtrer le cache local
|
||||
final cachedEvaluations = await localDataSource.obtenirEvaluationsCachees();
|
||||
final evaluationsDemande = cachedEvaluations
|
||||
.where((evaluation) => evaluation.demandeId == demandeId)
|
||||
.toList();
|
||||
|
||||
return Right(evaluationsDemande.map((model) => model.toEntity()).toList());
|
||||
}
|
||||
} on ServerException catch (e) {
|
||||
return Left(ServerFailure(e.message));
|
||||
} on CacheException catch (e) {
|
||||
return Left(CacheFailure(e.message));
|
||||
} catch (e) {
|
||||
return Left(UnexpectedFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
Future<Either<Failure, List<EvaluationAide>>> obtenirEvaluationsProposition(String propositionId) async {
|
||||
try {
|
||||
if (await isConnected()) {
|
||||
final result = await remoteDataSource.obtenirEvaluationsProposition(propositionId);
|
||||
|
||||
// Mettre en cache les résultats
|
||||
for (final evaluation in result) {
|
||||
await localDataSource.cacherEvaluation(evaluation);
|
||||
}
|
||||
|
||||
return Right(result.map((model) => model.toEntity()).toList());
|
||||
} else {
|
||||
// Mode hors ligne : filtrer le cache local
|
||||
final cachedEvaluations = await localDataSource.obtenirEvaluationsCachees();
|
||||
final evaluationsProposition = cachedEvaluations
|
||||
.where((evaluation) => evaluation.propositionId == propositionId)
|
||||
.toList();
|
||||
|
||||
return Right(evaluationsProposition.map((model) => model.toEntity()).toList());
|
||||
}
|
||||
} on ServerException catch (e) {
|
||||
return Left(ServerFailure(e.message));
|
||||
} on CacheException catch (e) {
|
||||
return Left(CacheFailure(e.message));
|
||||
} catch (e) {
|
||||
return Left(UnexpectedFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
Future<Either<Failure, EvaluationAide>> signalerEvaluation({
|
||||
required String evaluationId,
|
||||
required String motif,
|
||||
}) async {
|
||||
try {
|
||||
if (await isConnected()) {
|
||||
final result = await remoteDataSource.signalerEvaluation(
|
||||
evaluationId: evaluationId,
|
||||
motif: motif,
|
||||
);
|
||||
|
||||
// Mettre à jour le cache
|
||||
await localDataSource.cacherEvaluation(result);
|
||||
|
||||
return Right(result.toEntity());
|
||||
} else {
|
||||
return Left(NetworkFailure('Aucune connexion internet disponible'));
|
||||
}
|
||||
} on ServerException catch (e) {
|
||||
return Left(ServerFailure(e.message));
|
||||
} on CacheException catch (e) {
|
||||
return Left(CacheFailure(e.message));
|
||||
} catch (e) {
|
||||
return Left(UnexpectedFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
Future<Either<Failure, StatistiquesEvaluation>> calculerMoyenneDemande(String demandeId) async {
|
||||
try {
|
||||
if (await isConnected()) {
|
||||
final result = await remoteDataSource.calculerMoyenneDemande(demandeId);
|
||||
return Right(result.toEntity());
|
||||
} else {
|
||||
return Left(NetworkFailure('Aucune connexion internet disponible'));
|
||||
}
|
||||
} on ServerException catch (e) {
|
||||
return Left(ServerFailure(e.message));
|
||||
} catch (e) {
|
||||
return Left(UnexpectedFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
Future<Either<Failure, StatistiquesEvaluation>> calculerMoyenneProposition(String propositionId) async {
|
||||
try {
|
||||
if (await isConnected()) {
|
||||
final result = await remoteDataSource.calculerMoyenneProposition(propositionId);
|
||||
return Right(result.toEntity());
|
||||
} else {
|
||||
return Left(NetworkFailure('Aucune connexion internet disponible'));
|
||||
}
|
||||
} on ServerException catch (e) {
|
||||
return Left(ServerFailure(e.message));
|
||||
} catch (e) {
|
||||
return Left(UnexpectedFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
// Statistiques
|
||||
Future<Either<Failure, Map<String, dynamic>>> obtenirStatistiquesSolidarite(String organisationId) async {
|
||||
try {
|
||||
// Essayer d'abord le cache local
|
||||
final cachedStats = await localDataSource.obtenirStatistiquesCachees(organisationId);
|
||||
if (cachedStats != null && await _estCacheStatistiquesValide(organisationId)) {
|
||||
return Right(cachedStats);
|
||||
}
|
||||
|
||||
// Si pas en cache ou cache expiré, aller chercher sur le serveur
|
||||
if (await isConnected()) {
|
||||
final result = await remoteDataSource.obtenirStatistiquesSolidarite(organisationId);
|
||||
|
||||
// Mettre en cache le résultat
|
||||
await localDataSource.cacherStatistiques(organisationId, result);
|
||||
|
||||
return Right(result);
|
||||
} else {
|
||||
// Si pas de connexion, utiliser le cache même s'il est expiré
|
||||
if (cachedStats != null) {
|
||||
return Right(cachedStats);
|
||||
}
|
||||
return Left(NetworkFailure('Aucune connexion internet et aucune donnée en cache'));
|
||||
}
|
||||
} on ServerException catch (e) {
|
||||
return Left(ServerFailure(e.message));
|
||||
} on CacheException catch (e) {
|
||||
return Left(CacheFailure(e.message));
|
||||
} catch (e) {
|
||||
return Left(UnexpectedFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
// Méthodes utilitaires privées
|
||||
Future<bool> _estCacheEvaluationsValide() async {
|
||||
try {
|
||||
final localDataSourceImpl = localDataSource as SolidariteLocalDataSourceImpl;
|
||||
return await localDataSourceImpl.estCacheEvaluationsValide();
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> _estCacheStatistiquesValide(String organisationId) async {
|
||||
try {
|
||||
final localDataSourceImpl = localDataSource as SolidariteLocalDataSourceImpl;
|
||||
return await localDataSourceImpl.estCacheStatistiquesValide(organisationId);
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user