233 lines
6.7 KiB
C#
233 lines
6.7 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)
|
|
.ThenBy(t => t.Libelle)
|
|
.Paginate(offset, 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
|
|
.SingleOrDefault(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 IEnumerable<Titre> SearchByStylePaginate(int offset, int limit, string libelle)
|
|
{
|
|
return this.SearchByStyle(libelle).Paginate(offset, limit);
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public int CountByStyle(string libelle)
|
|
{
|
|
return this.dataStore.Titres
|
|
.Where(t => t.Styles.Any(s => s.Libelle == libelle))
|
|
.Count();
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
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;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public int CountLike()
|
|
{
|
|
return this.dataStore.Titres.Sum(t => t.NbLikes);
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public int CountLecture()
|
|
{
|
|
return this.dataStore.Titres.Sum(t => t.NbLectures);
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
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();
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
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();
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
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);
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public IEnumerable<Titre> 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;
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public IEnumerable<Titre> 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;
|
|
}
|
|
}
|
|
} |