public ActionResult Create([DataSourceRequest] DataSourceRequest request, TagViewModel tag)
        {
            if (tag != null && ModelState.IsValid)
            {
                Tag t = new Tag
                {
                    Name = tag.TagName
                };

                db.Tags.Add(t);
                db.SaveChanges();
            }

            return Json(new[] { tag }.ToDataSourceResult(request, ModelState));
        }
        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 HttpResponseMessage PostCreate(PostRegisterModel inputPost, string sessionKey)
        {
            HttpResponseMessage responseMessage = this.PerformOperationAndHandleExceptions(
                () =>
                {
                    ForumContext context = new ForumContext();

                    using(context)
                    {
                        User currentUser = context.Users.FirstOrDefault(usr => usr.SessionKey == sessionKey);

                        int currentCategoryId = inputPost.CurrentCategoryId;

                        Category currentCategory = context.Categories.FirstOrDefault(cat => cat.Id == currentCategoryId);

                        if(currentUser == null)
                        {
                            throw new ArgumentNullException("You should be logged or registered to create new posts.");
                        }

                        if(currentCategory == null)
                        {
                            throw new ArgumentNullException("You try to create post in non-existing category.");
                        }

                        Post newPost = new Post()
                        {
                            Author = currentUser,
                            Category = currentCategory,
                            Content = inputPost.Content,
                            CreationDate = DateTime.Now,
                            Title = inputPost.Title
                        };

                        foreach(string tagName in inputPost.Tags)
                        {
                            Tag currentTag = context.Tags.FirstOrDefault(t => t.Name == tagName);

                            if(currentTag == null)
                            {
                                currentTag = new Tag()
                                {
                                    Name = tagName
                                };

                                context.Tags.Add(currentTag);
                                context.SaveChanges();

                                newPost.Tags.Add(currentTag);
                            }
                            else
                            {
                                newPost.Tags.Add(currentTag);
                            }
                        }

                        context.Posts.Add(newPost);
                        context.SaveChanges();

                        var resultPost = new PostModel
                        {
                            Id = newPost.Id,
                            Content = newPost.Content,
                            CategoryName = newPost.Category.Title,
                            CategoryId = newPost.Category.Id,
                            CreationDate = newPost.CreationDate,
                            Tags = (from t in newPost.Tags
                                   select t.Name),
                            Title = newPost.Title,
                            Author = newPost.Author.Username
                        };

                        HttpResponseMessage response = this.Request.CreateResponse(HttpStatusCode.Created, resultPost);

                        return response;
                    }
                });

            return responseMessage;
        }
        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);
        }