示例#1
0
        public async Task <IActionResult> AddTag(DeckEditorModel model)
        {
            var user = await _userManager.GetUserAsync(User);

            var deck = await _context.Decks
                       .Where(deck => deck.Id == model.Deck.Id)
                       .Include(deck => deck.Tags)
                       .FirstOrDefaultAsync();

            if (deck.CreatorId == user.Id && !String.IsNullOrEmpty(model.TagInput))
            {
                string tagName = model.TagInput.ToLower().Split(' ').Aggregate((x, y) => x += "_" + y);
                var    tag     = await _context.DeckTags.FindAsync(tagName);

                if (tag is null)
                {
                    tag = new DeckTag {
                        Name = tagName
                    };
                    await _context.DeckTags.AddAsync(tag);
                }

                deck.Tags.Add(tag);
                _context.Decks.Update(deck);
                await _context.SaveChangesAsync();
            }

            return(RedirectToAction(nameof(EditDeck), new { model.Deck.Id }));
        }
示例#2
0
        public async Task <IActionResult> EditDeck(DeckEditorModel model)
        {
            var deck = await _context.Decks
                       .Select(deck => new DeckDto
            {
                Id         = deck.Id,
                Name       = deck.Name,
                Difficulty = deck.Difficulty,
                IsPublic   = deck.IsPublic,
                CreatorId  = deck.CreatorId,
                Cards      = deck.Cards.Select(card => card).ToList(),
                Tags       = deck.Tags.Select(tag => tag).ToList(),
            })
                       .Where(deck => deck.Id == model.Deck.Id)
                       .FirstOrDefaultAsync();

            var user = await _userManager.GetUserAsync(User);

            if (deck.CreatorId != user.Id)
            {
                return(NotFound());
            }

            var cards = deck.Cards.Select(card => new CardEditorModel
            {
                Username      = user.UserName,
                Id            = card.Id,
                DeckId        = deck.Id,
                Word          = card.Word,
                Transcription = card.Transcription,
                Description   = card.Description,
                Difficulty    = card.Difficulty,
            }).ToList();

            if (model.CardSearchFilter is not null && model.CardSearchFilter != "")
            {
                cards = cards.Where(card => card.Word.ToLower().Contains(model.CardSearchFilter.ToLower())).ToList();
            }

            var tags = deck.Tags.Select(tag => new TagModel {
                Name = tag.Name
            }).ToList();

            return(View(new DeckEditorModel
            {
                Username = user.UserName,
                Cards = cards,
                Deck = new DeckModel
                {
                    Id = deck.Id,
                    Name = deck.Name,
                    Difficulty = deck.Difficulty,
                    IsPublic = deck.IsPublic,
                },
                Tags = tags,
            }));
        }
示例#3
0
        public async Task <IActionResult> SaveChanges(DeckEditorModel model)
        {
            if (ModelState.IsValid)
            {
                var deck = await _context.Decks.FindAsync(model.Deck.Id);

                if (deck is not null)
                {
                    using (var ms = new MemoryStream())
                    {
                        if (model.Deck.Cover is not null)
                        {
                            model.Deck.Cover.CopyTo(ms);
                            deck.Cover = ms.ToArray();
                        }
                        else if (model.Deck.CoverRemoved)
                        {
                            deck.Cover = null;
                        }
                    }

                    using (var ms = new MemoryStream())
                    {
                        if (model.Deck.Thumb is not null)
                        {
                            model.Deck.Thumb.CopyTo(ms);
                            deck.Thumbnail = ms.ToArray();
                        }
                        else if (model.Deck.ThumbRemoved)
                        {
                            deck.Thumbnail = null;
                        }
                    }

                    deck.IsPublic   = model.Deck.IsPublic;
                    deck.Name       = model.Deck.Name;
                    deck.Difficulty = model.Deck.Difficulty;
                    _context.Decks.Update(deck);
                    _context.SaveChanges();
                }
            }

            return(RedirectToAction(nameof(EditDeck), new { model.Deck.Id }));
        }
示例#4
0
 public void Setup()
 {
     _deckEditor       = new DeckEditorModel();
     _collectionEditor = new CollectionEditorModel();
 }