Copy blog code to new API micro service repo

This commit is contained in:
Robert Marshall 2020-04-10 09:28:43 +01:00
commit b23e96493c
24 changed files with 763 additions and 0 deletions

48
src/Robware.Api.Blog.sln Normal file
View file

@ -0,0 +1,48 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29613.14
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Robware.Api.Blog", "Robware.Api.Blog\Robware.Api.Blog.csproj", "{C5B2C08C-423C-494D-8C2B-77F2C73F36CC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Robware.Blog", "Robware.Blog\Robware.Blog.csproj", "{92F5058F-8F58-4A4D-A661-49A4E1198677}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Robware.Data", "Robware.Data\Robware.Data.csproj", "{8860B6F7-8740-4CF5-BF24-67D00CA7C1A3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Robware.Blog.Tests", "Robware.Blog.Tests\Robware.Blog.Tests.csproj", "{EEB41C17-B61C-451A-9C72-2B405DC42743}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{8448EA1E-66F2-4566-8EBC-FDE8FE9BD733}"
ProjectSection(SolutionItems) = preProject
..\.editorconfig = ..\.editorconfig
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C5B2C08C-423C-494D-8C2B-77F2C73F36CC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C5B2C08C-423C-494D-8C2B-77F2C73F36CC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C5B2C08C-423C-494D-8C2B-77F2C73F36CC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C5B2C08C-423C-494D-8C2B-77F2C73F36CC}.Release|Any CPU.Build.0 = Release|Any CPU
{92F5058F-8F58-4A4D-A661-49A4E1198677}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{92F5058F-8F58-4A4D-A661-49A4E1198677}.Debug|Any CPU.Build.0 = Debug|Any CPU
{92F5058F-8F58-4A4D-A661-49A4E1198677}.Release|Any CPU.ActiveCfg = Release|Any CPU
{92F5058F-8F58-4A4D-A661-49A4E1198677}.Release|Any CPU.Build.0 = Release|Any CPU
{8860B6F7-8740-4CF5-BF24-67D00CA7C1A3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8860B6F7-8740-4CF5-BF24-67D00CA7C1A3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8860B6F7-8740-4CF5-BF24-67D00CA7C1A3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8860B6F7-8740-4CF5-BF24-67D00CA7C1A3}.Release|Any CPU.Build.0 = Release|Any CPU
{EEB41C17-B61C-451A-9C72-2B405DC42743}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EEB41C17-B61C-451A-9C72-2B405DC42743}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EEB41C17-B61C-451A-9C72-2B405DC42743}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EEB41C17-B61C-451A-9C72-2B405DC42743}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6B846E24-8B8F-4472-AAF1-5500B4C6303F}
EndGlobalSection
EndGlobal

View file

@ -0,0 +1,53 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Robware.Blog;
namespace Robware.Api.Blog.Controllers {
[ApiController]
public class BlogController : ControllerBase {
private readonly ILogger<BlogController> _logger;
private readonly IBlogRepository _blogRepository;
public BlogController(ILogger<BlogController> logger, IBlogRepository blogRepository) {
_logger = logger;
_blogRepository = blogRepository;
}
[HttpGet(nameof(Get) + "/{url}")]
public async Task<BlogPost> Get(string url) {
_logger.Log(LogLevel.Information, $"{nameof(Get)}: {nameof(url)}={url}");
if (int.TryParse(url, out int id)) {
return await _blogRepository.GetPostByIdAsync(id);
}
return await _blogRepository.GetPostByUrlAsync(url);
}
//[HttpGet]
//Task<BlogPost> GetPostByUrl();
//[HttpGet]
//Task<IEnumerable<BlogPost>> GetLatestPosts(int limit, int offset = 0);
//[HttpGet]
//Task<BlogPost> GetLatestPost();
//[HttpGet]
//Task<int> GetCount();
//[HttpGet]
//Task<BlogPost> GetPostById(int id);
//[HttpPost]
//Task<BlogPost> SavePost(BlogPost post);
//[HttpGet]
//Task<IEnumerable<BlogPost>> GetAllPosts();
//[HttpGet]
//Task DeletePost(int id);
}
}

View file

@ -0,0 +1,16 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
namespace Robware.Api.Blog {
public class Program {
public static void Main(string[] args) {
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder => {
webBuilder.UseStartup<Startup>();
});
}
}

