Add basic blog repo and models to test potential project structure

This commit is contained in:
Robert Marshall 2019-04-14 10:25:52 +01:00
parent 7703f9d8bb
commit 19c3c49f4d
10 changed files with 91 additions and 3 deletions

View file

@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Website.Data;
using Website.ViewModels;
namespace Website.Controllers
{
public class BlogController : Controller
{
private readonly BlogRepository _repo;
public BlogController(BlogRepository repo)
{
_repo = repo;
}
public IActionResult Index()
{
return View();
}
public async Task<IActionResult> View(int id)
{
var post = await _repo.GetPost(id);
return View(post);
}
}
}

View file

@ -4,7 +4,7 @@ using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Website.Models;
using Website.ViewModels;
namespace Website.Controllers
{

View file

@ -0,0 +1,14 @@
using System.Threading.Tasks;
using Website.Models;
namespace Website.Data
{
public class BlogRepository
{
private readonly DatabaseProvider _dbProvider;
public BlogRepository(DatabaseProvider dbProvider) => _dbProvider = dbProvider;
public Task<BlogPost> GetPost(int id) => Task.Run(() => new BlogPost{Id=id});
}
}

View file

@ -0,0 +1,17 @@
using System.Data;
using System.Data.SqlClient;
using Microsoft.Extensions.Configuration;
namespace Website.Data
{
public class DatabaseProvider
{
private readonly string _connectionString;
public DatabaseProvider(IConfiguration config) => _connectionString = config["connectionString"];
public DatabaseProvider(string connectionString) => _connectionString = connectionString;
public IDbConnection NewConnection() => new SqlConnection(_connectionString);
}
}

View file

@ -0,0 +1,7 @@
namespace Website.Models
{
public class BlogPost
{
public int Id { get; internal set; }
}
}

View file

@ -9,6 +9,7 @@ using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Website.Data;
namespace Website
{
@ -31,10 +32,16 @@ namespace Website
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddSingleton<IConfiguration>(Configuration);
RegisterRepositories(services);
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
private void RegisterRepositories(IServiceCollection services) =>
services.AddSingleton<DatabaseProvider, DatabaseProvider>()
.AddSingleton<BlogRepository, BlogRepository>();
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{

View file

@ -1,6 +1,6 @@
using System;
namespace Website.Models
namespace Website.ViewModels
{
public class ErrorViewModel
{

View file

@ -0,0 +1,8 @@
@using Website.Models;
@model BlogPost;
@{
ViewData["Title"] = "Blog";
}
@Model.Id

View file

@ -1,3 +1,3 @@
@using Website
@using Website.Models
@using Website.ViewModels
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

View file

@ -7,8 +7,11 @@
<ItemGroup>
<PackageReference Include="dapper" Version="1.60.9" />
<PackageReference Include="dapper.contrib" Version="1.60.1" />
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
<PackageReference Include="mysqlconnector" Version="0.52.0" />
</ItemGroup>
</Project>