110 lines
3.5 KiB
C#
110 lines
3.5 KiB
C#
// <copyright file="LocalArtisteRepository.cs" company="PlaceholderCompany">
|
|
// Copyright (c) PlaceholderCompany. All rights reserved.
|
|
// </copyright>
|
|
|
|
namespace Webzine.Repository
|
|
{
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using Webzine.Entity;
|
|
using Webzine.Repository.Contracts;
|
|
|
|
/// <summary>
|
|
/// Initialise une classe <see cref="LocalArtisteRepository"/> qui implémente l'interface <see cref="IArtisteRepository"/>.
|
|
/// Gère les opérations liées aux artistes en utilisant une source de données locale (en mémoire).
|
|
/// </summary>
|
|
public class LocalArtisteRepository : IArtisteRepository
|
|
{
|
|
private readonly ILogger<LocalArtisteRepository> logger;
|
|
|
|
private readonly InMemoryDataStore dataStore;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="LocalArtisteRepository"/> class.
|
|
/// Est liéee à une liste d'artistes en local et utilise un logger pour enregistrer les opérations effectuées sur les artistes.
|
|
/// </summary>
|
|
/// <param name="logger">Le logger à utiliser pour enregistrer les messages de journalisation. Ne peut pas être null.</param>
|
|
/// <param name="dataStore">Le magasin de données en mémoire.</param>
|
|
public LocalArtisteRepository(InMemoryDataStore dataStore, ILogger<LocalArtisteRepository> logger)
|
|
{
|
|
this.logger = logger;
|
|
|
|
// this.artistes = artistes;
|
|
this.dataStore = dataStore;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public void Add(Artiste artiste)
|
|
{
|
|
this.dataStore.Artistes.Add(artiste);
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public void Delete(Artiste artiste)
|
|
{
|
|
this.dataStore.Artistes.Remove(artiste);
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public Artiste Find(int id)
|
|
{
|
|
return this.dataStore.Artistes.SingleOrDefault(a => a.IdArtiste == id);
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public Artiste FindByName(string nom)
|
|
{
|
|
var artiste = this.dataStore.Artistes.FirstOrDefault(a => a.Nom == nom);
|
|
|
|
if (artiste != null)
|
|
{
|
|
artiste.Titres = this.dataStore.Titres
|
|
.Where(t => t.IdArtiste == artiste.IdArtiste)
|
|
.ToList();
|
|
}
|
|
|
|
return artiste;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public IEnumerable<Artiste> FindAll()
|
|
{
|
|
return this.dataStore.Artistes;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public void Update(Artiste artiste)
|
|
{
|
|
var stored = this.dataStore.Artistes.FirstOrDefault(a => a.IdArtiste == artiste.IdArtiste);
|
|
if (stored == null)
|
|
{
|
|
this.logger.LogWarning("L'artiste {Id} n'a pas été trouvé pour l'update.", artiste.IdArtiste);
|
|
return;
|
|
}
|
|
|
|
stored.Nom = artiste.Nom;
|
|
stored.Biographie = artiste.Biographie;
|
|
stored.Titres = artiste.Titres;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public IEnumerable<Artiste> Search(string mot)
|
|
{
|
|
return this.dataStore.Artistes
|
|
.Where(a => a.Nom.ToLower().Contains(mot.ToLower()))
|
|
.ToList();
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public int Count()
|
|
{
|
|
return this.dataStore.Artistes.Count;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public int Count(Func<Artiste, bool> predicate)
|
|
{
|
|
return this.dataStore.Artistes.Count(predicate);
|
|
}
|
|
}
|
|
} |