public void TestCreateValidPost_ShouldReturnOK()
        {
            var testPost = new CreatePostDto
            {
                Title = "NEW POST",
                Text = "this is just a test post",
                Tags = new string[] { "post" }
            };

            var testUser = new UserDto
            {
                Username = "******",
                DisplayName = "Peter Petroff",
                AuthCode = "bfff2dd4f1b310eb0dbf593bd83f94dd8d34077e"
            };

            var response = httpServer.Post("api/users/register", testUser);

            string contentString = response.Content.ReadAsStringAsync().Result;
            var loggedUser = JsonConvert.DeserializeObject<LoggedUserDto>(contentString);

            var headers = new Dictionary<string, string>();
            headers["X-SessionKey"] = loggedUser.SessionKey;

            var createPostResponse = httpServer.Post("api/posts", testPost, headers);

            string resultString = createPostResponse.Content.ReadAsStringAsync().Result;
            var createdPost = JsonConvert.DeserializeObject<CreatePostDto>(resultString);

            Assert.AreEqual(HttpStatusCode.Created, createPostResponse.StatusCode);
            Assert.AreEqual(testPost.Title, createdPost.Title);
            Assert.AreEqual(testPost.Text, createdPost.Text);
            Assert.IsNotNull(createdPost.Id);
        }
        public HttpResponseMessage CreatePost(CreatePostDto value)
        {
            try
            {
                var sessionKey = ApiControllerHelper.GetHeaderValue(Request.Headers, "X-SessionKey");
                if (sessionKey == null)
                {
                    throw new ArgumentNullException("No session key provided in the request header!");
                }

                Validate(value.Title, "title");
                Validate(value.Text, "text");

                var context = new BloggingSystemContext();

                using (context)
                {
                    var user = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey);
                    if (user == null)
                    {
                        throw new ArgumentException("Users must be logged in to create posts!");
                    }

                    var newPost = new Post()
                    {
                        Title = value.Title,
                        Text = value.Text,
                        PostDate = DateTime.Now,
                        Author = user
                    };

                    string[] tagsFromTitle = value.Title.Split(
                        new char[] { ' ', ',', '.', ';', '!', '?', ':' },
                        StringSplitOptions.RemoveEmptyEntries);

                    List<string> tagsToCheck = new List<string>();

                    foreach (var tagFromTitle in tagsFromTitle)
                    {
                        tagsToCheck.Add(tagFromTitle);
                    }

                    if (value.Tags != null)
                    {
                        foreach (string tagName in value.Tags)
                        {
                            tagsToCheck.Add(tagName);
                        }
                    }

                    foreach (string tagName in tagsToCheck)
                    {
                        var matchingTag = context.Tags.FirstOrDefault(t => string.Compare(t.Name, tagName, true) == 0);
                        if (matchingTag == null)
                        {
                            // tag not found, insert it in the database
                            matchingTag = new Tag
                            {
                                Name = tagName.ToLower()
                            };

                            context.Tags.Add(matchingTag);
                            context.SaveChanges();
                        }

                        newPost.Tags.Add(matchingTag);
                    }

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

                    var createdPostDto = new CreatePostDto()
                    {
                        Id = newPost.Id,
                        Title = newPost.Title,
                        Tags = newPost.Tags.Select(t => t.Name),
                        Text = newPost.Text
                    };

                    var response = Request.CreateResponse(HttpStatusCode.Created, createdPostDto);
                    return response;
                }
            }
            catch (Exception ex)
            {
                var errorResponse = Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message);
                throw new HttpResponseException(errorResponse);
            }
        }