fix
This commit is contained in:
@@ -18,8 +18,27 @@ class CategoryRepositoryImpl implements CategoryRepository {
|
||||
@override
|
||||
Future<Either<Failure, List<Category>>> getAllCategories() async {
|
||||
try {
|
||||
final categories = await localDataSource.getAllCategories();
|
||||
// Try remote first (online-first)
|
||||
final categories = await remoteDataSource.getAllCategories();
|
||||
// Cache the results
|
||||
await localDataSource.cacheCategories(categories);
|
||||
return Right(categories.map((model) => model.toEntity()).toList());
|
||||
} on ServerException catch (e) {
|
||||
// Remote failed, try local cache
|
||||
try {
|
||||
final cachedCategories = await localDataSource.getAllCategories();
|
||||
return Right(cachedCategories.map((model) => model.toEntity()).toList());
|
||||
} on CacheException catch (cacheError) {
|
||||
return Left(ServerFailure('${e.message}. Cache also unavailable.'));
|
||||
}
|
||||
} on NetworkException catch (e) {
|
||||
// Network failed, try local cache
|
||||
try {
|
||||
final cachedCategories = await localDataSource.getAllCategories();
|
||||
return Right(cachedCategories.map((model) => model.toEntity()).toList());
|
||||
} on CacheException catch (cacheError) {
|
||||
return Left(NetworkFailure('${e.message}. Cache also unavailable.'));
|
||||
}
|
||||
} on CacheException catch (e) {
|
||||
return Left(CacheFailure(e.message));
|
||||
}
|
||||
@@ -28,11 +47,33 @@ class CategoryRepositoryImpl implements CategoryRepository {
|
||||
@override
|
||||
Future<Either<Failure, Category>> getCategoryById(String id) async {
|
||||
try {
|
||||
final category = await localDataSource.getCategoryById(id);
|
||||
if (category == null) {
|
||||
return Left(NotFoundFailure('Category not found'));
|
||||
}
|
||||
// Try remote first (online-first)
|
||||
final category = await remoteDataSource.getCategoryById(id);
|
||||
// Cache the result
|
||||
await localDataSource.updateCategory(category);
|
||||
return Right(category.toEntity());
|
||||
} on ServerException catch (e) {
|
||||
// Remote failed, try local cache
|
||||
try {
|
||||
final cachedCategory = await localDataSource.getCategoryById(id);
|
||||
if (cachedCategory == null) {
|
||||
return Left(NotFoundFailure('Category not found in cache'));
|
||||
}
|
||||
return Right(cachedCategory.toEntity());
|
||||
} on CacheException catch (cacheError) {
|
||||
return Left(ServerFailure('${e.message}. Cache also unavailable.'));
|
||||
}
|
||||
} on NetworkException catch (e) {
|
||||
// Network failed, try local cache
|
||||
try {
|
||||
final cachedCategory = await localDataSource.getCategoryById(id);
|
||||
if (cachedCategory == null) {
|
||||
return Left(NotFoundFailure('Category not found in cache'));
|
||||
}
|
||||
return Right(cachedCategory.toEntity());
|
||||
} on CacheException catch (cacheError) {
|
||||
return Left(NetworkFailure('${e.message}. Cache also unavailable.'));
|
||||
}
|
||||
} on CacheException catch (e) {
|
||||
return Left(CacheFailure(e.message));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user