View file

@ -0,0 +1,30 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:53729",
"sslPort": 44379
}
},
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "blog",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Robware.Api.Blog": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "weatherforecast",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:5001;http://localhost:5000"
}
}
}

View file

@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Robware.Blog\Robware.Blog.csproj" />
<ProjectReference Include="..\Robware.Data\Robware.Data.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,43 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Robware.Blog;
using Robware.Data;
namespace Robware.Api.Blog {
public class Startup {
public Startup(IConfiguration configuration) {
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services) {
services.AddControllers();
services
.AddSingleton<IDatabaseProvider>(new MySQLDatabaseProvider(Configuration.GetConnectionString("database")))
.AddSingleton<IBlogRepository, BlogRepository>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
if (env.IsDevelopment()) {
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints => {
endpoints.MapControllers();
});
}
}
}

View file

@ -0,0 +1,12 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"ConnectionStrings": {
"database": "Server=localhost;User ID=user;Password=pass;Database=db"
}
}

View file

@ -0,0 +1,10 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}

View file

@ -0,0 +1,42 @@
using FluentAssertions;
using Robware.Blog;
using Xunit;
namespace Website.Tests.Models {
public class BlogPostTests {
[Fact]
public void UpdateTitle_WithNewTitle_UpdatesTitlePropertyAndRegeneratesUrl() {
var post = new BlogPost();
post.UpdateTitle("new title");
post.Title.Should().Be("new title");
post.Url.Should().Be("new-title");
}
[Theory]
[InlineData("new:title", "new-title")]
[InlineData("new: title", "new-title")]
[InlineData("new$title", "new-title")]
[InlineData("new TITle", "new-title")]
public void UpdateTitle_WithUnsafeCharsInTitle_RegeneratesSafeUrl(string title, string url) {
var post = new BlogPost();
post.UpdateTitle(title);
post.Url.Should().Be(url);
}
[Fact]
public void UpdateDraft_WithContent_UpdatesDraftProperty() {
var post = new BlogPost();
post.UpdateDraft("content");
post.Draft.Should().Be("content");
}
[Fact]
public void Publish_SetsContentToDraft() {
var post = new BlogPost();
post.UpdateDraft("content");
post.Publish();
post.Content.Should().Be("content");
}
}
}

View file

@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="coverlet.collector" Version="1.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Robware.Blog\Robware.Blog.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,28 @@
using System;
using System.Text.RegularExpressions;
namespace Robware.Blog
{
public class BlogPost
{
public int Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public DateTime Timestamp { get; set; }
public string Draft { get; set; }
public string Url { get; set; }
public int UserId { get; set; }
private void GenerateUrl() {
Url = Regex.Replace(Title, @"[^a-zA-Z0-9\.]+", "-").ToLower();
}
public void UpdateTitle(string title) {
Title = title;
GenerateUrl();
}
public void UpdateDraft(string content) => Draft = content;
public void Publish() => Content = Draft;
}
}

View file

@ -0,0 +1,15 @@
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Robware.Blog {
public interface IBlogRepository {
Task<BlogPost> GetPostByUrlAsync(string url);
Task<IEnumerable<BlogPost>> GetLatestPostsAsync(int limit, int offset = 0);
Task<BlogPost> GetLatestPostAsync();
Task<int> GetCountAsync();
Task<BlogPost> GetPostByIdAsync(int id);
Task<BlogPost> SavePost(BlogPost post);
Task<IEnumerable<BlogPost>> GetAllPostsAsync();
Task DeletePostAsync(int id);
}
}

View file

@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
</Project>

View file

