public ServiceOperationResult UpdateAvailability(Guid aircraftAvailabilityId, Guid aircraftId, int?reroutingRadius,
                                                         List <AircraftAvailabilityLocationDto> departureLocations,
                                                         List <AircraftAvailabilityLocationDto> arrivalLocations, List <AircraftAvailabilityPeriodDto> availabileDates,
                                                         decimal?pricePerHour, decimal?minimumAcceptablePrice, bool sellCharterSeat)
        {
            using (FlyJetsDbContext dbContext = new FlyJetsDbContext(_config))
            {
                var operationResult = new ServiceOperationResult();
                operationResult.IsSuccessfull = true;

                var availability = dbContext.AircraftsAvailability
                                   .FirstOrDefault(av => av.Id == aircraftAvailabilityId);

                if (availability == null)
                {
                    operationResult.Errors = new List <ErrorCodes>()
                    {
                        ErrorCodes.NotFound
                    };
                    operationResult.IsSuccessfull = false;

                    return(operationResult);
                }

                availability.AircraftId   = aircraftId;
                availability.PricePerHour = pricePerHour;
                availability.MinimumAcceptablePricePerTrip = minimumAcceptablePrice;
                availability.SellCharterSeat = sellCharterSeat;
                availability.ReroutingRadius = reroutingRadius;

                var oldLocations = dbContext.AircraftAvailabilityLocations
                                   .Where(loc => loc.AircraftAvailabilityId == aircraftAvailabilityId)
                                   .ToList();

                foreach (var location in oldLocations)
                {
                    dbContext.Entry(location).State = EntityState.Deleted;
                }

                availability.Locations = new List <AircraftAvailabilityLocation>();

                if (reroutingRadius.HasValue)
                {
                    var homebaseLoc = (from aircraft in dbContext.Aircrafts
                                       join homebase in dbContext.LocationsTree on aircraft.HomeBaseId equals homebase.Id
                                       where aircraft.Id == aircraftId
                                       select new { Lat = homebase.Lat, Lng = homebase.Lng })
                                      .First();

                    var reroutingLocations = _locationService.GetLocationsWithinXMiles(homebaseLoc.Lat.Value, homebaseLoc.Lng.Value,
                                                                                       reroutingRadius.Value, (byte)LocationsTypes.Airport);

                    foreach (var reroutingLocation in reroutingLocations)
                    {
                        var newLoc = new AircraftAvailabilityLocation()
                        {
                            Id = Guid.NewGuid(),
                            AircraftAvailabilityId = availability.Id,
                            LocationTreeId         = reroutingLocation.Id,
                            IsForDeparture         = true,
                            Rerouting = true
                        };

                        availability.Locations.Add(newLoc);
                    }
                }

                foreach (var departureLocation in departureLocations)
                {
                    var newLoc = new AircraftAvailabilityLocation()
                    {
                        Id = Guid.NewGuid(),
                        AircraftAvailabilityId = availability.Id,
                        LocationTreeId         = departureLocation.LocationTreeId,
                        IsForDeparture         = true,
                        Rerouting = false
                    };

                    availability.Locations.Add(newLoc);
                }

                foreach (var arrivalLocation in arrivalLocations)
                {
                    var newLoc = new AircraftAvailabilityLocation()
                    {
                        Id = Guid.NewGuid(),
                        AircraftAvailabilityId = availability.Id,
                        LocationTreeId         = arrivalLocation.LocationTreeId,
                        IsForDeparture         = false,
                        Rerouting = false
                    };

                    availability.Locations.Add(newLoc);
                }

                var oldPeriods = dbContext.AircraftsAvailabilityPeriods
                                 .Where(p => p.AircraftAvailabilityId == aircraftAvailabilityId)
                                 .ToList();

                foreach (var period in oldPeriods)
                {
                    dbContext.Entry(period).State = EntityState.Deleted;
                }

                availability.Periods = new List <AircraftAvailabilityPeriod>();

                foreach (var availableDate in availabileDates)
                {
                    availability.Periods.Add(new AircraftAvailabilityPeriod()
                    {
                        Id = Guid.NewGuid(),
                        AircraftAvailabilityId = availability.Id,
                        From = availableDate.From,
                        To   = availableDate.To
                    });
                }

                dbContext.SaveChanges();

                return(operationResult);
            }
        }
        public ServiceOperationResult Update(Guid aircraftId, string tailNumber, Guid typeId, Guid modelId, int HomeBaseId,
                                             string argusSafetyRating, string wyvernSafetyRating, short?manufactureYear, short?lastIntRefurbish,
                                             short?lastExtRefurbish, byte maxPassengers, short?hoursFlown, short speed, short range,
                                             bool wiFi, bool bookableDemo, short?numberOfTelevision, short?cargoCapability, bool sellAsCharterAircraft,
                                             bool sellAsCharterSeat, decimal pricePerHour, List <AircraftDocumentDto> images, List <AircraftDocumentDto> documents)
        {
            using (FlyJetsDbContext dbContext = new FlyJetsDbContext(_config))
            {
                ServiceOperationResult result = new ServiceOperationResult();
                result.IsSuccessfull = true;

                var aircraft = dbContext.Aircrafts
                               .Include("Images")
                               .Include("Documents")
                               .FirstOrDefault(a => a.Id == aircraftId);

                aircraft.TailNumber         = tailNumber;
                aircraft.TypeId             = typeId;
                aircraft.ModelId            = modelId;
                aircraft.HomeBaseId         = HomeBaseId;
                aircraft.ArgusSafetyRating  = argusSafetyRating;
                aircraft.WyvernSafetyRating = wyvernSafetyRating;
                aircraft.ManufactureYear    = manufactureYear;
                aircraft.LastIntRefurbish   = lastIntRefurbish;
                aircraft.LastExtRefurbish   = lastExtRefurbish;
                aircraft.MaxPassengers      = maxPassengers;
                aircraft.HoursFlown         = hoursFlown;
                aircraft.Speed                 = speed;
                aircraft.Range                 = range;
                aircraft.WiFi                  = wiFi;
                aircraft.BookableDemo          = bookableDemo;
                aircraft.Television            = numberOfTelevision.HasValue && numberOfTelevision.Value > 0 ? true : false;
                aircraft.NumberOfTelevision    = numberOfTelevision;
                aircraft.CargoCapability       = cargoCapability;
                aircraft.SellAsCharterAircraft = true; // sellAsCharterAircraft;
                aircraft.SellAsCharterSeat     = true; // sellAsCharterSeat;
                aircraft.PricePerHour          = pricePerHour;

                foreach (var image in images)
                {
                    var currentImage = aircraft.Images.FirstOrDefault(img => img.Order == image.Order);

                    if (currentImage != null)
                    {
                        currentImage.FileName = image.FileName;
                    }
                    else
                    {
                        aircraft.Images.Add(new AircraftImage()
                        {
                            Id         = Guid.NewGuid(),
                            AircraftId = aircraft.Id,
                            Name       = image.Name,
                            FileName   = image.FileName,
                            Order      = image.Order
                        });
                    }
                }

                foreach (var document in documents)
                {
                    aircraft.Documents.Add(new AircraftDocument()
                    {
                        Id         = Guid.NewGuid(),
                        AircraftId = aircraft.Id,
                        FileName   = document.FileName,
                        Name       = "Document",
                        Type       = document.Type
                    });
                }



                dbContext.SaveChanges();

                return(result);
            }
        }
        public ServiceOperationResult CreateAvailability(Guid aircraftId, int?reroutingRadius,
                                                         List <AircraftAvailabilityLocationDto> departureLocations,
                                                         List <AircraftAvailabilityLocationDto> arrivalLocations, List <AircraftAvailabilityPeriodDto> availabileDates,
                                                         decimal?pricePerHour, decimal?minimumAcceptablePrice, bool sellCharterSeat)
        {
            using (var dbContext = new FlyJetsDbContext(_config))
            {
                ServiceOperationResult result = new ServiceOperationResult();

                result.IsSuccessfull = true;

                var newAvailability = new AircraftAvailability()
                {
                    Id           = Guid.NewGuid(),
                    AircraftId   = aircraftId,
                    PricePerHour = pricePerHour,
                    MinimumAcceptablePricePerTrip = minimumAcceptablePrice,
                    CreatedOn       = DateTime.UtcNow,
                    CreatedById     = _accountId,
                    SellCharterSeat = sellCharterSeat,
                    ReroutingRadius = reroutingRadius,
                    Available       = true
                };

                newAvailability.Locations = new List <AircraftAvailabilityLocation>();

                if (reroutingRadius.HasValue)
                {
                    var homebaseLoc = (from aircraft in dbContext.Aircrafts
                                       join homebase in dbContext.LocationsTree on aircraft.HomeBaseId equals homebase.Id
                                       where aircraft.Id == aircraftId
                                       select new { Lat = homebase.Lat, Lng = homebase.Lng })
                                      .First();

                    var reroutingLocations = _locationService.GetLocationsWithinXMiles(homebaseLoc.Lat.Value, homebaseLoc.Lng.Value,
                                                                                       reroutingRadius.Value, (byte)LocationsTypes.Airport);

                    foreach (var reroutingLocation in reroutingLocations)
                    {
                        var newLoc = new AircraftAvailabilityLocation()
                        {
                            Id = Guid.NewGuid(),
                            AircraftAvailabilityId = newAvailability.Id,
                            LocationTreeId         = reroutingLocation.Id,
                            IsForDeparture         = true,
                            Rerouting = true
                        };

                        newAvailability.Locations.Add(newLoc);
                    }
                }

                foreach (var departureLocation in departureLocations)
                {
                    var newLoc = new AircraftAvailabilityLocation()
                    {
                        Id = Guid.NewGuid(),
                        AircraftAvailabilityId = newAvailability.Id,
                        LocationTreeId         = departureLocation.LocationTreeId,
                        IsForDeparture         = true,
                        Rerouting = false
                    };

                    newAvailability.Locations.Add(newLoc);
                }

                foreach (var arrivalLocation in arrivalLocations)
                {
                    var newLoc = new AircraftAvailabilityLocation()
                    {
                        Id = Guid.NewGuid(),
                        AircraftAvailabilityId = newAvailability.Id,
                        LocationTreeId         = arrivalLocation.LocationTreeId,
                        IsForDeparture         = false,
                        Rerouting = false
                    };

                    newAvailability.Locations.Add(newLoc);
                }

                newAvailability.Periods = new List <AircraftAvailabilityPeriod>();

                foreach (var availableDate in availabileDates)
                {
                    newAvailability.Periods.Add(new AircraftAvailabilityPeriod()
                    {
                        Id = Guid.NewGuid(),
                        AircraftAvailabilityId = newAvailability.Id,
                        From = availableDate.From,
                        To   = availableDate.To
                    });
                }

                dbContext.AircraftsAvailability.Add(newAvailability);
                dbContext.SaveChanges();

                return(result);
            }
        }
        public ServiceOperationResult Create(string tailNumber, Guid typeId, Guid modelId, int HomeBaseId,
                                             string argusSafetyRating, string wyvernSafetyRating, short?manufactureYear, short?lastIntRefurbish,
                                             short?lastExtRefurbish, byte maxPassengers, short?hoursFlown, short speed, short range,
                                             bool wiFi, bool bookableDemo, short?numberOfTelevision, short?cargoCapability, bool sellAsCharterAircraft,
                                             bool sellAsCharterSeat, decimal pricePerHour, List <AircraftDocumentDto> images, List <AircraftDocumentDto> documents)
        {
            using (FlyJetsDbContext dbContext = new FlyJetsDbContext(_config))
            {
                ServiceOperationResult result = new ServiceOperationResult();
                result.IsSuccessfull = true;

                var newAircraft = new Aircraft();

                newAircraft.Id                    = Guid.NewGuid();
                newAircraft.TailNumber            = tailNumber;
                newAircraft.TypeId                = typeId;
                newAircraft.ModelId               = modelId;
                newAircraft.HomeBaseId            = HomeBaseId;
                newAircraft.ArgusSafetyRating     = argusSafetyRating;
                newAircraft.WyvernSafetyRating    = wyvernSafetyRating;
                newAircraft.ManufactureYear       = manufactureYear;
                newAircraft.LastIntRefurbish      = lastIntRefurbish;
                newAircraft.LastExtRefurbish      = lastExtRefurbish;
                newAircraft.MaxPassengers         = maxPassengers;
                newAircraft.HoursFlown            = hoursFlown;
                newAircraft.Speed                 = speed;
                newAircraft.Range                 = range;
                newAircraft.WiFi                  = wiFi;
                newAircraft.BookableDemo          = bookableDemo;
                newAircraft.Television            = numberOfTelevision.HasValue;
                newAircraft.NumberOfTelevision    = numberOfTelevision;
                newAircraft.CargoCapability       = cargoCapability;
                newAircraft.SellAsCharterAircraft = true; //sellAsCharterAircraft;
                newAircraft.SellAsCharterSeat     = true; //sellAsCharterSeat;
                newAircraft.PricePerHour          = pricePerHour;
                newAircraft.Available             = true;

                newAircraft.ProviderId  = _accountId;
                newAircraft.CreatedById = _accountId;
                newAircraft.CreatedOn   = DateTime.UtcNow;

                newAircraft.Images = new List <AircraftImage>();

                foreach (var image in images)
                {
                    newAircraft.Images.Add(new AircraftImage()
                    {
                        Id         = Guid.NewGuid(),
                        AircraftId = newAircraft.Id,
                        Name       = image.Name,
                        FileName   = image.FileName,
                        Order      = image.Order
                    });
                }

                newAircraft.Documents = new List <AircraftDocument>();

                foreach (var document in documents)
                {
                    newAircraft.Documents.Add(new AircraftDocument()
                    {
                        Id         = Guid.NewGuid(),
                        AircraftId = newAircraft.Id,
                        FileName   = document.FileName,
                        Type       = document.Type,
                        Name       = "Document"
                    });
                }


                dbContext.Aircrafts.Add(newAircraft);
                dbContext.SaveChanges();

                return(result);
            }
        }