diff --git a/Controllers/FormationsApi.cs b/Controllers/FormationsApi.cs index 8170e30..6c58dde 100644 --- a/Controllers/FormationsApi.cs +++ b/Controllers/FormationsApi.cs @@ -18,6 +18,9 @@ using IO.Swagger.DTO; using EPAServeur.IServices; using System.Linq; using System.Threading.Tasks; +using Microsoft.Extensions.Logging; +using EPAServeur.Exceptions; +using Microsoft.EntityFrameworkCore; namespace IO.Swagger.Controllers { @@ -28,10 +31,12 @@ namespace IO.Swagger.Controllers public class FormationsApiController : ControllerBase { private readonly IFormationService formationService; + private readonly ILogger logger; - public FormationsApiController(IFormationService _formationService) + public FormationsApiController(IFormationService _formationService, ILogger _logger) { formationService = _formationService; + logger = _logger; } /// @@ -49,7 +54,28 @@ namespace IO.Swagger.Controllers [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] public virtual async Task AjouterFormation([FromBody] FormationDTO body) { - FormationDTO nouvelleFormation = await formationService.AddFormationAsync(body); + logger.LogInformation("Ajout d'une nouvelle formation."); + + FormationDTO nouvelleFormation = null; + + try + { + nouvelleFormation = await formationService.AddFormationAsync(body); + } + catch (FormationInvalidException) + { + logger.LogWarning("Des données sont manquants, la nouvelle formation ne peut pas être ajoutée."); + } + catch (DbUpdateException) + { + logger.LogError("Une erreur est survenue dans la base de données durant l'ajout de la nouvelle formation."); + } + catch (Exception) + { + logger.LogError("Une erreur inconnue est survenue lors de l'ajout de la formation note."); + } + + logger.LogInformation("Nouvelle formation ajoutée."); return Created("", nouvelleFormation); } @@ -69,15 +95,38 @@ namespace IO.Swagger.Controllers [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] public virtual async Task DeleteFormation([FromRoute][Required] long? idFormation) { - if ( ! await formationService.DeleteFormationByIdAsync(idFormation)) + try { + logger.LogInformation("Suppresion de la formation {idFormation}.", idFormation); + + FormationDTO formation = await formationService.DeleteFormationByIdAsync(idFormation); + } + catch (FormationNotFoundException) + { + logger.LogError("Impossible de supprimer la formation d'id {idFormation} car elle n'existe pas.", idFormation); + ErreurDTO erreur = new ErreurDTO() { Code = "404", Message = "Aucune formation trouvée" }; + return NotFound(erreur); } + catch (DbUpdateConcurrencyException) + { + logger.LogWarning("La formation {idFormation} n'a pas pu être supprimée car elle est prise par une autre ressource.", idFormation); + } + catch (DbUpdateException) + { + logger.LogError("Une erreur a eu lieu, la formation {idFormation} n'a pas pu être supprimée.", idFormation); + } + catch (Exception) + { + logger.LogError("Une erreur inconnue est survenue lors de la suppression de la formation {idFormation}.", idFormation); + } + + logger.LogInformation("Formation {idFormation} supprimée avec succès.", idFormation); return NoContent(); } @@ -105,8 +154,19 @@ namespace IO.Swagger.Controllers [SwaggerResponse(statusCode: 404, type: typeof(ErreurDTO), description: "Ressource n'a pas été trouvée")] public virtual async Task GetFormationAnnulees([FromQuery][Required()] bool? asc, [FromQuery][Required()] int? numPage, [FromQuery][Required()] int? parPAge, [FromQuery] int? idAgence, [FromQuery] string texte, [FromQuery] string tri) { - //TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - IEnumerable formations = await formationService.GetFormationAnnuleesAsync(asc, numPage, parPAge, idAgence, texte, tri); + logger.LogInformation("Récupération de la liste des formations annulées."); + + IEnumerable formations = null; + + try + { + formations = await formationService.GetFormationAnnuleesAsync(asc, numPage, parPAge, idAgence, texte, tri); + } + catch (Exception) + { + logger.LogError("Une erreur inconnue est survenue lors de la récupération de la liste des formations annulées."); + } + if (formations == null) { ErreurDTO erreur = new ErreurDTO() @@ -114,9 +174,12 @@ namespace IO.Swagger.Controllers Code = "404", Message = "Aucune formation annulée" }; + return NotFound(erreur); } + logger.LogInformation("Liste des formations annulées récupérée."); + return Ok(formations); } @@ -138,16 +201,41 @@ namespace IO.Swagger.Controllers [SwaggerResponse(statusCode: 404, type: typeof(ErreurDTO), description: "Ressource n'a pas été trouvée")] public virtual async Task GetFormationById([FromRoute][Required] long? idFormation) { - FormationDTO formationDTO = await formationService.GetFormationByIdAsync(idFormation); - if (formationDTO == null) + logger.LogInformation("Récupération de la formation {idFormation}.", idFormation); + + FormationDTO formationDTO = null; + + try + { + formationDTO = await formationService.GetFormationByIdAsync(idFormation); + } + catch (FormationNotFoundException) { + logger.LogError("Aucune formation ne correspond à l'id {idFormation} recherchée.", idFormation); + ErreurDTO erreurDTO = new ErreurDTO() { Code = "404", Message = "La formation n'existe pas", }; + return NotFound(erreurDTO); } + catch (DbUpdateConcurrencyException) + { + logger.LogWarning("La formation {idFormation} n'a pas pu être récupérée car elle est prise par une autre ressource.", idFormation); + } + catch (DbUpdateException) + { + logger.LogError("Une erreur a eu lieu, la formation {idFormation} n'a pas pu être récupérée.", idFormation); + } + catch (Exception) + { + logger.LogError("Une erreur inconnue est survenue lors de la récupération de la formation {idFormation}.", idFormation); + } + + logger.LogInformation("Formation {idFormation} récupérée.", idFormation); + return Ok(formationDTO); } @@ -174,8 +262,19 @@ namespace IO.Swagger.Controllers [SwaggerResponse(statusCode: 404, type: typeof(ErreurDTO), description: "Ressource n'a pas été trouvée")] public virtual async Task GetFormationRealisee([FromQuery][Required()] bool? asc, [FromQuery][Required()] int? numPage, [FromQuery][Required()] int? parPAge, [FromQuery] int? idAgence, [FromQuery] string texte, [FromQuery] string tri) { - //TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - IEnumerable formations = await formationService.GetFormationRealiseeAsync(asc, numPage, parPAge, idAgence, texte, tri); + logger.LogInformation("Récupération de la liste des formations réalisées."); + + IEnumerable formations = null; + + try + { + formations = await formationService.GetFormationRealiseeAsync(asc, numPage, parPAge, idAgence, texte, tri); + } + catch (Exception) + { + logger.LogError("Une erreur inconnue est survenue lors de la récupération de la liste des formations réalisées."); + } + if (formations == null) { ErreurDTO erreur = new ErreurDTO() @@ -183,9 +282,12 @@ namespace IO.Swagger.Controllers Code = "404", Message = "Aucune formation réalisée" }; + return NotFound(erreur); } + logger.LogInformation("Liste des formations réalisées récupérée."); + return Ok(formations); } @@ -210,10 +312,21 @@ namespace IO.Swagger.Controllers [SwaggerOperation("GetFormations")] [SwaggerResponse(statusCode: 200, type: typeof(List), description: "OK")] [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] - public virtual async Task GetFormations([FromQuery][Required()]bool? asc, [FromQuery][Required()]int? numPage, [FromQuery][Required()]int? parPAge, [FromQuery]int? idAgence, [FromQuery]int? statutFormation, [FromQuery]string texte, [FromQuery]string tri) + public virtual async Task GetFormations([FromQuery][Required()] bool? asc, [FromQuery][Required()] int? numPage, [FromQuery][Required()] int? parPAge, [FromQuery] int? idAgence, [FromQuery] int? statutFormation, [FromQuery] string texte, [FromQuery] string tri) { - //TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - IEnumerable formations = await formationService.GetFormationsAsync(asc, numPage, parPAge, idAgence, texte, tri); + logger.LogInformation("Récupération de la liste des formations."); + + IEnumerable formations = null; + + try + { + formations = await formationService.GetFormationsAsync(asc, numPage, parPAge, idAgence, statutFormation, texte, tri); + } + catch (Exception) + { + logger.LogError("Une erreur inconnue est survenue lors de la récupération de la liste des formations."); + } + if (formations == null) { ErreurDTO erreur = new ErreurDTO() @@ -221,9 +334,12 @@ namespace IO.Swagger.Controllers Code = "404", Message = "Aucune formation" }; + return NotFound(erreur); } + logger.LogInformation("Liste des formations récupérée."); + return Ok(formations); } @@ -242,8 +358,19 @@ namespace IO.Swagger.Controllers [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] public virtual async Task GetModesFormation() { - //TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - IEnumerable modeFormations = await formationService.GetModesFormationAsync(); + logger.LogInformation("Récupération de la liste des modes de formation."); + + IEnumerable modeFormations = null; + + try + { + modeFormations = await formationService.GetModesFormationAsync(); + } + catch (Exception) + { + logger.LogError("Une erreur inconnue est survenue lors de la récupération de la liste des modes de formation."); + } + if (modeFormations == null) { ErreurDTO erreur = new ErreurDTO() @@ -251,9 +378,12 @@ namespace IO.Swagger.Controllers Code = "404", Message = "Aucun mode de formation" }; + return NotFound(erreur); } + logger.LogInformation("Liste des modes de formation récupérée."); + return Ok(modeFormations); } @@ -272,8 +402,19 @@ namespace IO.Swagger.Controllers [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] public virtual async Task GetOriginesFormation() { - //TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - IEnumerable origineFormations = await formationService.GetOriginesFormationAsync(); + logger.LogInformation("Récupération de la liste des origines de formation."); + + IEnumerable origineFormations = null; + + try + { + origineFormations = await formationService.GetOriginesFormationAsync(); + } + catch (Exception) + { + logger.LogError("Une erreur inconnue est survenue lors de la récupération de la liste des origines de formation."); + } + if (origineFormations == null) { ErreurDTO erreur = new ErreurDTO() @@ -284,6 +425,8 @@ namespace IO.Swagger.Controllers return NotFound(erreur); } + logger.LogInformation("Liste des origines de formation récupérée."); + return Ok(origineFormations); } @@ -310,8 +453,19 @@ namespace IO.Swagger.Controllers [SwaggerResponse(statusCode: 404, type: typeof(ErreurDTO), description: "Ressource n'a pas été trouvée")] public virtual async Task GetProchainesFormation([FromQuery][Required()] bool? asc, [FromQuery][Required()] int? numPage, [FromQuery][Required()] int? parPAge, [FromQuery] int? idAgence, [FromQuery] string texte, [FromQuery] string tri) { - //TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - IEnumerable formations = await formationService.GetProchainesFormationAsync(asc, numPage, parPAge, idAgence, texte, tri); + logger.LogInformation("Récupération de la liste des prochaines formations."); + + IEnumerable formations = null; + + try + { + formations = await formationService.GetProchainesFormationAsync(asc, numPage, parPAge, idAgence, texte, tri); + } + catch (Exception) + { + logger.LogError("Une erreur inconnue est survenue lors de la récupération de la liste des prochaines formations."); + } + if (formations == null) { ErreurDTO erreur = new ErreurDTO() @@ -319,9 +473,12 @@ namespace IO.Swagger.Controllers Code = "404", Message = "Aucune prochaine formation" }; + return NotFound(erreur); } + logger.LogInformation("Liste des prochaines formations récupérée."); + return Ok(formations); } @@ -340,8 +497,19 @@ namespace IO.Swagger.Controllers [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] public virtual async Task GetStatutsFormation() { - //TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - IEnumerable statutFormations = await formationService.GetStatutsFormationAsync(); + logger.LogInformation("Récupération de la liste des statuts de formation."); + + IEnumerable statutFormations = null; + + try + { + statutFormations = await formationService.GetStatutsFormationAsync(); + } + catch (Exception) + { + logger.LogError("Une erreur inconnue est survenue lors de la récupération de la liste des statuts de formation."); + } + if (statutFormations == null) { ErreurDTO erreur = new ErreurDTO() @@ -349,9 +517,12 @@ namespace IO.Swagger.Controllers Code = "404", Message = "Aucun statut de formation" }; + return NotFound(erreur); } + logger.LogInformation("Liste des statuts de formation récupérée."); + return Ok(statutFormations); } @@ -370,8 +541,19 @@ namespace IO.Swagger.Controllers [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] public virtual async Task GetTypesFormation() { - //TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - IEnumerable typeFormations = await formationService.GetTypesFormationAsync(); + logger.LogInformation("Récupération de la liste des types de formation."); + + IEnumerable typeFormations = null; + + try + { + typeFormations = await formationService.GetTypesFormationAsync(); + } + catch (Exception) + { + logger.LogError("Une erreur inconnue est survenue lors de la récupération de la liste des types de formation."); + } + if (typeFormations == null) { ErreurDTO erreur = new ErreurDTO() @@ -379,9 +561,12 @@ namespace IO.Swagger.Controllers Code = "404", Message = "Aucun type de formation" }; + return NotFound(erreur); } + logger.LogInformation("Liste des types de formation récupérée."); + return Ok(typeFormations); } @@ -402,12 +587,61 @@ namespace IO.Swagger.Controllers [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] public virtual async Task UpdateFormation([FromBody] FormationDTO body, [FromRoute][Required] long? idFormation) { - FormationDTO formation = await formationService.UpdateFormationAsync(idFormation, body); + logger.LogInformation("Mise à jour de la formation d'id {idFormation}.", idFormation); + + FormationDTO formation = null; + + try + { + formation = await formationService.UpdateFormationAsync(idFormation, body); + } + catch (FormationInvalidException) + { + logger.LogWarning("Des données sont manquants, la formation {idFormation} ne peut pas être mise à jour.", idFormation); + } + catch (FormationIncompatibleIdException) + { + logger.LogError("L'id de la formation à mettre à jour {body.Id} et l'id de la formation avec les nouvelles informations {idFormation} sont différents.", body.Id, idFormation); + } + catch (DbUpdateConcurrencyException) + { + logger.LogError("La formation {idFormation} n'a pas pu être mise à jour car elle est prise par une autre ressource.", idFormation); + } + catch (DbUpdateException) + { + logger.LogError("Une erreur est survenue dans la base de données lors de la mise à jour de la formation {idFormation}.", idFormation); + } + catch (Exception) + { + logger.LogError("Une erreur inconnue est survenue lors de la mise à jour de la formation {idFormation}.", idFormation); + } + if (formation == null) { - formation = formationService.AddFormation(body); + try + { + formation = await formationService.AddFormationAsync(body); + } + catch (FormationInvalidException) + { + logger.LogWarning("Des données sont manquants, la nouvelle formation ne peut pas être ajoutée."); + } + catch (DbUpdateException) + { + logger.LogError("Une erreur est survenue dans la base de données durant l'ajout de la nouvelle formation."); + } + catch (Exception) + { + logger.LogError("Une erreur inconnue est survenue lors de l'ajout de la formation note."); + } + + logger.LogInformation("Nouvelle formation ajoutée."); + return Created("", formation); } + + logger.LogInformation("Update effectué avec succès"); + return Ok(formation); } } diff --git a/Exceptions/FormationIncompatibleIdException.cs b/Exceptions/FormationIncompatibleIdException.cs new file mode 100644 index 0000000..2fc7201 --- /dev/null +++ b/Exceptions/FormationIncompatibleIdException.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace EPAServeur.Exceptions +{ + /// + /// Exception à jeter lorsque l'id de la formation avec les données à mettre à jour et l'id de la formation à mettre sont différents + /// + public class FormationIncompatibleIdException : Exception + { + /// + /// Initialise une nouvelle instance de la classe class. + /// + public FormationIncompatibleIdException() + { + + } + + /// + /// Initialise une nouvelle instance de la classe class. + /// + /// + public FormationIncompatibleIdException(string message) : base(message) + { + + } + + /// + /// Initialise une nouvelle instance de la classe class. + /// + /// + /// + public FormationIncompatibleIdException(string message, Exception inner) : base(message, inner) + { + + } + } +} diff --git a/Exceptions/FormationInvalidException.cs b/Exceptions/FormationInvalidException.cs new file mode 100644 index 0000000..0d5e444 --- /dev/null +++ b/Exceptions/FormationInvalidException.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace EPAServeur.Exceptions +{ + /// + /// Exception à jeter lorsq'une formation est invalide + /// + public class FormationInvalidException : Exception + { + /// + /// Initialise une nouvelle instance de la classe class. + /// + public FormationInvalidException() + { + + } + + /// + /// Initialise une nouvelle instance de la classe class. + /// + /// + public FormationInvalidException(string message) : base(message) + { + + } + + /// + /// Initialise une nouvelle instance de la classe class. + /// + /// + /// + public FormationInvalidException(string message, Exception inner) : base(message, inner) + { + + } + } +} diff --git a/Exceptions/FormationNotFoundException.cs b/Exceptions/FormationNotFoundException.cs new file mode 100644 index 0000000..93fdcc9 --- /dev/null +++ b/Exceptions/FormationNotFoundException.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace EPAServeur.Exceptions +{ + /// + /// Exception à jeter lorsqu'une formation n'a pas été trouvée + /// + public class FormationNotFoundException : Exception + { + /// + /// Initialise une nouvelle instance de la classe class. + /// + public FormationNotFoundException() + { + + } + + /// + /// Initialise une nouvelle instance de la classe class. + /// + /// + public FormationNotFoundException(string message) : base(message) + { + + } + + /// + /// Initialise une nouvelle instance de la classe class. + /// + /// + /// + public FormationNotFoundException(string message, Exception inner) : base(message, inner) + { + + } + } +} diff --git a/Exceptions/ModeFormationNotFoundException.cs b/Exceptions/ModeFormationNotFoundException.cs new file mode 100644 index 0000000..c3c1f4a --- /dev/null +++ b/Exceptions/ModeFormationNotFoundException.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace EPAServeur.Exceptions +{ + /// + /// Exception à jeter lorsqu'un mode de formation n'a pas été trouvé + /// + public class ModeFormationNotFoundException : Exception + { + /// + /// Initialise une nouvelle instance de la classe class. + /// + public ModeFormationNotFoundException() + { + + } + + /// + /// Initialise une nouvelle instance de la classe class. + /// + /// + public ModeFormationNotFoundException(string message) : base(message) + { + + } + + /// + /// Initialise une nouvelle instance de la classe class. + /// + /// + /// + public ModeFormationNotFoundException(string message, Exception inner) : base(message, inner) + { + + } + } +} diff --git a/Exceptions/OrigineFormationNotFoundException.cs b/Exceptions/OrigineFormationNotFoundException.cs new file mode 100644 index 0000000..26438b6 --- /dev/null +++ b/Exceptions/OrigineFormationNotFoundException.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace EPAServeur.Exceptions +{ + /// + /// Exception à jeter lorsqu'une origine de formation n'a pas été trouvée + /// + public class OrigineFormationNotFoundException : Exception + { + /// + /// Initialise une nouvelle instance de la classe class. + /// + public OrigineFormationNotFoundException() + { + + } + + /// + /// Initialise une nouvelle instance de la classe class. + /// + /// + public OrigineFormationNotFoundException(string message) : base(message) + { + + } + + /// + /// Initialise une nouvelle instance de la classe class. + /// + /// + /// + public OrigineFormationNotFoundException(string message, Exception inner) : base(message, inner) + { + + } + } +} diff --git a/Exceptions/StatutFormationNotFoundException.cs b/Exceptions/StatutFormationNotFoundException.cs new file mode 100644 index 0000000..66c7167 --- /dev/null +++ b/Exceptions/StatutFormationNotFoundException.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace EPAServeur.Exceptions +{ + /// + /// Exception à jeter lorsqu'un statut de formation n'a pas été trouvé + /// + public class StatutFormationNotFoundException : Exception + { + /// + /// Initialise une nouvelle instance de la classe class. + /// + public StatutFormationNotFoundException() + { + + } + + /// + /// Initialise une nouvelle instance de la classe class. + /// + /// + public StatutFormationNotFoundException(string message) : base(message) + { + + } + + /// + /// Initialise une nouvelle instance de la classe class. + /// + /// + /// + public StatutFormationNotFoundException(string message, Exception inner) : base(message, inner) + { + + } + } +} diff --git a/Exceptions/TypeFormationNotFoundException.cs b/Exceptions/TypeFormationNotFoundException.cs new file mode 100644 index 0000000..54f1de8 --- /dev/null +++ b/Exceptions/TypeFormationNotFoundException.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace EPAServeur.Exceptions +{ + /// + /// Exception à jeter lorsqu'un type de formation n'a pas été trouvé + /// + public class TypeFormationNotFoundException : Exception + { + /// + /// Initialise une nouvelle instance de la classe class. + /// + public TypeFormationNotFoundException() + { + + } + + /// + /// Initialise une nouvelle instance de la classe class. + /// + /// + public TypeFormationNotFoundException(string message) : base(message) + { + + } + + /// + /// Initialise une nouvelle instance de la classe class. + /// + /// + /// + public TypeFormationNotFoundException(string message, Exception inner) : base(message, inner) + { + + } + } +} diff --git a/IServices/IFormationService.cs b/IServices/IFormationService.cs index 4a08b04..6b455c8 100644 --- a/IServices/IFormationService.cs +++ b/IServices/IFormationService.cs @@ -12,8 +12,8 @@ namespace EPAServeur.IServices { FormationDTO GetFormationById(long? idFormation); Task GetFormationByIdAsync(long? idFormation); - IEnumerable GetFormations(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri); - Task> GetFormationsAsync(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri); + IEnumerable GetFormations(bool? asc, int? numPage, int? parPAge, int? idAgence, int? statutFormation, string texte, string tri); + Task> GetFormationsAsync(bool? asc, int? numPage, int? parPAge, int? idAgence, int? statutFormation, string texte, string tri); IEnumerable GetFormationAnnulees(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri); Task> GetFormationAnnuleesAsync(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri); IEnumerable GetFormationRealisee(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri); @@ -33,8 +33,8 @@ namespace EPAServeur.IServices Task AddFormationAsync(FormationDTO formationDTO); FormationDTO UpdateFormation(long? idFormation, FormationDTO formationDTO); Task UpdateFormationAsync(long? idFormation, FormationDTO formationDTO); - bool DeleteFormationById(long? idFormation); - Task DeleteFormationByIdAsync(long? idFormation); + FormationDTO DeleteFormationById(long? idFormation); + Task DeleteFormationByIdAsync(long? idFormation); } } diff --git a/Services/FormationService.cs b/Services/FormationService.cs index 150f207..f164303 100644 --- a/Services/FormationService.cs +++ b/Services/FormationService.cs @@ -1,4 +1,5 @@ using EPAServeur.Context; +using EPAServeur.Exceptions; using EPAServeur.IServices; using EPAServeur.Models.Formation; using IO.Swagger.DTO; @@ -47,7 +48,7 @@ namespace EPAServeur.Services .FirstOrDefault(formation => formation.Id == idFormation); if (formation == null) - return null; + throw new FormationNotFoundException(); return GetFormationDTO(formation); } @@ -66,7 +67,7 @@ namespace EPAServeur.Services .FirstOrDefaultAsync(formation => formation.Id == idFormation); if (formation == null) - return null; + throw new FormationNotFoundException(); return GetFormationDTO(formation); } @@ -81,7 +82,7 @@ namespace EPAServeur.Services /// Texte permettant d'identifier l'objet rechercher /// Colonne du tableau sur lequel le tri s'effectue /// - public IEnumerable GetFormations(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri) + public IEnumerable GetFormations(bool? asc, int? numPage, int? parPAge, int? idAgence, int? statutFormation, string texte, string tri) { IEnumerable formations; IEnumerable formationDTOs; @@ -94,34 +95,38 @@ namespace EPAServeur.Services int skip = (numPage.Value - 1) * parPAge.Value; int take = parPAge.Value; - if (idAgence != null) + if (statutFormation != null && idAgence != null) { - try - { - formations = epContext.Formation.Where(formation => formation.IdAgence == idAgence) - .Include(formation => formation.Statut) - .Include(formation => formation.ModeFormation) - .Include(formation => formation.Origine) - .Include(formation => formation.TypeFormation); - } - catch (Exception ex) - { - throw; - } + formations = epContext.Formation + .Include(formation => formation.Statut) + .Include(formation => formation.ModeFormation) + .Include(formation => formation.Origine) + .Include(formation => formation.TypeFormation) + .Where(formation => formation.Statut.Id == statutFormation && formation.IdAgence == idAgence); + } + else if (statutFormation != null && idAgence == null) + { + formations = epContext.Formation + .Include(formation => formation.Statut) + .Include(formation => formation.ModeFormation) + .Include(formation => formation.Origine) + .Include(formation => formation.TypeFormation) + .Where(formation => formation.Statut.Id == statutFormation); + } + else if (idAgence != null) + { + formations = epContext.Formation.Where(formation => formation.IdAgence == idAgence) + .Include(formation => formation.Statut) + .Include(formation => formation.ModeFormation) + .Include(formation => formation.Origine) + .Include(formation => formation.TypeFormation); } else { - try - { - formations = epContext.Formation.Include(formation => formation.Statut) - .Include(formation => formation.ModeFormation) - .Include(formation => formation.Origine) - .Include(formation => formation.TypeFormation); - } - catch (Exception ex) - { - throw; - } + formations = epContext.Formation.Include(formation => formation.Statut) + .Include(formation => formation.ModeFormation) + .Include(formation => formation.Origine) + .Include(formation => formation.TypeFormation); } @@ -143,7 +148,7 @@ namespace EPAServeur.Services /// Texte permettant d'identifier l'objet rechercher /// Colonne du tableau sur lequel le tri s'effectue /// - public async Task> GetFormationsAsync(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri) + public async Task> GetFormationsAsync(bool? asc, int? numPage, int? parPAge, int? idAgence, int? statutFormation, string texte, string tri) { IEnumerable formations; IEnumerable formationDTOs; @@ -156,34 +161,42 @@ namespace EPAServeur.Services int skip = (numPage.Value - 1) * parPAge.Value; int take = parPAge.Value; - if (idAgence != null) + if (statutFormation != null && idAgence != null) { - try - { - formations = await epContext.Formation - .Include(formation => formation.Statut) - .Include(formation => formation.ModeFormation) - .Include(formation => formation.Origine) - .Include(formation => formation.TypeFormation).Where(formation => formation.IdAgence == idAgence).ToListAsync(); - } - catch (Exception ex) - { - throw; - } + formations = await epContext.Formation + .Include(formation => formation.Statut) + .Include(formation => formation.ModeFormation) + .Include(formation => formation.Origine) + .Include(formation => formation.TypeFormation) + .Where(formation => formation.Statut.Id == statutFormation && formation.IdAgence == idAgence).ToListAsync(); + } + else if (statutFormation != null && idAgence == null) + { + formations = await epContext.Formation + .Include(formation => formation.Statut) + .Include(formation => formation.ModeFormation) + .Include(formation => formation.Origine) + .Include(formation => formation.TypeFormation) + .Where(formation => formation.Statut.Id == statutFormation).ToListAsync(); + } + else if (idAgence != null) + { + + formations = await epContext.Formation + .Include(formation => formation.Statut) + .Include(formation => formation.ModeFormation) + .Include(formation => formation.Origine) + .Include(formation => formation.TypeFormation).Where(formation => formation.IdAgence == idAgence).ToListAsync(); + } else { - try - { - formations = await epContext.Formation.Include(formation => formation.Statut) - .Include(formation => formation.ModeFormation) - .Include(formation => formation.Origine) - .Include(formation => formation.TypeFormation).ToListAsync(); - } - catch (Exception ex) - { - throw; - } + + formations = await epContext.Formation.Include(formation => formation.Statut) + .Include(formation => formation.ModeFormation) + .Include(formation => formation.Origine) + .Include(formation => formation.TypeFormation).ToListAsync(); + } @@ -308,34 +321,23 @@ namespace EPAServeur.Services if (idAgence != null) { - try - { - formations = epContext.Formation.Where(formation => formation.IdAgence == idAgence && formation.Statut.Id == 3) - .Include(formation => formation.Statut) - .Include(formation => formation.ModeFormation) - .Include(formation => formation.Origine) - .Include(formation => formation.TypeFormation); - } - catch (Exception ex) - { - throw; - } + + formations = epContext.Formation.Where(formation => formation.IdAgence == idAgence && formation.Statut.Id == 3) + .Include(formation => formation.Statut) + .Include(formation => formation.ModeFormation) + .Include(formation => formation.Origine) + .Include(formation => formation.TypeFormation); } else { - try - { - formations = epContext.Formation.Where(formation => formation.Statut.Id == 3) - .Include(formation => formation.Statut) - .Include(formation => formation.ModeFormation) - .Include(formation => formation.Origine) - .Include(formation => formation.TypeFormation); - } - catch (Exception ex) - { - throw; - } + + formations = epContext.Formation.Where(formation => formation.Statut.Id == 3) + .Include(formation => formation.Statut) + .Include(formation => formation.ModeFormation) + .Include(formation => formation.Origine) + .Include(formation => formation.TypeFormation); + } @@ -372,34 +374,22 @@ namespace EPAServeur.Services if (idAgence != null) { - try - { - formations = await epContext.Formation.Where(formation => formation.IdAgence == idAgence && formation.Statut.Id == 3) - .Include(formation => formation.Statut) - .Include(formation => formation.ModeFormation) - .Include(formation => formation.Origine) - .Include(formation => formation.TypeFormation).ToListAsync(); - } - catch (Exception ex) - { - throw; - } + + formations = await epContext.Formation.Where(formation => formation.IdAgence == idAgence && formation.Statut.Id == 3) + .Include(formation => formation.Statut) + .Include(formation => formation.ModeFormation) + .Include(formation => formation.Origine) + .Include(formation => formation.TypeFormation).ToListAsync(); } else { - try - { - formations = await epContext.Formation.Where(formation => formation.Statut.Id == 3) - .Include(formation => formation.Statut) - .Include(formation => formation.ModeFormation) - .Include(formation => formation.Origine) - .Include(formation => formation.TypeFormation).ToListAsync(); - } - catch (Exception ex) - { - throw; - } + + formations = await epContext.Formation.Where(formation => formation.Statut.Id == 3) + .Include(formation => formation.Statut) + .Include(formation => formation.ModeFormation) + .Include(formation => formation.Origine) + .Include(formation => formation.TypeFormation).ToListAsync(); } @@ -435,19 +425,14 @@ namespace EPAServeur.Services int take = parPAge.Value; if (idAgence != null) - try - { - formations = epContext.Formation.Where(formation => formation.IdAgence == idAgence && (formation.Statut.Id == 1 || formation.Statut.Id == 2)) - .Include(formation => formation.Statut) - .Include(formation => formation.ModeFormation) - .Include(formation => formation.Origine) - .Include(formation => formation.TypeFormation); - } - catch (Exception ex) - { - throw; - } + { + formations = epContext.Formation.Where(formation => formation.IdAgence == idAgence && (formation.Statut.Id == 1 || formation.Statut.Id == 2)) + .Include(formation => formation.Statut) + .Include(formation => formation.ModeFormation) + .Include(formation => formation.Origine) + .Include(formation => formation.TypeFormation); + } else { formations = epContext.Formation.Where(formation => (formation.Statut.Id == 1 || formation.Statut.Id == 2)) @@ -489,18 +474,14 @@ namespace EPAServeur.Services int take = parPAge.Value; if (idAgence != null) - try - { - formations = await epContext.Formation.Where(formation => formation.IdAgence == idAgence && (formation.Statut.Id == 1 || formation.Statut.Id == 2)) - .Include(formation => formation.Statut) - .Include(formation => formation.ModeFormation) - .Include(formation => formation.Origine) - .Include(formation => formation.TypeFormation).ToListAsync(); - } - catch (Exception ex) - { - throw; - } + { + formations = await epContext.Formation.Where(formation => formation.IdAgence == idAgence && (formation.Statut.Id == 1 || formation.Statut.Id == 2)) + .Include(formation => formation.Statut) + .Include(formation => formation.ModeFormation) + .Include(formation => formation.Origine) + .Include(formation => formation.TypeFormation).ToListAsync(); + + } else { @@ -527,14 +508,8 @@ namespace EPAServeur.Services { IEnumerable modeFormations; IEnumerable modeFormationDTOs; - try - { - modeFormations = epContext.ModeFormation; - } - catch (Exception ex) - { - throw; - } + + modeFormations = epContext.ModeFormation; if (modeFormations == null) return null; @@ -552,14 +527,7 @@ namespace EPAServeur.Services { IEnumerable modeFormations; IEnumerable modeFormationDTOs; - try - { - modeFormations = await epContext.ModeFormation.ToListAsync(); - } - catch (Exception ex) - { - throw; - } + modeFormations = await epContext.ModeFormation.ToListAsync(); if (modeFormations == null) return null; @@ -578,14 +546,7 @@ namespace EPAServeur.Services IEnumerable origineFormations; IEnumerable origineFormationDTOs; - try - { - origineFormations = epContext.OrigineFormation; - } - catch (Exception ex) - { - throw; - } + origineFormations = epContext.OrigineFormation; if (origineFormations == null) return null; @@ -604,14 +565,7 @@ namespace EPAServeur.Services IEnumerable origineFormations; IEnumerable origineFormationDTOs; - try - { - origineFormations = await epContext.OrigineFormation.ToListAsync(); - } - catch (Exception ex) - { - throw; - } + origineFormations = await epContext.OrigineFormation.ToListAsync(); if (origineFormations == null) return null; @@ -630,15 +584,7 @@ namespace EPAServeur.Services IEnumerable statutFormations; IEnumerable statutFormationDTOs; - try - { - statutFormations = epContext.StatutFormation; - } - catch (Exception ex) - { - - throw; - } + statutFormations = epContext.StatutFormation; if (statutFormations == null) return null; @@ -657,15 +603,7 @@ namespace EPAServeur.Services IEnumerable statutFormations; IEnumerable statutFormationDTOs; - try - { - statutFormations = await epContext.StatutFormation.ToListAsync(); - } - catch (Exception ex) - { - - throw; - } + statutFormations = await epContext.StatutFormation.ToListAsync(); if (statutFormations == null) return null; @@ -684,14 +622,7 @@ namespace EPAServeur.Services IEnumerable typeFormations; IEnumerable typeFormationDTOs; - try - { - typeFormations = epContext.TypeFormation; - } - catch (Exception ex) - { - throw; - } + typeFormations = epContext.TypeFormation; if (typeFormations == null) return null; @@ -710,14 +641,7 @@ namespace EPAServeur.Services IEnumerable typeFormations; IEnumerable typeFormationDTOs; - try - { - typeFormations = await epContext.TypeFormation.ToListAsync(); - } - catch (Exception ex) - { - throw; - } + typeFormations = await epContext.TypeFormation.ToListAsync(); if (typeFormations == null) return null; @@ -734,27 +658,21 @@ namespace EPAServeur.Services /// public FormationDTO AddFormation(FormationDTO formationDTO) { + if (!IsFormationValide(formationDTO)) + throw new FormationInvalidException(); + Formation formation = new Formation(); formation = SetFormation(formation, formationDTO); if (formation.Statut != null) - { epContext.StatutFormation.Attach(formation.Statut); - } epContext.OrigineFormation.Attach(formation.Origine); epContext.ModeFormation.Attach(formation.ModeFormation); epContext.TypeFormation.Attach(formation.TypeFormation); epContext.Add(formation); - try - { - epContext.SaveChanges(); - } - catch (Exception ex) - { - throw; - } + epContext.SaveChanges(); return GetFormationDTO(formation); } @@ -766,27 +684,21 @@ namespace EPAServeur.Services /// public async Task AddFormationAsync(FormationDTO formationDTO) { + if (!IsFormationValide(formationDTO)) + throw new FormationInvalidException(); + Formation formation = new Formation(); formation = SetFormation(formation, formationDTO); if (formation.Statut != null) - { epContext.StatutFormation.Attach(formation.Statut); - } epContext.OrigineFormation.Attach(formation.Origine); epContext.ModeFormation.Attach(formation.ModeFormation); epContext.TypeFormation.Attach(formation.TypeFormation); epContext.Add(formation); - try - { - await epContext.SaveChangesAsync(); - } - catch (Exception ex) - { - throw; - } + await epContext.SaveChangesAsync(); return GetFormationDTO(formation); } @@ -799,27 +711,20 @@ namespace EPAServeur.Services /// public FormationDTO UpdateFormation(long? idFormation, FormationDTO formationDTO) { - if (formationDTO == null && !formationDTO.Id.HasValue && formationDTO.Id.Value != idFormation) - { - return null; - } + if (!IsFormationValide(formationDTO)) + throw new FormationInvalidException(); + + if (formationDTO == null && !formationDTO.Id.HasValue && formationDTO.Id.Value != idFormation) + throw new FormationIncompatibleIdException(); Formation formation = epContext.Formation.Find(idFormation); if (formation == null) - { return null; - } formation = SetFormation(formation, formationDTO); - try - { - epContext.SaveChanges(); - } - catch (Exception ex) - { - throw; - } + + epContext.SaveChanges(); return GetFormationDTO(formation); } @@ -832,27 +737,19 @@ namespace EPAServeur.Services /// public async Task UpdateFormationAsync(long? idFormation, FormationDTO formationDTO) { + if (!IsFormationValide(formationDTO)) + throw new FormationInvalidException(); + if (formationDTO == null && !formationDTO.Id.HasValue && formationDTO.Id.Value != idFormation) - { - return null; - } + throw new FormationIncompatibleIdException(); Formation formation = await epContext.Formation.FindAsync(idFormation); if (formation == null) - { return null; - } formation = SetFormation(formation, formationDTO); - try - { - await epContext.SaveChangesAsync(); - } - catch (Exception ex) - { - throw; - } + await epContext.SaveChangesAsync(); return GetFormationDTO(formation); } @@ -862,25 +759,19 @@ namespace EPAServeur.Services /// /// /// - public bool DeleteFormationById(long? idFormation) + public FormationDTO DeleteFormationById(long? idFormation) { Formation formation = epContext.Formation.Find(idFormation); if (formation == null) - return false; + throw new FormationNotFoundException(); epContext.Remove(formation); - try - { - epContext.SaveChanges(); - } - catch (Exception) - { - throw; - } - return true; + epContext.SaveChanges(); + + return GetFormationDTO(formation); } /// @@ -888,29 +779,32 @@ namespace EPAServeur.Services /// /// /// - public async Task DeleteFormationByIdAsync(long? idFormation) + public async Task DeleteFormationByIdAsync(long? idFormation) { Formation formation = await epContext.Formation.FindAsync(idFormation); if (formation == null) - return false; + throw new FormationNotFoundException(); epContext.Remove(formation); - try - { - await epContext.SaveChangesAsync(); - } - catch (Exception) - { - throw; - } + await epContext.SaveChangesAsync(); - return true; + return GetFormationDTO(formation); } #endregion #region Méthodes Privée + /// + /// Vérifier si un objet FormationDTO est valide pour ajout ou mise à jour + /// + /// Un objet FormationDTO est valide si aucune de ses propriétés n'est à null + /// + /// true si l'objet est valide, false sinon + private bool IsFormationValide(FormationDTO formation) + { + return !(formation == null || formation.IdAgence == null || formation.Intitule == null || formation.Organisme == null); + } #region Object to DTO ///