From 9e4b0e817db841fb0959612fc29932d27e6a9a20 Mon Sep 17 00:00:00 2001 From: mirage <119869686+ClementBobin@users.noreply.github.com> Date: Wed, 25 Mar 2026 14:10:23 +0100 Subject: [PATCH] refactor: implement DbTitreRepository methods for database operations and logging --- Webzine.Repository/DbTitreRepository.cs | 140 +++++++++++++++++++++--- 1 file changed, 127 insertions(+), 13 deletions(-) diff --git a/Webzine.Repository/DbTitreRepository.cs b/Webzine.Repository/DbTitreRepository.cs index df9d069..86f3fe9 100644 --- a/Webzine.Repository/DbTitreRepository.cs +++ b/Webzine.Repository/DbTitreRepository.cs @@ -1,3 +1,4 @@ +using Webzine.EntitiesContext; using Microsoft.Extensions.Logging; using Webzine.Entity; using Webzine.Repository.Contracts; @@ -10,14 +11,17 @@ namespace Webzine.Repository; public class DbTitreRepository : ITitreRepository { private readonly ILogger logger; + private readonly WebzineDbContext context; /// /// Initializes a new instance of the class. /// /// Le service de journalisation injecté pour suivre les opérations du repository. - public DbTitreRepository(ILogger logger) + /// Le contexte de base de données injecté. + public DbTitreRepository(ILogger logger, WebzineDbContext context) { this.logger = logger; + this.context = context; this.logger.LogDebug(1, "NLog injected into DbTitreRepository"); } @@ -27,16 +31,21 @@ public class DbTitreRepository : ITitreRepository /// L'objet titre à ajouter. public void Add(Titre titre) { - throw new NotImplementedException(); + this.logger.LogInformation($"Adding new titre: {titre.Libelle}"); + this.context.Titres.Add(titre); + this.context.SaveChanges(); + this.logger.LogDebug($"Titre added with Id: {titre.IdTitre}"); } /// /// Remonte le nombre de titres. /// - /// Le nombre total de titres présents dans la base de données après l'incrémentation du nombre de lectures. + /// Le nombre total de titres présents dans la base de données. public int Count() { - throw new NotImplementedException(); + var count = this.context.Titres.Count(); + this.logger.LogDebug($"Total titres count: {count}"); + return count; } /// @@ -45,7 +54,10 @@ public class DbTitreRepository : ITitreRepository /// L'objet titre à supprimer. public void Delete(Titre titre) { - throw new NotImplementedException(); + this.logger.LogInformation($"Deleting titre with Id: {titre.IdTitre}"); + this.context.Titres.Remove(titre); + this.context.SaveChanges(); + this.logger.LogDebug($"Titre deleted: {titre.IdTitre}"); } /// @@ -56,7 +68,18 @@ public class DbTitreRepository : ITitreRepository /// Une collection de titres correspondant au critère de recherche, triée par libellé. public IEnumerable FindTitres(int offset, int limit) { - throw new NotImplementedException(); + this.logger.LogDebug($"Finding titres with offset: {offset}, limit: {limit}"); + + var titres = this.context.Titres + .Include(t => t.Artiste) + .Include(t => t.Styles) + .OrderBy(t => t.Libelle) + .Skip(offset) + .Take(limit) + .ToList(); + + this.logger.LogDebug($"Found {titres.Count} titres"); + return titres; } /// @@ -65,7 +88,19 @@ public class DbTitreRepository : ITitreRepository /// L'objet titre pour lequel le nombre de lectures doit être incrémenté. public void IncrementNbLectures(Titre titre) { - throw new NotImplementedException(); + this.logger.LogInformation($"Incrementing lecture count for titre Id: {titre.IdTitre}"); + + var existingTitre = this.context.Titres.Find(titre.IdTitre); + if (existingTitre != null) + { + existingTitre.NbLectures++; + this.context.SaveChanges(); + this.logger.LogDebug($"New lecture count: {existingTitre.NbLectures}"); + } + else + { + this.logger.LogWarning($"Titre with Id {titre.IdTitre} not found for incrementing lectures"); + } } /// @@ -74,7 +109,19 @@ public class DbTitreRepository : ITitreRepository /// L'objet titre pour lequel le nombre de likes doit être incrémenté. public void IncrementNbLikes(Titre titre) { - throw new NotImplementedException(); + this.logger.LogInformation($"Incrementing like count for titre Id: {titre.IdTitre}"); + + var existingTitre = this.context.Titres.Find(titre.IdTitre); + if (existingTitre != null) + { + existingTitre.NbLikes++; + this.context.SaveChanges(); + this.logger.LogDebug($"New like count: {existingTitre.NbLikes}"); + } + else + { + this.logger.LogWarning($"Titre with Id {titre.IdTitre} not found for incrementing likes"); + } } /// @@ -83,7 +130,28 @@ public class DbTitreRepository : ITitreRepository /// L'objet titre à mettre à jour. public void Update(Titre titre) { - throw new NotImplementedException(); + this.logger.LogInformation($"Updating titre with Id: {titre.IdTitre}"); + + var existingTitre = this.context.Titres.Find(titre.IdTitre); + if (existingTitre != null) + { + this.context.Entry(existingTitre).CurrentValues.SetValues(titre); + + // Handle many-to-many relationships + this.context.Entry(existingTitre).Collection(t => t.Styles).Load(); + existingTitre.Styles.Clear(); + foreach (var style in titre.Styles) + { + existingTitre.Styles.Add(style); + } + + this.context.SaveChanges(); + this.logger.LogDebug($"Titre updated: {titre.IdTitre}"); + } + else + { + this.logger.LogWarning($"Titre with Id {titre.IdTitre} not found for update"); + } } /// @@ -93,7 +161,17 @@ public class DbTitreRepository : ITitreRepository /// Une collection de titres correspondant au critère de recherche, triée par libellé. public IEnumerable Search(string mot) { - throw new NotImplementedException(); + this.logger.LogInformation($"Searching titres with keyword: {mot}"); + + var titres = this.context.Titres + .Include(t => t.Artiste) + .Include(t => t.Styles) + .Where(t => t.Libelle.ToLower().Contains(mot.ToLower())) + .OrderBy(t => t.Libelle) + .ToList(); + + this.logger.LogDebug($"Found {titres.Count} titres matching '{mot}'"); + return titres; } /// @@ -103,7 +181,24 @@ public class DbTitreRepository : ITitreRepository /// Le titre correspondant à l'identifiant fourni, ou null si aucun titre n'est trouvé. public Titre? Find(int idTitre) { - throw new NotImplementedException(); + this.logger.LogDebug($"Finding titre with Id: {idTitre}"); + + var titre = this.context.Titres + .Include(t => t.Artiste) + .Include(t => t.Styles) + .Include(t => t.Commentaires) + .FirstOrDefault(t => t.IdTitre == idTitre); + + if (titre == null) + { + this.logger.LogWarning($"Titre with Id {idTitre} not found"); + } + else + { + this.logger.LogDebug($"Found titre: {titre.Libelle}"); + } + + return titre; } /// @@ -112,7 +207,16 @@ public class DbTitreRepository : ITitreRepository /// Une collection de tous les titres présents dans la base de données. public IEnumerable FindAll() { - throw new NotImplementedException(); + this.logger.LogDebug("Finding all titres"); + + var titres = this.context.Titres + .Include(t => t.Artiste) + .Include(t => t.Styles) + .OrderBy(t => t.Libelle) + .ToList(); + + this.logger.LogDebug($"Found {titres.Count} total titres"); + return titres; } /// @@ -122,6 +226,16 @@ public class DbTitreRepository : ITitreRepository /// Une collection de titres correspondant au critère de recherche, triée par libellé. public IEnumerable SearchByStyle(string libelle) { - throw new NotImplementedException(); + this.logger.LogInformation($"Searching titres by style: {libelle}"); + + 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) + .ToList(); + + this.logger.LogDebug($"Found {titres.Count} titres for style '{libelle}'"); + return titres; } } \ No newline at end of file