@ -0,0 +1,97 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Dapper;
using Robware.Blog;
using Robware.Data.States;
namespace Robware.Data
{
public class BlogRepository : IBlogRepository
{
private readonly IDatabaseProvider _dbProvider;
public BlogRepository(IDatabaseProvider dbProvider) => _dbProvider = dbProvider;
private static BlogPost MapBlogPost(BlogPostState state) => new BlogPost {
Id = state.Post_Id,
Title = state.Post_Title,
Content = state.Post_Content,
Timestamp = state.Post_Timestamp,
Draft = state.Post_Draft,
Url = state.Post_Url,
UserId = state.User_Id
};
public async Task<BlogPost> GetPostByUrlAsync(string url) {
const string query = "SELECT * FROM blog_posts WHERE post_url=@url AND post_deleted=0";
using (var connection = _dbProvider.NewConnection()) {
connection.Open();
var result = await connection.QueryAsync<BlogPostState>(query, new{url});
return MapBlogPost(result.First());
}
}
public async Task<IEnumerable<BlogPost>> GetLatestPostsAsync(int limit, int offset = 0) {
const string query = "SELECT * FROM blog_posts WHERE post_content<>'' AND post_deleted=0 ORDER BY post_timestamp DESC LIMIT @offset,@limit";
using (var connection = _dbProvider.NewConnection()) {
connection.Open();
var results = await connection.QueryAsync<BlogPostState>(query, new{limit, offset});
return results.Select(MapBlogPost);
}
}
public async Task<BlogPost> GetLatestPostAsync() => (await GetLatestPostsAsync(1)).First();
public async Task<int> GetCountAsync()
{
var query="SELECT COUNT(*) FROM blog_posts WHERE post_content<>'' AND post_deleted=0";
using(var connection = _dbProvider.NewConnection()) {
connection.Open();
var result = await connection.QueryAsync<int>(query);
return result.First();
}
}
public async Task<BlogPost> GetPostByIdAsync(int id) {
const string query = "SELECT * FROM blog_posts WHERE post_id=@id AND post_deleted=0";
using (var connection = _dbProvider.NewConnection()) {
connection.Open();
var result = await connection.QueryAsync<BlogPostState>(query, new {id});
return MapBlogPost(result.First());
}
}
public async Task<BlogPost> SavePost(BlogPost post) {
const string newPostQuery = "INSERT INTO blog_posts (post_title, post_content, post_draft, post_url) VALUES (@title, @content, @draft, @url); SELECT CAST(LAST_INSERT_ID() as int)";
const string updatePostQuery = "UPDATE blog_posts SET post_id = @id, post_title = @title, post_content = @content, post_draft = @draft, post_url = @url WHERE post_id = @id ";
var newPost = post.Id == 0;
using (var connection = _dbProvider.NewConnection()) {
connection.Open();
var result = await connection.QueryAsync<int>(newPost ? newPostQuery : updatePostQuery, post);
return newPost ? await GetPostByIdAsync(result.Single()) : post;
}
}
public async Task<IEnumerable<BlogPost>> GetAllPostsAsync() {
const string query = "SELECT * FROM blog_posts WHERE post_deleted=0";
using (var connection = _dbProvider.NewConnection()) {
connection.Open();
var result = await connection.QueryAsync<BlogPostState>(query);
return result.Select(MapBlogPost);
}
}
public async Task DeletePostAsync(int id) {
const string query = "UPDATE blog_posts SET post_deleted=1 WHERE post_id=@id";
using (var connection = _dbProvider.NewConnection()) {
connection.Open();
await connection.ExecuteAsync(query, new {id});
}
}
}
}

View file

@ -0,0 +1,9 @@
using System.Data;
namespace Robware.Data
{
public interface IDatabaseProvider
{
IDbConnection NewConnection();
}
}

View file

@ -0,0 +1,14 @@
using System.Data;
using MySql.Data.MySqlClient;
namespace Robware.Data
{
public class MySQLDatabaseProvider:IDatabaseProvider
{
private readonly string _connectionString;
public MySQLDatabaseProvider(string connectionString) => _connectionString = connectionString;
public IDbConnection NewConnection() => new MySqlConnection(_connectionString);
}
}

View file

@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="dapper" Version="2.0.35" />
<PackageReference Include="dapper.contrib" Version="2.0.35" />
<PackageReference Include="mysqlconnector" Version="0.63.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Robware.Blog\Robware.Blog.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,19 @@
using System;
using Dapper.Contrib.Extensions;
namespace Robware.Data.States
{
[Table("blog_posts")]
public class BlogPostState
{
[Key]
public int Post_Id { get; set; }
public string Post_Title { get; set; }
public string Post_Content { get; set; }
public DateTime Post_Timestamp { get; set; }
public string Post_Draft { get; set; }
public string Post_Url { get; set; }
public bool Post_Deleted{ get; set; }
public int User_Id { get; set; }
}
}