public async Task <DomainResult <ObjectId> > Handle(CreateConnectionCommand 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 <ObjectId>("Origin or Destination not found")); } var connection = new Connection(origin, destination, command.Time, command.Cost); var alreadyExists = await _connectionRepository.AlreadyExistsAsync(x => connection.IsTheSame(x)); if (alreadyExists) { return(DomainResult.Failure <ObjectId>("Connection already exists.", HttpStatusCode.Conflict)); } await _connectionRepository.CreateAsync(connection); await _mediator.Publish(new ConnectionCreatedEvent(connection)); return(DomainResult.Ok(connection.Id)); }
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 <DomainResult <string> > Handle(CreateUserSessionCommand command, CancellationToken cancellationToken) { var user = await _accountRepository.GetUserByEmailAndPassword(command.Email, command.Password); if (user is null) { return(DomainResult.Failure <string>("User not found", HttpStatusCode.Unauthorized)); } var token = _jwtAuthService.CreateJwtToken(user); return(DomainResult.Ok(token)); }
public async Task <DomainResult <BestRoutePath> > Handle(FindTheBestRoutePathCommand command, CancellationToken cancellationToken) { var route = await _routeRepository.FindAsync(command.RouteId); if (route is null) { return(DomainResult.Failure <BestRoutePath>("Route was not found")); } var points = await _pointRepository.GetAllActivePoints(); var connections = await _connectionRepository.GetAllActiveConnections(); return(new RouteService(connections, points).FindBestPath(route)); }
public DomainResult <BestRoutePath> FindBestPath(Route route) { var path = _dijkstra.FindBestPath(route.Origin.Id, route.Destination.Id); var wayPoints = FindWayPoints(path, route.Origin, route.Destination); if (wayPoints.Any() is false || path.Any() is false) { return(DomainResult.Failure <BestRoutePath>("Theres is no exist a valid path for this route")); } var weight = path.Last().Distance; var result = new BestRoutePath(route.Id, route.Origin, route.Destination, wayPoints, weight); return(DomainResult.Ok(result)); }
public async Task <DomainResult <ObjectId> > Handle(CreatePointCommand command, CancellationToken cancellationToken) { var point = Point.Create(command); var alreadyExists = await _pointRepository.AlreadyExistsAsync(x => x.IsTheSame(point)); if (alreadyExists) { return(DomainResult.Failure <ObjectId>("Point already exists", HttpStatusCode.Conflict)); } await _pointRepository.CreateAsync(point); await _mediator.Publish(new PointCreatedEvent(point)); return(DomainResult.Ok(point.Id)); }
public async Task <DomainResult> Handle(InactivePointCommand command, CancellationToken cancellationToken) { var point = await _pointRepository.FindAsync(command.Id); if (point is null) { return(DomainResult.Failure <string>("Point not found")); } point.Inactive(); await _pointRepository.UpdateAsync(point); await _mediator.Publish(new PointInactivatedEvent(point)); return(DomainResult.Ok()); }
public async Task <DomainResult> Handle(InactiveConnectionCommand command, CancellationToken cancellationToken) { var connection = await _connectionRepository.FindAsync(command.Id); if (connection is null) { return(DomainResult.Failure <string>("Connection not found")); } connection.Inactive(); await _connectionRepository.UpdateAsync(connection); await _mediator.Publish(new ConnectionInactivatedEvent(connection)); return(DomainResult.Ok()); }
public async Task <DomainResult> Handle(InactiveRouteCommand command, CancellationToken cancellationToken) { var route = await _routeRepository.FindAsync(command.Id); if (route is null) { return(DomainResult.Failure <string>("Route not found")); } route.Inactive(); await _routeRepository.UpdateAsync(route); await _mediator.Publish(new RouteInactivatedEvent(route)); return(DomainResult.Ok()); }
public async Task <DomainResult> Handle(UpdatedConnectionCommand 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")); } if (origin.Equals(destination)) { return(DomainResult.Failure <string>("You can't create a connection to the same origin and destination")); } var connection = await _connectionRepository.FindAsync(command.Id); if (connection is null) { return(DomainResult.Failure <string>("Connection not found")); } var arePointsChanged = connection.ArePointsChanged(origin, destination); connection.Update(origin, destination, command.Time, command.Cost); if (arePointsChanged && await _connectionRepository.AlreadyExistsAsync(x => connection.IsTheSame(x))) { return(DomainResult.Failure <string>("Connection already exists.", HttpStatusCode.Conflict)); } await _connectionRepository.UpdateAsync(connection); await _mediator.Publish(new ConnectionUpdatedEvent(connection)); return(DomainResult.Ok()); }
public async Task <DomainResult> Handle(UpdatePointCommand command, CancellationToken cancellationToken) { var point = await _pointRepository.FindAsync(command.Id); if (point is null) { return(DomainResult.Failure <string>("Point not found")); } point.Update(command); var alreadyExists = await _pointRepository.AlreadyExistsAsync(x => x.IsTheSame(point)); if (alreadyExists) { return(DomainResult.Failure <string>("Point already exists", HttpStatusCode.Conflict)); } await _pointRepository.UpdateAsync(point); await _mediator.Publish(new PointUpdatedEvent(point)); return(DomainResult.Ok()); }