This commit is contained in:
Phuoc Nguyen
2025-10-28 16:48:31 +07:00
parent 5cfc56f40d
commit 4b35d236df
11 changed files with 390 additions and 102 deletions

View File

@@ -2,6 +2,7 @@ import '../../../../core/constants/api_endpoints.dart';
import '../../../../core/errors/exceptions.dart';
import '../../../../core/network/api_client.dart';
import '../../../../core/network/api_response.dart';
import '../models/create_product_warehouse_request.dart';
import '../models/product_detail_request_model.dart';
import '../models/product_model.dart';
import '../models/product_stage_model.dart';
@@ -24,6 +25,14 @@ abstract class ProductsRemoteDataSource {
/// Returns List<ProductStageModel> with all stages for the product
/// Throws [ServerException] if the API call fails
Future<List<ProductStageModel>> getProductDetail(ProductDetailRequestModel request);
/// Create product warehouse entry (import/export operation)
///
/// [request] - Request containing all product warehouse details
///
/// Returns void on success
/// Throws [ServerException] if the API call fails
Future<void> createProductWarehouse(CreateProductWarehouseRequest request);
}
/// Implementation of ProductsRemoteDataSource using ApiClient
@@ -126,4 +135,47 @@ class ProductsRemoteDataSourceImpl implements ProductsRemoteDataSource {
throw ServerException('Failed to get product stages: ${e.toString()}');
}
}
@override
Future<void> createProductWarehouse(
CreateProductWarehouseRequest request) async {
try {
// The API expects an array of requests
final requestData = [request.toJson()];
// Make API call to create product warehouse
final response = await apiClient.post(
ApiEndpoints.createProductWarehouse,
data: requestData,
);
// Parse the API response
final apiResponse = ApiResponse.fromJson(
response.data as Map<String, dynamic>,
(json) => json, // We don't need to parse the response value
);
// Check if the API call was successful
if (!apiResponse.isSuccess) {
// Throw exception with error message from API
throw ServerException(
apiResponse.errors.isNotEmpty
? apiResponse.errors.first
: 'Failed to create product warehouse entry',
);
}
} 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 create product warehouse entry: ${e.toString()}');
}
}
}