public async Task <IActionResult> Details(int id) { PostID = id; TempData["postId"] = id; counter = counter + 1; var forCounter = await context.Posts.FirstOrDefaultAsync(i => i.Id == id); forCounter.ShowingCount++; await context.SaveChangesAsync(); var comments = context.Comments.Include(i => i.User).Where(i => i.PostId == id).ToList(); var comment = context.Comments.Include(i => i.User).Where(i => i.PostId == id) .Select(i => new ShowCommentViewModel { CommentText = i.Text, CommentUser = i.User.UserName, Id = i.Id, Date = i.CommentDate, ImageUrl = i.User.ImageUrl }).ToList(); var post = context.Posts.Include(i => i.User).Where(i => i.Id == id) .Select(i => new DetailViewModel { Id = i.Id, PostName = i.PostName, PostTitle = i.PostTitle, PostUser = i.User.UserName, PostDate = i.PostDate, ImageUrl = i.ImageUrl, Comments = comments, PostDescription = i.PostDescription, ShowingCount = i.ShowingCount, PostUserId = i.User.Id }).FirstOrDefault(); //ViewBag.Comments = context.Comments.Include(i=>i.User).Where(i => i.PostId == post.Id).ToList(); return(View(post)); }
public async Task <IActionResult> Add(AddPostVM model, IFormFile file) { var user = await userManager.FindByNameAsync(User.Identity.Name); if (file != null) { var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot\\img", file.FileName); using (var fs = new FileStream(path, FileMode.Create)) { await file.CopyToAsync(fs); model.ImageUrl = file.FileName; } } if (ModelState.IsValid) { Post p = new Post { PostName = model.Name, PostTitle = model.Title, PostDescription = model.Description, UserId = user.Id, PostDate = DateTime.Now, ImageUrl = model.ImageUrl }; context.Posts.Add(p); await context.SaveChangesAsync(); return(Redirect("/")); } return(View(model)); }