This commit is contained in:
2025-10-28 00:43:32 +07:00
parent de49f564b1
commit df99d0c9e3
19 changed files with 1000 additions and 1453 deletions

View File

@@ -1,7 +1,10 @@
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/product_detail_request_model.dart';
import '../models/product_model.dart';
import '../models/product_stage_model.dart';
/// Abstract interface for products remote data source
abstract class ProductsRemoteDataSource {
@@ -13,6 +16,14 @@ abstract class ProductsRemoteDataSource {
/// Returns List<ProductModel>
/// Throws [ServerException] if the API call fails
Future<List<ProductModel>> getProducts(int warehouseId, String type);
/// Get product stages for a product in a warehouse
///
/// [request] - Request containing warehouseId and productId
///
/// Returns List<ProductStageModel> with all stages for the product
/// Throws [ServerException] if the API call fails
Future<List<ProductStageModel>> getProductDetail(ProductDetailRequestModel request);
}
/// Implementation of ProductsRemoteDataSource using ApiClient
@@ -59,4 +70,55 @@ class ProductsRemoteDataSourceImpl implements ProductsRemoteDataSource {
throw ServerException('Failed to get products: ${e.toString()}');
}
}
@override
Future<List<ProductStageModel>> getProductDetail(
ProductDetailRequestModel request) async {
try {
// Make API call to get product stages
final response = await apiClient.post(
ApiEndpoints.productDetail,
data: request.toJson(),
);
// Parse the API response - the Value field contains a list of stage objects
final apiResponse = ApiResponse.fromJson(
response.data as Map<String, dynamic>,
(json) {
// The API returns a list of stages for the product
final list = json as List;
if (list.isEmpty) {
throw const ServerException('Product stages not found');
}
// Parse all stages from the list
return list
.map((e) => ProductStageModel.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 product stages',
);
}
} 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 product stages: ${e.toString()}');
}
}
}