diff --git a/MyFirstBlog/Controllers/PostsController.cs b/MyFirstBlog/Controllers/PostsController.cs index 8fa6bf2c..91acb54e 100644 --- a/MyFirstBlog/Controllers/PostsController.cs +++ b/MyFirstBlog/Controllers/PostsController.cs @@ -1,34 +1,38 @@ -namespace MyFirstBlog.Controllers; - using Microsoft.AspNetCore.Mvc; using MyFirstBlog.Dtos; using MyFirstBlog.Services; [ApiController] [Route("posts")] +public class PostsController : ControllerBase +{ + private readonly IPostService _postService; -public class PostsController : ControllerBase { - private IPostService _postService; - - public PostsController(IPostService postService) { + public PostsController(IPostService postService) + { _postService = postService; } - // Get /posts [HttpGet] - public IEnumerable GetPosts() { - return _postService.GetPosts(); - } + public IEnumerable GetPosts() => _postService.GetPosts(); - // Get /posts/:slug [HttpGet("{slug}")] - public ActionResult GetPost(string slug) { + public ActionResult GetPost(string slug) + { var post = _postService.GetPost(slug); + if (post == null) return NotFound(); + return post; + } - if (post is null) { - return NotFound(); + [HttpPost] + public ActionResult CreatePost([FromBody] CreatePostRequest request) + { + if (string.IsNullOrWhiteSpace(request.Title)) + { + return BadRequest(new { errors = new[] { "Title cannot be blank" } }); } - return post; + var createdPost = _postService.CreatePost(request.Title, request.Description); + return Created(string.Empty, new { post = createdPost }); } } diff --git a/MyFirstBlog/Dtos/CreatePostRequest.cs b/MyFirstBlog/Dtos/CreatePostRequest.cs new file mode 100644 index 00000000..c1934490 --- /dev/null +++ b/MyFirstBlog/Dtos/CreatePostRequest.cs @@ -0,0 +1,8 @@ +namespace MyFirstBlog.Dtos +{ + public record CreatePostRequest + { + public string Title { get; init; } = default!; + public string Description { get; init; } = default!; + } +} diff --git a/MyFirstBlog/Program.cs b/MyFirstBlog/Program.cs index 4042b610..75976ae9 100644 --- a/MyFirstBlog/Program.cs +++ b/MyFirstBlog/Program.cs @@ -1,16 +1,19 @@ +using Microsoft.EntityFrameworkCore; using MyFirstBlog.Helpers; using MyFirstBlog.Services; var MyAllowLocalhostOrigins = "_myAllowLocalhostOrigins"; var builder = WebApplication.CreateBuilder(args); - +Console.WriteLine("CNSTR => " + builder.Configuration.GetConnectionString("DefaultConnection")); var services = builder.Services; var env = builder.Environment; // Add services to the container. -services.AddDbContext(); +services.AddDbContext(options => + options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection"))); + services.AddCors(policyBuilder => { policyBuilder.AddPolicy( MyAllowLocalhostOrigins, diff --git a/MyFirstBlog/Services/PostService.cs b/MyFirstBlog/Services/PostService.cs index 6bac099f..5ef6f23c 100644 --- a/MyFirstBlog/Services/PostService.cs +++ b/MyFirstBlog/Services/PostService.cs @@ -8,30 +8,53 @@ namespace MyFirstBlog.Services; public interface IPostService { IEnumerable GetPosts(); - PostDto GetPost(String slug); + PostDto GetPost(string slug); + PostDto CreatePost(string title, string description); } public class PostService : IPostService { - private DataContext _context; + private readonly DataContext _context; public PostService(DataContext context) { _context = context; } - public IEnumerable GetPosts() - { - return _context.Posts.Select(post => post.AsDto()); - } + public IEnumerable GetPosts() => + _context.Posts.Select(post => post.AsDto()); public PostDto GetPost(string slug) { return getPost(slug).AsDto(); } + public PostDto CreatePost(string title, string description) + { + var slug = GenerateSlug(title); + var newPost = new Post + { + Id = Guid.NewGuid(), + Title = title, + Slug = slug, + Body = description, + CreatedDate = DateTime.UtcNow + }; + + _context.Posts.Add(newPost); + _context.SaveChanges(); + + return newPost.AsDto(); + } + + private string GenerateSlug(string title) + { + return Regex.Replace(title.ToLower(), @"\s+", "-"); + } + private Post getPost(string slug) { - return _context.Posts.Where(a=>a.Slug==slug.ToString()).SingleOrDefault(); + return _context.Posts.SingleOrDefault(p => p.Slug == slug); } } + diff --git a/MyFirstBlog/appsettings.Development.json b/MyFirstBlog/appsettings.Development.json index 68bd7dc3..75dfafa3 100644 --- a/MyFirstBlog/appsettings.Development.json +++ b/MyFirstBlog/appsettings.Development.json @@ -1,6 +1,6 @@ { "ConnectionStrings": { - "DefaultConnection": "Host=localhost; Database=bvc-blog; Username=postgres; Password=postgres" + "DefaultConnection": "Host=localhost; Database=bvc-blog; Username=postgres; Password=1234" }, "Logging": { "LogLevel": { diff --git a/MyFirstBlog/appsettings.json b/MyFirstBlog/appsettings.json index 10f68b8c..ce545901 100644 --- a/MyFirstBlog/appsettings.json +++ b/MyFirstBlog/appsettings.json @@ -5,5 +5,10 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "ConnectionStrings": { + "DefaultConnection": "Host=localhost;Port=5432;Database=bvc-blog;Username=postgres;Password=1234" + } + + }