Exemplo n.º 1
0
        public async Task <FlightDto> UpdateFlight(UpdateFlightArgs args)
        {
            var flightToUpdate = await _flightsRepository.GetAsync(args.Id);

            if (flightToUpdate == null)
            {
                throw new FlightNotFoundException(Convert.ToInt32(StatusCodes.NotFound), string.Format(ErrorMessages.FLIGHT_NOT_FOUND, args.Id));
            }

            CheckFlightUnicity(args.Id, args.FlightNumber);

            var flightRelatedData = GetFlightRelatedData(args.DepartureAirportId, args.DestinationAirportId, args.AircraftId);

            var departure   = flightRelatedData.Departure;
            var destination = flightRelatedData.Destination;
            var aircraft    = flightRelatedData.Aircraft;

            var distance          = _flightHelper.CalculateDistance(new GpsCoordinates(departure.Latitude, departure.Longitude), new GpsCoordinates(departure.Latitude, destination.Longitude));
            var estimatedDuration = _flightHelper.CalculateDuration(distance, aircraft.MilesPerHour);


            _mapper.MapFromUpdateFlightArgs(flightToUpdate, args);

            flightToUpdate.Distance = distance;
            flightToUpdate.EstimatedFlightDuration = estimatedDuration;
            flightToUpdate.EstimatedFuelNeeded     = _flightHelper.CalculateFuelConsumption(estimatedDuration, aircraft.ConsumptionKgPerH, aircraft.TakeOffEffort);

            var updatedFlight = await _flightsRepository.UpdateAsync(flightToUpdate);

            return(_mapper.MapFromFlight(updatedFlight));
        }
Exemplo n.º 2
0
        public void FlightServicesTests_UpdateFlight_ShouldThrowDuplicateFlightException()
        {
            // Arrange
            _flightRepositoryMock.Setup(repo => repo.Search(It.IsAny <Expression <Func <Flight, bool> > >()))
            .Returns(new List <Flight>
            {
                _flightsRecordsMock.First()
            }.AsQueryable());

            var flightToUpdate = new UpdateFlightArgs
            {
                Id                   = 2,
                FlightNumber         = "FakeFlight_1",
                DepartureAirportId   = 1,
                DestinationAirportId = 2,
                AircraftId           = 1,
                FlightDate           = DateTime.Now,
                FlightTime           = DateTime.Now
            };

            var flightServices = new FlightServices(_flightRepositoryMock.Object,
                                                    _airportRepositoryMock.Object,
                                                    _aircraftRepositoryMock.Object,
                                                    _mapperWrapper);

            // Act
            Check.ThatCode(async() => await flightServices.UpdateFlight(flightToUpdate))
            .Throws <DuplicateFlightException>()
            .WithMessage(ErrorMessages.DUPLICATED_FLIGHT_NUMBER);
        }
Exemplo n.º 3
0
        public async Task FlightServicesTests_UpdateFlight_ShouldUpdateSuccessfulyFlight()
        {
            // Arrange
            _flightRepositoryMock.Setup(repo => repo.Search(It.IsAny <Expression <Func <Flight, bool> > >()))
            .Returns(new List <Flight>().AsQueryable());

            var flightToUpdate = new UpdateFlightArgs
            {
                Id                   = 1,
                FlightNumber         = "Updated_FakeFlight_1",
                DepartureAirportId   = 3,
                DestinationAirportId = 4,
                AircraftId           = 1,
                FlightDate           = DateTime.Now,
                FlightTime           = DateTime.Now
            };

            var flightServices = new FlightServices(_flightRepositoryMock.Object,
                                                    _airportRepositoryMock.Object,
                                                    _aircraftRepositoryMock.Object,
                                                    _mapperWrapper);

            // Act
            var updatedFlight = await flightServices.UpdateFlight(flightToUpdate);

            // Assert
            Check.That(updatedFlight).IsNotNull();
            _flightRepositoryMock.Verify(repo => repo.UpdateAsync(It.IsAny <Flight>()), Times.Once);
        }
Exemplo n.º 4
0
 public void MapFromUpdateFlightArgs(Flight flight, UpdateFlightArgs flightArgs)
 {
     flight.Id                   = flightArgs.Id;
     flight.FlightNumber         = flightArgs.FlightNumber;
     flight.AirportDepartureId   = flightArgs.DepartureAirportId;
     flight.AirportDestinationId = flightArgs.DestinationAirportId;
     flight.FlightDate           = flightArgs.FlightDate;
     flight.FlightTime           = flightArgs.FlightTime;
     flight.AircraftId           = flightArgs.AircraftId;
 }
Exemplo n.º 5
0
        public async Task <IActionResult> UpdateFlightPost(FlightDto flight)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var flightArgs = new UpdateFlightArgs
            {
                Id                   = flight.Id,
                FlightNumber         = flight.FlightNumber,
                DepartureAirportId   = flight.DepartureAirportId,
                DestinationAirportId = flight.DestinationAirportId,
                FlightDate           = flight.FlightDate,
                FlightTime           = flight.FlightTime,
                AircraftId           = flight.AircraftId
            };

            var updatedFlight = await _flightServices.UpdateFlight(flightArgs);

            return(RedirectToAction("index"));
        }
Exemplo n.º 6
0
        public void FlightServicesTests_UpdateFlight_ShouldThrowAircraftNotFoundException()
        {
            // Arrange
            var flightToUpdate = new UpdateFlightArgs
            {
                Id                   = 1,
                FlightNumber         = "FakeFlight_30",
                DepartureAirportId   = 1,
                DestinationAirportId = 2,
                AircraftId           = 134343,
                FlightDate           = DateTime.Now,
                FlightTime           = DateTime.Now
            };

            var flightServices = new FlightServices(_flightRepositoryMock.Object,
                                                    _airportRepositoryMock.Object,
                                                    _aircraftRepositoryMock.Object,
                                                    _mapperWrapper);

            // Act
            Check.ThatCode(async() => await flightServices.UpdateFlight(flightToUpdate))
            .Throws <AircraftNotFoundException>()
            .WithMessage(string.Format(ErrorMessages.AIRCRAFT_NOT_FOUND, flightToUpdate.AircraftId));
        }
Exemplo n.º 7
0
        public void FlightServicesTests_UpdateFlight_ShouldThrowSameDepartureAndDestinationAirportsException()
        {
            // Arrange
            var flightToUpdate = new UpdateFlightArgs
            {
                Id                   = 1,
                FlightNumber         = "FakeFlight_1",
                DepartureAirportId   = 1,
                DestinationAirportId = 1,
                AircraftId           = 1,
                FlightDate           = DateTime.Now,
                FlightTime           = DateTime.Now
            };

            var flightServices = new FlightServices(_flightRepositoryMock.Object,
                                                    _airportRepositoryMock.Object,
                                                    _aircraftRepositoryMock.Object,
                                                    _mapperWrapper);

            // Act
            Check.ThatCode(async() => await flightServices.UpdateFlight(flightToUpdate))
            .Throws <SameDepartureAndDestinationAirportsException>()
            .WithMessage(ErrorMessages.SAME_DEPARTURE_AND_DESTINATION);
        }