diff --git a/.anima/.gitignore b/.anima/.gitignore new file mode 100644 index 00000000..5e465967 --- /dev/null +++ b/.anima/.gitignore @@ -0,0 +1 @@ +cache \ No newline at end of file diff --git a/MyFirstBlog.sln b/MyFirstBlog.sln index e2ffab4c..d65e3b45 100644 --- a/MyFirstBlog.sln +++ b/MyFirstBlog.sln @@ -1,9 +1,8 @@  Microsoft Visual Studio Solution File, Format Version 12.00 +# Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MyFirstBlog", "MyFirstBlog\MyFirstBlog.csproj", "{BB64A02C-C5D9-4D64-A357-7322C54B1419}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MyFirstBlogTests", "MyFirstBlogTests\MyFirstBlogTests.csproj", "{84D99732-A7E9-4E69-B5C7-7DFAFE9815B7}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -18,5 +17,9 @@ Global {84D99732-A7E9-4E69-B5C7-7DFAFE9815B7}.Debug|Any CPU.Build.0 = Debug|Any CPU {84D99732-A7E9-4E69-B5C7-7DFAFE9815B7}.Release|Any CPU.ActiveCfg = Release|Any CPU {84D99732-A7E9-4E69-B5C7-7DFAFE9815B7}.Release|Any CPU.Build.0 = Release|Any CPU + {29489431-06DB-46F2-96A1-0965F8BF8E6A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {29489431-06DB-46F2-96A1-0965F8BF8E6A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {29489431-06DB-46F2-96A1-0965F8BF8E6A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {29489431-06DB-46F2-96A1-0965F8BF8E6A}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/MyFirstBlog/Controllers/ApiController.cs b/MyFirstBlog/Controllers/ApiController.cs new file mode 100644 index 00000000..698d03e3 --- /dev/null +++ b/MyFirstBlog/Controllers/ApiController.cs @@ -0,0 +1,22 @@ +using Microsoft.AspNetCore.Mvc; +using MyFirstBlog.Models; + +namespace MyFirstBlog.Controllers +{ + [ApiController] + [Route("api/[controller]")] + public class PostsController : ControllerBase + { + [HttpPost] + public IActionResult CreatePost([FromBody] PostDto postDto) + { + if (string.IsNullOrWhiteSpace(postDto.Title)) + { + return BadRequest(new { errors = new[] { "Title cannot be blank" } }); + } + + var post = new { title = postDto.Title, description = postDto.Description }; + return CreatedAtAction(nameof(CreatePost), new { post }); + } + } +} diff --git a/MyFirstBlog/Controllers/PostsController.cs b/MyFirstBlog/Controllers/PostsController.cs index 8fa6bf2c..669bb7cd 100644 --- a/MyFirstBlog/Controllers/PostsController.cs +++ b/MyFirstBlog/Controllers/PostsController.cs @@ -1,10 +1,10 @@ -namespace MyFirstBlog.Controllers; +/* namespace MyFirstBlog.Controllers; using Microsoft.AspNetCore.Mvc; using MyFirstBlog.Dtos; using MyFirstBlog.Services; -[ApiController] + [ApiController] [Route("posts")] public class PostsController : ControllerBase { @@ -32,3 +32,4 @@ public ActionResult GetPost(string slug) { return post; } } +*/ \ No newline at end of file diff --git a/MyFirstBlog/Models/PostDto.cs b/MyFirstBlog/Models/PostDto.cs new file mode 100644 index 00000000..3e4cdbee --- /dev/null +++ b/MyFirstBlog/Models/PostDto.cs @@ -0,0 +1,8 @@ +namespace MyFirstBlog.Models +{ + public class PostDto + { + public string Title { get; set; } + public string Description { get; set; } + } +} diff --git a/MyFirstBlog/MyFirstBlog.csproj b/MyFirstBlog/MyFirstBlog.csproj index 04ccc9c1..65f00012 100644 --- a/MyFirstBlog/MyFirstBlog.csproj +++ b/MyFirstBlog/MyFirstBlog.csproj @@ -16,6 +16,10 @@ + + + + diff --git a/MyFirstBlog/Tests/PostControllerTests.cs b/MyFirstBlog/Tests/PostControllerTests.cs new file mode 100644 index 00000000..7fac0503 --- /dev/null +++ b/MyFirstBlog/Tests/PostControllerTests.cs @@ -0,0 +1,28 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using MyFirstBlog.Controllers; +using MyFirstBlog.Models; +using Microsoft.AspNetCore.Mvc; + +namespace MyFirstBlog.Tests; + +[TestClass] +public class PostsControllerTests +{ + [TestMethod] + public void CreatePost_ShouldReturn201_WhenPostIsValid() + { + var controller = new PostsController(); + var result = controller.CreatePost(new PostDto { Title = "Test Title", Description = "Test Description" }); + + Assert.IsInstanceOfType(result, typeof(CreatedAtActionResult)); + } + + [TestMethod] + public void CreatePost_ShouldReturn400_WhenTitleIsBlank() + { + var controller = new PostsController(); + var result = controller.CreatePost(new PostDto { Title = "", Description = "Test Description" }); + + Assert.IsInstanceOfType(result, typeof(BadRequestObjectResult)); + } +} diff --git a/MyFirstBlogTests/MyFirstBlogTests.csproj b/MyFirstBlogTests/MyFirstBlogTests.csproj deleted file mode 100644 index 79ed3371..00000000 --- a/MyFirstBlogTests/MyFirstBlogTests.csproj +++ /dev/null @@ -1,16 +0,0 @@ - - - - net5.0 - - false - - - - - - - - - - diff --git a/MyFirstBlogTests/UnitTest1.cs b/MyFirstBlogTests/UnitTest1.cs deleted file mode 100644 index 921a6668..00000000 --- a/MyFirstBlogTests/UnitTest1.cs +++ /dev/null @@ -1,18 +0,0 @@ -using NUnit.Framework; - -namespace MyFirstBlogTests -{ - public class Tests - { - [SetUp] - public void Setup() - { - } - - [Test] - public void Test1() - { - Assert.Pass(); - } - } -}