public void GetPosts_AllParamsArevalid_ShouldGetZeroPost()
        {
            this.ClearDatabase();

            var testPost = new PostModel()
            {
                Tags = new string[] { "test", "tag" },
                Text = "Smashing game!",
                Title = "Knights of the old republic"
            };

            string sessionKey = this.RegisterTestUser().SessionKey;

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

            for (int i = 0; i < 3; i++)
            {
                this.httpServer.Post("api/posts", testPost, headers);
            }

            var response = this.httpServer.Get("api/posts?tags=mag,bag", headers);
            var contentString = response.Content.ReadAsStringAsync().Result;
            GetPostModel[] postModels = JsonConvert.DeserializeObject<GetPostModel[]>(contentString);

            Assert.AreEqual(0, postModels.Length);
        }
        public void CreatePost_WhenPostModelIsValid_ShouldSaveToDatabase()
        {
            var testPost = new PostModel()
            {
                Tags = new string[] { "cool", "game" },
                Text = "Wow that was a great game!",
                Title = "Age of empires"
            };

            string sessionKey = this.RegisterTestUser();

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

            var response = this.httpServer.Post("api/posts", testPost, headers);
            var contentString = response.Content.ReadAsStringAsync().Result;
            PostResponseModel postModel = JsonConvert.DeserializeObject<PostResponseModel>(contentString);

            Assert.AreEqual(testPost.Title, postModel.Title);
            Assert.IsNotNull(postModel.Id);
        }
        private int PostATestPost(string sessionKey)
        {
            var testPost = new PostModel()
            {
                Tags = new string[] { "cool", "game" },
                Text = "Wow that was a great game!",
                Title = "Age of empires"
            };

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

            var response = this.httpServer.Post("api/posts", testPost, headers);
            var contentString = response.Content.ReadAsStringAsync().Result;
            PostResponseModel postModel = JsonConvert.DeserializeObject<PostResponseModel>(contentString);

            return postModel.Id;
        }
        public void CreatePost_WhenPostModelIsValid_NoTagsGiven()
        {
            var testPost = new PostModel()
            {
                Tags = new string[] { },
                Text = "Smashing game!",
                Title = "Knights of the old republic"
            };

            string sessionKey = this.RegisterTestUser();

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

            var response = this.httpServer.Post("api/posts", testPost, headers);
            var contentString = response.Content.ReadAsStringAsync().Result;
            PostResponseModel postModel = JsonConvert.DeserializeObject<PostResponseModel>(contentString);

            Assert.AreEqual(testPost.Title, postModel.Title);
            Assert.IsNotNull(postModel.Id);
        }
        public void CreatePost_WhenPostModelIsInvalid_TitleIsNull()
        {
            var testPost = new PostModel()
            {
                Tags = new string[] { },
                Text = "Super mega duper game!",
                Title = null
            };

            string sessionKey = this.RegisterTestUser();

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

            var response = this.httpServer.Post("api/posts", testPost, headers);
            Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode);
        }
        public void CreatePost_WhenPostModelIsInvalid_GivenTextIsTooShort()
        {
            var testPost = new PostModel()
            {
                Tags = new string[] { "gta" },
                Text = "Cool game.",
                Title = "Grand theft auto."
            };

            string sessionKey = this.RegisterTestUser();

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

            var response = this.httpServer.Post("api/posts", testPost, headers);
            Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode);
        }
        public void CreatePost_WhenPostModelIsInvalid_TextIsNEmptySpaces()
        {
            var testPost = new PostModel()
            {
                Tags = new string[] { },
                Text = "            ",
                Title = "Knights of the old republic"
            };

            string sessionKey = this.RegisterTestUser();

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

            var response = this.httpServer.Post("api/posts", testPost, headers);
            Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode);
        }
        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;
        }