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
407 changes: 402 additions & 5 deletions .gitignore

Large diffs are not rendered by default.

Binary file removed .vs/MyFirstBlog/DesignTimeBuild/.dtbcache.v2
Binary file not shown.
Binary file not shown.
Binary file not shown.
1,016 changes: 0 additions & 1,016 deletions .vs/MyFirstBlog/config/applicationhost.config

This file was deleted.

Binary file removed .vs/MyFirstBlog/v17/.futdcache.v2
Binary file not shown.
Binary file removed .vs/MyFirstBlog/v17/.suo
Binary file not shown.
149 changes: 0 additions & 149 deletions .vs/MyFirstBlog/v17/DocumentLayout.json

This file was deleted.

Binary file removed .vs/ProjectEvaluation/myfirstblog.metadata.v7.bin
Binary file not shown.
Binary file removed .vs/ProjectEvaluation/myfirstblog.projects.v7.bin
Binary file not shown.
27 changes: 22 additions & 5 deletions MyFirstBlog/Controllers/PostsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,45 @@ namespace MyFirstBlog.Controllers;
[ApiController]
[Route("posts")]

public class PostsController : ControllerBase {
public class PostsController : ControllerBase
{
private IPostService _postService;

public PostsController(IPostService postService) {
public PostsController(IPostService postService)
{
_postService = postService;
}

// Get /posts
[HttpGet]
public IEnumerable<PostDto> GetPosts() {
public IEnumerable<PostDto> GetPosts()
{
return _postService.GetPosts();
}

// Get /posts/:slug
[HttpGet("{slug}")]
public ActionResult<PostDto> GetPost(string slug) {
public ActionResult<PostDto> GetPost(string slug)
{
var post = _postService.GetPost(slug);

if (post is null) {
if (post is null)
{
return NotFound();
}

return post;
}

[HttpPost]
public ActionResult<PostDto> CreatePost([FromBody] PostDto postDto)
{
if (postDto is null)
return BadRequest("Post data is required.");

var created = _postService.CreatePost(postDto);

return CreatedAtAction(nameof(GetPost), new { slug = created.Slug }, created);
}

}
4 changes: 2 additions & 2 deletions MyFirstBlog/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using MyFirstBlog.Helpers;
using MyFirstBlog.Services;

var MyAllowLocalhostOrigins = "_myAllowLocalhostOrigins";
var MyAllowLocalhostOrigins = "_myAllowLocalhostOrigins";

var builder = WebApplication.CreateBuilder(args);

Expand All @@ -13,7 +13,7 @@
services.AddDbContext<DataContext>();

services.AddCors(policyBuilder => {
policyBuilder.AddPolicy( MyAllowLocalhostOrigins,
policyBuilder.AddPolicy(MyAllowLocalhostOrigins,
policy => {
policy.WithOrigins("http://localhost:3000").AllowAnyHeader().AllowAnyMethod();
});
Expand Down
20 changes: 19 additions & 1 deletion 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);
PostDto CreatePost(PostDto postDto);
}

public class PostService : IPostService
Expand All @@ -32,6 +33,23 @@ public PostDto GetPost(string slug)

private Post getPost(string slug)
{
return _context.Posts.Where(a=>a.Slug==slug.ToString()).SingleOrDefault();
return _context.Posts.Where(a => a.Slug == slug.ToString()).SingleOrDefault();
}

public PostDto CreatePost(PostDto postDto)
{
var post = new Post
{
Id = postDto.Id != Guid.Empty ? postDto.Id : Guid.NewGuid(),
Title = postDto.Title,
Slug = postDto.Slug,
Body = postDto.Body,
CreatedDate = postDto.CreatedDate != default ? postDto.CreatedDate : DateTime.UtcNow
};

_context.Posts.Add(post);
_context.SaveChanges();

return post.AsDto();
}
}