feat: implémentation TitreAdminService pour la gestion des titres et ajout TitreAdminModèle de commande
This commit is contained in:
130
Webzine.Business/TitreAdminService.cs
Normal file
130
Webzine.Business/TitreAdminService.cs
Normal file
@@ -0,0 +1,130 @@
|
||||
namespace Webzine.Business;
|
||||
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
using Webzine.Business.Contracts;
|
||||
using Webzine.Business.Contracts.Models;
|
||||
using Webzine.Entity;
|
||||
using Webzine.Repository.Contracts;
|
||||
|
||||
/// <summary>
|
||||
/// Implémentation de <see cref="ITitreAdminService"/>.
|
||||
/// Orchestre la résolution des styles, la construction de l'entité
|
||||
/// et la délégation au repository.
|
||||
/// </summary>
|
||||
public class TitreAdminService : ITitreAdminService
|
||||
{
|
||||
private readonly ITitreRepository titreRepository;
|
||||
private readonly IArtisteRepository artisteRepository;
|
||||
private readonly IStyleRepository styleRepository;
|
||||
private readonly ILogger<TitreAdminService> logger;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="TitreAdminService"/> class.
|
||||
/// </summary>
|
||||
/// <param name="titreRepository">Repository des titres.</param>
|
||||
/// <param name="artisteRepository">Repository des artistes.</param>
|
||||
/// <param name="styleRepository">Repository des styles.</param>
|
||||
/// <param name="logger">Service de journalisation.</param>
|
||||
public TitreAdminService(
|
||||
ITitreRepository titreRepository,
|
||||
IArtisteRepository artisteRepository,
|
||||
IStyleRepository styleRepository,
|
||||
ILogger<TitreAdminService> logger)
|
||||
{
|
||||
this.titreRepository = titreRepository;
|
||||
this.artisteRepository = artisteRepository;
|
||||
this.styleRepository = styleRepository;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void CreerTitre(TitreAdminCommande commande)
|
||||
{
|
||||
this.logger.LogInformation(
|
||||
"Création d'un nouveau titre '{Libelle}' pour l'artiste ID {IdArtiste}.",
|
||||
commande.Libelle,
|
||||
commande.IdArtiste);
|
||||
|
||||
Artiste artiste = this.artisteRepository.Find(commande.IdArtiste);
|
||||
List<Style> styles = this.ResoudreStyles(commande.Styles);
|
||||
|
||||
var titre = new Titre
|
||||
{
|
||||
IdArtiste = artiste.IdArtiste,
|
||||
Artiste = artiste,
|
||||
Libelle = commande.Libelle,
|
||||
Album = commande.Album,
|
||||
Chronique = commande.Chronique,
|
||||
DateCreation = DateTime.UtcNow,
|
||||
DateSortie = commande.DateSortie,
|
||||
Duree = commande.Duree,
|
||||
UrlJaquette = commande.UrlJaquette,
|
||||
UrlEcoute = commande.UrlEcoute ?? string.Empty,
|
||||
Styles = styles,
|
||||
Commentaires = new List<Commentaire>(),
|
||||
};
|
||||
|
||||
this.titreRepository.Add(titre);
|
||||
|
||||
this.logger.LogInformation("Titre '{Libelle}' créé avec succès (ID {IdTitre}).", titre.Libelle, titre.IdTitre);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void ModifierTitre(TitreAdminCommande commande)
|
||||
{
|
||||
this.logger.LogInformation("Modification du titre ID {Id} ('{Libelle}').", commande.Id, commande.Libelle);
|
||||
|
||||
List<Style> styles = this.ResoudreStyles(commande.Styles);
|
||||
|
||||
// On charge le titre existant pour ne pas écraser NbLectures / NbLikes
|
||||
Titre existant = this.titreRepository.Find(commande.Id);
|
||||
|
||||
existant.IdArtiste = commande.IdArtiste;
|
||||
existant.Libelle = commande.Libelle;
|
||||
existant.Album = commande.Album;
|
||||
existant.Chronique = commande.Chronique;
|
||||
existant.DateSortie = commande.DateSortie;
|
||||
existant.Duree = commande.Duree;
|
||||
existant.UrlJaquette = commande.UrlJaquette;
|
||||
existant.UrlEcoute = commande.UrlEcoute ?? string.Empty;
|
||||
|
||||
existant.Styles.Clear();
|
||||
foreach (var style in styles)
|
||||
{
|
||||
existant.Styles.Add(style);
|
||||
}
|
||||
|
||||
this.titreRepository.Update(existant);
|
||||
|
||||
this.logger.LogInformation("Titre ID {Id} modifié avec succès.", commande.Id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Résout les entités <see cref="Style"/> à partir d'une liste d'identifiants.
|
||||
/// Les identifiants introuvables sont ignorés avec un avertissement.
|
||||
/// </summary>
|
||||
/// <param name="styleIds">Identifiants des styles sélectionnés.</param>
|
||||
/// <returns>Liste des styles résolus.</returns>
|
||||
private List<Style> ResoudreStyles(List<int> styleIds)
|
||||
{
|
||||
var styles = new List<Style>();
|
||||
|
||||
foreach (int id in styleIds)
|
||||
{
|
||||
Style style = this.styleRepository.Find(id);
|
||||
|
||||
if (style.IdStyle == 0)
|
||||
{
|
||||
this.logger.LogWarning("Style ID {IdStyle} introuvable — ignoré lors de la résolution.", id);
|
||||
continue;
|
||||
}
|
||||
|
||||
styles.Add(style);
|
||||
}
|
||||
|
||||
this.logger.LogDebug("{NbResolus}/{NbDemandes} styles résolus avec succès.", styles.Count, styleIds.Count);
|
||||
|
||||
return styles;
|
||||
}
|
||||
}
|
||||
@@ -21,4 +21,15 @@
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Webzine.Business.Contracts\Webzine.Business.Contracts.csproj" />
|
||||
<ProjectReference Include="..\Webzine.Repository.Contracts\Webzine.Repository.Contracts.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Reference Include="Microsoft.Extensions.Logging.Abstractions">
|
||||
<HintPath>..\..\..\..\..\..\..\.nuget\packages\microsoft.extensions.logging.abstractions\10.0.5\lib\net10.0\Microsoft.Extensions.Logging.Abstractions.dll</HintPath>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user