Ejemplo n.º 1
0
        /// <summary>
        /// Randoms the words.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <returns>
        /// Exam words for test
        /// </returns>
        public async Task <ExamTest> RandomWords(StartRequestExamTestModel model)
        {
            var knownLanguageType =
                await _flashcardDbContext.LanguageTypes.FirstOrDefaultAsync(l => l.Id == model.KnownLanguageTypeId);

            if (knownLanguageType == null)
            {
                throw new NotFoundException("Known language type not found");
            }

            var learningLanguageType =
                await _flashcardDbContext.LanguageTypes.FirstOrDefaultAsync(l => l.Id == model.LearningLanguageTypeId);

            if (learningLanguageType == null)
            {
                throw new NotFoundException("Learning language type not found");
            }

            var examTest = new ExamTest
            {
                KnownLanguageType    = knownLanguageType,
                LearningLanguageType = learningLanguageType,
                Created = DateTime.Now
            };

            using (var transaction = _flashcardDbContext.Database.BeginTransaction())
            {
                try
                {
                    await _flashcardDbContext.ExamTests.AddAsync(examTest);

                    var examTestWords = (await _wordService.GetRandomWords(model.CategoryId, model.WordsLength))?.Select(w => new ExamTestWord
                    {
                        ExamTest = examTest,
                        Word     = w
                    }) ?? new List <ExamTestWord>();

                    if (!examTestWords.Any())
                    {
                        throw new BadRequestException("No words to fetch");
                    }

                    await _flashcardDbContext.ExamTestWords.AddRangeAsync(examTestWords);

                    _flashcardDbContext.SaveChanges();
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }

            return(examTest);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Adds the specified category.
        /// </summary>
        /// <param name="categoryModel">The category.</param>
        /// <returns></returns>
        public async Task AddAsync(Category categoryModel)
        {
            var category = await _flashcardDbContext.Categories.FirstOrDefaultAsync(c =>
                                                                                    c.Name.Trim().ToLower() == categoryModel.Name.Trim().ToLower());

            CheckCategoryName(category);

            await _flashcardDbContext.Categories.AddAsync(new Category
            {
                Name = categoryModel.Name
            });

            _flashcardDbContext.SaveChanges();
        }
Ejemplo n.º 3
0
 public void AddFlashcard(Flashcard flashcard)
 {
     _context.Flashcards.Add(flashcard);
     _context.SaveChanges();
 }
Ejemplo n.º 4
0
 public void AddCardset(Cardset cardset)
 {
     _context.Cardsets.Add(cardset);
     _context.SaveChanges();
 }