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

@ -6,26 +6,32 @@ namespace HackerNet.Web.Controllers;
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public HomeController(ILogger<HomeController> 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 });
}
}

View File

@ -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);

View File

@ -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<ILinkRepository>(linksRepository);
builder.Services.AddSingleton<ICommentRepository>(commentsRepository);
builder.Services.AddSingleton<IReadStore>(new MemoryReadStore(linksRepository, commentsRepository));
builder.Services.AddSingleton<LinkService>();
builder.Services.AddControllersWithViews(o =>
//ServiceCollectionExtensions.AddHackerNetServices(builder.Services);
builder.Services
.AddHackerNetServices()
.AddControllersWithViews(o =>
{
o.Filters.Add<CustomExceptionFilter>();
//o.Filters.Add<CustomExceptionFilter>();
});
var app = builder.Build();
@ -60,7 +50,7 @@ app.Use(async (ctx, next) =>
app.UseHttpsRedirection();
app.UseStaticFiles();
// app.UseStatusCodePages()
app.UseStatusCodePagesWithRedirects("/{0}");
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; }
[Required]
[Required(ErrorMessage = "Le contenu est obligatoire")]
[Display(Name = "Votre commentaire")]
public string Content { get; set; }
}

View File

@ -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)
{

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,7 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\HackerNet.Application\HackerNet.Application.csproj" />
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<ProjectReference Include="..\HackerNet.Application\HackerNet.Application.csproj" />
</ItemGroup>
<PropertyGroup>