コード例 #1
0
        /// <summary>
        /// Get the sighting ID for the current sighting being edited, which will
        /// be null for new sightings
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public int?GetCurrentSightingId(string userName)
        {
            string key = GetCacheKey(SightingDetailsKeyPrefix, userName);
            SightingDetailsViewModel model = _cache.Get <SightingDetailsViewModel>(key);

            return(model?.SightingId);
        }
コード例 #2
0
        public async Task <IActionResult> Index(SightingDetailsViewModel model)
        {
            IActionResult result;
            string        newLocation  = (model.NewLocation ?? "").Trim();
            bool          haveLocation = (model.LocationId > 0) || !string.IsNullOrEmpty(newLocation);

            if (haveLocation && ModelState.IsValid)
            {
                _wizard.CacheSightingDetailsModel(model, User.Identity.Name);
                result = RedirectToAction("Index", "FlightDetails", new { number = model.FlightNumber });
            }
            else
            {
                if (!haveLocation)
                {
                    model.LocationErrorMessage = "You must select a location or give a new location name";
                }

                List <Location> locations = await _wizard.GetLocationsAsync();

                model.SetLocations(locations);
                result = View(model);
            }

            return(result);
        }
コード例 #3
0
        /// <summary>
        /// Retrieve or construct the sighting details model
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public async Task <SightingDetailsViewModel> GetSightingDetailsModelAsync(string userName, int?sightingId)
        {
            // Retrieve the model from the cache
            string key = GetCacheKey(SightingDetailsKeyPrefix, userName);
            SightingDetailsViewModel model = _cache.Get <SightingDetailsViewModel>(key);

            if ((model == null) || (model.SightingId != sightingId))
            {
                // Not cached or the ID has changed, so create a new one and set the "last sighting added" message
                string lastAdded = GetLastSightingAddedMessage(userName);
                ClearCachedLastSightingAddedMessage(userName);

                // If an existing sighting is specified, construct the model using its
                // details
                if (sightingId != null)
                {
                    Sighting sighting = await _sightings.GetSightingAsync(sightingId ?? 0);

                    model = new SightingDetailsViewModel
                    {
                        SightingId = sightingId,
                        LastSightingAddedMessage = lastAdded,
                        Altitude     = sighting.Altitude,
                        Date         = sighting.Date,
                        FlightNumber = sighting.Flight.Number,
                        LocationId   = sighting.LocationId,
                        Registration = sighting.Aircraft.Registration
                    };
                }
                else
                {
                    model = new SightingDetailsViewModel
                    {
                        LastSightingAddedMessage = lastAdded,
                        Date       = GetDefaultDate(userName),
                        LocationId = GetDefaultLocationId(userName)
                    };
                }
            }

            // Set the available locations
            List <Location> locations = await GetLocationsAsync();

            model.SetLocations(locations);

            return(model);
        }
コード例 #4
0
        /// <summary>
        /// Retrieve or constuct the flight details model
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public async Task <AircraftDetailsViewModel> GetAircraftDetailsModelAsync(string userName)
        {
            // Retrieve the model from the cache
            string key = GetCacheKey(AircraftDetailsKeyPrefix, userName);
            AircraftDetailsViewModel model = _cache.Get <AircraftDetailsViewModel>(key);

            if (model == null)
            {
                // Not cached, so create a new one, using the cached sighting details
                // model to supply the aircraft registration
                key = GetCacheKey(SightingDetailsKeyPrefix, userName);
                SightingDetailsViewModel sighting = _cache.Get <SightingDetailsViewModel>(key);
                model = new AircraftDetailsViewModel {
                    Registration = sighting.Registration
                };
            }

            // Set the list of available manufacturers
            List <Manufacturer> manufacturers = await GetManufacturersAsync();

            model.SetManufacturers(manufacturers);

            // See if this is an existing aircraft
            Aircraft aircraft = await _aircraft.GetAircraftByRegistrationAsync(model.Registration);

            if (aircraft != null)
            {
                // It it, so assign the aircraft properties
                model.AircraftId     = aircraft.Id;
                model.SerialNumber   = aircraft.SerialNumber;
                model.ManufacturerId = aircraft.Model.ManufacturerId;
                model.ModelId        = aircraft.ModelId;
                model.Age            = DateTime.Now.Year - aircraft.Manufactured;

                // Load the models for the aircraft's manufacturer
                List <Model> models = await GetModelsAsync(model.ManufacturerId ?? 0);

                model.SetModels(models);
            }

            return(model);
        }
