add list links query and display it in the links homepage

This commit is contained in:
YuukanOO 2020-12-04 16:55:50 +01:00
parent d5a239c1de
commit e0b125e10b
13 changed files with 191 additions and 6 deletions

36
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,36 @@
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (web)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/Apps/Website/bin/Debug/net5.0/Website.dll",
"args": [],
"cwd": "${workspaceFolder}/Apps/Website",
"stopAtEntry": false,
// Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser
"serverReadyAction": {
"action": "openExternally",
"pattern": "\\bNow listening on:\\s+(https?://\\S+)"
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"sourceFileMap": {
"/Views": "${workspaceFolder}/Views"
}
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
]
}

42
.vscode/tasks.json vendored Normal file
View File

@ -0,0 +1,42 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/Apps/Website/Website.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/Apps/Website/Website.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"${workspaceFolder}/Apps/Website/Website.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
}
]
}

View File

@ -6,6 +6,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="MediatR" Version="9.0.0" /> <PackageReference Include="MediatR" Version="9.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

13
Application/IDbContext.cs Normal file
View File

@ -0,0 +1,13 @@
using HN.Domain;
using Microsoft.EntityFrameworkCore;
namespace HN.Application
{
/// <summary>
/// Interface permettant l'accès aux DbSet pour toute la partie Query.
/// </summary>
public interface IDbContext
{
DbSet<Link> Links { get; }
}
}

View File

@ -0,0 +1,11 @@
using System;
namespace HN.Application
{
public sealed class LinkDTO
{
public Guid Id { get; set; }
public string Url { get; set; }
public DateTime CreatedAt { get; set; }
}
}

View File

@ -0,0 +1,9 @@
using MediatR;
namespace HN.Application
{
public sealed class ListLinksQuery : IRequest<LinkDTO[]>
{
}
}

View File

@ -0,0 +1,30 @@
using System.Threading;
using System.Linq;
using System.Threading.Tasks;
using MediatR;
namespace HN.Application
{
public sealed class ListLinksQueryHandler : IRequestHandler<ListLinksQuery, LinkDTO[]>
{
private readonly IDbContext _context;
public ListLinksQueryHandler(IDbContext context)
{
_context = context;
}
public Task<LinkDTO[]> Handle(ListLinksQuery request, CancellationToken cancellationToken)
{
var links = from link in _context.Links
select new LinkDTO
{
Id = link.Id,
Url = link.Url,
CreatedAt = link.CreatedAt
};
return Task.FromResult(links.ToArray());
}
}
}

View File

@ -14,9 +14,9 @@ namespace Website.Controllers
_bus = bus; _bus = bus;
} }
public IActionResult Index() public async Task<IActionResult> Index()
{ {
return View(); return View(await _bus.Send(new ListLinksQuery()));
} }
public IActionResult Create() public IActionResult Create()

View File

