public async Task <IActionResult> RemoveTagFromChooseCard(ChooseCardModel model) { model.FilterTags = model.FilterTagsString .Split("#", StringSplitOptions.RemoveEmptyEntries) .ToList(); model.FilterTags.Remove(model.TagToRemove); model.TagFilter = String.Empty; if (model.FilterTags.Any()) { model.FilterTagsString = model.FilterTags.Aggregate((x, y) => "#" + x + "#" + y + "#"); if (!model.FilterTagsString.EndsWith('#')) { model.FilterTagsString += "#"; } } else { model.FilterTagsString = String.Empty; } return(View(nameof(ChooseCard), await CreateChooseCardModel(model))); }
public async Task <IActionResult> EditCard(ChooseCardModel model) { var card = await _context.Cards .Where(card => card.Id == model.CardId) .Include(card => card.Tags) .FirstOrDefaultAsync(); if (card is null) { return(View(new CardEditorModel { DeckId = model.DeckId, Tags = new List <TagModel>(), Errors = new List <string>(), })); } else { return(View(new CardEditorModel { Username = (await _userManager.GetUserAsync(User)).UserName, DeckId = model.DeckId, ExistingId = card.Id, Word = card.Word, Transcription = card.Transcription, Description = card.Description, Difficulty = card.Difficulty, Tags = card.Tags.Select(tag => new TagModel { Name = tag.Name }).ToList(), Errors = new List <string>(), })); } }
public async Task <IActionResult> ChooseCard(ChooseCardModel model) { var currUser = await _userManager.GetUserAsync(User); var user = await _context.Users .Where(u => u.UserName == currUser.UserName) .Include(u => u.Decks) .FirstOrDefaultAsync(); if (_context.Decks.Where(deck => deck.CreatorId == user.Id).Any()) { return(View(await CreateChooseCardModel(model))); } return(RedirectToAction(nameof(EditDeck), new { model.DeckId })); }
private async Task <ChooseCardModel> CreateChooseCardModel(ChooseCardModel model) { var userWithDecks = await _userManager.GetUserAsync(User); var cardsInModel = await _context.Cards .Select(card => card) .Include(card => card.Tags) .ToListAsync(); bool isSearchEmpty = true; if (model.SearchFilter is not null) { cardsInModel = cardsInModel .Where(card => card.Word .ToLower() .Contains(model.SearchFilter .ToLower())) .ToList(); isSearchEmpty = false; } if (!String.IsNullOrEmpty(model.TagFilter)) { if (model.FilterTagsString is null) { model.FilterTagsString = String.Empty; } if (!model.FilterTagsString.Contains($"#{model.TagFilter}#")) { model.FilterTagsString += $"#{model.TagFilter.ToLower().Split(' ').Aggregate((x, y) => x += "_" + y)}#"; } } if (!String.IsNullOrEmpty(model.FilterTagsString)) { model.FilterTags = model.FilterTagsString .Split("#", StringSplitOptions.RemoveEmptyEntries) .ToList(); cardsInModel = cardsInModel .Where(card => !model.FilterTags .Except(card.Tags is null ? new List <string>() : card.Tags.Select(tag => tag.Name)) .Any()) .ToList(); } else if (isSearchEmpty) { cardsInModel = new List <Card>(); } model.Cards = cardsInModel .Select(card => new CardModel { Id = card.Id, Word = card.Word, Description = card.Description }) .ToList(); model.TagFilter = String.Empty; return(model); }