From 5ca2b509fe53c6a5ee5dfca8129d04c4fd3452fc Mon Sep 17 00:00:00 2001 From: Julien LEICHER Date: Tue, 14 Dec 2021 13:52:50 +0100 Subject: [PATCH] ajout classe d'extension --- .../Controllers/HomeController.cs | 42 +++++++++++-------- .../Controllers/LinksController.cs | 4 +- Apps/HackerNet.Web/Program.cs | 24 ++++------- Apps/HackerNet.Web/Views/Home/404.cshtml | 5 +++ HackerNet.Application/LinkService.cs | 3 +- .../AspNet}/Filters/CustomExceptionFilter.cs | 6 +-- .../AspNet/ServiceCollectionExtensions.cs | 25 +++++++++++ .../HackerNet.Infrastructure.csproj | 5 ++- 8 files changed, 71 insertions(+), 43 deletions(-) create mode 100644 Apps/HackerNet.Web/Views/Home/404.cshtml rename {Apps/HackerNet.Web => HackerNet.Infrastructure/AspNet}/Filters/CustomExceptionFilter.cs (52%) create mode 100644 HackerNet.Infrastructure/AspNet/ServiceCollectionExtensions.cs diff --git a/Apps/HackerNet.Web/Controllers/HomeController.cs b/Apps/HackerNet.Web/Controllers/HomeController.cs index 291097a..090608a 100644 --- a/Apps/HackerNet.Web/Controllers/HomeController.cs +++ b/Apps/HackerNet.Web/Controllers/HomeController.cs @@ -6,26 +6,32 @@ namespace HackerNet.Web.Controllers; public class HomeController : Controller { - private readonly ILogger _logger; + private readonly ILogger _logger; - public HomeController(ILogger logger) - { - _logger = logger; - } + public HomeController(ILogger logger) + { + _logger = logger; + } - public IActionResult Index() - { - return View(); - } + public IActionResult Index() + { + return View(); + } - public IActionResult Privacy() - { - return View(); - } + [HttpGet("/404")] + public IActionResult Status404() + { + return View("404"); + } - [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] - public IActionResult Error() - { - return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); - } + public IActionResult Privacy() + { + return View(); + } + + [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] + public IActionResult Error() + { + return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); + } } diff --git a/Apps/HackerNet.Web/Controllers/LinksController.cs b/Apps/HackerNet.Web/Controllers/LinksController.cs index ace95bb..aac6ecd 100644 --- a/Apps/HackerNet.Web/Controllers/LinksController.cs +++ b/Apps/HackerNet.Web/Controllers/LinksController.cs @@ -1,5 +1,5 @@ using HackerNet.Application; -using HackerNet.Web.Filters; +using HackerNet.Infrastructure.AspNet.Filters; using HackerNet.Web.Models; using Microsoft.AspNetCore.Mvc; @@ -23,7 +23,7 @@ public class LinksController : HackerController } [HttpGet] - [TypeFilter(typeof(CustomExceptionFilter))] + [CustomExceptionFilter] public IActionResult Detail(Guid id) { var link = _linkService.GetLinkDetail(id); diff --git a/Apps/HackerNet.Web/Program.cs b/Apps/HackerNet.Web/Program.cs index 9d60a84..e89e6a4 100644 --- a/Apps/HackerNet.Web/Program.cs +++ b/Apps/HackerNet.Web/Program.cs @@ -1,24 +1,14 @@ -using HackerNet.Application; -using HackerNet.Domain; -using HackerNet.Infrastructure.Repositories.Memory; -using HackerNet.Web.Filters; +using HackerNet.Infrastructure.AspNet; var builder = WebApplication.CreateBuilder(args); // Add services to the container. -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); - -builder.Services.AddSingleton(linksRepository); -builder.Services.AddSingleton(commentsRepository); -builder.Services.AddSingleton(new MemoryReadStore(linksRepository, commentsRepository)); - -builder.Services.AddSingleton(); -builder.Services.AddControllersWithViews(o => +//ServiceCollectionExtensions.AddHackerNetServices(builder.Services); +builder.Services + .AddHackerNetServices() + .AddControllersWithViews(o => { - o.Filters.Add(); + //o.Filters.Add(); }); var app = builder.Build(); @@ -60,7 +50,7 @@ app.Use(async (ctx, next) => app.UseHttpsRedirection(); app.UseStaticFiles(); -// app.UseStatusCodePages() +app.UseStatusCodePagesWithRedirects("/{0}"); app.UseRouting(); diff --git a/Apps/HackerNet.Web/Views/Home/404.cshtml b/Apps/HackerNet.Web/Views/Home/404.cshtml new file mode 100644 index 0000000..6933496 --- /dev/null +++ b/Apps/HackerNet.Web/Views/Home/404.cshtml @@ -0,0 +1,5 @@ +@{ + ViewData["Title"] = "Page non trouvée"; +} + +

Impossible de trouver la page que vous recherchez.

\ No newline at end of file diff --git a/HackerNet.Application/LinkService.cs b/HackerNet.Application/LinkService.cs index 97c7640..6a53db0 100644 --- a/HackerNet.Application/LinkService.cs +++ b/HackerNet.Application/LinkService.cs @@ -81,6 +81,7 @@ public class PublishCommentCommand { public Guid LinkId { get; set; } - [Required] + [Required(ErrorMessage = "Le contenu est obligatoire")] + [Display(Name = "Votre commentaire")] public string Content { get; set; } } \ No newline at end of file diff --git a/Apps/HackerNet.Web/Filters/CustomExceptionFilter.cs b/HackerNet.Infrastructure/AspNet/Filters/CustomExceptionFilter.cs similarity index 52% rename from Apps/HackerNet.Web/Filters/CustomExceptionFilter.cs rename to HackerNet.Infrastructure/AspNet/Filters/CustomExceptionFilter.cs index 7eb5331..6348951 100644 --- a/Apps/HackerNet.Web/Filters/CustomExceptionFilter.cs +++ b/HackerNet.Infrastructure/AspNet/Filters/CustomExceptionFilter.cs @@ -1,11 +1,11 @@ using Microsoft.AspNetCore.Mvc; 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) { diff --git a/HackerNet.Infrastructure/AspNet/ServiceCollectionExtensions.cs b/HackerNet.Infrastructure/AspNet/ServiceCollectionExtensions.cs new file mode 100644 index 0000000..a6bf8de --- /dev/null +++ b/HackerNet.Infrastructure/AspNet/ServiceCollectionExtensions.cs @@ -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(linksRepository); + services.AddSingleton(commentsRepository); + services.AddSingleton(new MemoryReadStore(linksRepository, commentsRepository)); + + services.AddSingleton(); + + return services; + } +} \ No newline at end of file diff --git a/HackerNet.Infrastructure/HackerNet.Infrastructure.csproj b/HackerNet.Infrastructure/HackerNet.Infrastructure.csproj index 01c7859..64e55bc 100644 --- a/HackerNet.Infrastructure/HackerNet.Infrastructure.csproj +++ b/HackerNet.Infrastructure/HackerNet.Infrastructure.csproj @@ -1,7 +1,8 @@ - - + + +