refactor: implement DbTitreRepository methods for database operations and logging
This commit is contained in:
@@ -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<DbTitreRepository> logger;
|
||||
private readonly WebzineDbContext context;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="DbTitreRepository"/> class.
|
||||
/// </summary>
|
||||
/// <param name="logger">Le service de journalisation injecté pour suivre les opérations du repository.</param>
|
||||
public DbTitreRepository(ILogger<DbTitreRepository> logger)
|
||||
/// <param name="context">Le contexte de base de données injecté.</param>
|
||||
public DbTitreRepository(ILogger<DbTitreRepository> 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
|
||||
/// <param name="titre">L'objet titre à ajouter.</param>
|
||||
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}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remonte le nombre de titres.
|
||||
/// </summary>
|
||||
/// <returns>Le nombre total de titres présents dans la base de données après l'incrémentation du nombre de lectures.</returns>
|
||||
/// <returns>Le nombre total de titres présents dans la base de données.</returns>
|
||||
public int Count()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var count = this.context.Titres.Count();
|
||||
this.logger.LogDebug($"Total titres count: {count}");
|
||||
return count;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -45,7 +54,10 @@ public class DbTitreRepository : ITitreRepository
|
||||
/// <param name="titre">L'objet titre à supprimer.</param>
|
||||
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}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -56,7 +68,18 @@ public class DbTitreRepository : ITitreRepository
|
||||
/// <returns>Une collection de titres correspondant au critère de recherche, triée par libellé.</returns>
|
||||
public IEnumerable<Titre> 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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -65,7 +88,19 @@ public class DbTitreRepository : ITitreRepository
|
||||
/// <param name="titre">L'objet titre pour lequel le nombre de lectures doit être incrémenté.</param>
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -74,7 +109,19 @@ public class DbTitreRepository : ITitreRepository
|
||||
/// <param name="titre">L'objet titre pour lequel le nombre de likes doit être incrémenté.</param>
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -83,7 +130,28 @@ public class DbTitreRepository : ITitreRepository
|
||||
/// <param name="titre">L'objet titre à mettre à jour.</param>
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -93,7 +161,17 @@ public class DbTitreRepository : ITitreRepository
|
||||
/// <returns>Une collection de titres correspondant au critère de recherche, triée par libellé.</returns>
|
||||
public IEnumerable<Titre> 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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -103,7 +181,24 @@ public class DbTitreRepository : ITitreRepository
|
||||
/// <returns>Le titre correspondant à l'identifiant fourni, ou null si aucun titre n'est trouvé.</returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -112,7 +207,16 @@ public class DbTitreRepository : ITitreRepository
|
||||
/// <returns>Une collection de tous les titres présents dans la base de données.</returns>
|
||||
public IEnumerable<Titre> 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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -122,6 +226,16 @@ public class DbTitreRepository : ITitreRepository
|
||||
/// <returns>Une collection de titres correspondant au critère de recherche, triée par libellé.</returns>
|
||||
public IEnumerable<Titre> 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user