refactor: renommer et implémenter les repositories pour les styles et titres avec journalisation
This commit is contained in:
@@ -1,17 +1,19 @@
|
||||
// using Webzine.Entity;
|
||||
|
||||
using Webzine.Entity;
|
||||
|
||||
namespace Webzine.Repository.Contracts
|
||||
{
|
||||
public interface IStyleRepository
|
||||
{
|
||||
// void Add(Style style);
|
||||
void Add(Style style);
|
||||
|
||||
// void Delete(Style style);
|
||||
void Delete(Style style);
|
||||
|
||||
// Style Find(int id);
|
||||
Style Find(int id);
|
||||
|
||||
// IEnumerable<Style> FindAll();
|
||||
IEnumerable<Style> FindAll();
|
||||
|
||||
// void Update(Style style);
|
||||
void Update(Style style);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
namespace Webzine.Repository;
|
||||
|
||||
public class DbEntityRepository
|
||||
{
|
||||
|
||||
}
|
||||
69
Webzine.Repository/DbStyleRepository.cs
Normal file
69
Webzine.Repository/DbStyleRepository.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Webzine.Entity;
|
||||
using Webzine.Repository.Contracts;
|
||||
|
||||
namespace Webzine.Repository;
|
||||
|
||||
/// <summary>
|
||||
/// Classe qui implémente le repository pour les styles en utilisant une base de données.
|
||||
/// </summary>
|
||||
public class DbStyleRepository : IStyleRepository
|
||||
{
|
||||
private readonly ILogger<DbStyleRepository> logger;
|
||||
|
||||
/// <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)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.logger.LogDebug(1, "NLog injected into DbStyleRepository");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ajoute un style à la base de données.
|
||||
/// </summary>
|
||||
/// <param name="style">L'objet style à ajouter.</param>
|
||||
public void Add(Style style)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Supprime un style de la base de données.
|
||||
/// </summary>
|
||||
/// <param name="style">L'objet style à supprimer.</param>
|
||||
public void Delete(Style style)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Trouve un style dans la base de données en fonction de son identifiant.
|
||||
/// </summary>
|
||||
/// <param name="id">L'identifiant du style à trouver.</param>
|
||||
/// <returns>Le style correspondant à l'identifiant fourni, ou null si aucun style n'est trouvé.</returns>
|
||||
public Style Find(int id)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Trouve tous les styles dans la base de données.
|
||||
/// </summary>
|
||||
/// <returns>Une collection de tous les styles présents dans la base de données.</returns>
|
||||
public IEnumerable<Style> FindAll()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Met à jour les informations d'un style dans la base de données.
|
||||
/// </summary>
|
||||
/// <param name="style">L'objet style avec les informations mises à jour.</param>
|
||||
public void Update(Style style)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
62
Webzine.Repository/DbTitreRepository.cs
Normal file
62
Webzine.Repository/DbTitreRepository.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Webzine.Entity;
|
||||
using Webzine.Repository.Contracts;
|
||||
|
||||
namespace Webzine.Repository;
|
||||
|
||||
/// <summary>
|
||||
/// Classe qui implémente le repository pour les titres en utilisant une base de données.
|
||||
/// </summary>
|
||||
public class DbTitreRepository : ITitreRepository
|
||||
{
|
||||
private readonly ILogger<DbTitreRepository> logger;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="DbTitreRepository"/> class.
|
||||
/// </summary>
|
||||
/// <param name="logger">Le service de journalisation injecté pour suivre les opérations du repository.</param>
|
||||
public DbTitreRepository(ILogger<DbTitreRepository> logger)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.logger.LogDebug(1, "NLog injected into DbTitreRepository");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Recherche les titres dont le libellé contient le mot spécifié, en ignorant la casse.
|
||||
/// </summary>
|
||||
/// <param name="mot">Le mot à rechercher dans les libellés des titres.</param>
|
||||
/// <returns>Une collection de titres correspondant au critère de recherche, triée par libellé.</returns>
|
||||
public IEnumerable<Titre> Search(string mot)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Trouve un titre dans la base de données en fonction de son identifiant.
|
||||
/// </summary>
|
||||
/// <param name="idTitre">L'identifiant du titre à trouver.</param>
|
||||
/// <returns>Le titre correspondant à l'identifiant fourni, ou null si aucun titre n'est trouvé.</returns>
|
||||
public Titre? Find(int idTitre)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Trouve tous les titres dans la base de données.
|
||||
/// </summary>
|
||||
/// <returns>Une collection de tous les titres présents dans la base de données.</returns>
|
||||
public IEnumerable<Titre> FindAll()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Recherche les titres associés à un style dont le libellé correspond à celui spécifié, en ignorant la casse.
|
||||
/// </summary>
|
||||
/// <param name="libelle">Le libellé du style à rechercher.</param>
|
||||
/// <returns>Une collection de titres correspondant au critère de recherche, triée par libellé.</returns>
|
||||
public IEnumerable<Titre> SearchByStyle(string libelle)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
@@ -1,71 +0,0 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Webzine.Entity;
|
||||
using Webzine.Entity.Fixtures;
|
||||
using Webzine.Repository.Contracts;
|
||||
|
||||
namespace Webzine.Repository;
|
||||
|
||||
/// <summary>
|
||||
/// Classe qui permet d'initialiser un jeu de données
|
||||
/// pour tester l'application
|
||||
/// </summary>
|
||||
public class LocalEntityRepository : ITitreRepository
|
||||
{
|
||||
private readonly ILogger<LocalEntityRepository> _logger;
|
||||
private readonly List<Titre> _titres;
|
||||
|
||||
/// <summary>
|
||||
/// Initialise une nouvelle instance du <see cref="LocalEntityRepository"/> avec un service de journalisation injecte.
|
||||
/// </summary>
|
||||
/// <param name="logger">Service de journalisation injecte pour suivre les operations du repository.</param>
|
||||
public LocalEntityRepository(ILogger<LocalEntityRepository> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
_logger.LogDebug(1, "NLog injected into LocalEntityRepository");
|
||||
|
||||
var factory = new DataFactory();
|
||||
var artistes = factory.GenerateArtists(10);
|
||||
var styles = factory.GenerateStyles(10);
|
||||
|
||||
_titres = factory.GenerateTitres(30, artistes, styles);
|
||||
factory.GenerateCommentaires(50, _titres);
|
||||
}
|
||||
|
||||
public IEnumerable<Titre> Search(string mot)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(mot))
|
||||
{
|
||||
return Enumerable.Empty<Titre>();
|
||||
}
|
||||
|
||||
return _titres
|
||||
.Where(t => !string.IsNullOrWhiteSpace(t.Libelle)
|
||||
&& t.Libelle.Contains(mot, StringComparison.OrdinalIgnoreCase))
|
||||
.OrderBy(t => t.Libelle)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public Titre? Find(int idTitre)
|
||||
{
|
||||
return _titres.FirstOrDefault(t => t.IdTitre == idTitre);
|
||||
}
|
||||
|
||||
public IEnumerable<Titre> FindAll()
|
||||
{
|
||||
return _titres;
|
||||
}
|
||||
|
||||
public IEnumerable<Titre> SearchByStyle(string libelle)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(libelle))
|
||||
{
|
||||
return Enumerable.Empty<Titre>();
|
||||
}
|
||||
|
||||
return _titres
|
||||
.Where(t => t.Styles.Any(s => !string.IsNullOrWhiteSpace(s.Libelle)
|
||||
&& s.Libelle.Contains(libelle, StringComparison.OrdinalIgnoreCase)))
|
||||
.OrderBy(t => t.Libelle)
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
90
Webzine.Repository/LocalStyleRepository.cs
Normal file
90
Webzine.Repository/LocalStyleRepository.cs
Normal file
@@ -0,0 +1,90 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Webzine.Entity;
|
||||
using Webzine.Repository.Contracts;
|
||||
|
||||
namespace Webzine.Repository;
|
||||
|
||||
/// <summary>
|
||||
/// Classe qui implémente le repository pour les styles en utilisant une liste locale comme source de données.
|
||||
/// </summary>
|
||||
public class LocalStyleRepository : IStyleRepository
|
||||
{
|
||||
private readonly ILogger<LocalStyleRepository> logger;
|
||||
private readonly List<Style> styles;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="LocalStyleRepository"/> class.
|
||||
/// </summary>
|
||||
/// <param name="logger">Le service de journalisation injecté pour suivre les opérations du repository.</param>
|
||||
/// <param name="styles">La liste de styles à utiliser comme source de données pour le repository.</param>
|
||||
public LocalStyleRepository(ILogger<LocalStyleRepository> logger, List<Style> styles)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.styles = styles;
|
||||
this.logger.LogDebug(1, "NLog injected into LocalEntityRepository");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ajoute un style à la liste des styles.
|
||||
/// </summary>
|
||||
/// <param name="style">L'objet style à ajouter.</param>
|
||||
public void Add(Style style)
|
||||
{
|
||||
this.styles.Add(style);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Supprime un style de la liste des styles.
|
||||
/// </summary>
|
||||
/// <param name="style">L'objet style à supprimer.</param>
|
||||
public void Delete(Style style)
|
||||
{
|
||||
if (!this.styles.Contains(style))
|
||||
{
|
||||
this.logger.LogWarning("Le style avec l'identifiant {IdStyle} n'existe pas dans la liste.", style.IdStyle);
|
||||
}
|
||||
|
||||
this.styles.Remove(style);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Trouve un style dans la liste des styles en fonction de son identifiant.
|
||||
/// </summary>
|
||||
/// <param name="id">L'identifiant du style à trouver.</param>
|
||||
/// <returns>Le style correspondant à l'identifiant fourni, ou null si aucun style n'est trouvé.</returns>
|
||||
public Style Find(int id)
|
||||
{
|
||||
if (id <= 0)
|
||||
{
|
||||
this.logger.LogWarning("Le style fourni est null.");
|
||||
}
|
||||
|
||||
Style style = this.styles.First(s => s.IdStyle == id);
|
||||
return style;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Trouve tous les styles dans la liste des styles.
|
||||
/// </summary>
|
||||
/// <returns>Une collection de tous les styles présents dans la liste.</returns>
|
||||
public IEnumerable<Style> FindAll()
|
||||
{
|
||||
return this.styles;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Met à jour un style dans la liste des styles en fonction de son identifiant.
|
||||
/// </summary>
|
||||
/// <param name="style">L'objet style à mettre à jour.</param>
|
||||
public void Update(Style style)
|
||||
{
|
||||
int index = this.styles.FindIndex(s => s.IdStyle == style.IdStyle);
|
||||
|
||||
if (index == -1)
|
||||
{
|
||||
this.logger.LogWarning("Aucun style trouvé avec l'identifiant {IdStyle}.", style.IdStyle);
|
||||
}
|
||||
|
||||
this.styles[index] = style;
|
||||
}
|
||||
}
|
||||
106
Webzine.Repository/LocalTitreRepository.cs
Normal file
106
Webzine.Repository/LocalTitreRepository.cs
Normal file
@@ -0,0 +1,106 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Webzine.Entity;
|
||||
using Webzine.Repository.Contracts;
|
||||
|
||||
namespace Webzine.Repository;
|
||||
|
||||
/// <summary>
|
||||
/// Classe qui implémente le repository pour les titres en utilisant une liste locale comme source de données.
|
||||
/// </summary>
|
||||
public class LocalTitreRepository : ITitreRepository
|
||||
{
|
||||
private readonly ILogger<LocalTitreRepository> logger;
|
||||
private readonly List<Titre> titres;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="LocalTitreRepository"/> class.
|
||||
/// </summary>
|
||||
/// <param name="logger">Le service de journalisation injecté pour suivre les opérations du repository.</param>
|
||||
/// <param name="titres">La liste de titres à utiliser comme source de données pour le repository.</param>
|
||||
public LocalTitreRepository(ILogger<LocalTitreRepository> logger, List<Titre> titres)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.titres = titres;
|
||||
this.logger.LogDebug(1, "NLog injected into LocalTitreRepository");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Recherche les titres dont le libellé contient le mot spécifié, en ignorant la casse.
|
||||
/// </summary>
|
||||
/// <param name="mot">Le mot à rechercher dans les libellés des titres.</param>
|
||||
/// <returns>Une collection de titres correspondant au critère de recherche, triée par libellé.</returns>
|
||||
public IEnumerable<Titre> Search(string mot)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(mot))
|
||||
{
|
||||
this.logger.LogWarning("Search called with an empty or whitespace string.");
|
||||
return Enumerable.Empty<Titre>();
|
||||
}
|
||||
|
||||
IEnumerable<Titre> list = this.titres
|
||||
.Where(t => !string.IsNullOrWhiteSpace(t.Libelle)
|
||||
&& t.Libelle.Contains(mot, StringComparison.OrdinalIgnoreCase))
|
||||
.OrderBy(t => t.Libelle)
|
||||
.ToList();
|
||||
|
||||
if (!list.Any())
|
||||
{
|
||||
this.logger.LogInformation("No titres found matching the search term '{Mot}'.", mot);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Trouve un titre dans la liste des titres en fonction de son identifiant.
|
||||
/// </summary>
|
||||
/// <param name="idTitre">L'identifiant du titre à trouver.</param>
|
||||
/// <returns>Le titre correspondant à l'identifiant fourni, ou null si aucun titre n'est trouvé.</returns>
|
||||
public Titre? Find(int idTitre)
|
||||
{
|
||||
Titre? titre = titres.FirstOrDefault(t => t.IdTitre == idTitre);
|
||||
|
||||
if (titre == null)
|
||||
{
|
||||
this.logger.LogInformation("No titre found with IdTitre {IdTitre}.", idTitre);
|
||||
}
|
||||
|
||||
return titre;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Trouve tous les titres dans la liste des titres.
|
||||
/// </summary>
|
||||
/// <returns>Une collection de tous les titres présents dans la liste.</returns>
|
||||
public IEnumerable<Titre> FindAll()
|
||||
{
|
||||
return this.titres;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Recherche les titres associés à un style dont le libellé contient la chaîne spécifiée, en ignorant la casse.
|
||||
/// </summary>
|
||||
/// <param name="libelle">Le libellé du style à rechercher dans les titres.</param>
|
||||
/// <returns>Une collection de titres correspondant au critère de recherche, triée par libellé.</returns>
|
||||
public IEnumerable<Titre> SearchByStyle(string libelle)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(libelle))
|
||||
{
|
||||
this.logger.LogWarning("SearchByStyle called with an empty or whitespace string.");
|
||||
return Enumerable.Empty<Titre>();
|
||||
}
|
||||
|
||||
IEnumerable<Titre> list = this.titres
|
||||
.Where(t => t.Styles.Any(s => !string.IsNullOrWhiteSpace(s.Libelle)
|
||||
&& s.Libelle.Contains(libelle, StringComparison.OrdinalIgnoreCase)))
|
||||
.OrderBy(t => t.Libelle)
|
||||
.ToList();
|
||||
|
||||
if (!list.Any())
|
||||
{
|
||||
this.logger.LogInformation("No titres found matching the style '{Libelle}'.", libelle);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,11 @@ try
|
||||
// Necessite le package Nuget Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.
|
||||
.AddRazorRuntimeCompilation();
|
||||
|
||||
builder.Services.AddSingleton<ITitreRepository, LocalEntityRepository>();
|
||||
builder.Services.AddSingleton<ITitreRepository, LocalTitreRepository>();
|
||||
builder.Services.AddSingleton<IStyleRepository, LocalStyleRepository>();
|
||||
|
||||
builder.Services.AddScoped<ITitreRepository, DbTitreRepository>();
|
||||
builder.Services.AddScoped<IStyleRepository, DbStyleRepository>();
|
||||
|
||||
// NLog: Setup NLog for Dependency injection
|
||||
builder.Logging.ClearProviders();
|
||||
|
||||
Reference in New Issue
Block a user