diff --git a/Webzine.Repository.Contracts/ICommentaireRepository.cs b/Webzine.Repository.Contracts/ICommentaireRepository.cs
index bcd07d2..c87fb9e 100644
--- a/Webzine.Repository.Contracts/ICommentaireRepository.cs
+++ b/Webzine.Repository.Contracts/ICommentaireRepository.cs
@@ -39,6 +39,6 @@ namespace Webzine.Repository.Contracts
/// récupérer les commentaires.
/// Le nombre maximum de commentaires à récupérer.
/// Une collection de commentaires paginée.
- IEnumerable Paginate(int offset, int limit);
+ IEnumerable FindCommentaires(int offset, int limit);
}
}
\ No newline at end of file
diff --git a/Webzine.Repository/DbArtisteRepository.cs b/Webzine.Repository/DbArtisteRepository.cs
index 2c50cdc..01bc409 100644
--- a/Webzine.Repository/DbArtisteRepository.cs
+++ b/Webzine.Repository/DbArtisteRepository.cs
@@ -78,7 +78,7 @@ namespace Webzine.Repository
{
Artiste artiste = this.context.Artistes
.Include(a => a.Titres)
- .FirstOrDefault(a => a.IdArtiste == id);
+ .SingleOrDefault(a => a.IdArtiste == id);
return artiste;
}
catch (Exception ex)
@@ -112,11 +112,12 @@ namespace Webzine.Repository
try
{
// .AsNoTracking() rend la requête beaucoup plus rapide pour de la lecture
+ // Pas besoin de faire un ToList() ici, car on retourne un IEnumerable et EF Core gère l'exécution différée de la requête.
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);
+ .Include(t => t.Titres);
+
+ this.logger.LogDebug("La liste d'artistes a été récupérée de la base.");
return artistes;
}
catch (Exception ex)
@@ -131,6 +132,13 @@ namespace Webzine.Repository
{
try
{
+ Artiste existingArtiste = this.Find(artiste.IdArtiste); // Vérifie que l'artiste existe avant de tenter de le mettre à jour
+ if (existingArtiste == null)
+ {
+ this.logger.LogWarning("L'artiste {Id} n'a pas été trouvé pour l'update.", artiste.IdArtiste);
+ throw new InvalidOperationException($"L'artiste avec l'ID {artiste.IdArtiste} n'a pas été trouvé pour la mise à jour.");
+ }
+
this.context.Artistes.Update(artiste);
this.context.SaveChanges();
this.logger.LogDebug("Artiste {Id} ({Nom}) mis à jour avec succès.", artiste.IdArtiste, artiste.Nom);
@@ -159,8 +167,7 @@ namespace Webzine.Repository
var artiste = this.context.Artistes
.Where(a => a.Nom.ToLower().Contains(mot.ToLower()))
.Include(t => t.Titres)
- .AsNoTracking()
- .ToList();
+ .AsNoTracking();
return artiste;
}
catch (Exception ex)
diff --git a/Webzine.Repository/DbCommentaireRepository.cs b/Webzine.Repository/DbCommentaireRepository.cs
index b5696ec..a101dfd 100644
--- a/Webzine.Repository/DbCommentaireRepository.cs
+++ b/Webzine.Repository/DbCommentaireRepository.cs
@@ -77,7 +77,7 @@ public class DbCommentaireRepository : ICommentaireRepository
// On inclut le titre car il est souvent affiché avec le commentaire
return this.context.Commentaires
.Include(c => c.Titre)
- .FirstOrDefault(c => c.IdCommentaire == idCommentaire);
+ .SingleOrDefault(c => c.IdCommentaire == idCommentaire);
}
///
@@ -86,27 +86,32 @@ public class DbCommentaireRepository : ICommentaireRepository
var commentaires = this.context.Commentaires
.AsNoTracking()
.Include(c => c.Titre)
- .OrderByDescending(c => c.DateCreation)
- .ToList();
+ .OrderByDescending(c => c.DateCreation);
- this.logger.LogDebug("Nombre de commentaires trouvés : {Count}", commentaires.Count);
+ this.logger.LogDebug("La liste de commentaires a été récupérée.");
return commentaires;
}
///
- public IEnumerable Paginate(int offset, int limit)
+ public IEnumerable FindCommentaires(int offset, int limit)
{
- this.logger.LogDebug("Recherche paginée des commentaires (offset : {Offset}, limit : {Limit})", offset, limit);
+ try
+ {
+ 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)
- .Take(limit)
- .ToList();
+ var commentaires = this.context.Commentaires
+ .AsNoTracking()
+ .Include(c => c.Titre)
+ .OrderByDescending(c => c.DateCreation)
+ .Skip(offset)
+ .Take(limit);
- this.logger.LogDebug("{Count} commentaires trouvés pour cette page", commentaires.Count);
- return commentaires;
+ return commentaires;
+ }
+ catch (Exception ex)
+ {
+ this.logger.LogError(ex, "Erreur lors de la pagination des commentaires (offset : {Offset}, limit : {Limit})", offset, limit);
+ throw new Exception("Une erreur est survenue lors de la pagination des commentaires.", ex);
+ }
}
}
\ No newline at end of file
diff --git a/Webzine.Repository/DbStyleRepository.cs b/Webzine.Repository/DbStyleRepository.cs
index 7b8c0c7..0412a90 100644
--- a/Webzine.Repository/DbStyleRepository.cs
+++ b/Webzine.Repository/DbStyleRepository.cs
@@ -96,7 +96,7 @@ public class DbStyleRepository : IStyleRepository
var style = this.context.Styles
.AsNoTracking()
.Include(s => s.Titres)
- .FirstOrDefault(s => s.IdStyle == id);
+ .SingleOrDefault(s => s.IdStyle == id);
if (style == null)
{
@@ -124,10 +124,9 @@ public class DbStyleRepository : IStyleRepository
var styles = this.context.Styles
.AsNoTracking()
- .OrderBy(s => s.Libelle)
- .ToList();
+ .OrderBy(s => s.Libelle);
- this.logger.LogDebug("{Count} styles récupérés", styles.Count);
+ this.logger.LogDebug("La liste de styles a été récupérée.");
return styles;
}
catch (Exception ex)
@@ -143,15 +142,15 @@ public class DbStyleRepository : IStyleRepository
try
{
this.logger.LogInformation("Mise à jour du style avec l'ID: {IdStyle}", style.IdStyle);
+ Style existingStyle = this.Find(style.IdStyle); // Vérifie que le style existe avant de tenter de le mettre à jour
- var existingStyle = this.context.Styles.Find(style.IdStyle);
if (existingStyle == null)
{
- this.logger.LogWarning("Style avec l'ID {IdStyle} non trouvé pour la mise à jour", style.IdStyle);
- throw new InvalidOperationException($"Style avec l'ID {style.IdStyle} non trouvé.");
+ this.logger.LogWarning("Style avec l'ID {IdStyle} non trouvé pour l'update.", style.IdStyle);
+ throw new InvalidOperationException($"Style avec l'ID {style.IdStyle} non trouvé pour la mise à jour.");
}
- existingStyle.Libelle = style.Libelle;
+ this.context.Styles.Update(style);
this.context.SaveChanges();
this.logger.LogDebug("Style mis à jour avec succès: {IdStyle}", style.IdStyle);
diff --git a/Webzine.Repository/DbTitreRepository.cs b/Webzine.Repository/DbTitreRepository.cs
index 5ae3c9a..524f924 100644
--- a/Webzine.Repository/DbTitreRepository.cs
+++ b/Webzine.Repository/DbTitreRepository.cs
@@ -104,10 +104,9 @@ public class DbTitreRepository : ITitreRepository
.OrderBy(t => t.Libelle)
.Skip(offset)
.Take(limit)
- .AsNoTracking()
- .ToList();
+ .AsNoTracking();
- this.logger.LogDebug("{Count} titres trouvés", titres.Count);
+ this.logger.LogDebug("La liste de titres a été récupérée.");
return titres;
}
catch (Exception ex)
@@ -182,7 +181,7 @@ public class DbTitreRepository : ITitreRepository
{
this.logger.LogInformation("Mise à jour du titre avec l'ID: {IdTitre}", titre.IdTitre);
- var existingTitre = this.context.Titres.Find(titre.IdTitre);
+ Titre existingTitre = this.Find(titre.IdTitre);
if (existingTitre != null)
{
this.context.Entry(existingTitre).CurrentValues.SetValues(titre);
@@ -228,10 +227,9 @@ public class DbTitreRepository : ITitreRepository
.Include(t => t.Styles)
.Where(t => t.Libelle.ToLower().Contains(mot.ToLower()))
.OrderBy(t => t.Libelle)
- .AsNoTracking()
- .ToList();
+ .AsNoTracking();
- this.logger.LogDebug("{Count} titres trouvés correspondant à '{Mot}'", titres.Count, mot);
+ this.logger.LogDebug("La liste de titres a été récupérée.");
return titres;
}
catch (Exception ex)
@@ -278,10 +276,9 @@ public class DbTitreRepository : ITitreRepository
.Include(t => t.Styles)
.Include(t => t.Commentaires)
.OrderBy(t => t.Libelle)
- .AsNoTracking()
- .ToList();
+ .AsNoTracking();
- this.logger.LogDebug("{Count} titres récupérés", titres.Count);
+ this.logger.LogDebug("La liste de titres a été récupérée avec FindAll.");
return titres;
}
catch (Exception ex)
@@ -303,10 +300,9 @@ public class DbTitreRepository : ITitreRepository
.Include(t => t.Styles)
.Where(t => t.Styles.Any(s => s.Libelle.ToLower() == libelle.ToLower()))
.OrderBy(t => t.Libelle)
- .AsNoTracking()
- .ToList();
+ .AsNoTracking();
- this.logger.LogDebug("{Count} titres trouvés pour le style '{Libelle}'", titres.Count, libelle);
+ this.logger.LogDebug("La liste de titres a été récupérée pour le style '{Libelle}'", libelle);
return titres;
}
catch (Exception ex)
diff --git a/Webzine.Repository/LocalArtisteRepository.cs b/Webzine.Repository/LocalArtisteRepository.cs
index 1aa7137..402aca1 100644
--- a/Webzine.Repository/LocalArtisteRepository.cs
+++ b/Webzine.Repository/LocalArtisteRepository.cs
@@ -75,16 +75,16 @@ namespace Webzine.Repository
///
public void Update(Artiste artiste)
{
- var stored = this.dataStore.Artistes.FirstOrDefault(a => a.IdArtiste == artiste.IdArtiste);
- if (stored == null)
+ Artiste existingArtiste = this.Find(artiste.IdArtiste);
+ if (existingArtiste == null)
{
this.logger.LogWarning("L'artiste {Id} n'a pas été trouvé pour l'update.", artiste.IdArtiste);
return;
}
- stored.Nom = artiste.Nom;
- stored.Biographie = artiste.Biographie;
- stored.Titres = artiste.Titres;
+ existingArtiste.Nom = artiste.Nom;
+ existingArtiste.Biographie = artiste.Biographie;
+ existingArtiste.Titres = artiste.Titres;
}
///
diff --git a/Webzine.Repository/LocalCommentaireRepository.cs b/Webzine.Repository/LocalCommentaireRepository.cs
index 98eea4d..4e2ad79 100644
--- a/Webzine.Repository/LocalCommentaireRepository.cs
+++ b/Webzine.Repository/LocalCommentaireRepository.cs
@@ -47,7 +47,7 @@ namespace Webzine.Repository
///
public Commentaire Find(int idCommentaire)
{
- return this.dataStore.Commentaires.FirstOrDefault(c => c.IdCommentaire == idCommentaire);
+ return this.dataStore.Commentaires.SingleOrDefault(c => c.IdCommentaire == idCommentaire);
}
///
@@ -59,13 +59,12 @@ namespace Webzine.Repository
}
///
- public IEnumerable Paginate(int offset, int limit)
+ public IEnumerable FindCommentaires(int offset, int limit)
{
return this.dataStore.Commentaires
.OrderByDescending(c => c.DateCreation)
.Skip(offset)
- .Take(limit)
- .ToList();
+ .Take(limit);
}
}
}
\ No newline at end of file
diff --git a/Webzine.Repository/LocalStyleRepository.cs b/Webzine.Repository/LocalStyleRepository.cs
index 6fbcc41..ed26315 100644
--- a/Webzine.Repository/LocalStyleRepository.cs
+++ b/Webzine.Repository/LocalStyleRepository.cs
@@ -55,15 +55,15 @@ public class LocalStyleRepository : IStyleRepository
///
public void Update(Style style)
{
- var stored = this.dataStore.Styles.FirstOrDefault(s => s.IdStyle == style.IdStyle);
- if (stored == null)
+ Style existingStyle = this.Find(style.IdStyle);
+ if (existingStyle == null)
{
this.logger.LogWarning("Style with id {IdStyle} not found for update.", style.IdStyle);
return;
}
- stored.Libelle = style.Libelle;
- stored.Titres = style.Titres;
+ existingStyle.Libelle = style.Libelle;
+ existingStyle.Titres = style.Titres;
}
///
diff --git a/Webzine.Repository/LocalTitreRepository.cs b/Webzine.Repository/LocalTitreRepository.cs
index b414a62..de237ef 100644
--- a/Webzine.Repository/LocalTitreRepository.cs
+++ b/Webzine.Repository/LocalTitreRepository.cs
@@ -96,7 +96,7 @@ public class LocalTitreRepository : ITitreRepository
public Titre Find(int idTitre)
{
return this.dataStore.Titres
- .FirstOrDefault(t => t.IdTitre == idTitre);
+ .SingleOrDefault(t => t.IdTitre == idTitre);
}
///
@@ -115,18 +115,20 @@ public class LocalTitreRepository : ITitreRepository
///
public void Update(Titre titre)
{
- var stored = this.dataStore.Titres.FirstOrDefault(t => t.IdTitre == titre.IdTitre);
- if (stored == null)
+ // 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;
}
- stored.Libelle = titre.Libelle;
- stored.DateCreation = titre.DateCreation;
- stored.NbLectures = titre.NbLectures;
- stored.NbLikes = titre.NbLikes;
- stored.IdArtiste = titre.IdArtiste;
- stored.Styles = titre.Styles;
+ existingTitre.Libelle = titre.Libelle;
+ existingTitre.DateCreation = titre.DateCreation;
+ existingTitre.NbLectures = titre.NbLectures;
+ existingTitre.NbLikes = titre.NbLikes;
+ existingTitre.IdArtiste = titre.IdArtiste;
+ existingTitre.Styles = titre.Styles;
}
}
\ No newline at end of file