diff --git a/Webzine.Repository.Contracts/IArtisteRepository.cs b/Webzine.Repository.Contracts/IArtisteRepository.cs index 4eef62b..936e694 100644 --- a/Webzine.Repository.Contracts/IArtisteRepository.cs +++ b/Webzine.Repository.Contracts/IArtisteRepository.cs @@ -3,8 +3,8 @@ namespace Webzine.Repository.Contracts using Webzine.Entity; /// - /// Défini une interface pour gérer les opérations de base de données liées aux artistes. - /// // TODO interface n'est pas que liée à la bdd, elle est aussi utilisée pour la gestion en mémoire + /// Défini une interface pour gérer les opérations des artistes dans la source de données. + /// public interface IArtisteRepository { /// diff --git a/Webzine.Repository/DbArtisteRepository.cs b/Webzine.Repository/DbArtisteRepository.cs index f1d5856..6d34837 100644 --- a/Webzine.Repository/DbArtisteRepository.cs +++ b/Webzine.Repository/DbArtisteRepository.cs @@ -112,7 +112,10 @@ namespace Webzine.Repository try { // .AsNoTracking() rend la requête beaucoup plus rapide pour de la lecture - var artistes = this.context.Artistes.AsNoTracking().Include(t => t.Titres).ToList(); + var artistes = this.context.Artistes + .AsNoTracking() + .Include(t => t.Titres) + .ToList(); this.logger.LogDebug("{Count} artistes récupérés de la base.", artistes.Count); return artistes; } diff --git a/Webzine.Repository/DbCommentaireRepository.cs b/Webzine.Repository/DbCommentaireRepository.cs index 0c41ffd..b5696ec 100644 --- a/Webzine.Repository/DbCommentaireRepository.cs +++ b/Webzine.Repository/DbCommentaireRepository.cs @@ -84,6 +84,7 @@ public class DbCommentaireRepository : ICommentaireRepository public IEnumerable FindAll() { var commentaires = this.context.Commentaires + .AsNoTracking() .Include(c => c.Titre) .OrderByDescending(c => c.DateCreation) .ToList(); @@ -98,6 +99,7 @@ public class DbCommentaireRepository : ICommentaireRepository this.logger.LogDebug("Recherche paginée des commentaires (offset : {Offset}, limit : {Limit})", offset, limit); var commentaires = this.context.Commentaires + .AsNoTracking() .Include(c => c.Titre) .OrderByDescending(c => c.DateCreation) .Skip(offset) diff --git a/Webzine.Repository/DbStyleRepository.cs b/Webzine.Repository/DbStyleRepository.cs index 452b312..9e69837 100644 --- a/Webzine.Repository/DbStyleRepository.cs +++ b/Webzine.Repository/DbStyleRepository.cs @@ -94,6 +94,7 @@ public class DbStyleRepository : IStyleRepository this.logger.LogDebug("Recherche du style avec l'ID: {Id}", id); var style = this.context.Styles + .AsNoTracking() .Include(s => s.Titres) .FirstOrDefault(s => s.IdStyle == id); @@ -122,6 +123,7 @@ public class DbStyleRepository : IStyleRepository this.logger.LogDebug("Tri des styles par libellé"); var styles = this.context.Styles + .AsNoTracking() .OrderBy(s => s.Libelle) .ToList(); diff --git a/Webzine.Repository/DbTitreRepository.cs b/Webzine.Repository/DbTitreRepository.cs index 0675c33..5ae3c9a 100644 --- a/Webzine.Repository/DbTitreRepository.cs +++ b/Webzine.Repository/DbTitreRepository.cs @@ -33,7 +33,6 @@ public class DbTitreRepository : ITitreRepository try { this.logger.LogInformation("Ajout d'un nouveau titre: {Libelle}", titre.Libelle); - this.logger.LogDebug("Début de l'ajout du titre en base de données"); // TODO trop de logs this.context.Titres.Add(titre); this.context.SaveChanges(); @@ -57,8 +56,7 @@ public class DbTitreRepository : ITitreRepository { try { - this.logger.LogDebug("Comptage des titres en base de données"); - var count = this.context.Titres.Count(); + var count = Enumerable.Count(this.context.Titres); this.logger.LogDebug("Nombre total de titres: {Count}", count); return count; } @@ -75,7 +73,6 @@ public class DbTitreRepository : ITitreRepository try { this.logger.LogInformation("Suppression du titre avec l'ID: {IdTitre}", titre.IdTitre); - this.logger.LogDebug("Début de la suppression du titre en base de données"); this.context.Titres.Remove(titre); this.context.SaveChanges(); @@ -100,7 +97,6 @@ public class DbTitreRepository : ITitreRepository try { this.logger.LogDebug("Recherche des titres avec offset: {Offset}, limit: {Limit}", offset, limit); - this.logger.LogDebug("Préparation de la requête avec les inclusions Artiste et Styles"); var titres = this.context.Titres .Include(t => t.Artiste) @@ -108,6 +104,7 @@ public class DbTitreRepository : ITitreRepository .OrderBy(t => t.Libelle) .Skip(offset) .Take(limit) + .AsNoTracking() .ToList(); this.logger.LogDebug("{Count} titres trouvés", titres.Count); @@ -126,12 +123,10 @@ public class DbTitreRepository : ITitreRepository try { this.logger.LogInformation("Incrémentation du nombre de lectures pour le titre ID: {IdTitre}", titre.IdTitre); - this.logger.LogDebug("Recherche du titre en base de données"); var existingTitre = this.context.Titres.Find(titre.IdTitre); if (existingTitre != null) { - this.logger.LogDebug("Titre trouvé, incrémentation du compteur de lectures"); existingTitre.NbLectures++; this.context.SaveChanges(); this.logger.LogDebug("Nouveau nombre de lectures: {NbLectures}", existingTitre.NbLectures); @@ -186,16 +181,13 @@ public class DbTitreRepository : ITitreRepository try { this.logger.LogInformation("Mise à jour du titre avec l'ID: {IdTitre}", titre.IdTitre); - this.logger.LogDebug("Début de la mise à jour du titre en base de données"); var existingTitre = this.context.Titres.Find(titre.IdTitre); if (existingTitre != null) { - this.logger.LogDebug("Titre trouvé, mise à jour des propriétés"); this.context.Entry(existingTitre).CurrentValues.SetValues(titre); - // Handle many-to-many relationships - this.logger.LogDebug("Mise à jour des relations many-to-many (Styles)"); + // Relation many-to-many this.context.Entry(existingTitre).Collection(t => t.Styles).Load(); existingTitre.Styles.Clear(); foreach (var style in titre.Styles) @@ -230,7 +222,6 @@ public class DbTitreRepository : ITitreRepository try { this.logger.LogInformation("Recherche des titres avec le mot-clé: {Mot}", mot); - this.logger.LogDebug("Préparation de la requête de recherche avec les inclusions"); var titres = this.context.Titres .Include(t => t.Artiste) @@ -282,14 +273,12 @@ public class DbTitreRepository : ITitreRepository { try { - this.logger.LogDebug("Récupération de tous les titres"); // TODO trop de logs - this.logger.LogDebug("Préparation de la requête avec les inclusions Artiste et Styles"); - var titres = this.context.Titres .Include(t => t.Artiste) .Include(t => t.Styles) .Include(t => t.Commentaires) .OrderBy(t => t.Libelle) + .AsNoTracking() .ToList(); this.logger.LogDebug("{Count} titres récupérés", titres.Count); @@ -308,13 +297,13 @@ public class DbTitreRepository : ITitreRepository try { this.logger.LogInformation("Recherche des titres par style: {Libelle}", libelle); - this.logger.LogDebug("Préparation de la requête de recherche par style"); var titres = this.context.Titres .Include(t => t.Artiste) .Include(t => t.Styles) .Where(t => t.Styles.Any(s => s.Libelle.ToLower() == libelle.ToLower())) .OrderBy(t => t.Libelle) + .AsNoTracking() .ToList(); this.logger.LogDebug("{Count} titres trouvés pour le style '{Libelle}'", titres.Count, libelle); diff --git a/Webzine.Repository/LocalArtisteRepository.cs b/Webzine.Repository/LocalArtisteRepository.cs index ebab75c..8a4f51c 100644 --- a/Webzine.Repository/LocalArtisteRepository.cs +++ b/Webzine.Repository/LocalArtisteRepository.cs @@ -37,7 +37,6 @@ namespace Webzine.Repository public void Add(Artiste artiste) { throw new NotSupportedException("Mode Local"); // TODO à implémenter - throw new NotSupportedException("Mode Local"); // TODO à implémenter } /// diff --git a/Webzine.Repository/LocalCommentaireRepository.cs b/Webzine.Repository/LocalCommentaireRepository.cs index 8f71958..7096f49 100644 --- a/Webzine.Repository/LocalCommentaireRepository.cs +++ b/Webzine.Repository/LocalCommentaireRepository.cs @@ -22,7 +22,6 @@ namespace Webzine.Repository private readonly InMemoryDataStore dataStore; /// - /// Initializes a new instance of the class. /// Initialise une nouvelle instance du . /// Gère les opérations liées aux commentaires en utilisant une source de données locale (en mémoire). /// diff --git a/Webzine.Repository/LocalTitreRepository.cs b/Webzine.Repository/LocalTitreRepository.cs index a970bf9..5c5199b 100644 --- a/Webzine.Repository/LocalTitreRepository.cs +++ b/Webzine.Repository/LocalTitreRepository.cs @@ -34,10 +34,8 @@ public class LocalTitreRepository : ITitreRepository /// 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; + // On appelle directement LINQ count pour ne pas confondre avec la méthode Count() de l'interface ITitreRepository + return Enumerable.Count(this.dataStore.Titres); } /// @@ -58,20 +56,40 @@ public class LocalTitreRepository : ITitreRepository /// public void IncrementNbLectures(Titre titre) { - titre.NbLectures++; + 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) { - titre.NbLikes++; // TODO rien n'est enregistré + 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 != null && t.Libelle.Contains(mot)); // TODO attention au null, et à la casse, et à l'indexation pour les performances + .Where(t => t.Libelle.ToLower().Contains(mot.ToLower())) + .ToList(); } ///