Get a blog post (testing mysql connection)

This commit is contained in:
Robert Marshall 2019-04-14 11:35:40 +01:00
parent 19c3c49f4d
commit 5a0d0933ce
6 changed files with 20 additions and 7 deletions

View file

@ -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();
}
}
}
}