コード例 #1
0
        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);
        }
コード例 #2
0
        public HttpResponseMessage PutComment(int id, string sessionKey, CommentSimpleModel commentModel)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
                () =>
            {
                User user = VerifySessionKey(sessionKey);

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

                Post post = postRepository.Get(id);

                if (post == null)
                {
                    throw new InvalidOperationException("No such post");
                }

                if (commentModel == null)
                {
                    throw new InvalidOperationException("Invalid comment data");
                }

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

                IRepository <Comment> commentRepository = this.data.GetCommentsRepository();

                Comment comment = new Comment()
                {
                    Content = commentModel.Text,
                    Post    = post,
                    User    = user
                };

                commentRepository.Add(comment);

                var response = this.Request.CreateResponse(HttpStatusCode.OK);

                return(response);
            });

            return(responseMsg);
        }