Save a new post
This commit is contained in:
parent
71bddddc13
commit
a85c12f91c
5 changed files with 101 additions and 3 deletions
|
@ -5,12 +5,18 @@ using Microsoft.Extensions.Logging;
|
|||
using NSubstitute;
|
||||
using NSubstitute.ExceptionExtensions;
|
||||
using Robware.Api.Blog.Controllers;
|
||||
using Robware.Api.Blog.Models;
|
||||
using Robware.Blog;
|
||||
using Robware.Data;
|
||||
using Xunit;
|
||||
|
||||
namespace Robware.Api.Blog.Tests {
|
||||
public class BlogControllerTests {
|
||||
private bool ShouldBeEquivalentTo<T>(T post, T expected) {
|
||||
post.Should().BeEquivalentTo(expected);
|
||||
return true;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Get_WithUrl_ReturnsBlogPost() {
|
||||
var logger = Substitute.For<ILogger<BlogController>>();
|
||||
|
@ -154,5 +160,68 @@ namespace Robware.Api.Blog.Tests {
|
|||
var controller = new BlogController(logger, repo);
|
||||
(await controller.GetCount()).Value.Should().Be(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Save_WithNewSubmission_Returns200() {
|
||||
var logger = Substitute.For<ILogger<BlogController>>();
|
||||
var repo = Substitute.For<IBlogRepository>();
|
||||
|
||||
var submission = new BlogPostSubmission {
|
||||
Content = "content",
|
||||
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"
|
||||
};
|
||||
|
||||
await repo.Received(1).SavePost(Arg.Is<BlogPost>(post => ShouldBeEquivalentTo(post, expected)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Save_WithExistingSubmission_Returns200() {
|
||||
var logger = Substitute.For<ILogger<BlogController>>();
|
||||
var repo = Substitute.For<IBlogRepository>();
|
||||
repo.GetPostByIdAsync(1).Returns(new BlogPost {Content = "old content"});
|
||||
|
||||
var submission = new BlogPostSubmission {
|
||||
Id = 1,
|
||||
Content = "new content",
|
||||
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"
|
||||
};
|
||||
|
||||
await repo.Received(1).SavePost(Arg.Is<BlogPost>(post => ShouldBeEquivalentTo(post, expected)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Save_WithExistingSubmission_WherePostDoesntExist_Returns400() {
|
||||
var logger = Substitute.For<ILogger<BlogController>>();
|
||||
var repo = Substitute.For<IBlogRepository>();
|
||||
repo.GetPostByIdAsync(1).Throws(new ItemNotFoundException("", null));
|
||||
|
||||
var submission = new BlogPostSubmission {
|
||||
Id = 1,
|
||||
Content = "new content",
|
||||
Title = "title"
|
||||
};
|
||||
|
||||
var controller = new BlogController(logger, repo);
|
||||
(await controller.SavePost(submission)).Should().BeOfType<BadRequestObjectResult>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,8 @@ using System.Linq;
|
|||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Robware.Api.Blog.Models;
|
||||
using Robware.Blog;
|
||||
using Robware.Data;
|
||||
|
||||
|
@ -74,8 +76,24 @@ namespace Robware.Api.Blog.Controllers {
|
|||
}
|
||||
}
|
||||
|
||||
//[HttpPost]
|
||||
//Task<BlogPost> SavePost(BlogPost post);
|
||||
[HttpPost(nameof(SavePost))]
|
||||
public async Task<ActionResult> SavePost(BlogPostSubmission submission) {
|
||||
_logger.Log(LogLevel.Information, $"{nameof(SavePost)}: {nameof(submission)}={JsonConvert.SerializeObject(submission)}");
|
||||
|
||||
try {
|
||||
var post = submission.Id.HasValue ? await _blogRepository.GetPostByIdAsync(submission.Id.Value) : new BlogPost();
|
||||
|
||||
post.UpdateTitle(submission.Title);
|
||||
post.UpdateDraft(submission.Content);
|
||||
|
||||
await _blogRepository.SavePost(post);
|
||||
return Ok();
|
||||
}
|
||||
catch (ItemNotFoundException e) {
|
||||
_logger.Log(LogLevel.Error, e.Message);
|
||||
return BadRequest("Tried to update post that doesn't exist");
|
||||
}
|
||||
}
|
||||
|
||||
//[HttpGet]
|
||||
//Task<IEnumerable<BlogPost>> GetAllPosts();
|
||||
|
|
7
src/Robware.Api.Blog/Models/BlogPostSubmission.cs
Normal file
7
src/Robware.Api.Blog/Models/BlogPostSubmission.cs
Normal file
|
@ -0,0 +1,7 @@
|
|||
namespace Robware.Api.Blog.Models {
|
||||
public class BlogPostSubmission {
|
||||
public int? Id { get; set; }
|
||||
public string Title { get; set; }
|
||||
public string Content { get; set; }
|
||||
}
|
||||
}
|
|
@ -4,6 +4,10 @@
|
|||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Robware.Blog\Robware.Blog.csproj" />
|
||||
<ProjectReference Include="..\Robware.Data\Robware.Data.csproj" />
|
||||
|
|
|
@ -80,7 +80,7 @@ namespace Robware.Data
|
|||
|
||||
using (var connection = _dbProvider.NewConnection()) {
|
||||
connection.Open();
|
||||
var result = await DoQuery<int>(connection, newPost ? newPostQuery : updatePostQuery, post);
|
||||
var result = await connection.QueryAsync<int>(newPost ? newPostQuery : updatePostQuery, post);
|
||||
return newPost ? await GetPostByIdAsync(result.Single()) : post;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue