public async Task <DomainResult> Handle(UpdateRouteCommand command, CancellationToken cancellationToken)
        {
            var origin = await _pointRepository.FindAsync(command.OriginPointId);

            var destination = await _pointRepository.FindAsync(command.DestinationPointId);

            if (origin is null || destination is null)
            {
                return(DomainResult.Failure <string>("Origin or Destination not found"));
            }

            var route = await _routeRepository.FindAsync(command.Id);

            if (route is null)
            {
                return(DomainResult.Failure <string>("Route not found"));
            }

            var arePointsChanged = route.ArePointsChanged(origin, destination);

            route.Update(origin, destination);

            if (arePointsChanged && await _routeRepository.AlreadyExistsAsync(x => route.IsTheSame(x)))
            {
                return(DomainResult.Failure <string>("Route already exists", HttpStatusCode.Conflict));
            }

            await _routeRepository.UpdateAsync(route);

            await _mediator.Publish(new RouteUpdatedEvent(route));

            return(DomainResult.Ok());
        }
        public async Task <IActionResult> Update([FromBody] UpdateRouteCommand command)
        {
            var result = await _mediator.Send(command);

            return(result.Success
                ? NoContent()
                : Error(result.ErrorMessage, result.Code));
        }
        public async Task Handle_GivenNotFoundException()
        {
            //Arrange
            const int updatedId = 50;
            var       command   = new UpdateRouteCommand {
                RouteId = updatedId
            };

            //Assert
            await Assert.ThrowsAsync <NotFoundException>(() => _handler.Handle(command, CancellationToken.None));
        }
예제 #4
0
        public void UpdateRoute_CallIRouteRepositoryUpdate(
            [Frozen] IRouteRepository routeRepository,
            [Frozen] IRouteQueryables routeQueryables,
            UpdateRouteCommand message,
            RouteDto routeDto,
            Route route,
            UpdateRouteCommandHandler updateRouteCommandHandler)
        {
            //Information
            A.CallTo(() => routeQueryables.GetById(message.Route.RouteID)).Returns(routeDto);

            //Act
            updateRouteCommandHandler.ExecuteAsync(message);

            //Test
            A.CallTo(() => routeRepository.Update(message.Route)).MustHaveHappened();
        }
        public async Task Handle_GivenValidResult()
        {
            //Arrange
            const int updatedId = 13;
            var       command   = new UpdateRouteCommand {
                RouteId = updatedId, Enabled = false
            };

            //Act
            await _handler.Handle(command, CancellationToken.None);

            var unit = await Context.Routes.FindAsync(updatedId);

            //Assert
            Assert.NotNull(unit);
            Assert.Equal(updatedId, unit.RouteId);
            Assert.False(unit.Enabled);
        }
예제 #6
0
        public void UpdateRoute_NoUserWithThatIdExist(
            [Frozen] IRouteRepository routeRepository,
            [Frozen] IRouteQueryables routeQueryables,
            UpdateRouteCommand message,
            RouteDto routeDto,
            Route route,
            UpdateRouteCommandHandler updateRouteCommandHandler)
        {
            //Information
            A.CallTo(() => routeQueryables.GetById(message.Route.RouteID)).Returns(null);

            try
            {
                //Act
                updateRouteCommandHandler.ExecuteAsync(message);
            }
            catch (ArgumentNullException e)
            {
                Assert.AreEqual($"Value cannot be null.", e.Message);
            }
        }
예제 #7
0
        public async Task <IActionResult> Update([FromBody] UpdateRouteCommand command)
        {
            await Mediator.Send(command);

            return(NoContent());
        }