diff --git a/Webzine.Repository/DbStyleRepository.cs b/Webzine.Repository/DbStyleRepository.cs index 0f0255b..bf92035 100644 --- a/Webzine.Repository/DbStyleRepository.cs +++ b/Webzine.Repository/DbStyleRepository.cs @@ -1,4 +1,6 @@ +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; +using Webzine.EntitiesContext; using Webzine.Entity; using Webzine.Repository.Contracts; @@ -10,14 +12,17 @@ namespace Webzine.Repository; public class DbStyleRepository : IStyleRepository { private readonly ILogger logger; + private readonly WebzineDbContext context; /// /// Initializes a new instance of the class. /// /// Le service de journalisation injecté pour suivre les opérations du repository. - public DbStyleRepository(ILogger logger) + /// Le contexte de base de données injecté. + public DbStyleRepository(ILogger logger, WebzineDbContext context) { this.logger = logger; + this.context = context; this.logger.LogDebug(1, "NLog injected into DbStyleRepository"); } @@ -27,7 +32,12 @@ public class DbStyleRepository : IStyleRepository /// L'objet style à ajouter. public void Add(Style style) { - throw new NotImplementedException(); + this.logger.LogInformation($"Adding new style: {style.Libelle}"); + + this.context.Styles.Add(style); + this.context.SaveChanges(); + + this.logger.LogDebug($"Style added with Id: {style.IdStyle}"); } /// @@ -36,7 +46,20 @@ public class DbStyleRepository : IStyleRepository /// L'objet style à supprimer. public void Delete(Style style) { - throw new NotImplementedException(); + this.logger.LogInformation($"Deleting style with Id: {style.IdStyle}"); + + // Check if style exists + var existingStyle = this.context.Styles.Find(style.IdStyle); + if (existingStyle == null) + { + this.logger.LogWarning($"Style with Id {style.IdStyle} not found for deletion"); + throw new InvalidOperationException($"Style with Id {style.IdStyle} not found."); + } + + this.context.Styles.Remove(existingStyle); + this.context.SaveChanges(); + + this.logger.LogDebug($"Style deleted: {style.IdStyle}"); } /// @@ -46,7 +69,29 @@ public class DbStyleRepository : IStyleRepository /// Le style correspondant à l'identifiant fourni, ou null si aucun style n'est trouvé. public Style Find(int id) { - throw new NotImplementedException(); + if (id <= 0) + { + this.logger.LogWarning($"Tentative de recherche d'un style avec un Id invalide: {id}"); + return new Style(); + } + + this.logger.LogDebug($"Finding style with Id: {id}"); + + var style = this.context.Styles + .Include(s => s.Titres) + .FirstOrDefault(s => s.IdStyle == id); + + if (style == null) + { + this.logger.LogWarning($"Style with Id {id} not found"); + style = new Style(); + } + else + { + this.logger.LogDebug($"Found style: {style.Libelle}"); + } + + return style; } /// @@ -55,7 +100,14 @@ public class DbStyleRepository : IStyleRepository /// Une collection de tous les styles présents dans la base de données. public IEnumerable