Files
webzine/Webzine.Repository/LocalTitreRepository.cs
josephine.vetu b2dc449adb add TODO
2026-04-01 11:55:27 +02:00

102 lines
2.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)
{
throw new NotSupportedException("Mode local");
}
/// <inheritdoc/>
public int Count()
{
var count = this.dataStore.Titres.Count(); // TODO une seule ligne, et attention car les deux méthodes s'appelent pareil,
// il faut faire attention à ne pas confondre avec la méthode Count() de l'interface ITitreRepository
return count;
}
/// <inheritdoc/>
public void Delete(Titre titre)
{
throw new NotSupportedException("Mode Local");
}
/// <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)
{
titre.NbLectures++;
}
/// <inheritdoc/>
public void IncrementNbLikes(Titre titre)
{
titre.NbLikes++; // TODO rien n'est enregistré
}
/// <inheritdoc/>
public IEnumerable<Titre> Search(string mot)
{
return this.dataStore.Titres
.Where(t => t.Libelle != null && t.Libelle.Contains(mot)); // TODO attention au null, et à la casse, et à l'indexation pour les performances
}
/// <inheritdoc/>
public Titre Find(int idTitre)
{
return this.dataStore.Titres
.First(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)
{
throw new NotSupportedException("Mode local");
}
}