public virtual void Remove(IEnumerable <T> t)
 {
     _context.RemoveRange(t);
 }
Exemple #2
0
        //[Authorize]// [FromBody] ICollection<Thema> array
        public async Task <ActionResult <List <Thema> > > SaveAllThemes([FromBody] IEnumerable <Thema> themes)
        {
            // Проверить наличие таких же тем, если нету то сохранить
            string username  = null;
            string teacherId = "teacher2";


            var dbThemes    = _context.Themes.AsTracking();
            var dbQuestions = _context.Questions.AsTracking();

            foreach (Thema thema in themes)
            {
                var checkThema = await dbThemes.FirstOrDefaultAsync(a => a.Id.Equals(thema.Id));

                if (checkThema == null)
                {
                    _context.Themes.Add(thema);
                }
                else
                {
                    checkThema.Name        = thema.Name;
                    checkThema.Description = thema.Description;
                    _context.Themes.Update(checkThema);
                }

                var nowQuestions = await dbQuestions.Where(a => a.ThemaId.Equals(thema.Id)).AsTracking().ToListAsync();

                if (thema.Questions != null)
                {
                    foreach (Question question in thema.Questions)
                    {
                        var findTestThema = nowQuestions.FirstOrDefault(a => a.Id.Equals(question.Id));

                        if (findTestThema == null)
                        {
                            _context.Add(question);
                        }
                        else
                        {
                            _context.Update(question);
                            nowQuestions.Remove(findTestThema);
                        }
                    }
                    //Remove elements
                    _context.RemoveRange(nowQuestions);
                }

                thema.TeacherUserId = teacherId;
                var findTest = dbThemes.FirstOrDefault(a => a.Id.Equals(thema.Id));
                Console.WriteLine($"Find test {findTest}");
                if (findTest == null)
                {
                    _context.Themes.Add(thema);
                }
                else
                {
                    _context.Themes.Update(thema);
                }
            }
            _context.SaveChanges();
            return(Ok(themes));
        }