import 'package:injectable/injectable.dart'; import 'package:superport/core/constants/api_endpoints.dart'; import 'package:superport/core/errors/exceptions.dart'; import 'package:superport/data/datasources/remote/api_client.dart'; import 'package:superport/data/models/warehouse/warehouse_dto.dart'; abstract class WarehouseRemoteDataSource { Future getWarehouseLocations({ int page = 1, int perPage = 20, bool? isActive, }); Future getWarehouseLocationById(int id); Future createWarehouseLocation(CreateWarehouseLocationRequest request); Future updateWarehouseLocation(int id, UpdateWarehouseLocationRequest request); Future deleteWarehouseLocation(int id); Future getWarehouseEquipment( int warehouseId, { int page = 1, int perPage = 20, }); Future getWarehouseCapacity(int id); Future> getInUseWarehouseLocations(); } @LazySingleton(as: WarehouseRemoteDataSource) class WarehouseRemoteDataSourceImpl implements WarehouseRemoteDataSource { final ApiClient _apiClient; WarehouseRemoteDataSourceImpl({ required ApiClient apiClient, }) : _apiClient = apiClient; @override Future getWarehouseLocations({ int page = 1, int perPage = 20, bool? isActive, }) async { try { final queryParams = { 'page': page, 'per_page': perPage, }; if (isActive != null) queryParams['is_active'] = isActive; final response = await _apiClient.get( ApiEndpoints.warehouseLocations, queryParameters: queryParams, ); return WarehouseLocationListDto.fromJson(response.data); } catch (e) { throw _handleError(e); } } @override Future getWarehouseLocationById(int id) async { try { final response = await _apiClient.get( '${ApiEndpoints.warehouseLocations}/$id', ); return WarehouseLocationDto.fromJson(response.data); } catch (e) { throw _handleError(e); } } @override Future createWarehouseLocation(CreateWarehouseLocationRequest request) async { try { final response = await _apiClient.post( ApiEndpoints.warehouseLocations, data: request.toJson(), ); return WarehouseLocationDto.fromJson(response.data); } catch (e) { throw _handleError(e); } } @override Future updateWarehouseLocation(int id, UpdateWarehouseLocationRequest request) async { try { final response = await _apiClient.put( '${ApiEndpoints.warehouseLocations}/$id', data: request.toJson(), ); return WarehouseLocationDto.fromJson(response.data); } catch (e) { throw _handleError(e); } } @override Future deleteWarehouseLocation(int id) async { try { await _apiClient.delete( '${ApiEndpoints.warehouseLocations}/$id', ); } catch (e) { throw _handleError(e); } } @override Future getWarehouseEquipment( int warehouseId, { int page = 1, int perPage = 20, }) async { try { final queryParams = { 'page': page, 'per_page': perPage, }; final response = await _apiClient.get( '${ApiEndpoints.warehouseLocations}/$warehouseId/equipment', queryParameters: queryParams, ); return WarehouseEquipmentListDto.fromJson(response.data); } catch (e) { throw _handleError(e); } } @override Future getWarehouseCapacity(int id) async { try { final response = await _apiClient.get( '${ApiEndpoints.warehouseLocations}/$id/capacity', ); return WarehouseCapacityInfo.fromJson(response.data); } catch (e) { throw _handleError(e); } } @override Future> getInUseWarehouseLocations() async { try { final response = await _apiClient.get( '${ApiEndpoints.warehouseLocations}/in-use', ); if (response.data is List) { return (response.data as List) .map((item) => WarehouseLocationDto.fromJson(item)) .toList(); } else { throw ApiException(message: 'Invalid response format'); } } catch (e) { throw _handleError(e); } } Exception _handleError(dynamic error) { if (error is ApiException) { return error; } return ApiException( message: error.toString(), ); } }