Exemple #1
0
        public async Task <IActionResult> Add(AddLocationViewModel model)
        {
            if (ModelState.IsValid)
            {
                Location location = await _client.AddLocationAsync(model.Name);

                ModelState.Clear();
                model.Clear();
                model.Message = $"Location '{location.Name}' added successfully";
            }

            return(View(model));
        }
Exemple #2
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);
        }