Example #1
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);
        }