Gestion des objets null pour les mappages de model en dto dans le service formation

develop
jboinembalome 4 years ago
parent 67b9d60371
commit 2d38013907
  1. 83
      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)); throw new FormationNotFoundException(string.Format("Aucune formation trouvée avec l'id suivant: {0}.", idFormation));
IEnumerable<CollaborateurDTO> collaborateurDTOs = await GetCollaborateurDTOs(formation.ParticipationsFormation); if (formation.ParticipationsFormation.Count > 0)
{
return GetFormationDTO(formation, collaborateurDTOs); IEnumerable<CollaborateurDTO> collaborateurDTOs = await GetCollaborateurDTOs(formation.ParticipationsFormation);
return GetFormationDTO(formation, collaborateurDTOs);
}
else
{
return GetFormationDTO(formation);
}
} }
/// <summary> /// <summary>
@ -265,12 +271,19 @@ namespace EPAServeur.Services
/// <returns></returns> /// <returns></returns>
public async Task<FormationDTO> UpdateFormationAsync(long idFormation, FormationDTO formationDTO) public async Task<FormationDTO> 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); 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) if (formation == null)
throw new FormationNotFoundException(string.Format("Aucune formation trouvée avec l'id suivant: {0}.", idFormation)); throw new FormationNotFoundException(string.Format("Aucune formation trouvée avec l'id suivant: {0}.", idFormation));
@ -289,7 +302,14 @@ namespace EPAServeur.Services
/// <returns></returns> /// <returns></returns>
public async Task<FormationDTO> DeleteFormationByIdAsync(long idFormation) public async Task<FormationDTO> 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) if (formation == null)
throw new FormationNotFoundException(string.Format("Aucune formation trouvée avec l'id suivant: {0}.", idFormation)); throw new FormationNotFoundException(string.Format("Aucune formation trouvée avec l'id suivant: {0}.", idFormation));
@ -526,6 +546,9 @@ namespace EPAServeur.Services
/// <returns></returns> /// <returns></returns>
private FormationDTO GetFormationDTO(Formation formation) private FormationDTO GetFormationDTO(Formation formation)
{ {
if (formation == null)
return null;
FormationDTO formationDTO = new FormationDTO() FormationDTO formationDTO = new FormationDTO()
{ {
Id = formation.IdFormation, Id = formation.IdFormation,
@ -537,6 +560,7 @@ namespace EPAServeur.Services
Jour = formation.Jour, Jour = formation.Jour,
Organisme = formation.Organisme, Organisme = formation.Organisme,
EstCertifiee = formation.EstCertifiee, EstCertifiee = formation.EstCertifiee,
EstRealisee = formation.EstRealisee,
Origine = GetOrigineFormationDTO(formation.Origine), Origine = GetOrigineFormationDTO(formation.Origine),
Statut = GetStatutFormationDTO(formation.Statut), Statut = GetStatutFormationDTO(formation.Statut),
Mode = GetModeFormationDTO(formation.ModeFormation), Mode = GetModeFormationDTO(formation.ModeFormation),
@ -553,6 +577,9 @@ namespace EPAServeur.Services
/// <returns></returns> /// <returns></returns>
private FormationDTO GetFormationDTO(Formation formation, IEnumerable<CollaborateurDTO> collaborateurDTOs) private FormationDTO GetFormationDTO(Formation formation, IEnumerable<CollaborateurDTO> collaborateurDTOs)
{ {
if (formation == null || collaborateurDTOs == null || !collaborateurDTOs.Any())
return null;
FormationDTO formationDTO = new FormationDTO() FormationDTO formationDTO = new FormationDTO()
{ {
Id = formation.IdFormation, Id = formation.IdFormation,
@ -582,6 +609,9 @@ namespace EPAServeur.Services
/// <returns></returns> /// <returns></returns>
private FormationDetailsDTO GetFormationDetailsDTO(Formation formation) private FormationDetailsDTO GetFormationDetailsDTO(Formation formation)
{ {
if (formation == null)
return null;
FormationDetailsDTO formationDTO = new FormationDetailsDTO() FormationDetailsDTO formationDTO = new FormationDetailsDTO()
{ {
Id = formation.IdFormation, Id = formation.IdFormation,
@ -607,11 +637,13 @@ namespace EPAServeur.Services
{ {
if (origineFormation == null) if (origineFormation == null)
return null; return null;
OrigineFormationDTO origineFormationDTO = new OrigineFormationDTO() OrigineFormationDTO origineFormationDTO = new OrigineFormationDTO()
{ {
Id = origineFormation.IdOrigineFormation, Id = origineFormation.IdOrigineFormation,
Libelle = origineFormation.Libelle Libelle = origineFormation.Libelle
}; };
return origineFormationDTO; return origineFormationDTO;
} }
@ -624,11 +656,13 @@ namespace EPAServeur.Services
{ {
if (statutFormation == null) if (statutFormation == null)
return null; return null;
StatutFormationDTO statutFormationDTO = new StatutFormationDTO() StatutFormationDTO statutFormationDTO = new StatutFormationDTO()
{ {
Id = statutFormation.IdStatutFormation, Id = statutFormation.IdStatutFormation,
Libelle = statutFormation.Libelle Libelle = statutFormation.Libelle
}; };
return statutFormationDTO; return statutFormationDTO;
} }
@ -641,11 +675,13 @@ namespace EPAServeur.Services
{ {
if (modeFormation == null) if (modeFormation == null)
return null; return null;
ModeFormationDTO modeFormationDTO = new ModeFormationDTO() ModeFormationDTO modeFormationDTO = new ModeFormationDTO()
{ {
Id = modeFormation.IdModeFormation, Id = modeFormation.IdModeFormation,
Libelle = modeFormation.Libelle Libelle = modeFormation.Libelle
}; };
return modeFormationDTO; return modeFormationDTO;
} }
@ -658,11 +694,13 @@ namespace EPAServeur.Services
{ {
if (typeFormation == null) if (typeFormation == null)
return null; return null;
TypeFormationDTO typeFormationDTO = new TypeFormationDTO() TypeFormationDTO typeFormationDTO = new TypeFormationDTO()
{ {
Id = typeFormation.IdTypeFormation, Id = typeFormation.IdTypeFormation,
Libelle = typeFormation.Libelle Libelle = typeFormation.Libelle
}; };
return typeFormationDTO; return typeFormationDTO;
} }
@ -673,7 +711,7 @@ namespace EPAServeur.Services
/// <returns></returns> /// <returns></returns>
private async Task<IEnumerable<CollaborateurDTO>> GetCollaborateurDTOs(List<ParticipationFormation> participationsFormation) private async Task<IEnumerable<CollaborateurDTO>> GetCollaborateurDTOs(List<ParticipationFormation> participationsFormation)
{ {
if (participationsFormation.Count == 0) if (participationsFormation == null || participationsFormation.Count == 0)
return null; return null;
List<Guid?> guids = participationsFormation.SelectMany(participationFormation => new[] { (Guid?)participationFormation.DemandeFormation.Ep.IdCollaborateur, participationFormation.DemandeFormation.Ep.IdReferent }).ToList(); List<Guid?> guids = participationsFormation.SelectMany(participationFormation => new[] { (Guid?)participationFormation.DemandeFormation.Ep.IdCollaborateur, participationFormation.DemandeFormation.Ep.IdReferent }).ToList();
@ -707,6 +745,9 @@ namespace EPAServeur.Services
/// <returns></returns> /// <returns></returns>
private ParticipationFormationDTO GetParticipationFormationDTO(ParticipationFormation participationFormation, IEnumerable<CollaborateurDTO> collaborateurDTOs) private ParticipationFormationDTO GetParticipationFormationDTO(ParticipationFormation participationFormation, IEnumerable<CollaborateurDTO> collaborateurDTOs)
{ {
if (participationFormation == null || collaborateurDTOs == null || !collaborateurDTOs.Any())
return null;
ParticipationFormationDTO participationFormationDTO = new ParticipationFormationDTO() ParticipationFormationDTO participationFormationDTO = new ParticipationFormationDTO()
{ {
Id = participationFormation.IdParticipationFormation, Id = participationFormation.IdParticipationFormation,
@ -728,6 +769,9 @@ namespace EPAServeur.Services
/// <returns></returns> /// <returns></returns>
private CollaborateurDTO GetCollaborateurDTO(ParticipationFormation participationFormation, IEnumerable<CollaborateurDTO> collaborateurDTOs) private CollaborateurDTO GetCollaborateurDTO(ParticipationFormation participationFormation, IEnumerable<CollaborateurDTO> collaborateurDTOs)
{ {
if (participationFormation == null || collaborateurDTOs == null || !collaborateurDTOs.Any())
return null;
return collaborateurDTOs.FirstOrDefault(collaborateurDTO => collaborateurDTO.Id == participationFormation.DemandeFormation.Ep.IdCollaborateur); return collaborateurDTOs.FirstOrDefault(collaborateurDTO => collaborateurDTO.Id == participationFormation.DemandeFormation.Ep.IdCollaborateur);
} }
@ -741,6 +785,8 @@ namespace EPAServeur.Services
CollaborateurDTO collaborateur; CollaborateurDTO collaborateur;
CollaborateurDTO referent; CollaborateurDTO referent;
if (ep == null || collaborateurDTOs == null || !collaborateurDTOs.Any())
return null;
collaborateur = collaborateurDTOs.FirstOrDefault(collaborateurDTO => collaborateurDTO.Id == ep.IdCollaborateur); collaborateur = collaborateurDTOs.FirstOrDefault(collaborateurDTO => collaborateurDTO.Id == ep.IdCollaborateur);
referent = collaborateurDTOs.FirstOrDefault(collaborateurDTO => collaborateurDTO.Id == ep.IdReferent); referent = collaborateurDTOs.FirstOrDefault(collaborateurDTO => collaborateurDTO.Id == ep.IdReferent);
@ -773,6 +819,9 @@ namespace EPAServeur.Services
/// <returns></returns> /// <returns></returns>
private Formation SetFormation(Formation formation, FormationDTO formationDTO) private Formation SetFormation(Formation formation, FormationDTO formationDTO)
{ {
if (formation == null || formationDTO == null)
return null;
formation.Intitule = formationDTO.Intitule; formation.Intitule = formationDTO.Intitule;
formation.IdAgence = formationDTO.IdAgence.Value; formation.IdAgence = formationDTO.IdAgence.Value;
formation.DateDebut = formationDTO.DateDebut.Value; formation.DateDebut = formationDTO.DateDebut.Value;
@ -797,11 +846,15 @@ namespace EPAServeur.Services
/// <returns></returns> /// <returns></returns>
private OrigineFormation GetOrigineFormation(OrigineFormationDTO origineFormationDTO) private OrigineFormation GetOrigineFormation(OrigineFormationDTO origineFormationDTO)
{ {
if (origineFormationDTO == null)
return null;
OrigineFormation origineFormation = new OrigineFormation() OrigineFormation origineFormation = new OrigineFormation()
{ {
IdOrigineFormation = origineFormationDTO.Id.Value, IdOrigineFormation = origineFormationDTO.Id.Value,
Libelle = origineFormationDTO.Libelle Libelle = origineFormationDTO.Libelle
}; };
return origineFormation; return origineFormation;
} }
@ -812,11 +865,15 @@ namespace EPAServeur.Services
/// <returns></returns> /// <returns></returns>
private StatutFormation GetStatutFormation(StatutFormationDTO statutFormationDTO) private StatutFormation GetStatutFormation(StatutFormationDTO statutFormationDTO)
{ {
if (statutFormationDTO == null)
return null;
StatutFormation statutFormation = new StatutFormation() StatutFormation statutFormation = new StatutFormation()
{ {
IdStatutFormation = statutFormationDTO.Id.Value, IdStatutFormation = statutFormationDTO.Id.Value,
Libelle = statutFormationDTO.Libelle Libelle = statutFormationDTO.Libelle
}; };
return statutFormation; return statutFormation;
} }
@ -827,11 +884,15 @@ namespace EPAServeur.Services
/// <returns></returns> /// <returns></returns>
private ModeFormation GetModeFormation(ModeFormationDTO modeFormationDTO) private ModeFormation GetModeFormation(ModeFormationDTO modeFormationDTO)
{ {
if (modeFormationDTO == null)
return null;
ModeFormation modeFormation = new ModeFormation() ModeFormation modeFormation = new ModeFormation()
{ {
IdModeFormation = modeFormationDTO.Id.Value, IdModeFormation = modeFormationDTO.Id.Value,
Libelle = modeFormationDTO.Libelle Libelle = modeFormationDTO.Libelle
}; };
return modeFormation; return modeFormation;
} }
@ -842,11 +903,15 @@ namespace EPAServeur.Services
/// <returns></returns> /// <returns></returns>
private TypeFormation GetTypeFormation(TypeFormationDTO typeFormationDTO) private TypeFormation GetTypeFormation(TypeFormationDTO typeFormationDTO)
{ {
if (typeFormationDTO == null)
return null;
TypeFormation typeFormation = new TypeFormation() TypeFormation typeFormation = new TypeFormation()
{ {
IdTypeFormation = typeFormationDTO.Id.Value, IdTypeFormation = typeFormationDTO.Id.Value,
Libelle = typeFormationDTO.Libelle Libelle = typeFormationDTO.Libelle
}; };
return typeFormation; return typeFormation;
} }

Loading…
Cancel
Save