diff --git a/Website.Tests/Data/MySQLDatabaseProviderTests.cs b/Website.Tests/Data/MySQLDatabaseProviderTests.cs new file mode 100644 index 0000000..b4c4967 --- /dev/null +++ b/Website.Tests/Data/MySQLDatabaseProviderTests.cs @@ -0,0 +1,32 @@ +using Website.Data; +using Xunit; +using FluentAssertions; +using MySql.Data.MySqlClient; +using NSubstitute; +using Microsoft.Extensions.Configuration; + +namespace Website.Tests.Data +{ + public class MySQLDatabaseProviderTests + { + const string ConnectionString = "Server=host;User ID=username;Password=password;Database=database"; + + [Fact] + public void NewConnection_WithStringConstructor_ReturnsMySQLConnection() { + var provider = new MySQLDatabaseProvider(ConnectionString); + var connection = provider.NewConnection(); + connection.Should().BeOfType(); + (connection as MySqlConnection).ConnectionString.Should().Be(ConnectionString); + } + + [Fact] + public void NewConnection_WithConfigConstructor_ReturnsMySQLConnection() { + var config = Substitute.For(); + config.GetConnectionString("database").Returns(ConnectionString); + var provider = new MySQLDatabaseProvider(ConnectionString); + var connection = provider.NewConnection(); + connection.Should().BeOfType(); + (connection as MySqlConnection).ConnectionString.Should().Be(ConnectionString); + } + } +} \ No newline at end of file diff --git a/Website.Tests/Website.Tests.csproj b/Website.Tests/Website.Tests.csproj index 5b7e8ff..8a9ff2d 100644 --- a/Website.Tests/Website.Tests.csproj +++ b/Website.Tests/Website.Tests.csproj @@ -12,7 +12,9 @@ all + + diff --git a/Website/Data/BlogRepository.cs b/Website/Data/BlogRepository.cs index b186b82..8cb7e0d 100644 --- a/Website/Data/BlogRepository.cs +++ b/Website/Data/BlogRepository.cs @@ -2,21 +2,22 @@ using System.Threading.Tasks; using System.Linq; using Dapper; using Website.Models; +using Website.Data.States; namespace Website.Data { public class BlogRepository { - private readonly DatabaseProvider _dbProvider; + private readonly IDatabaseProvider _dbProvider; - public BlogRepository(DatabaseProvider dbProvider) => _dbProvider = dbProvider; + public BlogRepository(IDatabaseProvider dbProvider) => _dbProvider = dbProvider; public async Task GetPostAsync(int id) { - const string query = "SELECT * FROM blog_posts WHERE post_id=@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(query, new{id}); - return result.FirstOrDefault(); + var result = await connection.QueryAsync(query, new{id}); + return new BlogPost(result.First()); } } } diff --git a/Website/Data/DatabaseProvider.cs b/Website/Data/DatabaseProvider.cs deleted file mode 100644 index bfb9418..0000000 --- a/Website/Data/DatabaseProvider.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System.Data; -using Microsoft.Extensions.Configuration; -using MySql.Data.MySqlClient; - -namespace Website.Data -{ - public class DatabaseProvider - { - private readonly string _connectionString; - - public DatabaseProvider(IConfiguration config) => _connectionString = config.GetConnectionString("database"); - - public DatabaseProvider(string connectionString) => _connectionString = connectionString; - - public IDbConnection NewConnection() => new MySqlConnection(_connectionString); - } -} \ No newline at end of file diff --git a/Website/Data/IDatabaseProvider.cs b/Website/Data/IDatabaseProvider.cs new file mode 100644 index 0000000..7423ac0 --- /dev/null +++ b/Website/Data/IDatabaseProvider.cs @@ -0,0 +1,9 @@ +using System.Data; + +namespace Website.Data +{ + public interface IDatabaseProvider + { + IDbConnection NewConnection(); + } +} \ No newline at end of file diff --git a/Website/Data/MySQLDatabaseProvider.cs b/Website/Data/MySQLDatabaseProvider.cs new file mode 100644 index 0000000..e9155a0 --- /dev/null +++ b/Website/Data/MySQLDatabaseProvider.cs @@ -0,0 +1,17 @@ +using System.Data; +using Microsoft.Extensions.Configuration; +using MySql.Data.MySqlClient; + +namespace Website.Data +{ + public class MySQLDatabaseProvider:IDatabaseProvider + { + private readonly string _connectionString; + + public MySQLDatabaseProvider(IConfiguration config) => _connectionString = config.GetConnectionString("database"); + + public MySQLDatabaseProvider(string connectionString) => _connectionString = connectionString; + + public IDbConnection NewConnection() => new MySqlConnection(_connectionString); + } +} \ No newline at end of file diff --git a/Website/Data/States/BlogPostState.cs b/Website/Data/States/BlogPostState.cs new file mode 100644 index 0000000..3412f8c --- /dev/null +++ b/Website/Data/States/BlogPostState.cs @@ -0,0 +1,19 @@ +using System; +using Dapper.Contrib.Extensions; + +namespace Website.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; } + } +} \ No newline at end of file diff --git a/Website/Models/BlogPost.cs b/Website/Models/BlogPost.cs index 967d85a..17cfe01 100644 --- a/Website/Models/BlogPost.cs +++ b/Website/Models/BlogPost.cs @@ -1,9 +1,27 @@ +using System; +using Website.Data.States; + namespace Website.Models { public class BlogPost { - public int PostId { get; set; } - public string PostTitle { get; set; } - public string Post_Title { get; set; } + public BlogPost(BlogPostState state) + { + 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 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; } } } \ No newline at end of file diff --git a/Website/Startup.cs b/Website/Startup.cs index ca2dba7..a5fa2f0 100644 --- a/Website/Startup.cs +++ b/Website/Startup.cs @@ -39,7 +39,7 @@ namespace Website } private void RegisterRepositories(IServiceCollection services) => - services.AddSingleton() + services.AddSingleton() .AddSingleton(); // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. diff --git a/Website/Views/Blog/View.cshtml b/Website/Views/Blog/View.cshtml index 567204e..f683f4c 100644 --- a/Website/Views/Blog/View.cshtml +++ b/Website/Views/Blog/View.cshtml @@ -5,4 +5,4 @@ ViewData["Title"] = "Blog"; } -@Model.PostTitle \ No newline at end of file +@Model.Title \ No newline at end of file diff --git a/Website/appsettings.Development.json b/Website/appsettings.Development.json index 4e0c46f..de2d24c 100644 --- a/Website/appsettings.Development.json +++ b/Website/appsettings.Development.json @@ -1,9 +1,12 @@ { - "Logging": { - "LogLevel": { - "Default": "Debug", - "System": "Information", - "Microsoft": "Information" + "Logging": { + "LogLevel": { + "Default": "Debug", + "System": "Information", + "Microsoft": "Information" + } + }, + "ConnectionStrings": { + "database": "Server=localhost;User ID=user;Password=pass;Database=db" } - } -} +} \ No newline at end of file