Ejemplo n.º 1
0
        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[] { "Запись не найдена" }));
            }
        }
Ejemplo n.º 2
0
 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
     }));
 }
Ejemplo n.º 3
0
        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: Доделать. Ошибка связанная с созданием поста" }));
        }
Ejemplo n.º 4
0
        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);
            }
        }
Ejemplo n.º 5
0
 public static string Href(this PostsDb post, UrlHelper helper)
 {
     return(helper.RouteUrl(new { controller = "Blog", action = "Post", title = post.Title }));
 }