public ActionResult CreatePost(PostViewModel post)
        {
            var user = db.Users.FindUser(User.Identity.GetUserId());
            if (post != null && user != null && ModelState.IsValid)
            {
                Post postToAdd = new Post
                {
                    CategoryId = post.CategoryId,
                    Content = post.Content,
                    Title = post.Title,
                    User = user
                };

                if (post.Tags != null && post.Tags.Count() > 0)
                {
                    var tags = post.Tags.ElementAt(0).Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (var tag in tags)
                    {
                        var target = db.Tags.All().FirstOrDefault(t => t.Name == tag);
                        if (target == null)
                        {
                            target = new Tag
                            {
                                Name = tag
                            };
                        }

                        postToAdd.Tags.Add(target);
                    }
                }

                db.Posts.Add(postToAdd);
                db.SaveChanges();
                return RedirectToAction("Index", "Home");
            }

            var modelPost = db.Posts.All().Select(PostViewModel.FromPost).FirstOrDefault();

            ViewBag.Categories = db.Categories.All().ToList().Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() });

            return View(modelPost);
        }
        public ActionResult EditPost(PostViewModel post)
        {
            if (post != null && ModelState.IsValid)
            {
                Post postToEdit = db.Posts.GetById(post.Id);
                postToEdit.CategoryId = post.CategoryId;
                postToEdit.Content = post.Content;
                postToEdit.Title = post.Title;

                if (post.Tags != null && post.Tags.Count() > 0)
                {
                    foreach (var tag in postToEdit.Tags.ToList())
                    {
                        postToEdit.Tags.Remove(tag);
                    }

                    var tags = post.Tags.ElementAt(0).Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (var tag in tags)
                    {
                        var target = db.Tags.All().FirstOrDefault(t => t.Name == tag);
                        if (target == null)
                        {
                            target = new Tag
                            {
                                Name = tag
                            };
                        }

                        postToEdit.Tags.Add(target);
                    }
                }

                db.SaveChanges();
                return RedirectToAction("PostAndComments", "Posts", new { postId = post.Id });
            }

            ViewBag.Categories = db.Categories.All().ToList().Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() });

            return View(post);
        }
        public ActionResult EditPost(int? postId)
        {
            if (postId == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            var post = db.Posts.GetById((int)postId);
            var user = db.Users.FindUser(User.Identity.GetUserId());

            if (post == null)
            {
                return HttpNotFound();
            }

            var modelPost = new PostViewModel
                            {
                                Id = post.Id,
                                Title = post.Title,
                                Content = post.Content,
                                CategoryName = post.Category.Name,
                                CategoryId = post.Category.Id,
                                Creator = user.UserName,
                                CreatorId = user.Id,
                                Tags = post.Tags.Select(tag => tag.Name).ToList(),
                                Timespan = post.Timespan
                            };

            ViewBag.Categories = db.Categories.All().ToList().Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() });

            return View(modelPost);
        }