public HttpResponseMessage PostAPost(
            [FromBody]PostModel model,
            [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(() =>
                {
                    ValidateText(model.Text);
                    ValidateTitle(model.Title);

                    var context = new BloggingSystemContext();
                    using (context)
                    {
                        var user = GetValidUser(sessionKey);

                        if (user == null)
                        {
                            throw new InvalidOperationException("You are not logged in!");
                        }

                        var post = new Post()
                        {
                            Title = model.Title,
                            Text = model.Text,
                            PostDate = DateTime.Now,
                            User = user
                        };

                        HashSet<string> allTags = new HashSet<string>();
                        foreach (var tag in model.Tags)
                        {
                            var lowerTag = tag.ToLower();
                            if (!allTags.Contains(lowerTag))
                            {
                                allTags.Add(lowerTag);
                            }
                        }

                        string[] splitedTitle = model.Title.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        foreach (var tag in splitedTitle)
                        {
                            var lowerTag = tag.ToLower();
                            if (!allTags.Contains(lowerTag))
                            {
                                allTags.Add(lowerTag);
                            }
                        }

                        foreach (var tag in allTags)
                        {
                            var newTag = context.Tags.Where(t => t.Title == tag).FirstOrDefault();

                            if (newTag == null)
                            {
                                newTag = new Tag()
                                {
                                    Title = tag
                                };
                            }

                            post.Tags.Add(newTag);
                        }

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

                        PostResponseModel responseModel = new PostResponseModel()
                        {
                            Id = post.Id,
                            Title = post.Title
                        };

                        return Request.CreateResponse(HttpStatusCode.Created, responseModel);
                    }
                });

            return responseMsg;
        }
        public HttpResponseMessage Post(PostModel model, string sessionKey)
        {
            try
            {
                this.VerifySessionKey(sessionKey);

                if (string.IsNullOrEmpty(model.Title))
                {
                    throw new ArgumentNullException("Post title is mandatory");
                }

                if (string.IsNullOrEmpty(model.Text))
                {
                    throw new ArgumentNullException("Post content is mandatory");
                }
            }
            catch (Exception e)
            {
                var errorResponse = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, e.Message);
                return errorResponse;
            }

            var context = new BloggingSystemContext();

            var user = context.Users.FirstOrDefault(
                usr => usr.SessionKey == sessionKey);

            var post = new Post()
            {
                Title = model.Title,
                Text = model.Text,
                PostDate = DateTime.Now,
                User = user
            };

            var matches = Regex.Matches(model.Title, @"\b[a-zA-Z]{1,}\b");
            foreach (Match match in matches)
            {
                var tag = context.Tags.FirstOrDefault(t => t.Text == match.Value);

                if (tag == null)
                {
                    tag = new Tag()
                    {
                        Text = match.Value
                    };
                }

                post.Tags.Add(tag);
            }

            foreach (string tagText in model.Tags)
            {
                var tag = context.Tags.FirstOrDefault(t => t.Text == tagText);

                if (tag == null)
                {
                    tag = new Tag()
                    {
                        Text = tagText
                    };
                }

                post.Tags.Add(tag);
            }

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

            var result = new PostResponseModel()
            {
                Id = post.PostId,
                Title = post.Title
            };

            var response = this.Request.CreateResponse(HttpStatusCode.Created, result);

            return response;
        }