feat: implement database integration for style and title repositories
This commit is contained in:
@@ -1,4 +1,6 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Webzine.EntitiesContext;
|
||||||
using Webzine.Entity;
|
using Webzine.Entity;
|
||||||
using Webzine.Repository.Contracts;
|
using Webzine.Repository.Contracts;
|
||||||
|
|
||||||
@@ -10,14 +12,17 @@ namespace Webzine.Repository;
|
|||||||
public class DbStyleRepository : IStyleRepository
|
public class DbStyleRepository : IStyleRepository
|
||||||
{
|
{
|
||||||
private readonly ILogger<DbStyleRepository> logger;
|
private readonly ILogger<DbStyleRepository> logger;
|
||||||
|
private readonly WebzineDbContext context;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="DbStyleRepository"/> class.
|
/// Initializes a new instance of the <see cref="DbStyleRepository"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="logger">Le service de journalisation injecté pour suivre les opérations du repository.</param>
|
/// <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.logger = logger;
|
||||||
|
this.context = context;
|
||||||
this.logger.LogDebug(1, "NLog injected into DbStyleRepository");
|
this.logger.LogDebug(1, "NLog injected into DbStyleRepository");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,7 +32,12 @@ public class DbStyleRepository : IStyleRepository
|
|||||||
/// <param name="style">L'objet style à ajouter.</param>
|
/// <param name="style">L'objet style à ajouter.</param>
|
||||||
public void Add(Style style)
|
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>
|
/// <summary>
|
||||||
@@ -36,7 +46,20 @@ public class DbStyleRepository : IStyleRepository
|
|||||||
/// <param name="style">L'objet style à supprimer.</param>
|
/// <param name="style">L'objet style à supprimer.</param>
|
||||||
public void Delete(Style style)
|
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>
|
/// <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>
|
/// <returns>Le style correspondant à l'identifiant fourni, ou null si aucun style n'est trouvé.</returns>
|
||||||
public Style Find(int id)
|
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>
|
/// <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>
|
/// <returns>Une collection de tous les styles présents dans la base de données.</returns>
|
||||||
public IEnumerable<Style> FindAll()
|
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>
|
/// <summary>
|
||||||
@@ -64,6 +116,19 @@ public class DbStyleRepository : IStyleRepository
|
|||||||
/// <param name="style">L'objet style avec les informations mises à jour.</param>
|
/// <param name="style">L'objet style avec les informations mises à jour.</param>
|
||||||
public void Update(Style style)
|
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}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Webzine.EntitiesContext;
|
using Webzine.EntitiesContext;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Webzine.Entity;
|
using Webzine.Entity;
|
||||||
|
|||||||
@@ -24,6 +24,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\Webzine.EntitiesContext\Webzine.EntitiesContext.csproj" />
|
||||||
<ProjectReference Include="..\Webzine.Entity\Webzine.Entity.csproj" />
|
<ProjectReference Include="..\Webzine.Entity\Webzine.Entity.csproj" />
|
||||||
<ProjectReference Include="..\Webzine.Repository.Contracts\Webzine.Repository.Contracts.csproj" />
|
<ProjectReference Include="..\Webzine.Repository.Contracts\Webzine.Repository.Contracts.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
Reference in New Issue
Block a user