Пример #1
0
        public async Task <IActionResult> SignUp(SignUpModel model)
        {
            ViewBag.Title = "ДУБ - Регистрация";
            if (!ModelState.IsValid)
            {
                return(View());
            }
            if (await CheckEmail(model.Email))
            {
                ModelState.AddModelError("Email", "Пользователь с данной электронной почтой уже зарегистрирован!");
                return(View());
            }

            Autor autor = new Autor();

            model.ToAutor(ref autor);

            await _oak.Autors.AddAsync(autor);

            _oak.SaveChanges();

            await Authenticate(model.Email);

            return(RedirectToAction("All", "Articles"));
        }
Пример #2
0
        public async Task <IActionResult> EditCreate(long?id, int source, ArticleEditedModel model)
        {
            ViewBag.Source = source;
            model.FromRequest(Request.Form);

            if (!model.IsCorrect)
            {
                ModelState.AddModelError("Content", "Хотя бы одно поле текста и изображения должно быть заполнено!");

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

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

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

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

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

            Article article = new Article();

            article.Date = DateTime.Now;

            if (id != null)
            {
                article = await _oak.Articles.FirstOrDefaultAsync(a => a.ID == id);

                if (autor.ID != article.AutorID)
                {
                    return(RedirectToAction("Autor", "Autors", new { autor.ID }));
                }

                await _oak.Entry(article).Collection(a => a.ArtTexts).LoadAsync();

                await _oak.Entry(article).Collection(a => a.ArtSubtitles).LoadAsync();

                await _oak.Entry(article).Collection(a => a.ArtImages).LoadAsync();

                _oak.ArtTexts.RemoveRange(article.ArtTexts);
                _oak.ArtSubtitles.RemoveRange(article.ArtSubtitles);
                _oak.ArtImages.RemoveRange(article.ArtImages);
            }

            model.ToArticle(article, autor);
            if (id is null)
            {
                await _oak.Articles.AddAsync(article);
            }
            _oak.SaveChanges();


            //////////////////////
            return(RedirectToAction("ToSource", "Return", new { source }));
        }
Пример #3
0
        public async Task <IActionResult> Edit(ProfileEditedModel model)
        {
            Autor autor = await _oak.Autors.FirstOrDefaultAsync(a => a.Email == User.Identity.Name);

            model.ToAutor(ref autor);
            _oak.SaveChanges();

            return(RedirectToAction("Autor", "Autors", new { autor.ID }));
        }
Пример #4
0
        public IActionResult SetLike(int id)
        {
            string access  = "";
            var    article = _oak.Articles.FirstOrDefault(a => a.ID == id);

            if (article == null)
            {
                access = "error";
                return(Json(new { access = access }));
            }
            _oak.Entry(article).Collection(m => m.Likes).Load();

            var autor = _oak.Autors.FirstOrDefault(a => a.Email == User.Identity.Name);

            if (autor is null)
            {
                access = "none";
                return(Json(new { access = access, count = article.LikesCount }));
            }

            _oak.Entry(autor).Collection(a => a.Liked).Load();
            if (autor.Liked.Contains(article))
            {
                autor.Liked.Remove(article);
                article.LikesCount--;
                _oak.SaveChanges();
                access = "no like";
            }
            else
            {
                autor.Liked.Add(article);
                article.LikesCount++;
                _oak.SaveChanges();
                access = "like";
            }

            return(Json(new { access = access, count = article.LikesCount }));
        }
Пример #5
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 }));
        }