コード例 #5
0
        /// <summary>
        /// Retrieve or constuct the flight details model
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public async Task <FlightDetailsViewModel> GetFlightDetailsModelAsync(string userName)
        {
            // Retrieve the model from the cache
            string key = GetCacheKey(FlightDetailsKeyPrefix, userName);
            FlightDetailsViewModel model = _cache.Get <FlightDetailsViewModel>(key);

            if (model == null)
            {
                // Not cached, so create a new one, using the cached sighting details
                // model to supply the flight number
                key = GetCacheKey(SightingDetailsKeyPrefix, userName);
                SightingDetailsViewModel sighting = _cache.Get <SightingDetailsViewModel>(key);
                model = new FlightDetailsViewModel {
                    FlightNumber = sighting.FlightNumber
                };
            }

            // Set the available airlines
            List <Airline> airlines = await GetAirlinesAsync();

            model.SetAirlines(airlines);

            // Set the matching flight numbers
            List <Flight> flights = await GetFlightsAsync(model.FlightNumber);

            model.SetFlights(flights);

            // If we have any flights, pick the first one as the default selection from
            // which to populate the model
            Flight flight = flights?.FirstOrDefault();

            if (flight != null)
            {
                model.FlightId    = flight.Id;
                model.Embarkation = flight.Embarkation;
                model.Destination = flight.Destination;
                model.AirlineId   = flight.AirlineId;
            }

            return(model);
        }
コード例 #6
0
        /// <summary>
        /// Create a new sighting
        /// </summary>
        /// <param name="userName"></param>
        public async Task <Sighting> CreateSighting(string userName)
        {
            Sighting sighting = null;

            // Clear the last sighting added message
            ClearCachedLastSightingAddedMessage(userName);

            // Retrieve the sighting details from the cache
            string key = GetCacheKey(SightingDetailsKeyPrefix, userName);
            SightingDetailsViewModel details = _cache.Get <SightingDetailsViewModel>(key);

            if (details != null)
            {
                // Create the aircraft and flight, first
                Aircraft aircraft = await RetrieveOrCreateAircraft(userName);

                Flight flight = await RetrieveOrCreateFlight(userName);

                // Create the location, if required
                if (details.LocationId == 0)
                {
                    Location location = await _locations.AddLocationAsync(details.NewLocation);

                    details.LocationId = location.Id;
                }

                // If an existing sighting is being edited, then update it. Otherwise, create
                // a new one
                string message;
                if (details.SightingId != null)
                {
                    sighting = await _sightings.UpdateSightingAsync(details.SightingId ?? 0, details.Date ?? DateTime.Now, details.Altitude ?? 0, aircraft.Id, flight.Id, details.LocationId);

                    message = $"Your sighting of flight {sighting.Flight.Number}, " +
                              $"aircraft {sighting.Aircraft.Registration} ({sighting.Aircraft.Model.Manufacturer.Name} {sighting.Aircraft.Model.Name}), " +
                              $"at {sighting.Location.Name} on {sighting.Date.ToString("dd-MMM-yyyy")} " +
                              $"has been updated";
                }
                else
                {
                    sighting = await _sightings.AddSightingAsync(details.Date ?? DateTime.Now, details.Altitude ?? 0, aircraft.Id, flight.Id, details.LocationId);

                    message = $"Your sighting of flight {sighting.Flight.Number}, " +
                              $"aircraft {sighting.Aircraft.Registration} ({sighting.Aircraft.Model.Manufacturer.Name} {sighting.Aircraft.Model.Name}), " +
                              $"at {sighting.Location.Name} on {sighting.Date.ToString("dd-MMM-yyyy")} " +
                              $"has been added to the database";
                }

                // Cache the message giving its details and other properties that are
                // cached to improve data entry speed
                key = GetCacheKey(LastSightingAddedKeyPrefix, userName);
                _cache.Set <string>(key, message, _settings.Value.CacheLifetimeSeconds);

                key = GetCacheKey(DefaultDateKeyPrefix, userName);
                _cache.Set <DateTime>(key, sighting.Date, _settings.Value.CacheLifetimeSeconds);

                key = GetCacheKey(DefaultLocationKeyPrefix, userName);
                _cache.Set <int>(key, sighting.LocationId, _settings.Value.CacheLifetimeSeconds);
            }

            // Clear the cached data
            Reset(userName);

            return(sighting);
        }
