Exemplo n.º 1
0
        public async Task <IActionResult> EditCreate(long?id)
        {
            SectionEditedModel model = new SectionEditedModel();

            if (id != null)
            {
                Autor autor = await _oak.Autors.FirstOrDefaultAsync(a => a.Email == User.Identity.Name);

                if (autor is null)
                {
                    return(RedirectToAction("All", "Articles"));
                }

                Section section = await _oak.Sections.FirstOrDefaultAsync(s => s.ID == id);

                if (section is null)
                {
                    return(RedirectToAction("Error", "Articles"));
                }

                if (section.AutorID != autor.ID)
                {
                    return(RedirectToAction("All", "Articles"));
                }


                await _oak.Entry(section).Reference(s => s.Parent).LoadAsync();

                ViewBag.ParentName = (section.Parent is null) ? "..." : section.Parent.Name;
                model.FromSection(section);
            }
            else
            {
                ViewBag.ParentName = "...";
            }

            ViewBag.Source = -1;
            ViewBag.Title  = "Работа над ветвью";
            return(View(model));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> EditCreate(long?id, int source, SectionEditedModel model)
        {
            ViewBag.Source = source;

            if (!model.IsUnique(await _oak.Sections.ToListAsync()))
            {
                ModelState.AddModelError("Parent", "");
                ModelState.AddModelError("Name", "Данная ветвь уже существует! Измените предка или название!");

                Section selected = await _oak.Sections.FirstOrDefaultAsync(s => s.ID == model.Parent);

                ViewBag.ParentName = (selected is null) ? "..." : selected.Name;

                ViewBag.Title = "Работа над ветвью";
                return(View(model));
            }
            if (!model.IsCorrect(await _oak.Sections.FirstOrDefaultAsync(s => s.ID == model.Parent)))
            {
                ModelState.AddModelError("Parent", "");
                ModelState.AddModelError("Name", "Родительская и дочерняя ветви не могут совпадать!");

                Section selected = await _oak.Sections.FirstOrDefaultAsync(s => s.ID == model.Parent);

                ViewBag.ParentName = (selected is null) ? "..." : selected.Name;

                ViewBag.Title = "Работа над ветвью";
                return(View(model));
            }

            Autor autor = await _oak.Autors.FirstOrDefaultAsync(a => a.Email == User.Identity.Name);

            if (autor is null)
            {
                return(RedirectToAction("All", "Articles"));
            }

            if (id == null)
            {
                Section section = new Section();
                model.ToSection(ref section, await _oak.Sections.FirstOrDefaultAsync(s => s.ID == model.Parent), autor);

                await _oak.Sections.AddAsync(section);

                _oak.SaveChanges();
            }
            else
            {
                Section section = await _oak.Sections.FirstOrDefaultAsync(s => s.ID == id);

                if (section.AutorID != autor.ID)
                {
                    return(RedirectToAction("All", "Articles"));
                }

                model.ToSection(ref section, await _oak.Sections.FirstOrDefaultAsync(s => s.ID == model.Parent), autor);

                _oak.SaveChanges();
            }

            //////////////////////
            return(RedirectToAction("ToSource", "Return", new { source }));
        }