From 2d38013907770d73621b7b2a512d48f53c19c7d5 Mon Sep 17 00:00:00 2001 From: jboinembalome Date: Fri, 26 Feb 2021 15:16:36 +0100 Subject: [PATCH] Gestion des objets null pour les mappages de model en dto dans le service formation --- EPAServeur/Services/FormationService.cs | 83 ++++++++++++++++++++++--- 1 file changed, 74 insertions(+), 9 deletions(-) diff --git a/EPAServeur/Services/FormationService.cs b/EPAServeur/Services/FormationService.cs index e712c18..fb0b113 100644 --- a/EPAServeur/Services/FormationService.cs +++ b/EPAServeur/Services/FormationService.cs @@ -89,9 +89,15 @@ namespace EPAServeur.Services throw new FormationNotFoundException(string.Format("Aucune formation trouvée avec l'id suivant: {0}.", idFormation)); - IEnumerable collaborateurDTOs = await GetCollaborateurDTOs(formation.ParticipationsFormation); - - return GetFormationDTO(formation, collaborateurDTOs); + if (formation.ParticipationsFormation.Count > 0) + { + IEnumerable collaborateurDTOs = await GetCollaborateurDTOs(formation.ParticipationsFormation); + return GetFormationDTO(formation, collaborateurDTOs); + } + else + { + return GetFormationDTO(formation); + } } /// @@ -265,12 +271,19 @@ namespace EPAServeur.Services /// public async Task UpdateFormationAsync(long idFormation, FormationDTO formationDTO) { - if (!formationDTO.Id.HasValue || formationDTO.Id.Value != idFormation) - throw new FormationIncompatibleIdException("L'id de la formation a mettre à jour et la formation a mettre à jour sont incompatble."); - IsFormationValide(formationDTO); - Formation formation = await epContext.Formation.FindAsync(idFormation); + if (!formationDTO.Id.HasValue || formationDTO.Id.Value != idFormation) + throw new FormationIncompatibleIdException("L'id de la formation a mettre à jour et la formation a mettre à jour sont incompatble."); + + Formation formation = await epContext.Formation.Include(formation => formation.Statut) + .Include(formation => formation.ModeFormation) + .Include(formation => formation.Origine) + .Include(formation => formation.TypeFormation) + .Include(formation => formation.ParticipationsFormation) + .ThenInclude(participation => participation.DemandeFormation) + .ThenInclude(demande => demande.Ep) + .FirstOrDefaultAsync(formation => formation.IdFormation == idFormation); if (formation == null) throw new FormationNotFoundException(string.Format("Aucune formation trouvée avec l'id suivant: {0}.", idFormation)); @@ -289,7 +302,14 @@ namespace EPAServeur.Services /// public async Task DeleteFormationByIdAsync(long idFormation) { - Formation formation = await epContext.Formation.FindAsync(idFormation); + Formation formation = await epContext.Formation.Include(formation => formation.Statut) + .Include(formation => formation.ModeFormation) + .Include(formation => formation.Origine) + .Include(formation => formation.TypeFormation) + .Include(formation => formation.ParticipationsFormation) + .ThenInclude(participation => participation.DemandeFormation) + .ThenInclude(demande => demande.Ep) + .FirstOrDefaultAsync(formation => formation.IdFormation == idFormation); if (formation == null) throw new FormationNotFoundException(string.Format("Aucune formation trouvée avec l'id suivant: {0}.", idFormation)); @@ -526,6 +546,9 @@ namespace EPAServeur.Services /// private FormationDTO GetFormationDTO(Formation formation) { + if (formation == null) + return null; + FormationDTO formationDTO = new FormationDTO() { Id = formation.IdFormation, @@ -537,6 +560,7 @@ namespace EPAServeur.Services Jour = formation.Jour, Organisme = formation.Organisme, EstCertifiee = formation.EstCertifiee, + EstRealisee = formation.EstRealisee, Origine = GetOrigineFormationDTO(formation.Origine), Statut = GetStatutFormationDTO(formation.Statut), Mode = GetModeFormationDTO(formation.ModeFormation), @@ -553,6 +577,9 @@ namespace EPAServeur.Services /// private FormationDTO GetFormationDTO(Formation formation, IEnumerable collaborateurDTOs) { + if (formation == null || collaborateurDTOs == null || !collaborateurDTOs.Any()) + return null; + FormationDTO formationDTO = new FormationDTO() { Id = formation.IdFormation, @@ -582,6 +609,9 @@ namespace EPAServeur.Services /// private FormationDetailsDTO GetFormationDetailsDTO(Formation formation) { + if (formation == null) + return null; + FormationDetailsDTO formationDTO = new FormationDetailsDTO() { Id = formation.IdFormation, @@ -607,11 +637,13 @@ namespace EPAServeur.Services { if (origineFormation == null) return null; + OrigineFormationDTO origineFormationDTO = new OrigineFormationDTO() { Id = origineFormation.IdOrigineFormation, Libelle = origineFormation.Libelle }; + return origineFormationDTO; } @@ -624,11 +656,13 @@ namespace EPAServeur.Services { if (statutFormation == null) return null; + StatutFormationDTO statutFormationDTO = new StatutFormationDTO() { Id = statutFormation.IdStatutFormation, Libelle = statutFormation.Libelle }; + return statutFormationDTO; } @@ -641,11 +675,13 @@ namespace EPAServeur.Services { if (modeFormation == null) return null; + ModeFormationDTO modeFormationDTO = new ModeFormationDTO() { Id = modeFormation.IdModeFormation, Libelle = modeFormation.Libelle }; + return modeFormationDTO; } @@ -658,11 +694,13 @@ namespace EPAServeur.Services { if (typeFormation == null) return null; + TypeFormationDTO typeFormationDTO = new TypeFormationDTO() { Id = typeFormation.IdTypeFormation, Libelle = typeFormation.Libelle }; + return typeFormationDTO; } @@ -673,7 +711,7 @@ namespace EPAServeur.Services /// private async Task> GetCollaborateurDTOs(List participationsFormation) { - if (participationsFormation.Count == 0) + if (participationsFormation == null || participationsFormation.Count == 0) return null; List guids = participationsFormation.SelectMany(participationFormation => new[] { (Guid?)participationFormation.DemandeFormation.Ep.IdCollaborateur, participationFormation.DemandeFormation.Ep.IdReferent }).ToList(); @@ -707,6 +745,9 @@ namespace EPAServeur.Services /// private ParticipationFormationDTO GetParticipationFormationDTO(ParticipationFormation participationFormation, IEnumerable collaborateurDTOs) { + if (participationFormation == null || collaborateurDTOs == null || !collaborateurDTOs.Any()) + return null; + ParticipationFormationDTO participationFormationDTO = new ParticipationFormationDTO() { Id = participationFormation.IdParticipationFormation, @@ -728,6 +769,9 @@ namespace EPAServeur.Services /// private CollaborateurDTO GetCollaborateurDTO(ParticipationFormation participationFormation, IEnumerable collaborateurDTOs) { + if (participationFormation == null || collaborateurDTOs == null || !collaborateurDTOs.Any()) + return null; + return collaborateurDTOs.FirstOrDefault(collaborateurDTO => collaborateurDTO.Id == participationFormation.DemandeFormation.Ep.IdCollaborateur); } @@ -741,6 +785,8 @@ namespace EPAServeur.Services CollaborateurDTO collaborateur; CollaborateurDTO referent; + if (ep == null || collaborateurDTOs == null || !collaborateurDTOs.Any()) + return null; collaborateur = collaborateurDTOs.FirstOrDefault(collaborateurDTO => collaborateurDTO.Id == ep.IdCollaborateur); referent = collaborateurDTOs.FirstOrDefault(collaborateurDTO => collaborateurDTO.Id == ep.IdReferent); @@ -773,6 +819,9 @@ namespace EPAServeur.Services /// private Formation SetFormation(Formation formation, FormationDTO formationDTO) { + if (formation == null || formationDTO == null) + return null; + formation.Intitule = formationDTO.Intitule; formation.IdAgence = formationDTO.IdAgence.Value; formation.DateDebut = formationDTO.DateDebut.Value; @@ -797,11 +846,15 @@ namespace EPAServeur.Services /// private OrigineFormation GetOrigineFormation(OrigineFormationDTO origineFormationDTO) { + if (origineFormationDTO == null) + return null; + OrigineFormation origineFormation = new OrigineFormation() { IdOrigineFormation = origineFormationDTO.Id.Value, Libelle = origineFormationDTO.Libelle }; + return origineFormation; } @@ -812,11 +865,15 @@ namespace EPAServeur.Services /// private StatutFormation GetStatutFormation(StatutFormationDTO statutFormationDTO) { + if (statutFormationDTO == null) + return null; + StatutFormation statutFormation = new StatutFormation() { IdStatutFormation = statutFormationDTO.Id.Value, Libelle = statutFormationDTO.Libelle }; + return statutFormation; } @@ -827,11 +884,15 @@ namespace EPAServeur.Services /// private ModeFormation GetModeFormation(ModeFormationDTO modeFormationDTO) { + if (modeFormationDTO == null) + return null; + ModeFormation modeFormation = new ModeFormation() { IdModeFormation = modeFormationDTO.Id.Value, Libelle = modeFormationDTO.Libelle }; + return modeFormation; } @@ -842,11 +903,15 @@ namespace EPAServeur.Services /// private TypeFormation GetTypeFormation(TypeFormationDTO typeFormationDTO) { + if (typeFormationDTO == null) + return null; + TypeFormation typeFormation = new TypeFormation() { IdTypeFormation = typeFormationDTO.Id.Value, Libelle = typeFormationDTO.Libelle }; + return typeFormation; }