Get a blog post (testing mysql connection)
This commit is contained in:
parent
19c3c49f4d
commit
5a0d0933ce
6 changed files with 20 additions and 7 deletions
|
@ -1,4 +1,6 @@
|
|||
using System.Threading.Tasks;
|
||||
using System.Linq;
|
||||
using Dapper;
|
||||
using Website.Models;
|
||||
|
||||
namespace Website.Data
|
||||
|
@ -9,6 +11,13 @@ namespace Website.Data
|
|||
|
||||
public BlogRepository(DatabaseProvider dbProvider) => _dbProvider = dbProvider;
|
||||
|
||||
public Task<BlogPost> GetPost(int id) => Task.Run(() => new BlogPost{Id=id});
|
||||
public async Task<BlogPost> GetPostAsync(int id) {
|
||||
const string query = "SELECT * FROM blog_posts WHERE post_id=@id";
|
||||
using (var connection = _dbProvider.NewConnection()) {
|
||||
connection.Open();
|
||||
var result = await connection.QueryAsync<BlogPost>(query, new{id});
|
||||
return result.FirstOrDefault();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using MySql.Data.MySqlClient;
|
||||
|
||||
namespace Website.Data
|
||||
{
|
||||
|
@ -8,10 +8,10 @@ namespace Website.Data
|
|||
{
|
||||
private readonly string _connectionString;
|
||||
|
||||
public DatabaseProvider(IConfiguration config) => _connectionString = config["connectionString"];
|
||||
public DatabaseProvider(IConfiguration config) => _connectionString = config.GetConnectionString("database");
|
||||
|
||||
public DatabaseProvider(string connectionString) => _connectionString = connectionString;
|
||||
|
||||
public IDbConnection NewConnection() => new SqlConnection(_connectionString);
|
||||
public IDbConnection NewConnection() => new MySqlConnection(_connectionString);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue