Refacto StyleCop
This commit is contained in:
@@ -1,15 +0,0 @@
|
||||
// <copyright file="ArtisteRepository.cs" company="PlaceholderCompany">
|
||||
// Copyright (c) PlaceholderCompany. All rights reserved.
|
||||
// </copyright>
|
||||
|
||||
namespace Webzine.Repository
|
||||
{
|
||||
using Webzine.Repository.Contracts;
|
||||
/// <summary>
|
||||
/// Implémentation de l'interface IArtisteRepository.
|
||||
/// </summary>
|
||||
public class ArtisteRepository : IArtisteRepository
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,8 @@
|
||||
namespace Webzine.Repository
|
||||
// <copyright file="DbArtisteRepository.cs" company="PlaceholderCompany">
|
||||
// Copyright (c) PlaceholderCompany. All rights reserved.
|
||||
// </copyright>
|
||||
|
||||
namespace Webzine.Repository
|
||||
{
|
||||
using System.Data.Common;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
@@ -13,18 +17,18 @@
|
||||
/// </summary>
|
||||
public class DbArtisteRepository : IArtisteRepository
|
||||
{
|
||||
private WebzineDbContext _context;
|
||||
private readonly ILogger<LocalArtisteRepository> _logger;
|
||||
private WebzineDbContext context;
|
||||
private readonly ILogger<LocalArtisteRepository> logger;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="DbArtisteRepository"/> class.
|
||||
/// </summary>
|
||||
/// <param name="context">Le contexte de base de données à utiliser pour accéder aux entités et effectuer des opérations de
|
||||
/// persistance. Ne peut pas être null.</param>
|
||||
public DbArtisteRepository(WebzineDbContext context, ILogger<LocalArtisteRepository> logger)
|
||||
public DbArtisteRepository(WebzineDbContext context, ILogger<LocalArtisteRepository> logger)
|
||||
{
|
||||
this._logger = logger;
|
||||
this._context = context;
|
||||
this.logger = logger;
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
@@ -32,17 +36,17 @@
|
||||
{
|
||||
try
|
||||
{
|
||||
this._context.Artistes.Add(artiste);
|
||||
this._context.SaveChanges();
|
||||
this.context.Artistes.Add(artiste);
|
||||
this.context.SaveChanges();
|
||||
}
|
||||
catch (DbUpdateException dbex)
|
||||
{
|
||||
this._logger.LogError(dbex, "Erreur de base de données lors de l'ajout de l'artiste: {id}", artiste.IdArtiste);
|
||||
this.logger.LogError(dbex, "Erreur de base de données lors de l'ajout de l'artiste: {id}", artiste.IdArtiste);
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this._logger.LogError(ex, "Une erreur est survenue lors de l'ajout de l'artiste {Nom}.", artiste?.Nom);
|
||||
this.logger.LogError(ex, "Une erreur est survenue lors de l'ajout de l'artiste {Nom}.", artiste?.Nom);
|
||||
throw new Exception("Une erreur est survenue lors de l'ajout de l'artiste.", ex);
|
||||
}
|
||||
}
|
||||
@@ -57,17 +61,17 @@
|
||||
throw new ArgumentNullException(nameof(artiste), "L'artiste à supprimer ne peut pas être null.");
|
||||
}
|
||||
|
||||
this._context.Artistes.Remove(artiste);
|
||||
this._context.SaveChanges();
|
||||
this.context.Artistes.Remove(artiste);
|
||||
this.context.SaveChanges();
|
||||
}
|
||||
catch (DbUpdateException dbex)
|
||||
{
|
||||
this._logger.LogError(dbex, "Erreur de base de données lors de la suppression de l'artiste: {Id}", artiste.IdArtiste);
|
||||
this.logger.LogError(dbex, "Erreur de base de données lors de la suppression de l'artiste: {Id}", artiste.IdArtiste);
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this._logger.LogError(ex, "Une erreur est survenue lors de la suppression de l'artiste {Nom}.", artiste?.Nom);
|
||||
this.logger.LogError(ex, "Une erreur est survenue lors de la suppression de l'artiste {Nom}.", artiste?.Nom);
|
||||
throw new Exception("Une erreur est survenue lors de la suppression de l'artiste.", ex);
|
||||
}
|
||||
}
|
||||
@@ -77,14 +81,14 @@
|
||||
{
|
||||
try
|
||||
{
|
||||
Artiste artiste = this._context.Artistes
|
||||
Artiste artiste = this.context.Artistes
|
||||
.Include(a => a.Titres)
|
||||
.First(a => a.IdArtiste == id);
|
||||
return artiste;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this._logger.LogError(ex, "Erreur lors de la recherche de l'artiste: {Id}", id);
|
||||
this.logger.LogError(ex, "Erreur lors de la recherche de l'artiste: {Id}", id);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
@@ -94,20 +98,21 @@
|
||||
{
|
||||
try
|
||||
{
|
||||
var artiste = this._context.Artistes
|
||||
.Include(a => a.Titres)
|
||||
.FirstOrDefault(a => a.Nom == nom);
|
||||
var artiste = this.context.Artistes
|
||||
.Include(a => a.Titres)
|
||||
.FirstOrDefault(a => a.Nom == nom);
|
||||
|
||||
if (artiste == null)
|
||||
if (artiste == null)
|
||||
{
|
||||
this._logger.LogWarning("Pas d'artiste au nom {Nom}", nom);
|
||||
this.logger.LogWarning("Pas d'artiste au nom {Nom}", nom);
|
||||
artiste = new Artiste();
|
||||
}
|
||||
return artiste;
|
||||
|
||||
return artiste;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this._logger.LogError(ex, "Erreur lors de la recherche de l'artiste avec le nom: {Nom}", nom);
|
||||
this.logger.LogError(ex, "Erreur lors de la recherche de l'artiste avec le nom: {Nom}", nom);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
@@ -118,13 +123,13 @@
|
||||
try
|
||||
{
|
||||
// .AsNoTracking() rend la requête beaucoup plus rapide pour de la lecture
|
||||
var artistes = this._context.Artistes.AsNoTracking().ToList();
|
||||
this._logger.LogInformation("{Count} artistes récupérés de la base.", artistes.Count);
|
||||
var artistes = this.context.Artistes.AsNoTracking().ToList();
|
||||
this.logger.LogInformation("{Count} artistes récupérés de la base.", artistes.Count);
|
||||
return artistes;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this._logger.LogError(ex, "Erreur lors de la récupération de tous les artistes.");
|
||||
this.logger.LogError(ex, "Erreur lors de la récupération de tous les artistes.");
|
||||
return Enumerable.Empty<Artiste>(); // Retourne une liste vide au lieu de faire crash l'UI
|
||||
}
|
||||
}
|
||||
@@ -139,18 +144,18 @@
|
||||
|
||||
try
|
||||
{
|
||||
this._context.Artistes.Update(artiste);
|
||||
this._context.SaveChanges();
|
||||
this._logger.LogInformation("Artiste {Id} ({Nom}) mis à jour avec succès.", artiste.IdArtiste, artiste.Nom);
|
||||
this.context.Artistes.Update(artiste);
|
||||
this.context.SaveChanges();
|
||||
this.logger.LogInformation("Artiste {Id} ({Nom}) mis à jour avec succès.", artiste.IdArtiste, artiste.Nom);
|
||||
}
|
||||
catch (DbUpdateException ex)
|
||||
{
|
||||
this._logger.LogError(ex, "Erreur de base de données lors de la mise à jour de l'artiste ID: {IdArtiste}", artiste.IdArtiste);
|
||||
this.logger.LogError(ex, "Erreur de base de données lors de la mise à jour de l'artiste ID: {IdArtiste}", artiste.IdArtiste);
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this._logger.LogError(ex, "Erreur lors de la mise à jour de l'artiste {Id}.", artiste.IdArtiste);
|
||||
this.logger.LogError(ex, "Erreur lors de la mise à jour de l'artiste {Id}.", artiste.IdArtiste);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
namespace Webzine.Repository
|
||||
// <copyright file="LocalArtisteRepository.cs" company="PlaceholderCompany">
|
||||
// Copyright (c) PlaceholderCompany. All rights reserved.
|
||||
// </copyright>
|
||||
|
||||
namespace Webzine.Repository
|
||||
{
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Webzine.Entity;
|
||||
@@ -10,8 +14,8 @@
|
||||
/// </summary>
|
||||
public class LocalArtisteRepository : IArtisteRepository
|
||||
{
|
||||
private readonly ILogger<LocalArtisteRepository> _logger;
|
||||
private readonly List<Artiste> _artistes;
|
||||
private readonly ILogger<LocalArtisteRepository> logger;
|
||||
private readonly List<Artiste> artistes;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="LocalArtisteRepository"/> class.
|
||||
@@ -21,8 +25,8 @@
|
||||
/// <param name="logger">Le logger à utiliser pour enregistrer les messages de journalisation. Ne peut pas être null.</param>
|
||||
public LocalArtisteRepository(List<Artiste> artistes, ILogger<LocalArtisteRepository> logger)
|
||||
{
|
||||
this._logger = logger;
|
||||
this._artistes = artistes;
|
||||
this.logger = logger;
|
||||
this.artistes = artistes;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
@@ -30,18 +34,18 @@
|
||||
{
|
||||
try
|
||||
{
|
||||
if (this._artistes.Any(a => a.IdArtiste == artiste.IdArtiste))
|
||||
if (this.artistes.Any(a => a.IdArtiste == artiste.IdArtiste))
|
||||
{
|
||||
this._logger.LogWarning("Un artiste avec l'ID {Id} existe déjà. L'ajout est ignoré.", artiste.IdArtiste);
|
||||
this.logger.LogWarning("Un artiste avec l'ID {Id} existe déjà. L'ajout est ignoré.", artiste.IdArtiste);
|
||||
return;
|
||||
}
|
||||
|
||||
this._artistes.Add(artiste);
|
||||
this._logger.LogInformation("Artiste ajouté : {Nom}", artiste.Nom);
|
||||
this.artistes.Add(artiste);
|
||||
this.logger.LogInformation("Artiste ajouté : {Nom}", artiste.Nom);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this._logger.LogError(ex, "Erreur lors de l'ajout de l'artiste : {Nom}", artiste?.Nom);
|
||||
this.logger.LogError(ex, "Erreur lors de l'ajout de l'artiste : {Nom}", artiste?.Nom);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
@@ -51,12 +55,12 @@
|
||||
{
|
||||
try
|
||||
{
|
||||
this._artistes.Remove(artiste);
|
||||
this._logger.LogInformation("Artiste supprimé : {Nom}", artiste.Nom);
|
||||
this.artistes.Remove(artiste);
|
||||
this.logger.LogInformation("Artiste supprimé : {Nom}", artiste.Nom);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this._logger.LogError(ex, "Erreur lors de la suppression de l'artiste : {Nom}", artiste.Nom);
|
||||
this.logger.LogError(ex, "Erreur lors de la suppression de l'artiste : {Nom}", artiste.Nom);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
@@ -66,12 +70,12 @@
|
||||
{
|
||||
try
|
||||
{
|
||||
Artiste artiste = this._artistes.First(a => a.IdArtiste == id);
|
||||
Artiste artiste = this.artistes.First(a => a.IdArtiste == id);
|
||||
return artiste;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this._logger.LogError(ex, "Erreur lors de la recherche de l'artiste avec ID: {Id}", id);
|
||||
this.logger.LogError(ex, "Erreur lors de la recherche de l'artiste avec ID: {Id}", id);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
@@ -81,22 +85,21 @@
|
||||
{
|
||||
try
|
||||
{
|
||||
Artiste artiste = this._artistes.First(a => a.Nom == nom);
|
||||
Artiste artiste = this.artistes.First(a => a.Nom == nom);
|
||||
return artiste;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this._logger.LogError(ex, "Erreur lors de la recherche de l'artiste avec le nom: {Nom}", nom);
|
||||
throw;
|
||||
this.logger.LogError(ex, "Erreur lors de la recherche de l'artiste avec le nom: {Nom}", nom);
|
||||
throw;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
/// La liste retournée est une copie de la liste interne, donc elle ne peut être nulle.
|
||||
public IEnumerable<Artiste> FindAll()
|
||||
{
|
||||
return this._artistes;
|
||||
return this.artistes;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
@@ -104,7 +107,7 @@
|
||||
{
|
||||
try
|
||||
{
|
||||
var artisteToUpdate = this._artistes.FirstOrDefault(a => a.IdArtiste == artiste.IdArtiste);
|
||||
var artisteToUpdate = this.artistes.FirstOrDefault(a => a.IdArtiste == artiste.IdArtiste);
|
||||
|
||||
if (artisteToUpdate != null)
|
||||
{
|
||||
@@ -112,17 +115,17 @@
|
||||
artisteToUpdate.Biographie = artiste.Biographie;
|
||||
artisteToUpdate.Titres = artiste.Titres;
|
||||
|
||||
this._logger.LogInformation("Artiste {Id} mis à jour avec succès.", artiste.IdArtiste);
|
||||
this.logger.LogInformation("Artiste {Id} mis à jour avec succès.", artiste.IdArtiste);
|
||||
}
|
||||
else
|
||||
{
|
||||
this._logger.LogWarning("Mise à jour impossible : l'artiste avec l'ID {Id} n'existe pas.", artiste.IdArtiste);
|
||||
this.logger.LogWarning("Mise à jour impossible : l'artiste avec l'ID {Id} n'existe pas.", artiste.IdArtiste);
|
||||
throw new KeyNotFoundException($"L'artiste {artiste.IdArtiste} est introuvable.");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this._logger.LogError(ex, "Une erreur est survenue lors de la mise à jour de l'artiste {Id}.", artiste.IdArtiste);
|
||||
this.logger.LogError(ex, "Une erreur est survenue lors de la mise à jour de l'artiste {Id}.", artiste.IdArtiste);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Webzine.Repository
|
||||
{
|
||||
internal class StyleRepository
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -1,104 +0,0 @@
|
||||
// <copyright file="TitreRepository.cs" company="PlaceholderCompany">
|
||||
// Copyright (c) PlaceholderCompany. All rights reserved.
|
||||
// </copyright>
|
||||
|
||||
namespace Webzine.Repository
|
||||
{
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Webzine.EntitiesContext;
|
||||
using Webzine.Entity;
|
||||
using Webzine.Repository.Contracts;
|
||||
|
||||
/// <summary>
|
||||
/// Implémentation de l'interface ITitreRepository.
|
||||
/// </summary>
|
||||
public class TitreRepository : ITitreRepository
|
||||
{
|
||||
private readonly WebzineDbContext context;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="TitreRepository"/> class.
|
||||
/// </summary>
|
||||
public TitreRepository(WebzineDbContext context)
|
||||
{
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Rechercher un titre à l'aide de son nom.
|
||||
/// </summary>
|
||||
/// <param name="mot">Nom de la musique.</param>
|
||||
/// <returns>IEnumerable Titre.</returns>
|
||||
public IEnumerable<Titre> Search(string mot)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(mot))
|
||||
{
|
||||
return Enumerable.Empty<Titre>();
|
||||
}
|
||||
|
||||
return this.context.Titres
|
||||
.Where(t => !string.IsNullOrWhiteSpace(t.Libelle)
|
||||
&& t.Libelle.ToLower().Contains(mot.ToLower()))
|
||||
.OrderBy(t => t.Libelle)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retourne le titre demandé à partir de son identifiant.
|
||||
/// </summary>
|
||||
/// <param name="idTitre">Id du titre cherché.</param>
|
||||
/// <returns>Un titre.</returns>
|
||||
public Titre? Find(int idTitre)
|
||||
{
|
||||
var find = this.context.Titres
|
||||
.Where(t => t.IdTitre == idTitre)
|
||||
.First();
|
||||
|
||||
return find;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retourne les titres demandés (pour la pagination) triés
|
||||
/// selon la date de création(du plus récent à ancien).
|
||||
/// </summary>
|
||||
/// <returns>IEnumerable de Titre.</returns>
|
||||
public IEnumerable<Titre> FindTitres(
|
||||
int offset,
|
||||
int limit)
|
||||
{
|
||||
return this.context.Titres
|
||||
.OrderByDescending(t => t.DateCreation)
|
||||
.Include(t => t.Artiste)
|
||||
.Skip((offset - 1) * limit)
|
||||
.Take(limit)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retourne tous les titres.
|
||||
/// </summary>
|
||||
/// <returns>Liste de Titre.</returns>
|
||||
public IEnumerable<Titre> FindAll()
|
||||
{
|
||||
return this.context.Titres
|
||||
.AsNoTracking()
|
||||
.Include(t => t.Artiste)
|
||||
.OrderByDescending(t => t.DateCreation)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Trouver les titres correspondant à un style.
|
||||
/// </summary>
|
||||
/// <param name="libelle">Style de musique recherché.</param>
|
||||
/// <returns>IEnumerable Titre.</returns>
|
||||
public IEnumerable<Titre> SearchByStyle(string libelle)
|
||||
{
|
||||
return this.context.Titres
|
||||
.Where(t => t.Styles.Any(s => !string.IsNullOrWhiteSpace(s.Libelle)
|
||||
&& s.Libelle.ToLower().Contains(libelle.ToLower())))
|
||||
.OrderBy(t => t.Libelle)
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user