Remove MySQL stuff

This commit is contained in:
Robert Marshall 2020-06-22 11:59:10 +01:00
parent 71db39ca74
commit 881e7fa59d
12 changed files with 21 additions and 212 deletions

View file

@ -18,13 +18,10 @@ steps:
environment: environment:
ConnectionString: ConnectionString:
from_secret: ConnectionString from_secret: ConnectionString
MySQLConnectionString:
from_secret: MySQLConnectionString
commands: commands:
- chmod +x ./build.sh - chmod +x ./build.sh
- ./build.sh - ./build.sh
- sed -i "s/<DatabaseConnectionString>/$ConnectionString/g" output/appsettings.json - sed -i "s/<DatabaseConnectionString>/$ConnectionString/g" output/appsettings.json
- sed -i "s/<MySQLConnectionString>/$MySQLConnectionString/g" output/appsettings.json
- cp api.blog.service output/ - cp api.blog.service output/
- cp -r ./output/* /output - cp -r ./output/* /output
- name: restart service - name: restart service

View file

@ -26,7 +26,7 @@ namespace Robware.Api.Blog.Tests {
var expectation = new BlogPost(); var expectation = new BlogPost();
var controller = new BlogController(logger, repo, null); var controller = new BlogController(logger, repo);
(await controller.Get("url")).Value.Should().BeEquivalentTo(expectation); (await controller.Get("url")).Value.Should().BeEquivalentTo(expectation);
} }
@ -39,7 +39,7 @@ namespace Robware.Api.Blog.Tests {
var expectation = new BlogPost(); var expectation = new BlogPost();
var controller = new BlogController(logger, repo, null); var controller = new BlogController(logger, repo);
(await controller.Get("1")).Value.Should().BeEquivalentTo(expectation); (await controller.Get("1")).Value.Should().BeEquivalentTo(expectation);
} }
@ -50,7 +50,7 @@ namespace Robware.Api.Blog.Tests {
repo.GetPostByUrlAsync("url").Throws(new ItemNotFoundException("", null)); repo.GetPostByUrlAsync("url").Throws(new ItemNotFoundException("", null));
var controller = new BlogController(logger, repo, null); var controller = new BlogController(logger, repo);
(await controller.Get("url")).Result.Should().BeOfType<NotFoundObjectResult>(); (await controller.Get("url")).Result.Should().BeOfType<NotFoundObjectResult>();
} }
@ -61,7 +61,7 @@ namespace Robware.Api.Blog.Tests {
repo.GetPostByIdAsync(1).Throws(new ItemNotFoundException("", null)); repo.GetPostByIdAsync(1).Throws(new ItemNotFoundException("", null));
var controller = new BlogController(logger, repo, null); var controller = new BlogController(logger, repo);
(await controller.Get("1")).Result.Should().BeOfType<NotFoundObjectResult>(); (await controller.Get("1")).Result.Should().BeOfType<NotFoundObjectResult>();
} }
@ -74,7 +74,7 @@ namespace Robware.Api.Blog.Tests {
var expectation = new BlogPost[10]; var expectation = new BlogPost[10];
var controller = new BlogController(logger, repo, null); var controller = new BlogController(logger, repo);
(await controller.GetLatestPosts()).Value.Should().BeEquivalentTo(expectation); (await controller.GetLatestPosts()).Value.Should().BeEquivalentTo(expectation);
} }
@ -87,7 +87,7 @@ namespace Robware.Api.Blog.Tests {
var expectation = new BlogPost[1]; var expectation = new BlogPost[1];
var controller = new BlogController(logger, repo, null); var controller = new BlogController(logger, repo);
(await controller.GetLatestPosts(1)).Value.Should().BeEquivalentTo(expectation); (await controller.GetLatestPosts(1)).Value.Should().BeEquivalentTo(expectation);
} }
@ -100,7 +100,7 @@ namespace Robware.Api.Blog.Tests {
var expectation = new BlogPost[1]; var expectation = new BlogPost[1];
var controller = new BlogController(logger, repo, null); var controller = new BlogController(logger, repo);
(await controller.GetLatestPosts(1, 1)).Value.Should().BeEquivalentTo(expectation); (await controller.GetLatestPosts(1, 1)).Value.Should().BeEquivalentTo(expectation);
} }
@ -111,7 +111,7 @@ namespace Robware.Api.Blog.Tests {
repo.GetLatestPostsAsync(1, 1000).Throws(new ItemNotFoundException("", null)); repo.GetLatestPostsAsync(1, 1000).Throws(new ItemNotFoundException("", null));
var controller = new BlogController(logger, repo, null); var controller = new BlogController(logger, repo);
(await controller.GetLatestPosts(1, 1000)).Result.Should().BeOfType<NotFoundObjectResult>(); (await controller.GetLatestPosts(1, 1000)).Result.Should().BeOfType<NotFoundObjectResult>();
} }
@ -124,7 +124,7 @@ namespace Robware.Api.Blog.Tests {
var expectation = new BlogPost(); var expectation = new BlogPost();
var controller = new BlogController(logger, repo, null); var controller = new BlogController(logger, repo);
(await controller.GetLatestPost()).Value.Should().BeEquivalentTo(expectation); (await controller.GetLatestPost()).Value.Should().BeEquivalentTo(expectation);
} }
@ -135,7 +135,7 @@ namespace Robware.Api.Blog.Tests {
repo.GetLatestPostAsync().Throws(new ItemNotFoundException("", null)); repo.GetLatestPostAsync().Throws(new ItemNotFoundException("", null));
var controller = new BlogController(logger, repo, null); var controller = new BlogController(logger, repo);
(await controller.GetLatestPost()).Result.Should().BeOfType<NotFoundObjectResult>(); (await controller.GetLatestPost()).Result.Should().BeOfType<NotFoundObjectResult>();
} }
@ -146,7 +146,7 @@ namespace Robware.Api.Blog.Tests {
repo.GetCountAsync().Returns(1); repo.GetCountAsync().Returns(1);
var controller = new BlogController(logger, repo, null); var controller = new BlogController(logger, repo);
(await controller.GetCount()).Value.Should().Be(1); (await controller.GetCount()).Value.Should().Be(1);
} }
@ -157,7 +157,7 @@ namespace Robware.Api.Blog.Tests {
repo.GetCountAsync().Throws(new ItemNotFoundException("", null)); repo.GetCountAsync().Throws(new ItemNotFoundException("", null));
var controller = new BlogController(logger, repo, null); var controller = new BlogController(logger, repo);
(await controller.GetCount()).Value.Should().Be(0); (await controller.GetCount()).Value.Should().Be(0);
} }
@ -178,7 +178,7 @@ namespace Robware.Api.Blog.Tests {
}; };
repo.SavePost(Arg.Any<BlogPost>()).Returns(expected); repo.SavePost(Arg.Any<BlogPost>()).Returns(expected);
var controller = new BlogController(logger, repo, null); var controller = new BlogController(logger, repo);
(await controller.SavePost(submission)).Value.Should().Be(expected); (await controller.SavePost(submission)).Value.Should().Be(expected);
} }
@ -203,7 +203,7 @@ namespace Robware.Api.Blog.Tests {
}; };
repo.SavePost(existingPost).Returns(expected); repo.SavePost(existingPost).Returns(expected);
var controller = new BlogController(logger, repo, null); var controller = new BlogController(logger, repo);
(await controller.SavePost(submission)).Value.Should().BeEquivalentTo(expected); (await controller.SavePost(submission)).Value.Should().BeEquivalentTo(expected);
} }
@ -219,7 +219,7 @@ namespace Robware.Api.Blog.Tests {
Title = "title" Title = "title"
}; };
var controller = new BlogController(logger, repo, null); var controller = new BlogController(logger, repo);
(await controller.SavePost(submission)).Result.Should().BeOfType<BadRequestObjectResult>(); (await controller.SavePost(submission)).Result.Should().BeOfType<BadRequestObjectResult>();
} }
@ -229,7 +229,7 @@ namespace Robware.Api.Blog.Tests {
var repo = Substitute.For<IBlogRepository>(); var repo = Substitute.For<IBlogRepository>();
repo.GetAllPostsAsync().Returns(new BlogPost[10]); repo.GetAllPostsAsync().Returns(new BlogPost[10]);
var controller = new BlogController(logger, repo, null); var controller = new BlogController(logger, repo);
(await controller.GetAllPosts()).Value.Should().BeEquivalentTo(new BlogPost[10]); (await controller.GetAllPosts()).Value.Should().BeEquivalentTo(new BlogPost[10]);
} }
@ -238,7 +238,7 @@ namespace Robware.Api.Blog.Tests {
var logger = Substitute.For<ILogger<BlogController>>(); var logger = Substitute.For<ILogger<BlogController>>();
var repo = Substitute.For<IBlogRepository>(); var repo = Substitute.For<IBlogRepository>();
var controller = new BlogController(logger, repo, null); var controller = new BlogController(logger, repo);
(await controller.DeletePost(1)).Should().BeOfType<OkResult>(); (await controller.DeletePost(1)).Should().BeOfType<OkResult>();
await repo.Received(1).DeletePostAsync(1); await repo.Received(1).DeletePostAsync(1);
} }
@ -250,7 +250,7 @@ namespace Robware.Api.Blog.Tests {
var existingPost = new BlogPost {Draft = "content"}; var existingPost = new BlogPost {Draft = "content"};
repo.GetPostByIdAsync(1).Returns(existingPost); repo.GetPostByIdAsync(1).Returns(existingPost);
var controller = new BlogController(logger, repo, null); var controller = new BlogController(logger, repo);
(await controller.PublishPost(1)).Should().BeOfType<OkResult>(); (await controller.PublishPost(1)).Should().BeOfType<OkResult>();
await repo.Received(1).SavePost(Arg.Is<BlogPost>(post => post.Content == "content")); await repo.Received(1).SavePost(Arg.Is<BlogPost>(post => post.Content == "content"));
} }
@ -261,7 +261,7 @@ namespace Robware.Api.Blog.Tests {
var repo = Substitute.For<IBlogRepository>(); var repo = Substitute.For<IBlogRepository>();
repo.GetPostByIdAsync(1).Throws(new ItemNotFoundException("", null)); repo.GetPostByIdAsync(1).Throws(new ItemNotFoundException("", null));
var controller = new BlogController(logger, repo, null); var controller = new BlogController(logger, repo);
(await controller.PublishPost(1)).Should().BeOfType<BadRequestObjectResult>(); (await controller.PublishPost(1)).Should().BeOfType<BadRequestObjectResult>();
} }
} }

View file

@ -7,8 +7,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Robware.Api.Blog", "Robware
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Robware.Blog", "Robware.Blog\Robware.Blog.csproj", "{92F5058F-8F58-4A4D-A661-49A4E1198677}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Robware.Blog", "Robware.Blog\Robware.Blog.csproj", "{92F5058F-8F58-4A4D-A661-49A4E1198677}"
EndProject 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}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Robware.Blog.Tests", "Robware.Blog.Tests\Robware.Blog.Tests.csproj", "{EEB41C17-B61C-451A-9C72-2B405DC42743}"
EndProject EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{8448EA1E-66F2-4566-8EBC-FDE8FE9BD733}" Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{8448EA1E-66F2-4566-8EBC-FDE8FE9BD733}"

View file

@ -1,13 +1,10 @@
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using MongoDB.Driver;
using Newtonsoft.Json; using Newtonsoft.Json;
using Robware.Api.Blog.Models; using Robware.Api.Blog.Models;
using Robware.Blog; using Robware.Blog;
using Robware.Data;
namespace Robware.Api.Blog.Controllers { namespace Robware.Api.Blog.Controllers {
[ApiController] [ApiController]
@ -16,12 +13,10 @@ namespace Robware.Api.Blog.Controllers {
private readonly ILogger<BlogController> _logger; private readonly ILogger<BlogController> _logger;
private readonly IBlogRepository _blogRepository; private readonly IBlogRepository _blogRepository;
private readonly IDatabaseProvider _dbProvider;
public BlogController(ILogger<BlogController> logger, IBlogRepository blogRepository, IDatabaseProvider dbProvider) { public BlogController(ILogger<BlogController> logger, IBlogRepository blogRepository) {
_logger = logger; _logger = logger;
_blogRepository = blogRepository; _blogRepository = blogRepository;
_dbProvider = dbProvider;
} }
[HttpGet(nameof(Get) + "/{url}")] [HttpGet(nameof(Get) + "/{url}")]
@ -121,16 +116,5 @@ namespace Robware.Api.Blog.Controllers {
return BadRequest("Tried to publish post that doesn't exist"); return BadRequest("Tried to publish post that doesn't exist");
} }
} }
[HttpGet(nameof(Migrate))]
public async Task<ActionResult> Migrate() {
var mysqlRepo = new Data.BlogRepository(_dbProvider);
var existingPosts = await mysqlRepo.GetAllPostsAsync();
foreach (var post in existingPosts)
await _blogRepository.SavePost(post);
return Ok();
}
} }
} }

View file

@ -11,7 +11,6 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Robware.Blog\Robware.Blog.csproj" /> <ProjectReference Include="..\Robware.Blog\Robware.Blog.csproj" />
<ProjectReference Include="..\Robware.Data\Robware.Data.csproj" />
<ProjectReference Include="..\Robware.Data.MongoDB\Robware.Data.MongoDB.csproj" /> <ProjectReference Include="..\Robware.Data.MongoDB\Robware.Data.MongoDB.csproj" />
</ItemGroup> </ItemGroup>

View file

@ -20,7 +20,6 @@ namespace Robware.Api.Blog {
services.AddControllers(); services.AddControllers();
services services
.AddSingleton<Data.IDatabaseProvider>(new Data.MySQLDatabaseProvider(Configuration.GetConnectionString("mysql")))
.AddSingleton<IMongoDatabase>(SetupMongoDatabase()) .AddSingleton<IMongoDatabase>(SetupMongoDatabase())
.AddSingleton<IBlogRepository, BlogRepository>(); .AddSingleton<IBlogRepository, BlogRepository>();
} }

View file

@ -15,7 +15,6 @@
} }
}, },
"ConnectionStrings": { "ConnectionStrings": {
"database": "<DatabaseConnectionString>", "database": "<DatabaseConnectionString>"
"mysql": "<MySQLConnectionString>"
} }
} }

View file

@ -1,107 +0,0 @@
using System.Collections.Generic;
using System.Data;
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
};
private async Task<IEnumerable<T>> DoQuery<T>(IDbConnection connection, string query, object param = null) {
var result = await connection.QueryAsync<T>(query, param);
if (!result.Any())
throw new ItemNotFoundException(query, param);
return result;
}
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 DoQuery<BlogPostState>(connection, 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 DoQuery<BlogPostState>(connection, 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 DoQuery<int>(connection, 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 DoQuery<BlogPostState>(connection, 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

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

View file

@ -1,14 +0,0 @@
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

@ -1,18 +0,0 @@
<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

@ -1,19 +0,0 @@
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; }
}
}