/// <summary> /// Maps the user's preferences onto the specified view model as an asynchronous operation. /// </summary> /// <param name="model">The view model to map.</param> /// <param name="cancellationToken">The cancellation token to use.</param> /// <returns> /// A <see cref="Task"/> representing the asynchronous operation to map the view model. /// </returns> private async Task MapPreferencesAsync(LinePreferencesViewModel model, CancellationToken cancellationToken) { var user = await _userManager.GetUserAsync(User); if (user == null) { _logger?.LogError($"Failed to get user to render preferences."); return; } model.ETag = user.ETag; model.IsAuthenticated = true; model.IsLinkedToAlexa = !string.IsNullOrWhiteSpace(user.AlexaToken); ITflService service = _tflFactory.CreateService(); ICollection <LineInfo> lines = await service.GetLinesAsync(cancellationToken); MapFavoriteLines(model, lines, user.FavoriteLines); string updateResult = HttpContext.Request.Query["UpdateSuccess"].FirstOrDefault(); if (!string.IsNullOrEmpty(updateResult)) { model.UpdateResult = string.Equals(updateResult, bool.TrueString, StringComparison.OrdinalIgnoreCase); } }
/// <summary> /// Maps the user's favorite lines to the specified view model. /// </summary> /// <param name="model">The view model to map.</param> /// <param name="tflLines">The lines reported by the TfL service.</param> /// <param name="userFavorites">The user's favorite lines.</param> private void MapFavoriteLines( LinePreferencesViewModel model, ICollection <LineInfo> tflLines, ICollection <string> userFavorites) { if (tflLines.Count == 0) { _logger?.LogError($"Failed to map TfL lines as there were no values."); return; } foreach (LineInfo line in tflLines) { var favorite = new FavoriteLineItem() { DisplayName = line.Name, Id = line.Id, }; model.AllLines.Add(favorite); } model.AllLines .OrderBy((p) => p.DisplayName, StringComparer.Ordinal) .ToList(); if (userFavorites?.Count > 0) { foreach (var favorite in model.AllLines) { favorite.IsFavorite = userFavorites.Contains(favorite.Id, StringComparer.Ordinal); } } }
public async Task <IActionResult> Index(CancellationToken cancellationToken) { var model = new LinePreferencesViewModel(); if (User.Identity.IsAuthenticated) { await MapPreferencesAsync(model, cancellationToken); } return(View(model)); }