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));
IEnumerable<CollaborateurDTO> collaborateurDTOs = await GetCollaborateurDTOs(formation.ParticipationsFormation);
return GetFormationDTO(formation, collaborateurDTOs);
if (formation.ParticipationsFormation.Count > 0)
{
IEnumerable<CollaborateurDTO> collaborateurDTOs = await GetCollaborateurDTOs(formation.ParticipationsFormation);
return GetFormationDTO(formation, collaborateurDTOs);
}
else
{
return GetFormationDTO(formation);
}
}
/// <summary>
@ -265,12 +271,19 @@ namespace EPAServeur.Services
/// <returns></returns>
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);
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
/// <returns></returns>
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)
throw new FormationNotFoundException(string.Format("Aucune formation trouvée avec l'id suivant: {0}.", idFormation));
@ -526,6 +546,9 @@ namespace EPAServeur.Services
/// <returns></returns>
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
/// <returns></returns>
private FormationDTO GetFormationDTO(Formation formation, IEnumerable<CollaborateurDTO> collaborateurDTOs)
{
if (formation == null || collaborateurDTOs == null || !collaborateurDTOs.Any())
return null;
FormationDTO formationDTO = new FormationDTO()
{
Id = formation.IdFormation,
@ -582,6 +609,9 @@ namespace EPAServeur.Services
/// <returns></returns>
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
/// <returns></returns>
private async Task<IEnumerable<CollaborateurDTO>> GetCollaborateurDTOs(List<ParticipationFormation> participationsFormation)
{
if (participationsFormation.Count == 0)
if (participationsFormation == null || participationsFormation.Count == 0)
return null;
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>
private ParticipationFormationDTO GetParticipationFormationDTO(ParticipationFormation participationFormation, IEnumerable<CollaborateurDTO> collaborateurDTOs)
{
if (participationFormation == null || collaborateurDTOs == null || !collaborateurDTOs.Any())
return null;
ParticipationFormationDTO participationFormationDTO = new ParticipationFormationDTO()
{
Id = participationFormation.IdParticipationFormation,
@ -728,6 +769,9 @@ namespace EPAServeur.Services
/// <returns></returns>
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);
}
@ -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
/// <returns></returns>
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
/// <returns></returns>
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
/// <returns></returns>
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
/// <returns></returns>
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
/// <returns></returns>
private TypeFormation GetTypeFormation(TypeFormationDTO typeFormationDTO)
{
if (typeFormationDTO == null)
return null;
TypeFormation typeFormation = new TypeFormation()
{
IdTypeFormation = typeFormationDTO.Id.Value,
Libelle = typeFormationDTO.Libelle
};
return typeFormation;
}

Loading…
Cancel
Save