Merge remote-tracking branch 'origin/j2/refactor/controler-style-titre' into j2/ajout_repo

This commit is contained in:
Loic Masi
2026-03-26 13:58:45 +01:00
7 changed files with 126 additions and 72 deletions

View File

@@ -1,5 +1,6 @@
namespace Webzine.Repository
{
using System.Data.Common;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Webzine.EntitiesContext;
@@ -15,7 +16,6 @@
private WebzineDbContext _context;
private readonly ILogger<LocalArtisteRepository> _logger;
/// <summary>
/// Initializes a new instance of the <see cref="DbArtisteRepository"/> class.
/// </summary>
@@ -32,14 +32,14 @@
{
try
{
if (artiste == null)
{
throw new ArgumentNullException(nameof(artiste), "L'artiste à ajouter ne peut pas être null.");
}
this._context.Artistes.Add(artiste);
this._context.SaveChanges();
}
catch (DbUpdateException dbex)
{
this._logger.LogError(dbex, "Erreur de base de données lors de l'ajout de l'artiste: {id}", artiste.IdArtiste);
throw;
}
catch (Exception ex)
{
this._logger.LogError(ex, "Une erreur est survenue lors de l'ajout de l'artiste {Nom}.", artiste?.Nom);
@@ -50,7 +50,7 @@
/// <inheritdoc/>
public void Delete(Artiste artiste)
{
try
try
{
if (artiste == null)
{
@@ -60,6 +60,11 @@
this._context.Artistes.Remove(artiste);
this._context.SaveChanges();
}
catch (DbUpdateException dbex)
{
this._logger.LogError(dbex, "Erreur de base de données lors de la suppression de l'artiste: {Id}", artiste.IdArtiste);
throw;
}
catch (Exception ex)
{
this._logger.LogError(ex, "Une erreur est survenue lors de la suppression de l'artiste {Nom}.", artiste?.Nom);
@@ -70,35 +75,41 @@
/// <inheritdoc/>
public Artiste Find(int id)
{
Artiste artiste = this._context.Artistes
.Include(a => a.Titres)
.FirstOrDefault(a => a.IdArtiste == id);
if (artiste == null)
try
{
this._logger.LogWarning("Aucun artiste trouvé avec l'identifiant {Id}", id);
Artiste artiste = this._context.Artistes
.Include(a => a.Titres)
.First(a => a.IdArtiste == id);
return artiste;
}
catch (Exception ex)
{
this._logger.LogError(ex, "Erreur lors de la recherche de l'artiste: {Id}", id);
throw;
}
return artiste;
}
/// <inheritdoc/>
public Artiste FindByName(string nom)
{
if (string.IsNullOrWhiteSpace(nom))
try
{
this._logger.LogWarning("Tentative de recherche avec un nom vide ou null.");
return null;
}
var artiste = this._context.Artistes
.Include(a => a.Titres)
.FirstOrDefault(a => a.Nom == nom);
if (artiste == null)
{
this._logger.LogWarning("Recherche Nom : Aucun artiste trouvé pour '{Nom}'.", nom);
}
{
this._logger.LogWarning("Pas d'artiste au nom {Nom}", nom);
artiste = new Artiste();
}
return artiste;
}
catch (Exception ex)
{
this._logger.LogError(ex, "Erreur lors de la recherche de l'artiste avec le nom: {Nom}", nom);
throw;
}
}
/// <inheritdoc/>
@@ -132,6 +143,11 @@
this._context.SaveChanges();
this._logger.LogInformation("Artiste {Id} ({Nom}) mis à jour avec succès.", artiste.IdArtiste, artiste.Nom);
}
catch (DbUpdateException ex)
{
this._logger.LogError(ex, "Erreur de base de données lors de la mise à jour de l'artiste ID: {IdArtiste}", artiste.IdArtiste);
throw;
}
catch (Exception ex)
{
this._logger.LogError(ex, "Erreur lors de la mise à jour de l'artiste {Id}.", artiste.IdArtiste);

View File

@@ -30,12 +30,6 @@
{
try
{
if (artiste == null)
{
this._logger.LogError("L'artiste à ajouter ne peut pas être null.");
throw new ArgumentNullException(nameof(artiste));
}
if (this._artistes.Any(a => a.IdArtiste == artiste.IdArtiste))
{
this._logger.LogWarning("Un artiste avec l'ID {Id} existe déjà. L'ajout est ignoré.", artiste.IdArtiste);
@@ -70,23 +64,32 @@
/// <inheritdoc/>
public Artiste Find(int id)
{
Artiste artiste = this._artistes.FirstOrDefault(a => a.IdArtiste == id);
if (artiste == null)
try
{
this._logger.LogWarning("Aucun artiste trouvé avec l'identifiant {Id}", id);
Artiste artiste = this._artistes.First(a => a.IdArtiste == id);
return artiste;
}
catch (Exception ex)
{
this._logger.LogError(ex, "Erreur lors de la recherche de l'artiste avec ID: {Id}", id);
throw;
}
return artiste;
}
/// <inheritdoc/>
public Artiste FindByName(string nom)
{
Artiste artiste = this._artistes.FirstOrDefault(a => a.Nom == nom);
if (artiste == null)
try
{
this._logger.LogWarning("Aucun artiste trouvé avec le nom {Nom}", nom);
Artiste artiste = this._artistes.First(a => a.Nom == nom);
return artiste;
}
return artiste;
catch (Exception ex)
{
this._logger.LogError(ex, "Erreur lors de la recherche de l'artiste avec le nom: {Nom}", nom);
throw;
}
}
/// <inheritdoc/>
@@ -99,12 +102,6 @@
/// <inheritdoc/>
public void Update(Artiste artiste)
{
if (artiste == null)
{
this._logger.LogError("L'artiste à mettre à jour ne peut pas être null.");
throw new ArgumentNullException(nameof(artiste));
}
try
{
var artisteToUpdate = this._artistes.FirstOrDefault(a => a.IdArtiste == artiste.IdArtiste);

View File

@@ -31,7 +31,7 @@
[HttpGet("/artiste/{nom}")]
public IActionResult Index(string nom)
{
this.logger.LogInformation("Tentative d'accès à l'artiste avec le nom : {NomArtiste}", nom);
this._logger.LogInformation("Tentative d'accès à l'artiste avec le nom : {NomArtiste}", nom);
if (string.IsNullOrEmpty(nom))
{

View File

@@ -0,0 +1,35 @@
using Microsoft.AspNetCore.Mvc;
using Webzine.Repository.Contracts;
namespace Webzine.WebApplication.ViewComponents
{
/// <summary>
/// View component pour la sidebar, récupère les styles depuis le repository.
/// </summary>
public class SidebarViewComponent : ViewComponent
{
private readonly IStyleRepository styleRepository;
/// <summary>
/// Initializes a new instance of the <see cref="SidebarViewComponent"/> class.
/// </summary>
/// <param name="styleRepository">Repository des styles injecté.</param>
public SidebarViewComponent(IStyleRepository styleRepository)
{
this.styleRepository = styleRepository;
}
/// <summary>
/// Récupère tous les styles triés par libellé et les passe à la vue.
/// </summary>
/// <returns>Une vue contenant la liste des styles.</returns>
public IViewComponentResult Invoke()
{
var styles = this.styleRepository.FindAll()
.OrderBy(s => s.Libelle)
.ToList();
return this.View(styles);
}
}
}

View File

@@ -0,0 +1,23 @@
@model IEnumerable<Webzine.Entity.Style>
<aside class="col-lg-3 d-none d-lg-block">
<div>
<h2>À propos</h2>
<p>Retrouvez les dernières pépites sur notre webzine.</p>
</div>
<div>
<h2>Styles</h2>
<ul>
@foreach (var style in Model)
{
<li>
<a asp-controller="Titre"
asp-action="Style"
asp-route-style="@style.Libelle">
@style.Libelle
</a>
</li>
}
</ul>
</div>
</aside>

View File

@@ -11,19 +11,19 @@
<link rel="stylesheet" href="~/css/app.css">
</head>
<body>
<partial name="_Header"/>
<div class="container-fluid flex-grow-1 py-4">
<div class="row">
<main class="col mx-3">
@RenderBody()
</main>
@if(ViewContext.RouteData.Values["area"]?.ToString() != "Administration")
{
<partial name="_Sidebar" />
}
</div>
<partial name="_Header"/>
<div class="container-fluid flex-grow-1 py-4">
<div class="row">
<main class="col mx-3">
@RenderBody()
</main>
@if(ViewContext.RouteData.Values["area"]?.ToString() != "Administration")
{
@await Component.InvokeAsync("Sidebar")
}
</div>
<partial name="_Footer" />
</div>
<partial name="_Footer" />
</body>
</html>

View File

@@ -1,17 +0,0 @@
@*
For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
}
<aside class="col-lg-3 d-none d-lg-block">
<div>
<h2>À propos</h2>
<p>Retrouvez les dernières pépites sur notre webzine.</p>
</div>
<div>
<h2>Styles</h2>
<ul>
<li><a asp-controller="Titre" asp-action="Style" asp-route-id="Acid house">Acid house</a></li>
</ul>
</div>
</aside>