diff --git a/lib/api/grpc_client.dart b/lib/api/grpc_client.dart index 7ed5e0f..ec7fd9d 100644 --- a/lib/api/grpc_client.dart +++ b/lib/api/grpc_client.dart @@ -3,6 +3,7 @@ import 'dart:async'; import 'package:grpc/grpc.dart'; import '../generated/delivery_service.pbgrpc.dart'; +import '../models/delivery_route.dart'; import '../services/auth_service.dart'; import 'grpc_config.dart'; import 'types.dart'; @@ -241,6 +242,51 @@ class GrpcCqrsApiClient { } } + // ============================================================ + // Query Methods + // ============================================================ + + /// Gets all delivery routes. + /// + /// Returns a [Result] containing a list of [DeliveryRoute] objects. + /// Maps the gRPC [DeliveryRoutesResponse] to domain models. + /// + /// Example: + /// ```dart + /// final result = await client.getDeliveryRoutes(); + /// result.when( + /// success: (routes) => displayRoutes(routes), + /// onError: (error) => showError(error.message), + /// ); + /// ``` + Future>> getDeliveryRoutes() async { + final result = await _executeWithAuth( + (options) => deliveryClient.getDeliveryRoutes(Empty(), options: options), + ); + + return result.when( + success: (response) { + final routes = response.routes.map(_mapDeliveryRouteProto).toList(); + return Result.success(routes); + }, + onError: (error) => Result.error(error), + ); + } + + /// Maps a [DeliveryRouteProto] to a [DeliveryRoute] domain model. + DeliveryRoute _mapDeliveryRouteProto(DeliveryRouteProto proto) { + return DeliveryRoute( + id: proto.id, + routeId: proto.routeId, + name: proto.name, + routeName: proto.routeName, + deliveriesCount: proto.deliveriesCount, + deliveredCount: proto.deliveredCount, + completed: proto.completed, + createdAt: proto.createdAt, + ); + } + /// Shuts down the gRPC channel and releases resources. /// /// Should be called when the client is no longer needed to properly