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
|
@ -25,7 +25,7 @@ namespace Website.Controllers
|
|||
|
||||
public async Task<IActionResult> View(int id)
|
||||
{
|
||||
var post = await _repo.GetPost(id);
|
||||
var post = await _repo.GetPostAsync(id);
|
||||
return View(post);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -2,6 +2,8 @@ namespace Website.Models
|
|||
{
|
||||
public class BlogPost
|
||||
{
|
||||
public int Id { get; internal set; }
|
||||
public int PostId { get; set; }
|
||||
public string PostTitle { get; set; }
|
||||
public string Post_Title { get; set; }
|
||||
}
|
||||
}
|
|
@ -5,4 +5,4 @@
|
|||
ViewData["Title"] = "Blog";
|
||||
}
|
||||
|
||||
@Model.Id
|
||||
@Model.PostTitle
|
Loading…
Add table
Add a link
Reference in a new issue