#146 Erreur de rebase supprimées (lignes dupliquées. Ajout de AsNoTracking pour toutes les opérations de READ sur la bdd. Modification de la méthode Count().
This commit is contained in:
@@ -3,8 +3,8 @@ namespace Webzine.Repository.Contracts
|
||||
using Webzine.Entity;
|
||||
|
||||
/// <summary>
|
||||
/// Défini une interface <see cref="IArtisteRepository"/> pour gérer les opérations de base de données liées aux artistes.
|
||||
/// </summary> // 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 <see cref="IArtisteRepository"/> pour gérer les opérations des artistes dans la source de données.
|
||||
/// </summary>
|
||||
public interface IArtisteRepository
|
||||
{
|
||||
/// <summary>
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -84,6 +84,7 @@ public class DbCommentaireRepository : ICommentaireRepository
|
||||
public IEnumerable<Commentaire> 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)
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
|
||||
@@ -22,7 +22,6 @@ namespace Webzine.Repository
|
||||
private readonly InMemoryDataStore dataStore;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="LocalCommentaireRepository"/> class.
|
||||
/// Initialise une nouvelle instance du <see cref="LocalCommentaireRepository"/> .
|
||||
/// Gère les opérations liées aux commentaires en utilisant une source de données locale (en mémoire).
|
||||
/// </summary>
|
||||
|
||||
@@ -34,10 +34,8 @@ public class LocalTitreRepository : ITitreRepository
|
||||
/// <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;
|
||||
// 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/>
|
||||
@@ -58,20 +56,40 @@ public class LocalTitreRepository : ITitreRepository
|
||||
/// <inheritdoc/>
|
||||
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++;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
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++;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IEnumerable<Titre> Search(string mot)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(mot))
|
||||
{
|
||||
return Enumerable.Empty<Titre>();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
|
||||
Reference in New Issue
Block a user