fill
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
import '../../../../core/errors/exceptions.dart';
|
||||
import '../../../../core/network/api_client.dart';
|
||||
import '../../../../core/network/api_response.dart';
|
||||
import '../models/product_model.dart';
|
||||
|
||||
/// Abstract interface for products remote data source
|
||||
abstract class ProductsRemoteDataSource {
|
||||
/// Fetch products from the API
|
||||
///
|
||||
/// [warehouseId] - The ID of the warehouse
|
||||
/// [type] - The operation type ('import' or 'export')
|
||||
///
|
||||
/// Returns List<ProductModel>
|
||||
/// Throws [ServerException] if the API call fails
|
||||
Future<List<ProductModel>> getProducts(int warehouseId, String type);
|
||||
}
|
||||
|
||||
/// Implementation of ProductsRemoteDataSource using ApiClient
|
||||
class ProductsRemoteDataSourceImpl implements ProductsRemoteDataSource {
|
||||
final ApiClient apiClient;
|
||||
|
||||
ProductsRemoteDataSourceImpl(this.apiClient);
|
||||
|
||||
@override
|
||||
Future<List<ProductModel>> getProducts(int warehouseId, String type) async {
|
||||
try {
|
||||
// Make API call to get all products
|
||||
final response = await apiClient.get('/portalProduct/getAllProduct');
|
||||
|
||||
// Parse the API response using ApiResponse wrapper
|
||||
final apiResponse = ApiResponse.fromJson(
|
||||
response.data as Map<String, dynamic>,
|
||||
(json) => (json as List)
|
||||
.map((e) => ProductModel.fromJson(e as Map<String, dynamic>))
|
||||
.toList(),
|
||||
);
|
||||
|
||||
// Check if the API call was successful
|
||||
if (apiResponse.isSuccess && apiResponse.value != null) {
|
||||
return apiResponse.value!;
|
||||
} else {
|
||||
// Throw exception with error message from API
|
||||
throw ServerException(
|
||||
apiResponse.errors.isNotEmpty
|
||||
? apiResponse.errors.first
|
||||
: 'Failed to get products',
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
// Re-throw ServerException as-is
|
||||
if (e is ServerException) {
|
||||
rethrow;
|
||||
}
|
||||
// Re-throw NetworkException as-is
|
||||
if (e is NetworkException) {
|
||||
rethrow;
|
||||
}
|
||||
// Wrap other exceptions in ServerException
|
||||
throw ServerException('Failed to get products: ${e.toString()}');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user