public void GetPostsByTags_WhenTagsListIncorrect_Test()
        {
            string username    = "******";
            string displayName = "Test User";
            string authCode    = new String('*', 40);

            AddUser(username, displayName);
            LoginUser(username, authCode);
            string sessionKey = new string('*', 50);

            string title = "New post";
            string text  = "This is just a new post";

            string[]        tags1 = new string[] { "web", "services" };
            PostSimpleModel post1 = CreatePost(sessionKey, title, text, tags1);

            string[]        tags2 = new string[] { "web2", "services2" };
            PostSimpleModel post2 = CreatePost(sessionKey, title, text, tags2);

            string[]        tags3 = new string[] { "web3", "services3" };
            PostSimpleModel post3 = CreatePost(sessionKey, title, text, tags3);

            var response = this.httpServer.CreateGetRequest("api/posts?tags=&sessionKey=" + sessionKey);

            Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode);
        }
        public void PutCommentForPosts_WhenCommentEmpty_Test()
        {
            string username    = "******";
            string displayName = "Test User";
            string authCode    = new String('*', 40);

            AddUser(username, displayName);
            string sessionKey = LoginUser(username, authCode);

            string title = "New post";
            string text  = "This is just a new post";

            string[]        tags1 = new string[] { "web", "services" };
            PostSimpleModel post  = CreatePost(sessionKey, title, text, tags1);

            string             commentText    = "   ";
            CommentSimpleModel initialComment = new CommentSimpleModel()
            {
                Text = commentText
            };

            var response = this.httpServer.CreatePutRequest("api/posts/" + post.Id + "/comment?sessionKey=" + sessionKey, initialComment);

            Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode);

            Comment comment = this.context.Set <Comment>().FirstOrDefault(c => c.Post.PostId == post.Id && c.User.SessionKey == sessionKey);

            Assert.IsNull(comment);
        }
        public void GetPostsByTags_WhenNone_MeetCriteria_Test()
        {
            string username    = "******";
            string displayName = "Test User";
            string authCode    = new String('*', 40);

            AddUser(username, displayName);
            string sessionKey = LoginUser(username, authCode);

            string title = "New post";
            string text  = "This is just a new post";

            string[]        tags1 = new string[] { "web", "services" };
            PostSimpleModel post1 = CreatePost(sessionKey, title, text, tags1);

            string[]        tags2 = new string[] { "web2", "services2" };
            PostSimpleModel post2 = CreatePost(sessionKey, title, text, tags2);

            string[]        tags3 = new string[] { "web3", "services3" };
            PostSimpleModel post3 = CreatePost(sessionKey, title, text, tags3);

            var response      = this.httpServer.CreateGetRequest("api/posts?tags=web4,services4&sessionKey=" + sessionKey);
            var contentString = response.Content.ReadAsStringAsync().Result;
            IEnumerable <PostDetailModel> postModels = JsonConvert.DeserializeObject <IEnumerable <PostDetailModel> >(contentString);

            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
            Assert.AreEqual(0, postModels.Count());
        }
        private PostSimpleModel CreatePost(string sessionKey, string title, string text, string[] tags)
        {
            PostWithTagsModel initialPost = new PostWithTagsModel()
            {
                Title = title,
                Text  = text,
                Tags  = tags
            };

            var             response      = this.httpServer.CreatePostRequest("api/posts?sessionKey=" + sessionKey, initialPost);
            var             contentString = response.Content.ReadAsStringAsync().Result;
            PostSimpleModel postModel     = JsonConvert.DeserializeObject <PostSimpleModel>(contentString);

            return(postModel);
        }
Esempio n. 5
0
        public HttpResponseMessage CreatePost(string sessionKey, PostWithTagsModel postModel)
        {
            HttpResponseMessage responseMsg = this.PerformOperationAndHandleExceptions(
                () =>
            {
                User user = VerifySessionKey(sessionKey);

                if (postModel.Title.Trim() == string.Empty)
                {
                    throw new InvalidOperationException("The post title cannot be empty");
                }

                if (postModel.Text.Trim() == string.Empty)
                {
                    throw new InvalidOperationException("The post text cannot be empty");
                }

                if (postModel.Tags == null)
                {
                    throw new InvalidOperationException("Incorrect tags data");
                }

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

                IRepository <Post> postRepository = this.data.GetPostsRepository();
                postRepository.Add(post);

                string[] tagsFromTitle = postModel.Title.ToLower().Split(new string[] { " ", ".", ",", "-", "!", "?", ";", ":", "'" }, StringSplitOptions.None);

                HashSet <string> tags = new HashSet <string>();
                tags.UnionWith(tagsFromTitle);

                string[] postTagsToLower = postModel.Tags.Select(x => x.ToLower()).ToArray();
                tags.UnionWith(postTagsToLower);

                IRepository <Tag> tagRepository = this.data.GetTagsRepository();
                foreach (string tag in tags)
                {
                    IQueryable <Tag> tagsQuery = tagRepository.GetConstraint(t => t.Name == tag);

                    Tag postTag;
                    if (tagsQuery.Count() == 0)
                    {
                        postTag = new Tag()
                        {
                            Name = tag
                        };

                        postTag.Posts.Add(post);

                        tagRepository.Add(postTag);
                    }
                    else
                    {
                        postTag = tagsQuery.First();

                        postTag.Posts.Add(post);

                        tagRepository.Update(postTag.TagId, postTag);
                    }
                }

                var postResultModel = new PostSimpleModel()
                {
                    Id    = post.PostId,
                    Title = post.Title
                };

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

                return(response);
            });

            return(responseMsg);
        }