ajout classe d'extension

This commit is contained in:
Julien LEICHER 2021-12-14 13:52:50 +01:00
parent f9eeb453ac
commit 5ca2b509fe
No known key found for this signature in database
GPG Key ID: BE0761B6A007EB96
8 changed files with 71 additions and 43 deletions

View File

@ -18,6 +18,12 @@ public class HomeController : Controller
return View(); return View();
} }
[HttpGet("/404")]
public IActionResult Status404()
{
return View("404");
}
public IActionResult Privacy() public IActionResult Privacy()
{ {
return View(); return View();

View File

@ -1,5 +1,5 @@
using HackerNet.Application; using HackerNet.Application;
using HackerNet.Web.Filters; using HackerNet.Infrastructure.AspNet.Filters;
using HackerNet.Web.Models; using HackerNet.Web.Models;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
@ -23,7 +23,7 @@ public class LinksController : HackerController
} }
[HttpGet] [HttpGet]
[TypeFilter(typeof(CustomExceptionFilter))] [CustomExceptionFilter]
public IActionResult Detail(Guid id) public IActionResult Detail(Guid id)
{ {
var link = _linkService.GetLinkDetail(id); var link = _linkService.GetLinkDetail(id);

View File

@ -1,24 +1,14 @@
using HackerNet.Application; using HackerNet.Infrastructure.AspNet;
using HackerNet.Domain;
using HackerNet.Infrastructure.Repositories.Memory;
using HackerNet.Web.Filters;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
// Add services to the container. // Add services to the container.
var link = new Link("https://localhost:7050/", "Youhouuu"); //ServiceCollectionExtensions.AddHackerNetServices(builder.Services);
var comment = new Comment(link.Id, "Wow!"); builder.Services
var linksRepository = new MemoryLinkRepository(link); .AddHackerNetServices()
var commentsRepository = new MemoryCommentRepository(comment); .AddControllersWithViews(o =>
builder.Services.AddSingleton<ILinkRepository>(linksRepository);
builder.Services.AddSingleton<ICommentRepository>(commentsRepository);
builder.Services.AddSingleton<IReadStore>(new MemoryReadStore(linksRepository, commentsRepository));
builder.Services.AddSingleton<LinkService>();
builder.Services.AddControllersWithViews(o =>
{ {
o.Filters.Add<CustomExceptionFilter>(); //o.Filters.Add<CustomExceptionFilter>();
}); });
var app = builder.Build(); var app = builder.Build();
@ -60,7 +50,7 @@ app.Use(async (ctx, next) =>
app.UseHttpsRedirection(); app.UseHttpsRedirection();
app.UseStaticFiles(); app.UseStaticFiles();
// app.UseStatusCodePages() app.UseStatusCodePagesWithRedirects("/{0}");
app.UseRouting(); app.UseRouting();

View File

@ -0,0 +1,5 @@
@{
ViewData["Title"] = "Page non trouvée";
}
<p>Impossible de trouver la page que vous recherchez.</p>

View File

@ -81,6 +81,7 @@ public class PublishCommentCommand
{ {
public Guid LinkId { get; set; } public Guid LinkId { get; set; }
[Required] [Required(ErrorMessage = "Le contenu est obligatoire")]
[Display(Name = "Votre commentaire")]
public string Content { get; set; } public string Content { get; set; }
} }

View File

@ -1,11 +1,11 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.AspNetCore.Mvc.Filters;
namespace HackerNet.Web.Filters; namespace HackerNet.Infrastructure.AspNet.Filters;
public class CustomExceptionFilter : IExceptionFilter public class CustomExceptionFilter : ExceptionFilterAttribute
{ {
public void OnException(ExceptionContext context) public override void OnException(ExceptionContext context)
{ {
if (context.Exception is InvalidOperationException) if (context.Exception is InvalidOperationException)
{ {

View File

@ -0,0 +1,25 @@
using HackerNet.Application;
using HackerNet.Domain;
using HackerNet.Infrastructure.Repositories.Memory;
using Microsoft.Extensions.DependencyInjection;
namespace HackerNet.Infrastructure.AspNet;
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddHackerNetServices(this IServiceCollection services)
{
var link = new Link("https://localhost:7050/", "Youhouuu");
var comment = new Comment(link.Id, "Wow!");
var linksRepository = new MemoryLinkRepository(link);
var commentsRepository = new MemoryCommentRepository(comment);
services.AddSingleton<ILinkRepository>(linksRepository);
services.AddSingleton<ICommentRepository>(commentsRepository);
services.AddSingleton<IReadStore>(new MemoryReadStore(linksRepository, commentsRepository));
services.AddSingleton<LinkService>();
return services;
}
}

View File

@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<ItemGroup> <ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<ProjectReference Include="..\HackerNet.Application\HackerNet.Application.csproj" /> <ProjectReference Include="..\HackerNet.Application\HackerNet.Application.csproj" />
</ItemGroup> </ItemGroup>