From b70b255f2b973b48edea44c90a21989d1c5d0ced Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yana=C3=ABl=20GRETTE?= Date: Fri, 19 Feb 2021 14:42:17 +0100 Subject: [PATCH] =?UTF-8?q?Optimisation=20du=20code=20du=20service=20des?= =?UTF-8?q?=20demandes=20de=20d=C3=A9l=C3=A9gation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/DemandeDelegationService.cs | 34 ++++++++----------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/EPAServeur/Services/DemandeDelegationService.cs b/EPAServeur/Services/DemandeDelegationService.cs index 09979aa..48347e5 100644 --- a/EPAServeur/Services/DemandeDelegationService.cs +++ b/EPAServeur/Services/DemandeDelegationService.cs @@ -18,8 +18,7 @@ namespace EPAServeur.Services { #region variables private readonly EpContext context; - private static ICollaborateurApi collaborateurAPI; - private static ICollaborateurService collaborateurService; + private readonly ICollaborateurService collaborateurService; #endregion @@ -35,22 +34,19 @@ namespace EPAServeur.Services #region services async public async Task> RecupererDemandesDelegation(Guid? idReferent) { - CollaborateurDTO referentDTO; - try - { - referentDTO = await collaborateurService.GetCollaborateurByIdAsync(idReferent); - } - catch(CollaborateurNotFoundException) + IEnumerable demandesDelegation = context.DemandeDelegation.Include(d => d.Ep).Where(d => d.IdReferent.Equals(idReferent) && d.EtatDemande.Equals(EtatDemande.EnAttente)); + + List guids = demandesDelegation.SelectMany(demande => new[] { (Guid?)demande.Ep.IdCollaborateur, demande.Ep.IdReferent }).ToList(); + guids.Add(idReferent); + IEnumerable collaborateursDTO = await collaborateurService.GetCollaborateurDTOsAsync(guids); + + CollaborateurDTO referent = collaborateursDTO.FirstOrDefault(c => c.Id.Equals(idReferent)); + if(referent == null) { throw new ReferentNotFoundException(); } - - var tasks = from demandeDelegation in context.DemandeDelegation.Include( d => d.Ep) - where demandeDelegation.IdReferent.Equals(idReferent) && demandeDelegation.EtatDemande.Equals(EtatDemande.EnAttente) - select GetDemandeDelegationDTO(demandeDelegation, referentDTO) ; - - return await Task.WhenAll(tasks); + return demandesDelegation.Select(d => GetDemandeDelegationDTO(d, referent, collaborateursDTO)); } @@ -107,13 +103,13 @@ namespace EPAServeur.Services } - private static async Task GetEpInformationDTO(Ep ep) + private EpInformationDTO GetEpInformationDTO(Ep ep, IEnumerable collaborateurs) { return new EpInformationDTO() { Id = ep.IdEP, - Collaborateur = await collaborateurService.GetCollaborateurByIdAsync(ep.IdCollaborateur), - Referent = await collaborateurService.GetCollaborateurByIdAsync(ep.IdReferent), + Collaborateur = collaborateurs.FirstOrDefault(c => c.Id.Equals(ep.IdCollaborateur)), + Referent = collaborateurs.FirstOrDefault(c => c.Id.Equals(ep.IdReferent)), DatePrevisionnelle = ep.DatePrevisionnelle, DateDisponibilite = ep.DateDisponibilite, Statut = ep.Statut, @@ -122,7 +118,7 @@ namespace EPAServeur.Services }; } - private static async Task GetDemandeDelegationDTO(DemandeDelegation demande, CollaborateurDTO referent ) + private DemandeDelegationDTO GetDemandeDelegationDTO(DemandeDelegation demande, CollaborateurDTO referent, IEnumerable collaborateurs) { return new DemandeDelegationDTO() { @@ -131,7 +127,7 @@ namespace EPAServeur.Services EtatDemande = demande.EtatDemande, Referent = referent, RaisonDemande = demande.RaisonDemande, - Ep = await GetEpInformationDTO( demande.Ep) + Ep = GetEpInformationDTO(demande.Ep, collaborateurs) }; }