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
31 changes: 26 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
# Ignore build output directories
bin/
obj/
/packages/
riderModule.iml
/_ReSharper.Caches/
.fake
.vscode

# Ignore NuGet packages
*.nupkg
**/packages/*

# Ignore temporary files
*.tmp
*.temp
*.log
*.suo
*.cache
*.csproj.user
*.userosscache
*.sln.docstates
*.vs/

# Ignore Visual Studio/Code workspace and configuration files
.vscode/
.idea/
.vs/

# Ignore configuration files generated by NuGet
project.lock.json
project.fragment.lock.json
project.assets.json
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.
33 changes: 25 additions & 8 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(CreatePostDto postDto)
{
try
{
var createdPost = _postService.CreatePost(postDto);
return CreatedAtAction(nameof(GetPost), new { slug = createdPost.Slug }, createdPost);
}
catch (ArgumentException ex)
{
return BadRequest(new { errors = new[] { ex.Message } });
}
}
}
8 changes: 8 additions & 0 deletions MyFirstBlog/Dtos/CreatePostDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace MyFirstBlog.Dtos
{
public class CreatePostDto
{
public string Title { get; set; }
public string Description { get; set; }
}
}
80 changes: 56 additions & 24 deletions MyFirstBlog/Services/PostService.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,69 @@
namespace MyFirstBlog.Services;

using System;
using System.Linq;
using MyFirstBlog.Helpers;
using MyFirstBlog.Entities;
using System.Text.RegularExpressions;
using MyFirstBlog.Dtos;
using System.Collections.Generic;
using System.Text.RegularExpressions;

public interface IPostService
{
IEnumerable<PostDto> GetPosts();
PostDto GetPost(String slug);
}

public class PostService : IPostService
namespace MyFirstBlog.Services
{
private DataContext _context;

public PostService(DataContext context)
public interface IPostService
{
_context = context;
IEnumerable<PostDto> GetPosts();
PostDto GetPost(string slug);
PostDto CreatePost(CreatePostDto postDto);
}

public IEnumerable<PostDto> GetPosts()
public class PostService : IPostService
{
return _context.Posts.Select(post => post.AsDto());
}
private readonly DataContext _context;

public PostDto GetPost(string slug)
{
return getPost(slug).AsDto();
}
public PostService(DataContext context)
{
_context = context;
}

private Post getPost(string slug)
{
return _context.Posts.Where(a=>a.Slug==slug.ToString()).SingleOrDefault();
public IEnumerable<PostDto> GetPosts()
{
return _context.Posts.Select(post => post.AsDto());
}

public PostDto GetPost(string slug)
{
return getPost(slug)?.AsDto();
}

public PostDto CreatePost(CreatePostDto postDto)
{
if (string.IsNullOrWhiteSpace(postDto.Title))
{
throw new ArgumentException("Title cannot be blank.");
}

var newPost = new Post
{
Id = Guid.NewGuid(),
Title = postDto.Title,
Slug = GenerateSlug(postDto.Title),
Body = postDto.Description,
CreatedDate = DateTime.UtcNow
};

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

return newPost.AsDto();
}

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

private string GenerateSlug(string title)
{
return Regex.Replace(title.ToLower(), @"\s+", "-");
}
}
}