// GET: Admin/NewsPosts/Delete/5
        public async Task <IActionResult> Delete(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var newsPost = await _context.Posts
                           .Include(n => n.Author)
                           .FirstOrDefaultAsync(m => m.Id == id);

            if (newsPost == null)
            {
                return(NotFound());
            }

            NewsPostViewModel result = new NewsPostViewModel()
            {
                AuthorName = newsPost.Author.UserName,
                Content    = newsPost.Content,
                Date       = newsPost.Date.ToString("dd/MM/yyyy"),
                Id         = newsPost.Id,
                Title      = newsPost.Title,
            };

            return(View(result));
        }
Exemplo n.º 2
0
        public IActionResult Detail(int id)
        {
            var targetNews     = context.News.FirstOrDefault(x => x.Id == id);
            var comments       = context.Comments.Include(x => x.News).Where(y => y.News.Id == id).OrderByDescending(x => x.Written).ToList();
            var newsCategories = context.HasCategories.Where(x => x.NewsId == id).Select(y => y.Category).ToList();
            var allCategories  = context.Categories;

            var model = new NewsPostViewModel();

            if (id == 0)
            {
                return(View(model));
            }

            foreach (var comment in comments)
            {
                model.Comments.Add(new CommentViewModel()
                {
                    Id      = comment.Id,
                    Author  = comment.Author,
                    Written = comment.Written,
                    Body    = comment.Body,
                    News    = comment.News
                });
            }

            model.NewsViewModel = new NewsViewModel()
            {
                NewsId       = targetNews.Id,
                Title        = targetNews.Title,
                Written      = targetNews.Written,
                Author       = targetNews.Author,
                Body         = targetNews.Body,
                Categories   = newsCategories,
                CommentCount = comments.Count,
                ImageUrl     = targetNews.ImageUrl
            };

            foreach (var cat in allCategories)
            {
                model.Categories.Add(new CategoryViewModel()
                {
                    Category = cat,
                    Count    = context.HasCategories.Where(x => x.CategoryId == cat.Id).Count()
                });
            }

            model.CommentForm.NewsId = model.NewsViewModel.NewsId;

            return(View(model));
        }
Exemplo n.º 3
0
        public IActionResult AddComment(NewsPostViewModel model)
        {
            var targetNews = context.News.SingleOrDefault(x => x.Id == model.CommentForm.NewsId);
            var comment    = new Comment()
            {
                Author  = model.CommentForm.Name,
                Written = Convert.ToDateTime(DateTime.Now.ToString("yyyy'-'MM'-'dd' 'HH':'mm':'ss")),
                Body    = model.CommentForm.Message,
                News    = targetNews
            };

            context.Comments.Add(comment);

            context.SaveChanges();

            return(RedirectToAction("Detail", "News", new { id = targetNews.Id }));
        }