public bool DeleteFlashcard(int flashcardId)
        {
            var flashcardToRemove = _context.Flashcards.SingleOrDefault(f => f.Id == flashcardId);

            if (flashcardToRemove == null)
            {
                return(false);
            }
            _context.Flashcards.Remove(flashcardToRemove);
            _context.SaveChangesAsync();
            return(true);
        }
Esempio n. 2
0
        public bool DeleteCardset(int cardsetId)
        {
            var cardsetToRemove = _context.Cardsets.SingleOrDefault(c => c.Id == cardsetId);

            if (cardsetToRemove == null)
            {
                return(false);
            }
            _context.Cardsets.Remove(cardsetToRemove);
            _context.SaveChangesAsync();
            return(true);
        }
Esempio n. 3
0
        /// <summary>
        /// Adds the word asynchronous.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <returns>
        /// return <see cref="T:System.Threading.Tasks.Task" />
        /// </returns>
        /// <exception cref="NotFoundException">Category not found</exception>
        public async Task AddAsync(WordModel model)
        {
            if (string.IsNullOrWhiteSpace(model.Value))
            {
                throw new BadRequestException($"{nameof(model.Value)} could not be empty");
            }

            model.Categories.ThrowIfEmpty();

            var categories = _flashcardDbContext.Categories.Where(c =>
                                                                  model.Categories.Any(cm => cm.Id == c.Id));

            if (categories?.Count() == 0)
            {
                throw new NotFoundException("You must provide at least one category");
            }

            if (_flashcardDbContext.Words.Include(w => w.ParentWord).Any(w =>
                                                                         w.ParentWord == null && w.Value.ToString().Trim().ToLower() == model.Value.Trim().ToLower()))
            {
                throw new BadRequestException($"Word with the {model.Value} word exists");
            }

            var parentWord = await AddAsync(categories, model);

            await AddChildrenWords(categories, parentWord, model.TranslatedWords);

            try
            {
                await _flashcardDbContext.SaveChangesAsync();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
Esempio n. 4
0
        /// <summary>
        /// This function sorts the specified cards.
        /// </summary>
        public async Task <int> OrderCardNumbers(int deckId)
        {
            var deck = await dbContext.Decks.FindAsync(deckId);

            if (deck == null)
            {
                return(0);
            }

            var cards = await dbContext.Cards.Where(c => c.DeckId == deckId).ToListAsync();

            for (int i = 0; i < cards.Count; i++)
            {
                if (cards[i].Card_number != i)
                {
                    cards[i].Card_number = i;
                }
            }
            await dbContext.SaveChangesAsync();

            return(1);
        }