public Task <Unit> Handle(RegisterWalkOfInterestCommand request, CancellationToken cancellationToken)
        {
            // Id check
            if (request.WalkOfInterestId == null || request.WalkOfInterestId == Guid.Empty)
            {
                throw new ArgumentException("Id cannot be null or empty");
            }

            // Basic check that some route element ids are filled in at all
            if (request.RouteElementIds == null || request.RouteElementIds.Count < 3)
            {
                throw new ArgumentException("The route element id list is empty or has less than 3 ids, which cannot possible be a valid walk. A minumum walk is from one node, through a segment, to another node (node->segment->node) - i.e. 3 ids.");
            }

            // Check if the chain of route element ids are valid (is proper connected to each other in the route network graph)
            bool         shouldBeNode      = true;
            bool         firstElement      = true;
            GraphElement previousElement   = null;
            Guid         previousElementId = Guid.Empty;

            int position = 1;

            foreach (var routeElementId in request.RouteElementIds)
            {
                GraphElement currentElement = null;

                // Node
                if (shouldBeNode)
                {
                    if (!routeQueryService.CheckIfRouteNodeIdExists(routeElementId))
                    {
                        throw new ArgumentException("Route element id: " + routeElementId + " at position: " + position + " (in the route element ids property) is expected to be a node. But no node could be found with that id.");
                    }

                    currentElement = routeQueryService.GetRouteNodeInfo(routeElementId);

                    shouldBeNode = false;
                }

                // Segment
                else if (!shouldBeNode)
                {
                    if (!routeQueryService.CheckIfRouteSegmentIdExists(routeElementId))
                    {
                        throw new ArgumentException("Route element id: " + routeElementId + " at position: " + position + " (in the route element ids property) is expected to be a segment. But no segment could be found with that id.");
                    }

                    currentElement = routeQueryService.GetRouteSegmentInfo(routeElementId);

                    shouldBeNode = true;
                }

                // Check that the element is connected to the previous specified element in the route network graph
                if (!firstElement)
                {
                    if (!currentElement.NeighborElements.Exists(n => n == previousElement))
                    {
                        throw new ArgumentException("Route element id: " + routeElementId + " at position: " + position + " (in the route element ids property) is not neighboor to previous id: " + previousElementId + " according to the route network graph. Please check that the walk is valid.");
                    }
                }

                position++;
                firstElement      = false;
                previousElementId = routeElementId;
                previousElement   = currentElement;
            }

            // Check if first id is a node
            if (!(routeQueryService.GetRouteElementInfo(request.RouteElementIds[0]) is RouteNodeInfo))
            {
                throw new ArgumentException("First route element id (in the route element ids property) must be a node, but it's not.");
            }

            // Check if last id is a node
            if (!(routeQueryService.GetRouteElementInfo(request.RouteElementIds[request.RouteElementIds.Count - 1]) is RouteNodeInfo))
            {
                throw new ArgumentException("Last route element id (in the route element ids property) must be a node, but it's not.");
            }


            var walkOfInterest = new WalkOfInterest(request.WalkOfInterestId, request.RouteElementIds);

            repo.Store(walkOfInterest);

            return(Unit.Task);
        }
Exemple #2
0
        /// <summary>
        /// Add a new route segment to the network
        /// </summary>
        /// <param name="routeSegmentId"></param>
        /// <param name="fromNodeId"></param>
        /// <param name="toNodeId"></param>
        /// <param name="segmentKind"></param>
        /// <param name="geometry"></param>
        public void AddRouteSegment(Guid routeSegmentId, Guid fromNodeId, Guid toNodeId, RouteSegmentKindEnum segmentKind, RouteNetwork.Events.Model.Geometry geometry)
        {
            // Id check
            if (routeSegmentId == null || routeSegmentId == Guid.Empty)
            {
                throw new ArgumentException("Id cannot be null or empty");
            }

            // Check that segment not already exists
            if (routeNetworkState.CheckIfRouteSegmentIdExists(routeSegmentId))
            {
                throw new ArgumentException("A route segment with id: " + routeSegmentId + " already exists");
            }

            // Check if from node exists
            if (!routeNetworkState.CheckIfRouteNodeIdExists(fromNodeId))
            {
                throw new ArgumentException("The FromNodeId parameter is invalid. Cannot find any node in the network with id: " + fromNodeId);
            }

            // Check if to node exists
            if (!routeNetworkState.CheckIfRouteNodeIdExists(toNodeId))
            {
                throw new ArgumentException("The ToNodeId parameter is invalid. Cannot find any node in the network with id: " + toNodeId);
            }

            // Check that we got some valid geometry
            if (geometry == null)
            {
                throw new ArgumentException("Cannot create route segment with id: " + routeSegmentId + " because Geometry is null, which is not allowed.");
            }

            if (geometry.GeoJsonType == null)
            {
                throw new ArgumentException("Cannot create route segment with id: " + routeSegmentId + " because Geometry.GeoJsonType is null, which is not allowed.");
            }

            if (geometry.GeoJsonType.ToLower() != "linestring")
            {
                throw new ArgumentException("Cannot create route segment with id: " + routeSegmentId + " because Geometry.GeoJsonType is: " + geometry.GeoJsonType + ", which is not allowed in route segments. Expected LineString.");
            }

            if (geometry.GeoJsonCoordinates == null)
            {
                throw new ArgumentException("Cannot create route segment with id: " + routeSegmentId + " because Geometry.GeoJsonCoordinates is null, which is not allowed.");
            }

            // Try parse geojson
            var line = GeometryConversionHelper.ConvertFromLineGeoJson(geometry.GeoJsonCoordinates);

            // Create domain event
            var routeSegmentPlanned = new RouteSegmentPlanned()
            {
                Id              = routeSegmentId,
                FromNodeId      = fromNodeId,
                ToNodeId        = toNodeId,
                SegmentKind     = segmentKind,
                InitialGeometry = geometry
            };

            RaiseEvent(routeSegmentPlanned, false);
        }