diff --git a/Webzine.Repository/DbArtisteRepository.cs b/Webzine.Repository/DbArtisteRepository.cs index 5aa9bb7..4fa8e18 100644 --- a/Webzine.Repository/DbArtisteRepository.cs +++ b/Webzine.Repository/DbArtisteRepository.cs @@ -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 _logger; - /// /// Initializes a new instance of the class. /// @@ -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 @@ /// 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 @@ /// 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; } /// 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; + } } /// @@ -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); diff --git a/Webzine.Repository/LocalArtisteRepository.cs b/Webzine.Repository/LocalArtisteRepository.cs index 0841d3c..36b30c9 100644 --- a/Webzine.Repository/LocalArtisteRepository.cs +++ b/Webzine.Repository/LocalArtisteRepository.cs @@ -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 @@ /// 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; } /// 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; + } + } /// @@ -99,12 +102,6 @@ /// 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); diff --git a/Webzine.WebApplication/Controllers/ArtisteController.cs b/Webzine.WebApplication/Controllers/ArtisteController.cs index 68b2443..b63f140 100644 --- a/Webzine.WebApplication/Controllers/ArtisteController.cs +++ b/Webzine.WebApplication/Controllers/ArtisteController.cs @@ -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)) { diff --git a/Webzine.WebApplication/ViewComponents/SidebarViewComponent.cs b/Webzine.WebApplication/ViewComponents/SidebarViewComponent.cs new file mode 100644 index 0000000..27f2c7a --- /dev/null +++ b/Webzine.WebApplication/ViewComponents/SidebarViewComponent.cs @@ -0,0 +1,35 @@ +using Microsoft.AspNetCore.Mvc; +using Webzine.Repository.Contracts; + +namespace Webzine.WebApplication.ViewComponents +{ + /// + /// View component pour la sidebar, récupère les styles depuis le repository. + /// + public class SidebarViewComponent : ViewComponent + { + private readonly IStyleRepository styleRepository; + + /// + /// Initializes a new instance of the class. + /// + /// Repository des styles injecté. + public SidebarViewComponent(IStyleRepository styleRepository) + { + this.styleRepository = styleRepository; + } + + /// + /// Récupère tous les styles triés par libellé et les passe à la vue. + /// + /// Une vue contenant la liste des styles. + public IViewComponentResult Invoke() + { + var styles = this.styleRepository.FindAll() + .OrderBy(s => s.Libelle) + .ToList(); + + return this.View(styles); + } + } +} \ No newline at end of file diff --git a/Webzine.WebApplication/Views/Shared/Components/Sidebar/Default.cshtml b/Webzine.WebApplication/Views/Shared/Components/Sidebar/Default.cshtml new file mode 100644 index 0000000..d7ce8b9 --- /dev/null +++ b/Webzine.WebApplication/Views/Shared/Components/Sidebar/Default.cshtml @@ -0,0 +1,23 @@ +@model IEnumerable + + \ No newline at end of file diff --git a/Webzine.WebApplication/Views/Shared/_Layout.cshtml b/Webzine.WebApplication/Views/Shared/_Layout.cshtml index 234b357..5d11de0 100644 --- a/Webzine.WebApplication/Views/Shared/_Layout.cshtml +++ b/Webzine.WebApplication/Views/Shared/_Layout.cshtml @@ -11,19 +11,19 @@ - -
-
-
- @RenderBody() -
- @if(ViewContext.RouteData.Values["area"]?.ToString() != "Administration") - { - - } -
+ +
+
+
+ @RenderBody() +
+ @if(ViewContext.RouteData.Values["area"]?.ToString() != "Administration") + { + @await Component.InvokeAsync("Sidebar") + }
- +
+ diff --git a/Webzine.WebApplication/Views/Shared/_Sidebar.cshtml b/Webzine.WebApplication/Views/Shared/_Sidebar.cshtml deleted file mode 100644 index ab99e81..0000000 --- a/Webzine.WebApplication/Views/Shared/_Sidebar.cshtml +++ /dev/null @@ -1,17 +0,0 @@ -@* - For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 -*@ -@{ -} - \ No newline at end of file