Publish post
This commit is contained in:
parent
cfcef8b77d
commit
2a642faecd
2 changed files with 36 additions and 0 deletions
|
@ -242,5 +242,27 @@ namespace Robware.Api.Blog.Tests {
|
|||
(await controller.DeletePost(1)).Should().BeOfType<OkResult>();
|
||||
await repo.Received(1).DeletePostAsync(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task PublishPost_WithId_Returns200() {
|
||||
var logger = Substitute.For<ILogger<BlogController>>();
|
||||
var repo = Substitute.For<IBlogRepository>();
|
||||
var existingPost = new BlogPost {Draft = "content"};
|
||||
repo.GetPostByIdAsync(1).Returns(existingPost);
|
||||
|
||||
var controller = new BlogController(logger, repo);
|
||||
(await controller.PublishPost(1)).Should().BeOfType<OkResult>();
|
||||
await repo.Received(1).SavePost(Arg.Is<BlogPost>(post => post.Content == "content"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task PublishPost_WithIdForPostThatDoesntExist_Returns400() {
|
||||
var logger = Substitute.For<ILogger<BlogController>>();
|
||||
var repo = Substitute.For<IBlogRepository>();
|
||||
repo.GetPostByIdAsync(1).Throws(new ItemNotFoundException("", null));
|
||||
|
||||
var controller = new BlogController(logger, repo);
|
||||
(await controller.PublishPost(1)).Should().BeOfType<BadRequestObjectResult>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -103,5 +103,19 @@ namespace Robware.Api.Blog.Controllers {
|
|||
await _blogRepository.DeletePostAsync(id);
|
||||
return Ok();
|
||||
}
|
||||
|
||||
[HttpPost(nameof(PublishPost))]
|
||||
public async Task<ActionResult> PublishPost([FromBody] int id) {
|
||||
try {
|
||||
var post = await _blogRepository.GetPostByIdAsync(id);
|
||||
post.Publish();
|
||||
await _blogRepository.SavePost(post);
|
||||
return Ok();
|
||||
}
|
||||
catch (ItemNotFoundException e) {
|
||||
_logger.Log(LogLevel.Error, e.Message);
|
||||
return BadRequest("Tried to publish post that doesn't exist");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue