public async Task <ActionResult> DeletePost(int postId) { PostsDbEntities context = new PostsDbEntities(); CommentsEntities commentsContext = new CommentsEntities(); PostsDb post = new PostsDb(); post = await context.PostsDb.FindAsync(postId); if (post != null) { context.PostsDb.Remove(post); await context.SaveChangesAsync(); if (System.IO.File.Exists(post.Image)) { System.IO.File.Delete(post.Image); } // Удалить все комментарии связанные с постом var postsComments = commentsContext.Comments.Where(x => x.PostId == postId); commentsContext.Comments.RemoveRange(postsComments); await context.SaveChangesAsync(); return(RedirectToAction("Index")); } else { return(View("Error", new string[] { "Запись не найдена" })); } }
public static MvcHtmlString PostLink(this HtmlHelper helper, PostsDb post, string text) { return(helper.ActionLink(text, "Post", "Blog", new { title = post.Title.Replace(' ', '_') }, new { title = post.Title })); }
public ActionResult CreatePost(PostsDb post, HttpPostedFileBase image) { if (ModelState.IsValid) { PostsDbEntities context = new PostsDbEntities(); post.Time = this.HttpContext.Timestamp; post.Author = CurrentUser.UserName; BlogPostsManager bpManager = new BlogPostsManager(); post.Content = bpManager.AddLineBreaks(post.Content); if (image != null) { string imageName = post.Author; imageName += Guid.NewGuid(); string filePath = AppDomain.CurrentDomain.BaseDirectory; filePath += "Content\\PostImages\\" + imageName; image.SaveAs(filePath); post.Image = filePath; post.ImageMimeType = image.ContentType; } context.PostsDb.Add(post); try { context.SaveChanges(); } catch (DbEntityValidationException e) { #if false foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } #endif } return(Redirect("/Blog/Index")); } return(View("Error", new string[] { "TODO: Доделать. Ошибка связанная с созданием поста" })); }
public FilePathResult GetImage(int id) { PostsDbEntities context = new PostsDbEntities(); PostsDb post = new PostsDb(); post = context.PostsDb.FirstOrDefault(x => x.Id == id); if (post != null && post.Image != null) { return(File(post.Image, post.ImageMimeType)); } else { return(null); } }
public static string Href(this PostsDb post, UrlHelper helper) { return(helper.RouteUrl(new { controller = "Blog", action = "Post", title = post.Title })); }