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]
|
[Fact]
|
||||||
public async Task Save_WithNewSubmission_Returns200() {
|
public async Task Save_WithNewSubmission_ReturnsSavedPost() {
|
||||||
var logger = Substitute.For<ILogger<BlogController>>();
|
var logger = Substitute.For<ILogger<BlogController>>();
|
||||||
var repo = Substitute.For<IBlogRepository>();
|
var repo = Substitute.For<IBlogRepository>();
|
||||||
|
|
||||||
|
@ -171,23 +171,23 @@ namespace Robware.Api.Blog.Tests {
|
||||||
Title = "title"
|
Title = "title"
|
||||||
};
|
};
|
||||||
|
|
||||||
var controller = new BlogController(logger, repo);
|
|
||||||
(await controller.SavePost(submission)).Should().BeOfType<OkResult>();
|
|
||||||
|
|
||||||
var expected = new BlogPost {
|
var expected = new BlogPost {
|
||||||
Title = "title",
|
Title = "title",
|
||||||
Draft = "content",
|
Draft = "content",
|
||||||
Url = "title"
|
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]
|
[Fact]
|
||||||
public async Task Save_WithExistingSubmission_Returns200() {
|
public async Task Save_WithExistingSubmission_ReturnsSavedPost() {
|
||||||
var logger = Substitute.For<ILogger<BlogController>>();
|
var logger = Substitute.For<ILogger<BlogController>>();
|
||||||
var repo = Substitute.For<IBlogRepository>();
|
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 {
|
var submission = new BlogPostSubmission {
|
||||||
Id = 1,
|
Id = 1,
|
||||||
|
@ -195,17 +195,16 @@ namespace Robware.Api.Blog.Tests {
|
||||||
Title = "title"
|
Title = "title"
|
||||||
};
|
};
|
||||||
|
|
||||||
var controller = new BlogController(logger, repo);
|
|
||||||
(await controller.SavePost(submission)).Should().BeOfType<OkResult>();
|
|
||||||
|
|
||||||
var expected = new BlogPost {
|
var expected = new BlogPost {
|
||||||
Title = "title",
|
Title = "title",
|
||||||
Content = "old content",
|
Content = "old content",
|
||||||
Draft = "new content",
|
Draft = "new content",
|
||||||
Url = "title"
|
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]
|
[Fact]
|
||||||
|
@ -221,7 +220,7 @@ namespace Robware.Api.Blog.Tests {
|
||||||
};
|
};
|
||||||
|
|
||||||
var controller = new BlogController(logger, repo);
|
var controller = new BlogController(logger, repo);
|
||||||
(await controller.SavePost(submission)).Should().BeOfType<BadRequestObjectResult>();
|
(await controller.SavePost(submission)).Result.Should().BeOfType<BadRequestObjectResult>();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|
|
@ -78,7 +78,7 @@ namespace Robware.Api.Blog.Controllers {
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost(nameof(SavePost))]
|
[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)}");
|
_logger.Log(LogLevel.Information, $"{nameof(SavePost)}: {nameof(submission)}={JsonConvert.SerializeObject(submission)}");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -87,8 +87,7 @@ namespace Robware.Api.Blog.Controllers {
|
||||||
post.UpdateTitle(submission.Title);
|
post.UpdateTitle(submission.Title);
|
||||||
post.UpdateDraft(submission.Content);
|
post.UpdateDraft(submission.Content);
|
||||||
|
|
||||||
await _blogRepository.SavePost(post);
|
return await _blogRepository.SavePost(post);
|
||||||
return Ok();
|
|
||||||
}
|
}
|
||||||
catch (ItemNotFoundException e) {
|
catch (ItemNotFoundException e) {
|
||||||
_logger.Log(LogLevel.Error, e.Message);
|
_logger.Log(LogLevel.Error, e.Message);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue