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 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.º 3
0
        public Stack <PostsDb> GetPostsData()
        {
            PostsDbEntities context = new PostsDbEntities();
            Stack <PostsDb> stack   = new Stack <PostsDb>();

            if (context.PostsDb.Count() != 0)
            {
                foreach (PostsDb db in context.PostsDb)
                {
                    stack.Push(db);
                }
            }
            return(stack);
        }
Ejemplo n.º 4
0
        public ViewResult Post(string title)
        {
            PostsDbEntities context = new PostsDbEntities();

            title = title.Replace('_', ' ');

            var post = context.PostsDb.FirstOrDefault(p => p.Title == title);

            if (post == null)
            {
                throw new HttpException(404, "Post not found");
            }

            return(View(post));
        }
Ejemplo n.º 5
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);
            }
        }