Return the saved blog post
This commit is contained in:
parent
1bbad42a3f
commit
cfcef8b77d
2 changed files with 13 additions and 15 deletions
|
@ -162,7 +162,7 @@ namespace Robware.Api.Blog.Tests {
|
|||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Save_WithNewSubmission_Returns200() {
|
||||
public async Task Save_WithNewSubmission_ReturnsSavedPost() {
|
||||
var logger = Substitute.For<ILogger<BlogController>>();
|
||||
var repo = Substitute.For<IBlogRepository>();
|
||||
|
||||
|
@ -171,23 +171,23 @@ namespace Robware.Api.Blog.Tests {
|
|||
Title = "title"
|
||||
};
|
||||
|
||||
var controller = new BlogController(logger, repo);
|
||||
(await controller.SavePost(submission)).Should().BeOfType<OkResult>();
|
||||
|
||||
var expected = new BlogPost {
|
||||
Title = "title",
|
||||
Draft = "content",
|
||||
Url = "title"
|
||||
};
|
||||
repo.SavePost(Arg.Any<BlogPost>()).Returns(expected);
|
||||
|
||||
await repo.Received(1).SavePost(Arg.Is<BlogPost>(post => ShouldBeEquivalentTo(post, expected)));
|
||||
var controller = new BlogController(logger, repo);
|
||||
(await controller.SavePost(submission)).Value.Should().Be(expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Save_WithExistingSubmission_Returns200() {
|
||||
public async Task Save_WithExistingSubmission_ReturnsSavedPost() {
|
||||
var logger = Substitute.For<ILogger<BlogController>>();
|
||||
var repo = Substitute.For<IBlogRepository>();
|
||||
repo.GetPostByIdAsync(1).Returns(new BlogPost {Content = "old content"});
|
||||
var existingPost = new BlogPost {Content = "old content"};
|
||||
repo.GetPostByIdAsync(1).Returns(existingPost);
|
||||
|
||||
var submission = new BlogPostSubmission {
|
||||
Id = 1,
|
||||
|
@ -195,17 +195,16 @@ namespace Robware.Api.Blog.Tests {
|
|||
Title = "title"
|
||||
};
|
||||
|
||||
var controller = new BlogController(logger, repo);
|
||||
(await controller.SavePost(submission)).Should().BeOfType<OkResult>();
|
||||
|
||||
var expected = new BlogPost {
|
||||
Title = "title",
|
||||
Content = "old content",
|
||||
Draft = "new content",
|
||||
Url = "title"
|
||||
};
|
||||
repo.SavePost(existingPost).Returns(expected);
|
||||
|
||||
await repo.Received(1).SavePost(Arg.Is<BlogPost>(post => ShouldBeEquivalentTo(post, expected)));
|
||||
var controller = new BlogController(logger, repo);
|
||||
(await controller.SavePost(submission)).Value.Should().BeEquivalentTo(expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
@ -221,7 +220,7 @@ namespace Robware.Api.Blog.Tests {
|
|||
};
|
||||
|
||||
var controller = new BlogController(logger, repo);
|
||||
(await controller.SavePost(submission)).Should().BeOfType<BadRequestObjectResult>();
|
||||
(await controller.SavePost(submission)).Result.Should().BeOfType<BadRequestObjectResult>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
|
|
@ -78,7 +78,7 @@ namespace Robware.Api.Blog.Controllers {
|
|||
}
|
||||
|
||||
[HttpPost(nameof(SavePost))]
|
||||
public async Task<ActionResult> SavePost(BlogPostSubmission submission) {
|
||||
public async Task<ActionResult<BlogPost>> SavePost(BlogPostSubmission submission) {
|
||||
_logger.Log(LogLevel.Information, $"{nameof(SavePost)}: {nameof(submission)}={JsonConvert.SerializeObject(submission)}");
|
||||
|
||||
try {
|
||||
|
@ -87,8 +87,7 @@ namespace Robware.Api.Blog.Controllers {
|
|||
post.UpdateTitle(submission.Title);
|
||||
post.UpdateDraft(submission.Content);
|
||||
|
||||
await _blogRepository.SavePost(post);
|
||||
return Ok();
|
||||
return await _blogRepository.SavePost(post);
|
||||
}
|
||||
catch (ItemNotFoundException e) {
|
||||
_logger.Log(LogLevel.Error, e.Message);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue