public static PostReturnModel CreateModel(Post post)
        {
            PostReturnModel model = new PostReturnModel()
            {
                Id = post.Id,
                Title = post.Title
            };

            return model;
        }
        public static PostCreatedModel Parse(Post post)
        {
            PostCreatedModel resultPost = new PostCreatedModel()
            {
                Author = post.Author.Username,
                Content = post.Content,
                Title  = post.Title,
                CreationDate = post.CreationDate,
                Category = post.Category.Title,
                Tags = new List<string>()
            };

            foreach(Tag currentTag in post.Tags)
            {
                resultPost.Tags.Add(currentTag.Name);
            }

            return resultPost;
        }
        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(Post post)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
                () =>
                {
                    var context = new ForumContext();
                    using (context)
                    {
                        context.Posts.Add(post);
                        context.SaveChanges();

                        HttpResponseMessage response =
                            Request.CreateResponse(HttpStatusCode.Created, post);
                        response.Headers.Location =
                            new Uri(Url.Link("DefaultApi", new { id = post.Id }));
                        return response;
                    }
                });
            return responseMsg;
        }
        // POST api/posts
        public HttpResponseMessage Post(PostModel value, [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey)
        {
            var responceMsg = this.PerformOperationAndHandleExceptions(() =>
            {
                var context = new ForumDbContext();
                using (context)
                {
                    var user = context.Users.FirstOrDefault(usr => usr.SessionKey == sessionKey
                        && usr.Nickname == value.PostedBy);

                    if (user == null)
                    {
                        throw new InvalidOperationException("Invalid username and password.");
                    }

                    Post newPost = new Post()
                    {
                        PostDate = DateTime.Now,
                        PostContent = value.Content,
                        Thread = context.Threads.FirstOrDefault(x => x.Id == value.ThreadId),
                        User = user
                    };

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

                    //newPost.User = null;

                    var responce = this.Request.CreateResponse(HttpStatusCode.Created);

                    return responce;

                }
            });

            return responceMsg;
        }
 public ActionResult NewPost(Post newPost)
 {
     return PartialView(newPost);
 }
        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 Reply(Post post)
		{
			if (ModelState.IsValid)
			{
				post.Created = DateTime.Now;

				db.Posts.Add(post);
				db.SaveChanges();

				return RedirectToAction("Index");
			}

			return View();

		}