feat: implement database integration for style and title repositories

This commit is contained in:
mirage
2026-03-25 14:22:28 +01:00
parent fa1639e3b5
commit adeb2a0550
3 changed files with 73 additions and 6 deletions

View File

@@ -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<DbStyleRepository> logger;
private readonly WebzineDbContext context;
/// <summary>
/// Initializes a new instance of the <see cref="DbStyleRepository"/> class.
/// </summary>
/// <param name="logger">Le service de journalisation injecté pour suivre les opérations du repository.</param>
public DbStyleRepository(ILogger<DbStyleRepository> logger)
/// <param name="context">Le contexte de base de données injecté.</param>
public DbStyleRepository(ILogger<DbStyleRepository> 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
/// <param name="style">L'objet style à ajouter.</param>
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}");
}
/// <summary>
@@ -36,7 +46,20 @@ public class DbStyleRepository : IStyleRepository
/// <param name="style">L'objet style à supprimer.</param>
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}");
}
/// <summary>
@@ -46,7 +69,29 @@ public class DbStyleRepository : IStyleRepository
/// <returns>Le style correspondant à l'identifiant fourni, ou null si aucun style n'est trouvé.</returns>
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;
}
/// <summary>
@@ -55,7 +100,14 @@ public class DbStyleRepository : IStyleRepository
/// <returns>Une collection de tous les styles présents dans la base de données.</returns>
public IEnumerable<Style> FindAll()
{
throw new NotImplementedException();
this.logger.LogDebug("Finding all styles");
var styles = this.context.Styles
.OrderBy(s => s.Libelle)
.ToList();
this.logger.LogDebug($"Found {styles.Count} total styles");
return styles;
}
/// <summary>
@@ -64,6 +116,19 @@ public class DbStyleRepository : IStyleRepository
/// <param name="style">L'objet style avec les informations mises à jour.</param>
public void Update(Style style)
{
throw new NotImplementedException();
this.logger.LogInformation($"Updating style with Id: {style.IdStyle}");
var existingStyle = this.context.Styles.Find(style.IdStyle);
if (existingStyle == null)
{
this.logger.LogWarning($"Style with Id {style.IdStyle} not found for update");
throw new InvalidOperationException($"Style with Id {style.IdStyle} not found.");
}
// Update properties
existingStyle.Libelle = style.Libelle;
this.context.SaveChanges();
this.logger.LogDebug($"Style updated: {style.IdStyle}");
}
}