Esempio n. 1
0
        public async Task <IActionResult> UpdateSettings(SettingsModel settingsModel)
        {
            User user = await userManager.GetUserAsync(User);

            Settings prevSettings = context.Settings.Find(user.Id);

            Settings settings = new Settings
            {
                UserId      = user.Id,
                HoursPerDay = settingsModel.HoursPerDay,
                CardsPerDay = settingsModel.CardsPerDay,
                CardsOrder  = Int32.Parse(settingsModel.CardsOrder),
                DarkTheme   = settingsModel.DarkTheme,
                ShowImages  = settingsModel.ShowImages
            };

            if (prevSettings != null)
            {
                context.Remove(prevSettings);
            }

            await context.AddAsync(settings);

            await context.SaveChangesAsync();

            return(View(nameof(Settings), settingsModel));
        }
Esempio n. 2
0
        public async Task <IActionResult> CreateDeck(CreateDeckModel model)
        {
            if (ModelState.IsValid)
            {
                var user = await _userManager.GetUserAsync(User);

                var deck = new Deck {
                    Name = model.Name, Creator = user
                };

                await _context.AddAsync(deck);

                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(EditDeck), new { deckId = deck.Id }));
            }

            return(View(new CreateDeckModel {
                Username = (await _userManager.GetUserAsync(User)).UserName
            }));
        }
Esempio n. 3
0
        public async Task <IActionResult> DeleteCard(long deckId, long cardId)
        {
            var user = await _userManager.GetUserAsync(User);

            var deck = await _context.Decks
                       .Where(deck => deck.Id == deckId)
                       .Include(deck => deck.Cards)
                       .FirstOrDefaultAsync();

            if (user.Id == deck.CreatorId)
            {
                var card = deck.Cards
                           .Where(card => card.Id == cardId)
                           .FirstOrDefault();

                deck.Cards.Remove(card);
                deck.CardNumber--;
                await _context.SaveChangesAsync();
            }

            return(RedirectToAction(nameof(DeckEditorController.EditDeck), "DeckEditor", new { deckId }));
        }
Esempio n. 4
0
        public async Task <IActionResult> DeleteDeckFromDatabase([FromRoute] long deckId)
        {
            var user = await _userManager.GetUserAsync(User);

            if (user is not null)
            {
                var deck = await _context.Decks.FindAsync(deckId);

                if (deck is not null && deck.CreatorId == user.Id)
                {
                    _context.Decks.Remove(deck);
                    await _context.SaveChangesAsync();
                }
            }

            return(RedirectToAction(nameof(HomeController.Index), "Home"));
        }