namespace Webzine.Repository;
using Microsoft.Extensions.Logging;
using Webzine.Entity;
using Webzine.Repository.Contracts;
///
/// Classe qui implémente le repository pour les titres en utilisant une liste locale comme source de données.
///
public class LocalTitreRepository : ITitreRepository
{
private readonly ILogger logger;
private readonly InMemoryDataStore dataStore;
///
/// Initializes a new instance of the class.
///
/// Le service de journalisation injecté pour suivre les opérations du repository.
/// La liste de titres à utiliser comme source de données pour le repository.
public LocalTitreRepository(ILogger logger, InMemoryDataStore dataStore)
{
this.logger = logger;
this.dataStore = dataStore;
this.logger.LogDebug(1, "NLog injecté dans LocalTitreRepository");
}
///
public void Add(Titre titre)
{
this.dataStore.Titres.Add(titre);
}
///
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);
}
///
public void Delete(Titre titre)
{
this.dataStore.Titres.Remove(titre);
}
///
public IEnumerable FindTitres(int offset, int limit)
{
return this.dataStore.Titres
.OrderByDescending(t => t.DateCreation)
.ThenBy(t => t.Libelle)
.Paginate(offset, limit);
}
///
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++;
}
///
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++;
}
///
public IEnumerable Search(string mot)
{
if (string.IsNullOrWhiteSpace(mot))
{
return Enumerable.Empty();
}
return this.dataStore.Titres
.Where(t => t.Libelle.ToLower().Contains(mot.ToLower()))
.ToList();
}
///
public Titre Find(int idTitre)
{
return this.dataStore.Titres
.SingleOrDefault(t => t.IdTitre == idTitre);
}
///
public IEnumerable FindAll()
{
return this.dataStore.Titres;
}
///
public IEnumerable SearchByStyle(string libelle)
{
return this.dataStore.Titres
.Where(t => t.Styles.Any(s => s.Libelle == libelle));
}
///
public IEnumerable SearchByStylePaginate(int offset, int limit, string libelle)
{
return this.SearchByStyle(libelle).Paginate(offset, limit);
}
///
public int CountByStyle(string libelle)
{
return this.dataStore.Titres
.Where(t => t.Styles.Any(s => s.Libelle == libelle))
.Count();
}
///
public void Update(Titre titre)
{
// On trouve le titre stocké pour mettre à jour ses propriétés avec la méthode Find du repository
// pour éviter la duplication de code.
Titre existingTitre = this.Find(titre.IdTitre);
if (existingTitre == null)
{
this.logger.LogWarning("Titre avec l'ID {Id} non trouvé pour mise à jour.", titre.IdTitre);
return;
}
existingTitre.Libelle = titre.Libelle;
existingTitre.DateCreation = titre.DateCreation;
existingTitre.NbLectures = titre.NbLectures;
existingTitre.NbLikes = titre.NbLikes;
existingTitre.IdArtiste = titre.IdArtiste;
existingTitre.Styles = titre.Styles;
}
///
public int CountLike()
{
return this.dataStore.Titres.Sum(t => t.NbLikes);
}
///
public int CountLecture()
{
return this.dataStore.Titres.Sum(t => t.NbLectures);
}
///
public string? FindMostReviewedArtistName()
{
return this.dataStore.Titres
.GroupBy(t => t.Artiste)
.OrderByDescending(g => g.Count())
.ThenBy(g => g.Key?.Nom)
.Select(g => g.Key?.Nom)
.FirstOrDefault();
}
///
public string? FindArtistNameWithMostReviewedAlbums()
{
return this.dataStore.Titres
.GroupBy(t => t.Artiste)
.Select(g => new
{
ArtistName = g.Key?.Nom,
AlbumCount = g.Select(t => t.Album).Distinct().Count(),
})
.OrderByDescending(x => x.AlbumCount)
.ThenBy(x => x.ArtistName)
.Select(x => x.ArtistName)
.FirstOrDefault();
}
///
public (int IdTitre, string Libelle)? FindMostPlayedTitle()
{
Titre? titre = this.dataStore.Titres
.OrderByDescending(t => t.NbLectures)
.ThenBy(t => t.Libelle)
.FirstOrDefault();
return titre == null ? null : (titre.IdTitre, titre.Libelle);
}
///
public IEnumerable DerniereChronique(int offset, int limit)
{
try
{
return this.dataStore.Titres
.OrderByDescending(t => t.DateCreation)
.Paginate(offset, limit);
}
catch (Exception ex)
{
this.logger.LogError(ex, "Erreur lors de la recuperation des dernieres chroniques.");
throw;
}
}
///
public IEnumerable TopTitre(int offset, int limit)
{
try
{
this.logger.LogInformation("Récupération du Top Titre.");
var titres = this.dataStore.Titres
.OrderByDescending(t => t.NbLikes)
.Paginate(offset, limit);
return titres;
}
catch (Exception ex)
{
this.logger.LogError(ex, "Erreur lors de la récupération du Top Titre.");
throw;
}
}
}