From df6838dc7c28c3113ee73dc065b70fc87cfacccf Mon Sep 17 00:00:00 2001 From: gracealfon Date: Fri, 18 Jul 2025 19:35:42 -0600 Subject: [PATCH] new backend feature --- .gitignore | 1 + MyFirstBlog/Controllers/PostsController.cs | 17 ++++++++++++ MyFirstBlog/Dtos/CreatePostDto.cs | 13 +++++++++ MyFirstBlog/Entities/Post.cs | 1 + MyFirstBlog/Helpers/DataContext.cs | 31 +++++++++++----------- MyFirstBlog/Services/PostService.cs | 7 +++++ 6 files changed, 55 insertions(+), 15 deletions(-) create mode 100644 MyFirstBlog/Dtos/CreatePostDto.cs diff --git a/.gitignore b/.gitignore index 3a868930..01b67cae 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ riderModule.iml /_ReSharper.Caches/ .fake .vscode +/.vs diff --git a/MyFirstBlog/Controllers/PostsController.cs b/MyFirstBlog/Controllers/PostsController.cs index 8fa6bf2c..fa6255cc 100644 --- a/MyFirstBlog/Controllers/PostsController.cs +++ b/MyFirstBlog/Controllers/PostsController.cs @@ -31,4 +31,21 @@ public ActionResult GetPost(string slug) { return post; } + + // POST /posts + [HttpPost] + public IActionResult CreatePost([FromBody] PostDto postDto) + { + // simple validation + if (string.IsNullOrWhiteSpace(postDto.Title)) + { + return BadRequest(new { errors = new[] { "Title cannot be blank" } }); + } + + // use the service to create post + var createdPost = _postService.CreatePost(postDto); + + // return 201 Created with the new post + return Created("", new { post = new { createdPost.Title, createdPost.Body } }); + } } diff --git a/MyFirstBlog/Dtos/CreatePostDto.cs b/MyFirstBlog/Dtos/CreatePostDto.cs new file mode 100644 index 00000000..3cfc9e0c --- /dev/null +++ b/MyFirstBlog/Dtos/CreatePostDto.cs @@ -0,0 +1,13 @@ +namespace MyFirstBlog.Dtos; + +public class CreatePostDto +{ + public string Title + { + get; set; + } + public string Description + { + get; set; + } +} \ No newline at end of file diff --git a/MyFirstBlog/Entities/Post.cs b/MyFirstBlog/Entities/Post.cs index 9347e859..bc0b8c8e 100644 --- a/MyFirstBlog/Entities/Post.cs +++ b/MyFirstBlog/Entities/Post.cs @@ -5,4 +5,5 @@ public record Post { public string Slug { get; init; } = default!; public string Body { get; init; } = default!; public DateTime CreatedDate { get; init; } + public string Description { get; init; } = default!; } diff --git a/MyFirstBlog/Helpers/DataContext.cs b/MyFirstBlog/Helpers/DataContext.cs index 60ad2bfc..0b3ef1a0 100644 --- a/MyFirstBlog/Helpers/DataContext.cs +++ b/MyFirstBlog/Helpers/DataContext.cs @@ -1,22 +1,23 @@ -namespace MyFirstBlog.Helpers; - using Microsoft.EntityFrameworkCore; using MyFirstBlog.Entities; +using Microsoft.Extensions.Configuration; - -public class DataContext : DbContext +namespace MyFirstBlog.Helpers { - protected readonly IConfiguration Configuration; - - public DataContext(IConfiguration configuration) + public class DataContext : DbContext { - Configuration = configuration; - } + protected readonly IConfiguration Configuration; - protected override void OnConfiguring(DbContextOptionsBuilder options) - { - options.UseNpgsql(ConnectionHelper.GetConnectionString(Configuration)); - } + public DataContext(IConfiguration configuration) + { + Configuration = configuration; + } - public DbSet Posts { get; set; } -} \ No newline at end of file + protected override void OnConfiguring(DbContextOptionsBuilder options) + { + options.UseNpgsql(ConnectionHelper.GetConnectionString(Configuration)); + } + + public DbSet Posts { get; set; } + } +} diff --git a/MyFirstBlog/Services/PostService.cs b/MyFirstBlog/Services/PostService.cs index 6bac099f..fa59820f 100644 --- a/MyFirstBlog/Services/PostService.cs +++ b/MyFirstBlog/Services/PostService.cs @@ -9,6 +9,7 @@ public interface IPostService { IEnumerable GetPosts(); PostDto GetPost(String slug); + void AddPost(Post post); } public class PostService : IPostService @@ -30,6 +31,12 @@ public PostDto GetPost(string slug) return getPost(slug).AsDto(); } + public void AddPost(Post post) + { + _context.Posts.Add(post); + _context.SaveChanges(); + } + private Post getPost(string slug) { return _context.Posts.Where(a=>a.Slug==slug.ToString()).SingleOrDefault();