コード例 #7
0
        /// <summary>
        /// Cache the sighting details view model
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="model"></param>
        public void CacheSightingDetailsModel(SightingDetailsViewModel model, string userName)
        {
            string key = GetCacheKey(SightingDetailsKeyPrefix, userName);

            _cache.Set <SightingDetailsViewModel>(key, model, _settings.Value.CacheLifetimeSeconds);
        }
コード例 #8
0
        /// <summary>
        /// Construct the model to confirm sighting details
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public async Task <ConfirmDetailsViewModel> GetConfirmDetailsModelAsync(string userName)
        {
            // Get the sighting, flight details and aircraft models and map them
            // into the confirm details model
            ConfirmDetailsViewModel model = new ConfirmDetailsViewModel();

            string key = GetCacheKey(SightingDetailsKeyPrefix, userName);
            SightingDetailsViewModel sighting = _cache.Get <SightingDetailsViewModel>(key);

            _mapper.Map <SightingDetailsViewModel, ConfirmDetailsViewModel>(sighting, model);

            key = GetCacheKey(FlightDetailsKeyPrefix, userName);
            FlightDetailsViewModel flight = _cache.Get <FlightDetailsViewModel>(key);

            _mapper.Map <FlightDetailsViewModel, ConfirmDetailsViewModel>(flight, model);

            key = GetCacheKey(AircraftDetailsKeyPrefix, userName);
            AircraftDetailsViewModel aircraft = _cache.Get <AircraftDetailsViewModel>(key);

            _mapper.Map <AircraftDetailsViewModel, ConfirmDetailsViewModel>(aircraft, model);

            // For the location, if we have a new location specified then use that as the
            // location. Otherwise, look up the location by its ID
            if (sighting.LocationId == 0)
            {
                model.Location = sighting.NewLocation;
            }
            else
            {
                Location location = await _locations.GetLocationAsync(sighting.LocationId);

                model.Location = location.Name;
            }

            // Repeat the above logic for the airline
            if (flight.AirlineId == 0)
            {
                model.Airline = flight.NewAirline;
            }
            else
            {
                Airline airline = await _airlines.GetAirlineAsync(flight.AirlineId);

                model.Airline = airline.Name;
            }

            // Repeat the above logic for the manufacturer
            if ((aircraft.ManufacturerId ?? 0) == 0)
            {
                model.Manufacturer = aircraft.NewManufacturer;
            }
            else
            {
                int          manufacturerId = aircraft.ManufacturerId ?? 0;
                Manufacturer manufacturer   = await _manufacturers.GetManufacturerAsync(manufacturerId);

                model.Manufacturer = manufacturer.Name;
            }

            // Repeat the above logic for the model
            if ((aircraft.ModelId ?? 0) == 0)
            {
                model.Model = aircraft.NewModel;
            }
            else
            {
                int   modelId       = aircraft.ModelId ?? 0;
                Model aircraftModel = await _models.GetModelAsync(modelId);

                model.Model = aircraftModel.Name;
            }

            return(model);
        }
コード例 #9
0
        public async Task <IActionResult> Index(int?id = null)
        {
            SightingDetailsViewModel model = await _wizard.GetSightingDetailsModelAsync(User.Identity.Name, id);

            return(View(model));
        }