Exemplo n.º 1
0
        private async Task <RouteNodeAdded> InsertRouteNode(RouteNode routeNode)
        {
            await _geoDatabase.InsertRouteNode(routeNode);

            routeNode.ApplicationName = _applicationSettings.ApplicationName;
            return(_routeNodeEventFactory.CreateAdded(routeNode));
        }
Exemplo n.º 2
0
        public async Task Handle(ExistingRouteSegmentSplitted request, CancellationToken token)
        {
            _logger.LogInformation($"Starting {nameof(ExistingRouteSegmentSplittedHandler)}");

            var domainEvents = new List <RouteNetworkEvent>();

            if (request.InsertNode)
            {
                await _geoDatabase.InsertRouteNode(request.RouteNode);

                var routeNodeAddedEvent = _routeNodeEventFactory.CreateAdded(request.RouteNode);
                domainEvents.Add(routeNodeAddedEvent);
            }

            if (request.CreateNodeAddedEvent && !request.InsertNode)
            {
                var routeNodeAddedEvent = _routeNodeEventFactory.CreateAdded(request.RouteNode);
                domainEvents.Add(routeNodeAddedEvent);
            }

            var intersectingRouteSegment = await GetIntersectingRouteSegment(request.RouteSegmentDigitizedByUser, request.RouteNode);

            var routeSegmentsWkt = await _geoDatabase.GetRouteSegmentsSplittedByRouteNode(request.RouteNode, intersectingRouteSegment);

            var routeSegments = _routeSegmentFactory.Create(routeSegmentsWkt);

            SetSplittedRouteSegmentValuesToNewRouteSegments(routeSegments, intersectingRouteSegment);

            var routeSegmentAddedEvents = await InsertReplacementRouteSegments(routeSegments);

            domainEvents.AddRange(routeSegmentAddedEvents);

            var routeSegmentMarkedForDeletionEvent = await DeleteRouteSegment(intersectingRouteSegment, routeSegments);

            domainEvents.Add(routeSegmentMarkedForDeletionEvent);

            var cmdId = Guid.NewGuid();
            var routeNetworkCommand = new RouteNetworkCommand(nameof(ExistingRouteSegmentSplitted), cmdId, domainEvents.ToArray());

            _eventStore.Insert(routeNetworkCommand);
        }
        public async Task Handle(NewRouteSegmentDigitized request, CancellationToken token)
        {
            _logger.LogInformation($"Starting {nameof(NewRouteSegmentDigitizedHandler)}");

            if (request.RouteSegment is null)
            {
                throw new ArgumentNullException($"{nameof(RouteSegment)} cannot be null.");
            }

            var routeSegment = request.RouteSegment;
            var startNode    = (await _geoDatabase.GetIntersectingStartRouteNodes(routeSegment)).FirstOrDefault();
            var endNode      = (await _geoDatabase.GetIntersectingEndRouteNodes(routeSegment)).FirstOrDefault();

            if ((!(startNode is null) && !(endNode is null)) && startNode.Mrid == endNode.Mrid)
            {
                _logger.LogWarning($"Deleting RouteSegment with mrid '{routeSegment.Mrid}', because of both ends intersecting with the same RouteNode with mrid '{startNode.Mrid}'");
                await _geoDatabase.DeleteRouteSegment(routeSegment.Mrid);

                return;
            }

            var routeNetworkEvents = new List <RouteNetworkEvent>();

            if (startNode is null)
            {
                var startPoint = routeSegment.FindStartPoint();
                startNode              = _routeNodeFactory.Create(startPoint);
                startNode.Username     = routeSegment.Username;
                startNode.WorkTaskMrid = routeSegment.WorkTaskMrid;

                await _geoDatabase.InsertRouteNode(startNode);

                var startRouteNodeAddedEvent = _routeNodeEventFactory.CreateAdded(startNode);
                routeNetworkEvents.Add(startRouteNodeAddedEvent);
            }
            else if (_applicationSettings.EnableSegmentEndsAutoSnappingToRouteNode)
            {
                var lineString = routeSegment.GetLineString();
                lineString.Coordinates[0] = new Coordinate(startNode.GetPoint().Coordinate);
                routeSegment.Coord        = lineString.AsBinary();
                await _geoDatabase.UpdateRouteSegment(routeSegment);
            }

            if (endNode is null)
            {
                var endPoint = routeSegment.FindEndPoint();
                endNode              = _routeNodeFactory.Create(endPoint);
                endNode.Username     = routeSegment.Username;
                endNode.WorkTaskMrid = routeSegment.WorkTaskMrid;

                await _geoDatabase.InsertRouteNode(endNode);

                var endRouteNodeAddedEvent = _routeNodeEventFactory.CreateAdded(endNode);
                routeNetworkEvents.Add(endRouteNodeAddedEvent);
            }
            else if (_applicationSettings.EnableSegmentEndsAutoSnappingToRouteNode)
            {
                var lineString = routeSegment.GetLineString();
                lineString.Coordinates[lineString.Coordinates.Count() - 1] = new Coordinate(endNode.GetPoint().Coordinate);
                routeSegment.Coord = lineString.AsBinary();
                await _geoDatabase.UpdateRouteSegment(routeSegment);
            }

            var routeSegmentAddedEvent = _routeSegmentEventFactory.CreateAdded(routeSegment, startNode, endNode);

            routeNetworkEvents.Add(routeSegmentAddedEvent);

            var cmdId = Guid.NewGuid();
            var newRouteSegmentDigitizedCommand = new RouteNetworkCommand(nameof(NewRouteSegmentDigitized), cmdId, routeNetworkEvents.ToArray());

            _eventStore.Insert(newRouteSegmentDigitizedCommand);
        }