Put interface behind database provider for testing. Use state object to get data from database, then map to model.
This commit is contained in:
parent
5a0d0933ce
commit
20ccdd07f1
11 changed files with 118 additions and 34 deletions
|
@ -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<BlogPost> 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<BlogPost>(query, new{id});
|
||||
return result.FirstOrDefault();
|
||||
var result = await connection.QueryAsync<BlogPostState>(query, new{id});
|
||||
return new BlogPost(result.First());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue