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
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.
16 changes: 16 additions & 0 deletions MyFirstBlog/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Ignore Visual Studio files
.vs/
*.suo
*.user
*.vsidx

# Ignore build folders
bin/
obj/

# Ignore Rider, VS Code settings
.idea/
.vscode/

# Ignore test results or local test data
TestStore/
25 changes: 25 additions & 0 deletions MyFirstBlog/Controllers/PostsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,29 @@ public ActionResult<PostDto> GetPost(string slug) {

return post;
}

[HttpPost]
public IActionResult CreatePost([FromBody] CreatePostRequest request)
{
if (string.IsNullOrWhiteSpace(request.Title))
{
return BadRequest(new { errors = new[] { "Title cannot be blank" } });
}

var post = _postService.CreatePost(request.Title, request.Description);

return Created("", new { post });
}

}











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 class CreatePostRequest
{
public string Title { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
}

19 changes: 19 additions & 0 deletions MyFirstBlog/Dtos/DtoExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using MyFirstBlog.Entities;

namespace MyFirstBlog.Dtos;

public static class DtoExtensions
{
public static PostDto AsDto(this Post post)
{
return new PostDto
{
Id = post.Id,
Title = post.Title,
Slug = post.Slug,
Body = post.Body,
CreatedDate = post.CreatedDate
};
}
}

33 changes: 33 additions & 0 deletions MyFirstBlog/Entities/DataContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using MyFirstBlog.Entities;

namespace MyFirstBlog.Entities
{
public class DataContext : DbContext
{
protected readonly IConfiguration _configuration;

public DataContext(IConfiguration configuration)
{
_configuration = configuration;
}

public DataContext(IConfiguration configuration, DbContextOptions<DataContext> options)
: base(options)
{
_configuration = configuration;
}

protected override void OnConfiguring(DbContextOptionsBuilder options)
{
if (!options.IsConfigured)
{
options.UseNpgsql(_configuration.GetConnectionString("DefaultConnection"));
}
}

public DbSet<Post> Posts { get; set; }
}
}

22 changes: 0 additions & 22 deletions MyFirstBlog/Helpers/DataContext.cs

This file was deleted.

1 change: 1 addition & 0 deletions MyFirstBlog/Helpers/DatabaseHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace MyFirstBlog.Helpers;
using MyFirstBlog.Entities;

using Microsoft.EntityFrameworkCore;

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions MyFirstBlog/Migrations/DataContextModelSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using MyFirstBlog.Helpers;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using MyFirstBlog.Entities;

#nullable disable

Expand Down
3 changes: 3 additions & 0 deletions MyFirstBlog/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using MyFirstBlog.Helpers;
using MyFirstBlog.Entities;

using MyFirstBlog.Services;


var MyAllowLocalhostOrigins = "_myAllowLocalhostOrigins";

var builder = WebApplication.CreateBuilder(args);
Expand Down
27 changes: 27 additions & 0 deletions MyFirstBlog/Services/PostService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public interface IPostService
{
IEnumerable<PostDto> GetPosts();
PostDto GetPost(String slug);

PostDto CreatePost(string title, string body);
}

public class PostService : IPostService
Expand All @@ -34,4 +36,29 @@ private Post getPost(string slug)
{
return _context.Posts.Where(a=>a.Slug==slug.ToString()).SingleOrDefault();
}

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

var slug = Regex.Replace(title.ToLower(), @"\s+", "-");

var post = new Post
{
Id = Guid.NewGuid(),
Title = title,
Slug = slug,
Body = body,
CreatedDate = DateTime.UtcNow
};

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

return post.AsDto();
}

}
17 changes: 11 additions & 6 deletions MyFirstBlogTests/MyFirstBlogTests.csproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4"/>
<PackageReference Include="NUnit" Version="3.13.1"/>
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0"/>
<PackageReference Include="coverlet.collector" Version="3.0.2"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.17" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="NUnit" Version="3.13.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
<PackageReference Include="coverlet.collector" Version="3.0.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\MyFirstBlog\MyFirstBlog.csproj" />
</ItemGroup>

</Project>
Loading