public async Task PostedPost_ReturnsNewlyCreatedPost()
        {
            //Arrange
            await SetupAuthenticationRequestAsync();

            var now    = DateTime.Now;
            var author = $"Integ. Test User ({now})";
            var post   = new MaintainPostRequest()
            {
                Author       = author,
                Content      = "Integ. test - Post data",
                CreationDate = now,
                DisplayUntil = now.AddDays(7),
                Title        = "This is an integration test"
            };

            //Act
            var response = await TestClient.PostAsJsonAsync("api/v1/posts", post); //https://stackoverflow.com/questions/19158378/httpclient-not-supporting-postasjsonasync-method-c-sharp

            //Assert
            response.StatusCode.Should().Be(HttpStatusCode.Created);

            var newlyCreatedPost = await response.Content.ReadAsAsync <MaintainPostResponse>();

            newlyCreatedPost?.Should().NotBeNull();
            newlyCreatedPost.Author.Should().NotBeEmpty().And.Be(author);
        }
示例#2
0
        public async Task <ActionResult> Post(MaintainPostRequest createPostRequest) //Automatic model validation was set with a combination of Action Filters and the FluentValidation library
        {
            var post = createPostRequest.CastTo <Post>();

            post.UserId = HttpContext.GetCurrentUserId();

            if (await _postService.CreateAsync(post))
            {
                var response = post.CastTo <MaintainPostResponse>();

                return(CreatedAtRoute(new { id = response.Id }, response));
            }

            return(BadRequest());
        }
示例#3
0
        public async Task <ActionResult> Update(int id, MaintainPostRequest updatePostRequest)
        {
            var post = updatePostRequest.CastTo <Post>();

            post.Id = id;

            if (await _postService.UpdateAsync(post))
            {
                var response = post.CastTo <MaintainPostResponse>();

                return(Ok(response));
            }

            return(NotFound());
        }