public ActionResult Create(CommentaryViewModel model)
        {
            if (ModelState.IsValid)
            {
                using (var database = new BlogDbContext())
                {
                    var authorId = database.Users
                                   .Where(u => u.UserName == this.User.Identity.Name)
                                   .First()
                                   .Id;

                    int points = 0;

                    model.AuthorName = database.Users
                                       .Where(u => u.UserName == this.User.Identity.Name)
                                       .First()
                                       .FullName;

                    var comment = new Commentary(authorId, model.Content, points, model.ArticleId, model.AuthorName);

                    comment.AuhtorId = authorId;

                    database.Commentary.Add(comment);
                    database.SaveChanges();

                    return(RedirectToAction("Details\\" + model.ArticleId, "Article"));
                }
            }

            return(View(model));
        }
        public async Task <IActionResult> CreateCommentary(CommentaryViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            var relatedBlog = _context.Blogs.FirstOrDefault(x => x.Id == model.BlogId);

            if (relatedBlog == null)
            {
                return(Redirect(pathToHome));
            }


            var commentary = new Commentary()
            {
                Text     = model.Text, Blog = relatedBlog, BlogId = model.BlogId,
                DateTime = DateTime.Now, User = await _userManager.GetUserAsync(User)
            };

            _context.Commentaries.Add(commentary);
            _context.SaveChanges();
            _messageSender.Send(_messageSender.GetType().ToString());
            return(Redirect(pathToBlogs));
        }
        public IActionResult CreateCommentary(int blogId)
        {
            var model = new CommentaryViewModel()
            {
                BlogId = blogId
            };

            return(View(model));
        }
        public void Add(CommentaryViewModel viewModel)
        {
            var result = mapper.Map <Commentary>(viewModel);
            var news   = unitOfWork.Get <News>(result.NewsId.GetValueOrDefault());

            result.News = news;
            var author = unitOfWork.Get <User>(result.AuthorId.GetValueOrDefault());

            result.Author = author;
            unitOfWork.Add(result);
            unitOfWork.SaveChanges();
        }
        public IEnumerable <CommentaryViewModel> GetCommentaries(int id)
        {
            System.Linq.Expressions.Expression <Func <Commentary, bool> > expr =
                x => x.Id_Response == id;
            IEnumerable <Commentary>   commentaries = commentaryContext.Find(expr);
            List <CommentaryViewModel> list         = new List <CommentaryViewModel>();

            foreach (Commentary comm in commentaries)
            {
                CommentaryViewModel commentaryVM = new CommentaryViewModel();
                commentaryVM.Author  = comm.Author;
                commentaryVM.Message = comm.Message;
                list.Add(commentaryVM);
            }
            return(list.AsEnumerable());
        }
 public bool CreateCommentary(CommentaryViewModel comm)
 {
     if (comm != null)
     {
         Commentary commentary = new Commentary();
         commentary.Author = comm.Author;
         if (comm.Author == null || comm.Author.Trim().Equals(""))
         {
             commentary.Author = "Anonymous";
         }
         commentary.Message     = comm.Message;
         commentary.Id_Response = comm.Id_Response;
         commentaryContext.Add(commentary);
         commentaryContext.Save();
         return(true);
     }
     return(false);
 }
        public IActionResult UpdateCommentary(CommentaryViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var commentary = _context.Commentaries.Include(x => x.User).FirstOrDefault(x => x.Id == model.CommentaryId);

            if (commentary == null || User.Identity.Name != commentary.User.UserName)
            {
                return(Redirect(pathToHome));
            }


            commentary.Text = model.Text;
            _context.SaveChanges();
            return(Redirect(pathToBlogs));
        }
        public void SendCommentary(string user, string message, int authorId, int newsId, int commentId)
        {
            CommentaryViewModel viewModel = new CommentaryViewModel();
            var comments        = commentaryService.GetAll();
            var existingComment = comments.FirstOrDefault(x => x.Id == commentId);

            if (existingComment != null)
            {
                commentId++;
            }

            viewModel.AuthorName  = user;
            viewModel.Id          = commentId;
            viewModel.AuthorId    = authorId;
            viewModel.Description = message;
            viewModel.Created     = DateTime.Now;
            viewModel.NewsId      = newsId;
            commentaryService.Add(viewModel);
            Clients.All.SendAsync("ReceiveCommentary", viewModel.AuthorName, viewModel.Description, viewModel.AuthorId,
                                  viewModel.NewsId, viewModel.Id);
        }
        public ActionResult Edit(CommentaryViewModel model)
        {
            if (ModelState.IsValid)
            {
                using (var database = new BlogDbContext())
                {
                    var comment = database.Commentary
                                  .Where(c => c.Id == model.Id)
                                  .FirstOrDefault();

                    comment.Content = model.Content;

                    database.Entry(comment).State = EntityState.Modified;
                    database.SaveChanges();

                    return(RedirectToAction("Details\\" + model.ArticleId, "Article"));
                }
            }

            return(View(model));
        }
        public ActionResult Create(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            using (var database = new BlogDbContext())
            {
                var model = new CommentaryViewModel();

                var article = database.Articles
                              .Where(a => a.Id == id)
                              .First();

                model.ArticleId    = (int)id;
                model.ArticleTitle = article.Title;


                return(View(model));
            }
        }
        public async Task <IActionResult> UpdateCommentary(int commentaryId)
        {
            var commentary = _context.Commentaries.Include(x => x.User).FirstOrDefault(x => x.Id == commentaryId);

            if (commentary == null || User.Identity.Name != commentary.User.UserName)
            {
                return(Redirect(pathToHome));
            }

            var timeCheck = await _authorizationService.AuthorizeAsync(User, commentary, "CommentEditTime");

            if (timeCheck.Succeeded)
            {
                var model = new CommentaryViewModel()
                {
                    BlogId = commentary.BlogId, Text = commentary.Text
                };
                return(View(model));
            }

            return(Redirect(pathToHome));
        }