124 lines
4.4 KiB
C#
124 lines
4.4 KiB
C#
namespace Webzine.Business;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using Webzine.Business.Contracts;
|
|
using Webzine.Business.Contracts.Dto;
|
|
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(TitreAdminDTO 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 = DateTime.SpecifyKind(commande.DateSortie, DateTimeKind.Utc),
|
|
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(TitreAdminDTO 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 = DateTime.SpecifyKind(commande.DateSortie, DateTimeKind.Utc);
|
|
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);
|
|
|
|
styles.Add(style);
|
|
}
|
|
|
|
this.logger.LogDebug("{NbResolus}/{NbDemandes} styles résolus avec succès.", styles.Count, styleIds.Count);
|
|
|
|
return styles;
|
|
}
|
|
} |