Files
webzine/Webzine.Repository/LocalTitreRepository.cs

132 lines
3.9 KiB
C#

namespace Webzine.Repository;
using Microsoft.Extensions.Logging;
using Webzine.Entity;
using Webzine.Repository.Contracts;
/// <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 InMemoryDataStore dataStore;
/// <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="dataStore">La liste de titres à utiliser comme source de données pour le repository.</param>
public LocalTitreRepository(ILogger<LocalTitreRepository> logger, InMemoryDataStore dataStore)
{
this.logger = logger;
this.dataStore = dataStore;
this.logger.LogDebug(1, "NLog injecté dans LocalTitreRepository");
}
/// <inheritdoc/>
public void Add(Titre titre)
{
this.dataStore.Titres.Add(titre);
}
/// <inheritdoc/>
public int Count()
{
// On appelle directement LINQ count pour ne pas confondre avec la méthode Count() de l'interface ITitreRepository
return Enumerable.Count(this.dataStore.Titres);
}
/// <inheritdoc/>
public void Delete(Titre titre)
{
this.dataStore.Titres.Remove(titre);
}
/// <inheritdoc/>
public IEnumerable<Titre> FindTitres(int offset, int limit)
{
return this.dataStore.Titres
.OrderByDescending(t => t.DateCreation)
.Skip(offset)
.Take(limit);
}
/// <inheritdoc/>
public void IncrementNbLectures(Titre titre)
{
var stored = this.dataStore.Titres.FirstOrDefault(t => t.IdTitre == titre.IdTitre);
if (stored == null)
{
this.logger.LogWarning("Titre avec l'ID {Id} non trouvé pour incrémenter le nombre de lectures.", titre.IdTitre);
return;
}
stored.NbLectures++;
}
/// <inheritdoc/>
public void IncrementNbLikes(Titre titre)
{
var stored = this.dataStore.Titres.FirstOrDefault(t => t.IdTitre == titre.IdTitre);
if (stored == null)
{
this.logger.LogWarning("Titre avec l'ID {Id} non trouvé pour incrémenter le nombre de likes.", titre.IdTitre);
return;
}
stored.NbLikes++;
}
/// <inheritdoc/>
public IEnumerable<Titre> Search(string mot)
{
if (string.IsNullOrWhiteSpace(mot))
{
return Enumerable.Empty<Titre>();
}
return this.dataStore.Titres
.Where(t => t.Libelle.ToLower().Contains(mot.ToLower()))
.ToList();
}
/// <inheritdoc/>
public Titre Find(int idTitre)
{
return this.dataStore.Titres
.FirstOrDefault(t => t.IdTitre == idTitre);
}
/// <inheritdoc/>
public IEnumerable<Titre> FindAll()
{
return this.dataStore.Titres;
}
/// <inheritdoc/>
public IEnumerable<Titre> SearchByStyle(string libelle)
{
return this.dataStore.Titres
.Where(t => t.Styles.Any(s => s.Libelle == libelle));
}
/// <inheritdoc/>
public void Update(Titre titre)
{
var stored = this.dataStore.Titres.FirstOrDefault(t => t.IdTitre == titre.IdTitre);
if (stored == null)
{
this.logger.LogWarning("Titre avec l'ID {Id} non trouvé pour mise à jour.", titre.IdTitre);
return;
}
stored.Libelle = titre.Libelle;
stored.DateCreation = titre.DateCreation;
stored.NbLectures = titre.NbLectures;
stored.NbLikes = titre.NbLikes;
stored.IdArtiste = titre.IdArtiste;
stored.Styles = titre.Styles;
}
}