From f60c4ddb83c04c32826d36605734e4c587f23b64 Mon Sep 17 00:00:00 2001 From: jboinembalome Date: Wed, 9 Sep 2020 15:47:27 +0200 Subject: [PATCH] Ajout des try/catch dans la classe FormationService --- Services/FormationService.cs | 365 ++++++++++++++++++++++++++++------- 1 file changed, 295 insertions(+), 70 deletions(-) diff --git a/Services/FormationService.cs b/Services/FormationService.cs index edb6b6e..89b14d9 100644 --- a/Services/FormationService.cs +++ b/Services/FormationService.cs @@ -1,28 +1,42 @@ using EPAServeur.Context; using EPAServeur.IServices; using EPAServeur.Models.Formation; -using IO.Swagger.ApiCollaborateur; using IO.Swagger.DTO; -using IO.Swagger.ModelCollaborateur; using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.ChangeTracking; using System; using System.Collections.Generic; using System.Linq; -using System.Runtime.InteropServices.ComTypes; -using System.Threading.Tasks; namespace EPAServeur.Services { public class FormationService : IFormationService { + #region Variables + private readonly EpContext epContext; + #endregion + + #region Contructeurs + + /// + /// Constructeur de la classe FormationService + /// + /// public FormationService(EpContext _epContext) { epContext = _epContext; } + #endregion + + #region Méthodes Service + + /// + /// Récupérer une formation par son id + /// + /// + /// public FormationDTO GetFormationById(decimal? id) { int idFormation = 0; @@ -33,7 +47,7 @@ namespace EPAServeur.Services } catch (Exception ex) { - return null; + throw; } Formation formation = epContext.Formation.Include(formation => formation.Statut) @@ -48,6 +62,16 @@ namespace EPAServeur.Services return GetFormationDTO(formation); } + /// + /// Récupérer la liste des formations + /// + /// Préciser si les données sont dans l'ordre (true) ou dans l'ordre inverse (false) + /// Numéro de la page du tableau qui affiche les données + /// Nombre d'éléments affiché sur chaque page du tableau + /// id de l'agence à laquelle sont rattachées les données à récupérer + /// 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) { IEnumerable formations; @@ -62,16 +86,35 @@ namespace EPAServeur.Services int take = parPAge.Value; 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); + { + 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; + } + } else - formations = epContext.Formation.Include(formation => formation.Statut) - .Include(formation => formation.ModeFormation) - .Include(formation => formation.Origine) - .Include(formation => formation.TypeFormation); + { + try + { + formations = epContext.Formation.Include(formation => formation.Statut) + .Include(formation => formation.ModeFormation) + .Include(formation => formation.Origine) + .Include(formation => formation.TypeFormation); + } + catch (Exception ex) + { + throw; + } + } + if (formations == null) return new List(); @@ -81,7 +124,16 @@ namespace EPAServeur.Services return formationDTOs; } - + /// + /// Récupérer les formations annulées + /// + /// Préciser si les données sont dans l'ordre (true) ou dans l'ordre inverse (false) + /// Numéro de la page du tableau qui affiche les données + /// Nombre d'éléments affiché sur chaque page du tableau + /// id de l'agence à laquelle sont rattachées les données à récupérer + /// Texte permettant d'identifier l'objet rechercher + /// Colonne du tableau sur lequel le tri s'effectue + /// public IEnumerable GetFormationAnnulees(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri) { IEnumerable formations; @@ -116,6 +168,16 @@ namespace EPAServeur.Services return formationDTOs; } + /// + /// Récupérer les formations réalisées + /// + /// Préciser si les données sont dans l'ordre (true) ou dans l'ordre inverse (false) + /// Numéro de la page du tableau qui affiche les données + /// Nombre d'éléments affiché sur chaque page du tableau + /// id de l'agence à laquelle sont rattachées les données à récupérer + /// Texte permettant d'identifier l'objet rechercher + /// Colonne du tableau sur lequel le tri s'effectue + /// public IEnumerable GetFormationRealisee(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri) { IEnumerable formations; @@ -130,17 +192,37 @@ namespace EPAServeur.Services int take = parPAge.Value; if (idAgence != null) - 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); + { + 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; + } + } + else - 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); + { + 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; + } + } + if (formations == null) return new List(); @@ -150,6 +232,16 @@ namespace EPAServeur.Services return formationDTOs; } + /// + /// Récupérer les formations plannifiées et replannifiées + /// + /// Préciser si les données sont dans l'ordre (true) ou dans l'ordre inverse (false) + /// Numéro de la page du tableau qui affiche les données + /// Nombre d'éléments affiché sur chaque page du tableau + /// id de l'agence à laquelle sont rattachées les données à récupérer + /// Texte permettant d'identifier l'objet rechercher + /// Colonne du tableau sur lequel le tri s'effectue + /// public IEnumerable GetProchainesFormation(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri) { IEnumerable formations; @@ -164,17 +256,27 @@ namespace EPAServeur.Services int take = parPAge.Value; if (idAgence != null) - formations = epContext.Formation.Where(formation => formation.IdAgence == idAgence && (formation.Statut.Id == 1 || formation.Statut.Id == 2)) + 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; + } + else + { formations = epContext.Formation.Where(formation => (formation.Statut.Id == 1 || formation.Statut.Id == 2)) - .Include(formation => formation.Statut) - .Include(formation => formation.ModeFormation) - .Include(formation => formation.Origine) - .Include(formation => formation.TypeFormation); + .Include(formation => formation.Statut) + .Include(formation => formation.ModeFormation) + .Include(formation => formation.Origine) + .Include(formation => formation.TypeFormation); + } if (formations == null) return new List(); @@ -184,38 +286,103 @@ namespace EPAServeur.Services return formationDTOs; } + /// + /// Récupérer les modes de formation + /// + /// public IEnumerable GetModesFormation() { - IEnumerable modeFormations = epContext.ModeFormation; - IEnumerable modeFormationDTOs = modeFormations.Select(modeFormation => GetModeFormationDTO(modeFormation)); + IEnumerable modeFormations; + IEnumerable modeFormationDTOs; + try + { + modeFormations = epContext.ModeFormation; + } + catch (Exception ex) + { + throw; + } + + modeFormationDTOs = modeFormations.Select(modeFormation => GetModeFormationDTO(modeFormation)); return modeFormationDTOs; } + /// + /// Récupérer les origines de formation + /// + /// public IEnumerable GetOriginesFormation() { - IEnumerable origineFormations = epContext.OrigineFormation; - IEnumerable origineFormationDTOs = origineFormations.Select(origineFormation => GetOrigineFormationDTO(origineFormation)); + IEnumerable origineFormations; + IEnumerable origineFormationDTOs; + + try + { + origineFormations = epContext.OrigineFormation; + } + catch (Exception ex) + { + throw; + } + + origineFormationDTOs = origineFormations.Select(origineFormation => GetOrigineFormationDTO(origineFormation)); return origineFormationDTOs; } + /// + /// Récupérer les statuts de formation + /// + /// public IEnumerable GetStatutsFormation() { - IEnumerable statutFormations = epContext.StatutFormation; - IEnumerable statutFormationDTOs = statutFormations.Select(statutFormation => GetStatutFormationDTO(statutFormation)); + IEnumerable statutFormations; + IEnumerable statutFormationDTOs; + + try + { + statutFormations = epContext.StatutFormation; + } + catch (Exception ex) + { + + throw; + } + + statutFormationDTOs = statutFormations.Select(statutFormation => GetStatutFormationDTO(statutFormation)); return statutFormationDTOs; } + /// + /// Récupérer les types de formation + /// + /// public IEnumerable GetTypesFormation() { - IEnumerable typeFormations = epContext.TypeFormation; - IEnumerable typeFormationDTOs = typeFormations.Select(typeFormation => GetTypeFormationDTO(typeFormation)); + IEnumerable typeFormations; + IEnumerable typeFormationDTOs; + + try + { + typeFormations = epContext.TypeFormation; + } + catch (Exception ex) + { + throw; + } + + typeFormationDTOs = typeFormations.Select(typeFormation => GetTypeFormationDTO(typeFormation)); return typeFormationDTOs; } + /// + /// Ajouter une formation + /// + /// + /// public FormationDTO AddFormation(FormationDTO formationDTO) { Formation formation = new Formation(); @@ -235,14 +402,19 @@ namespace EPAServeur.Services { epContext.SaveChanges(); } - catch (Exception) + catch (Exception ex) { - return null; + throw; } return GetFormationDTO(formation); } + /// + /// Modifier une formation + /// + /// + /// public FormationDTO UpdateFormation(FormationDTO formationDTO) { Formation formation = epContext.Formation.FirstOrDefault(formation => formation.Id == formationDTO.Id); @@ -253,11 +425,23 @@ namespace EPAServeur.Services } formation = SetFormation(formation, formationDTO); - epContext.SaveChanges(); + try + { + epContext.SaveChanges(); + } + catch (Exception ex) + { + throw; + } return GetFormationDTO(formation); } + /// + /// Supprimer une formation + /// + /// + /// public bool DeleteFormationById(decimal? id) { int idFormation = 0; @@ -276,13 +460,30 @@ namespace EPAServeur.Services if (formation == null) return false; - epContext.Remove(formation); - epContext.SaveChanges(); + + try + { + epContext.SaveChanges(); + } + catch (Exception) + { + throw; + } + return true; } + #endregion + + #region Méthodes Privée + #region Object to DTO + /// + /// Récupère un objet FormationDTO en fonction d'un objet Formation + /// + /// + /// private FormationDTO GetFormationDTO(Formation formation) { FormationDTO formationDTO = new FormationDTO() @@ -302,21 +503,14 @@ namespace EPAServeur.Services Type = GetTypeFormationDTO(formation.TypeFormation) }; - //List participationFormationDTOs = epContext.ParticipationFormation.Where(participationFormation => participationFormation.Formation.Id == formation.Id) - // .Select(participationFormation => new ParticipationFormationDTO() - // { - // Id = participationFormation.Id, - // DateCreation = participationFormation.DateCreation, - // Formation = participationFormation.Formation.Intitule, - // Date = participationFormation.Formation.DateDebut, - // Statut = participationFormation.Formation.Statut.Libelle, - // EstEvaluee = participationFormation.EstEvaluee, - // }).ToList(); - - //formationDTO.ParticipantsFormation = participationFormationDTOs; return formationDTO; } + /// + /// Récupère un objet OrigineFormationDTO en fonction d'un objet OrigineFormation + /// + /// + /// private OrigineFormationDTO GetOrigineFormationDTO(OrigineFormation origineFormation) { if (origineFormation == null) @@ -329,6 +523,11 @@ namespace EPAServeur.Services return origineFormationDTO; } + /// + /// Récupère un objet StatutFormationDTO en fonction d'un objet StatutFormation + /// + /// + /// private StatutFormationDTO GetStatutFormationDTO(StatutFormation statutFormation) { if (statutFormation == null) @@ -341,6 +540,11 @@ namespace EPAServeur.Services return statutFormationDTO; } + /// + /// Récupère un objet ModeFormationDTO en fonction d'un objet ModeFormation + /// + /// + /// private ModeFormationDTO GetModeFormationDTO(ModeFormation modeFormation) { if (modeFormation == null) @@ -352,6 +556,11 @@ namespace EPAServeur.Services }; return modeFormationDTO; } + /// + /// Récupère un objet TypeFormationDTO en fonction d'un objet TypeFormation + /// + /// + /// private TypeFormationDTO GetTypeFormationDTO(TypeFormation typeFormation) { if (typeFormation == null) @@ -367,6 +576,13 @@ namespace EPAServeur.Services #endregion #region DTO to Object + + /// + /// Modifie un objet Formation en fonction d'un objet FormationDTO + /// + /// + /// + /// private Formation SetFormation(Formation formation, FormationDTO formationDTO) { formation.Intitule = formationDTO.Intitule; @@ -382,23 +598,14 @@ namespace EPAServeur.Services formation.ModeFormation = GetModeFormation(formationDTO.Mode); formation.TypeFormation = GetTypeFormation(formationDTO.Type); - - //List participationFormationDTOs = epContext.ParticipationFormation.Where(participationFormation => participationFormation.Formation.Id == formation.Id) - // .Select(participationFormation => new ParticipationFormationDTO() - // { - // Id = participationFormation.Id, - // DateCreation = participationFormation.DateCreation, - // Formation = participationFormation.Formation.Intitule, - // Date = participationFormation.Formation.DateDebut, - // Statut = participationFormation.Formation.Statut.Libelle, - // EstEvaluee = participationFormation.EstEvaluee, - // }).ToList(); - - //formationDTO.ParticipantsFormation = participationFormationDTOs; return formation; } - + /// + /// Récupère un objet OrigineFormation en fonction d'un objet OrigineFormationDTO + /// + /// + /// private OrigineFormation GetOrigineFormation(OrigineFormationDTO origineFormationDTO) { if (origineFormationDTO == null) @@ -411,6 +618,11 @@ namespace EPAServeur.Services return origineFormation; } + /// + /// Récupère un objet StatutFormation en fonction d'un objet StatutFormationDTO + /// + /// + /// private StatutFormation GetStatutFormation(StatutFormationDTO statutFormationDTO) { if (statutFormationDTO == null) @@ -423,6 +635,11 @@ namespace EPAServeur.Services return statutFormation; } + /// + /// Récupère un objet ModeFormation en fonction d'un objet ModeFormationDTO + /// + /// + /// private ModeFormation GetModeFormation(ModeFormationDTO modeFormationDTO) { if (modeFormationDTO == null) @@ -434,6 +651,12 @@ namespace EPAServeur.Services }; return modeFormation; } + + /// + /// Récupère un objet TypeFormation en fonction d'un objet TypeFormationDTO + /// + /// + /// private TypeFormation GetTypeFormation(TypeFormationDTO typeFormationDTO) { if (typeFormationDTO == null) @@ -448,5 +671,7 @@ namespace EPAServeur.Services #endregion + #endregion + } }