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 Microsoft.Extensions.Logging;
|
||||||
using Webzine.Entity;
|
using Webzine.Entity;
|
||||||
using Webzine.Repository.Contracts;
|
using Webzine.Repository.Contracts;
|
||||||
@@ -10,14 +11,17 @@ namespace Webzine.Repository;
|
|||||||
public class DbTitreRepository : ITitreRepository
|
public class DbTitreRepository : ITitreRepository
|
||||||
{
|
{
|
||||||
private readonly ILogger<DbTitreRepository> logger;
|
private readonly ILogger<DbTitreRepository> logger;
|
||||||
|
private readonly WebzineDbContext context;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="DbTitreRepository"/> class.
|
/// Initializes a new instance of the <see cref="DbTitreRepository"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="logger">Le service de journalisation injecté pour suivre les opérations du repository.</param>
|
/// <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.logger = logger;
|
||||||
|
this.context = context;
|
||||||
this.logger.LogDebug(1, "NLog injected into DbTitreRepository");
|
this.logger.LogDebug(1, "NLog injected into DbTitreRepository");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,16 +31,21 @@ public class DbTitreRepository : ITitreRepository
|
|||||||
/// <param name="titre">L'objet titre à ajouter.</param>
|
/// <param name="titre">L'objet titre à ajouter.</param>
|
||||||
public void Add(Titre titre)
|
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>
|
/// <summary>
|
||||||
/// Remonte le nombre de titres.
|
/// Remonte le nombre de titres.
|
||||||
/// </summary>
|
/// </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()
|
public int Count()
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
var count = this.context.Titres.Count();
|
||||||
|
this.logger.LogDebug($"Total titres count: {count}");
|
||||||
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -45,7 +54,10 @@ public class DbTitreRepository : ITitreRepository
|
|||||||
/// <param name="titre">L'objet titre à supprimer.</param>
|
/// <param name="titre">L'objet titre à supprimer.</param>
|
||||||
public void Delete(Titre titre)
|
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>
|
/// <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>
|
/// <returns>Une collection de titres correspondant au critère de recherche, triée par libellé.</returns>
|
||||||
public IEnumerable<Titre> FindTitres(int offset, int limit)
|
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>
|
/// <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>
|
/// <param name="titre">L'objet titre pour lequel le nombre de lectures doit être incrémenté.</param>
|
||||||
public void IncrementNbLectures(Titre titre)
|
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>
|
/// <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>
|
/// <param name="titre">L'objet titre pour lequel le nombre de likes doit être incrémenté.</param>
|
||||||
public void IncrementNbLikes(Titre titre)
|
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>
|
/// <summary>
|
||||||
@@ -83,7 +130,28 @@ public class DbTitreRepository : ITitreRepository
|
|||||||
/// <param name="titre">L'objet titre à mettre à jour.</param>
|
/// <param name="titre">L'objet titre à mettre à jour.</param>
|
||||||
public void Update(Titre titre)
|
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>
|
/// <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>
|
/// <returns>Une collection de titres correspondant au critère de recherche, triée par libellé.</returns>
|
||||||
public IEnumerable<Titre> Search(string mot)
|
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>
|
/// <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>
|
/// <returns>Le titre correspondant à l'identifiant fourni, ou null si aucun titre n'est trouvé.</returns>
|
||||||
public Titre? Find(int idTitre)
|
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>
|
/// <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>
|
/// <returns>Une collection de tous les titres présents dans la base de données.</returns>
|
||||||
public IEnumerable<Titre> FindAll()
|
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>
|
/// <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>
|
/// <returns>Une collection de titres correspondant au critère de recherche, triée par libellé.</returns>
|
||||||
public IEnumerable<Titre> SearchByStyle(string libelle)
|
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