Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ riderModule.iml
/_ReSharper.Caches/
.fake
.vscode
/.vs
17 changes: 17 additions & 0 deletions MyFirstBlog/Controllers/PostsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,21 @@ public ActionResult<PostDto> 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 } });
}
}
13 changes: 13 additions & 0 deletions MyFirstBlog/Dtos/CreatePostDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace MyFirstBlog.Dtos;

public class CreatePostDto
{
public string Title
{
get; set;
}
public string Description
{
get; set;
}
}
1 change: 1 addition & 0 deletions MyFirstBlog/Entities/Post.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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!;
}
31 changes: 16 additions & 15 deletions MyFirstBlog/Helpers/DataContext.cs
Original file line number Diff line number Diff line change
@@ -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<Post> Posts { get; set; }
}
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
options.UseNpgsql(ConnectionHelper.GetConnectionString(Configuration));
}

public DbSet<Post> Posts { get; set; }
}
}
7 changes: 7 additions & 0 deletions MyFirstBlog/Services/PostService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public interface IPostService
{
IEnumerable<PostDto> GetPosts();
PostDto GetPost(String slug);
void AddPost(Post post);
}

public class PostService : IPostService
Expand All @@ -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();
Expand Down