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
34 changes: 19 additions & 15 deletions MyFirstBlog/Controllers/PostsController.cs
Original file line number Diff line number Diff line change
@@ -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<PostDto> GetPosts() {
return _postService.GetPosts();
}
public IEnumerable<PostDto> GetPosts() => _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 == null) return NotFound();
return post;
}

if (post is null) {
return NotFound();
[HttpPost]
public ActionResult<object> 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 });
}
}
8 changes: 8 additions & 0 deletions MyFirstBlog/Dtos/CreatePostRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace MyFirstBlog.Dtos
{
public record CreatePostRequest
{
public string Title { get; init; } = default!;
public string Description { get; init; } = default!;
}
}
7 changes: 5 additions & 2 deletions MyFirstBlog/Program.cs
Original file line number Diff line number Diff line change
@@ -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<DataContext>();
services.AddDbContext<DataContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));


services.AddCors(policyBuilder => {
policyBuilder.AddPolicy( MyAllowLocalhostOrigins,
Expand Down
37 changes: 30 additions & 7 deletions MyFirstBlog/Services/PostService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,53 @@ namespace MyFirstBlog.Services;
public interface IPostService
{
IEnumerable<PostDto> 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<PostDto> GetPosts()
{
return _context.Posts.Select(post => post.AsDto());
}
public IEnumerable<PostDto> 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);
}
}

2 changes: 1 addition & 1 deletion MyFirstBlog/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
7 changes: 6 additions & 1 deletion MyFirstBlog/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"ConnectionStrings": {
"DefaultConnection": "Host=localhost;Port=5432;Database=bvc-blog;Username=postgres;Password=1234"
}


}