@ -1,3 +1,4 @@
using HN.Application;
using HN.Domain; using HN.Domain;
using HN.Infrastructure; using HN.Infrastructure;
using MediatR; using MediatR;
@ -23,6 +24,7 @@ namespace Website
public void ConfigureServices(IServiceCollection services) public void ConfigureServices(IServiceCollection services)
{ {
services.AddDbContext<HNDbContext>(options => options.UseSqlite(Configuration.GetConnectionString("Default"))); services.AddDbContext<HNDbContext>(options => options.UseSqlite(Configuration.GetConnectionString("Default")));
services.AddScoped<IDbContext, HNDbContext>();
services.AddScoped<ILinkRepository, LinkRepository>(); services.AddScoped<ILinkRepository, LinkRepository>();
services.AddMediatR(typeof(HN.Application.AddLinkCommand)); services.AddMediatR(typeof(HN.Application.AddLinkCommand));
services.AddControllersWithViews(); services.AddControllersWithViews();

View File

@ -1,7 +1,14 @@
@model HN.Application.LinkDTO[]
@{ @{
ViewData["Title"] = "Latest Links"; ViewData["Title"] = "Latest Links";
} }
<a asp-action="Create" title="New post">Post a new super duber link</a> <a asp-action="Create" title="New post">Post a new super duber link</a>
<p>Here are the links</p> <p>Here are the links</p>
<ul>
@foreach (var link in Model)
{
<li>@link.Url created at @link.CreatedAt.ToLocalTime()</li>
}
</ul>

View File

@ -1,9 +1,10 @@
using HN.Domain; using HN.Application;
using HN.Domain;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
namespace HN.Infrastructure namespace HN.Infrastructure
{ {
public sealed class HNDbContext : DbContext public sealed class HNDbContext : DbContext, IDbContext
{ {
public DbSet<Link> Links { get; set; } public DbSet<Link> Links { get; set; }

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Domain\Domain.csproj" /> <ProjectReference Include="..\Application\Application.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -7,6 +7,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Domain", "Domain\Domain.csp
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Application", "Application\Application.csproj", "{2993DFBE-7A6D-408B-AC94-C8C7B020C685}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Application", "Application\Application.csproj", "{2993DFBE-7A6D-408B-AC94-C8C7B020C685}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Infrastructure", "Infrastructure\Infrastructure.csproj", "{4EBF14D3-A0FF-4F72-92F9-FBEE5474E662}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Apps", "Apps", "{7D23D5C3-15B1-407D-9FE1-E30C3FBBA1A4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Website", "Apps\Website\Website.csproj", "{44842863-BEB0-4718-BBD0-F7640D7AE0D0}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -44,5 +50,32 @@ Global
{2993DFBE-7A6D-408B-AC94-C8C7B020C685}.Release|x64.Build.0 = Release|Any CPU {2993DFBE-7A6D-408B-AC94-C8C7B020C685}.Release|x64.Build.0 = Release|Any CPU
{2993DFBE-7A6D-408B-AC94-C8C7B020C685}.Release|x86.ActiveCfg = Release|Any CPU {2993DFBE-7A6D-408B-AC94-C8C7B020C685}.Release|x86.ActiveCfg = Release|Any CPU
{2993DFBE-7A6D-408B-AC94-C8C7B020C685}.Release|x86.Build.0 = Release|Any CPU {2993DFBE-7A6D-408B-AC94-C8C7B020C685}.Release|x86.Build.0 = Release|Any CPU
{4EBF14D3-A0FF-4F72-92F9-FBEE5474E662}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4EBF14D3-A0FF-4F72-92F9-FBEE5474E662}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4EBF14D3-A0FF-4F72-92F9-FBEE5474E662}.Debug|x64.ActiveCfg = Debug|Any CPU
{4EBF14D3-A0FF-4F72-92F9-FBEE5474E662}.Debug|x64.Build.0 = Debug|Any CPU
{4EBF14D3-A0FF-4F72-92F9-FBEE5474E662}.Debug|x86.ActiveCfg = Debug|Any CPU
{4EBF14D3-A0FF-4F72-92F9-FBEE5474E662}.Debug|x86.Build.0 = Debug|Any CPU
{4EBF14D3-A0FF-4F72-92F9-FBEE5474E662}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4EBF14D3-A0FF-4F72-92F9-FBEE5474E662}.Release|Any CPU.Build.0 = Release|Any CPU
{4EBF14D3-A0FF-4F72-92F9-FBEE5474E662}.Release|x64.ActiveCfg = Release|Any CPU
{4EBF14D3-A0FF-4F72-92F9-FBEE5474E662}.Release|x64.Build.0 = Release|Any CPU
{4EBF14D3-A0FF-4F72-92F9-FBEE5474E662}.Release|x86.ActiveCfg = Release|Any CPU
{4EBF14D3-A0FF-4F72-92F9-FBEE5474E662}.Release|x86.Build.0 = Release|Any CPU
{44842863-BEB0-4718-BBD0-F7640D7AE0D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{44842863-BEB0-4718-BBD0-F7640D7AE0D0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{44842863-BEB0-4718-BBD0-F7640D7AE0D0}.Debug|x64.ActiveCfg = Debug|Any CPU
{44842863-BEB0-4718-BBD0-F7640D7AE0D0}.Debug|x64.Build.0 = Debug|Any CPU
{44842863-BEB0-4718-BBD0-F7640D7AE0D0}.Debug|x86.ActiveCfg = Debug|Any CPU
{44842863-BEB0-4718-BBD0-F7640D7AE0D0}.Debug|x86.Build.0 = Debug|Any CPU
{44842863-BEB0-4718-BBD0-F7640D7AE0D0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{44842863-BEB0-4718-BBD0-F7640D7AE0D0}.Release|Any CPU.Build.0 = Release|Any CPU
{44842863-BEB0-4718-BBD0-F7640D7AE0D0}.Release|x64.ActiveCfg = Release|Any CPU
{44842863-BEB0-4718-BBD0-F7640D7AE0D0}.Release|x64.Build.0 = Release|Any CPU
{44842863-BEB0-4718-BBD0-F7640D7AE0D0}.Release|x86.ActiveCfg = Release|Any CPU
{44842863-BEB0-4718-BBD0-F7640D7AE0D0}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{44842863-BEB0-4718-BBD0-F7640D7AE0D0} = {7D23D5C3-15B1-407D-9FE1-E30C3FBBA1A4}
EndGlobalSection EndGlobalSection
EndGlobal EndGlobal