diff --git a/EPAServeur/Services/FormationService.cs b/EPAServeur/Services/FormationService.cs index 67c4c95..5040dc2 100644 --- a/EPAServeur/Services/FormationService.cs +++ b/EPAServeur/Services/FormationService.cs @@ -82,7 +82,7 @@ namespace EPAServeur.Services /// 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, int? statutFormation, string texte, string tri) + public IEnumerable GetFormations(bool? asc, int? numPage, int? parPAge, long? idAgence, int? statutFormation, string texte, string tri) { IEnumerable formations; IEnumerable formationDTOs; @@ -129,10 +129,6 @@ namespace EPAServeur.Services .Include(formation => formation.TypeFormation).Skip(skip).Take(take); } - - if (formations == null) - return null; - formationDTOs = formations.Where(formation => formation.Intitule.ToLower().Contains(texte)).Select(formation => GetFormationDTO(formation)); return formationDTOs; @@ -148,7 +144,7 @@ namespace EPAServeur.Services /// Texte permettant d'identifier l'objet rechercher /// Colonne du tableau sur lequel le tri s'effectue /// - public async Task> GetFormationsAsync(bool? asc, int? numPage, int? parPAge, int? idAgence, int? statutFormation, string texte, string tri) + public async Task> GetFormationsAsync(bool? asc, int? numPage, int? parPAge, long? idAgence, int? statutFormation, string texte, string tri) { IEnumerable formations; IEnumerable formationDTOs; @@ -199,10 +195,6 @@ namespace EPAServeur.Services } - - if (formations == null) - return null; - formationDTOs = formations.Where(formation => formation.Intitule.ToLower().Contains(texte)).Select(formation => GetFormationDTO(formation)); return formationDTOs; @@ -218,7 +210,7 @@ namespace EPAServeur.Services /// 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) + public IEnumerable GetFormationAnnulees(bool? asc, int? numPage, int? parPAge, long? idAgence, string texte, string tri) { IEnumerable formations; IEnumerable formationDTOs; @@ -244,9 +236,6 @@ namespace EPAServeur.Services .Include(formation => formation.Origine) .Include(formation => formation.TypeFormation).Skip(skip).Take(take); - if (formations == null) - return null; - formationDTOs = formations.Where(formation => formation.Intitule.ToLower().Contains(texte)).Select(formation => GetFormationDTO(formation)); return formationDTOs; @@ -262,7 +251,7 @@ namespace EPAServeur.Services /// Texte permettant d'identifier l'objet rechercher /// Colonne du tableau sur lequel le tri s'effectue /// - public async Task> GetFormationAnnuleesAsync(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri) + public async Task> GetFormationAnnuleesAsync(bool? asc, int? numPage, int? parPAge, long? idAgence, string texte, string tri) { IEnumerable formations; IEnumerable formationDTOs; @@ -288,9 +277,6 @@ namespace EPAServeur.Services .Include(formation => formation.Origine) .Include(formation => formation.TypeFormation).Skip(skip).Take(take).ToListAsync(); - if (formations == null) - return null; - formationDTOs = formations.Where(formation => formation.Intitule.ToLower().Contains(texte)).Select(formation => GetFormationDTO(formation)); return formationDTOs; @@ -306,7 +292,7 @@ namespace EPAServeur.Services /// 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) + public IEnumerable GetFormationRealisees(bool? asc, int? numPage, int? parPAge, long? idAgence, string texte, string tri) { IEnumerable formations; IEnumerable formationDTOs; @@ -340,10 +326,6 @@ namespace EPAServeur.Services } - - if (formations == null) - return null; - formationDTOs = formations.Where(formation => formation.Intitule.ToLower().Contains(texte)).Select(formation => GetFormationDTO(formation)); return formationDTOs; @@ -359,7 +341,7 @@ namespace EPAServeur.Services /// Texte permettant d'identifier l'objet rechercher /// Colonne du tableau sur lequel le tri s'effectue /// - public async Task> GetFormationRealiseeAsync(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri) + public async Task> GetFormationRealiseesAsync(bool? asc, int? numPage, int? parPAge, long? idAgence, string texte, string tri) { IEnumerable formations; IEnumerable formationDTOs; @@ -392,10 +374,6 @@ namespace EPAServeur.Services .Include(formation => formation.TypeFormation).Skip(skip).Take(take).ToListAsync(); } - - if (formations == null) - return null; - formationDTOs = formations.Where(formation => formation.Intitule.ToLower().Contains(texte)).Select(formation => GetFormationDTO(formation)); return formationDTOs; @@ -411,7 +389,7 @@ namespace EPAServeur.Services /// 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) + public IEnumerable GetProchainesFormation(bool? asc, int? numPage, int? parPAge, long? idAgence, string texte, string tri) { IEnumerable formations; IEnumerable formationDTOs; @@ -442,9 +420,6 @@ namespace EPAServeur.Services .Include(formation => formation.TypeFormation).Skip(skip).Take(take); } - if (formations == null) - return null; - formationDTOs = formations.Where(formation => formation.Intitule.ToLower().Contains(texte)).Select(formation => GetFormationDTO(formation)); return formationDTOs; @@ -460,7 +435,7 @@ namespace EPAServeur.Services /// Texte permettant d'identifier l'objet rechercher /// Colonne du tableau sur lequel le tri s'effectue /// - public async Task> GetProchainesFormationAsync(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri) + public async Task> GetProchainesFormationAsync(bool? asc, int? numPage, int? parPAge, long? idAgence, string texte, string tri) { IEnumerable formations; IEnumerable formationDTOs; @@ -492,9 +467,6 @@ namespace EPAServeur.Services .Include(formation => formation.TypeFormation).Skip(skip).Take(take).ToListAsync(); } - if (formations == null) - return null; - formationDTOs = formations.Where(formation => formation.Intitule.ToLower().Contains(texte)).Select(formation => GetFormationDTO(formation)); return formationDTOs; @@ -511,9 +483,6 @@ namespace EPAServeur.Services modeFormations = epContext.ModeFormation; - if (modeFormations == null) - return null; - modeFormationDTOs = modeFormations.Select(modeFormation => GetModeFormationDTO(modeFormation)); return modeFormationDTOs; @@ -529,9 +498,6 @@ namespace EPAServeur.Services IEnumerable modeFormationDTOs; modeFormations = await epContext.ModeFormation.ToListAsync(); - if (modeFormations == null) - return null; - modeFormationDTOs = modeFormations.Select(modeFormation => GetModeFormationDTO(modeFormation)); return modeFormationDTOs; @@ -548,9 +514,6 @@ namespace EPAServeur.Services origineFormations = epContext.OrigineFormation; - if (origineFormations == null) - return null; - origineFormationDTOs = origineFormations.Select(origineFormation => GetOrigineFormationDTO(origineFormation)); return origineFormationDTOs; @@ -567,9 +530,6 @@ namespace EPAServeur.Services origineFormations = await epContext.OrigineFormation.ToListAsync(); - if (origineFormations == null) - return null; - origineFormationDTOs = origineFormations.Select(origineFormation => GetOrigineFormationDTO(origineFormation)); return origineFormationDTOs; @@ -586,9 +546,6 @@ namespace EPAServeur.Services statutFormations = epContext.StatutFormation; - if (statutFormations == null) - return null; - statutFormationDTOs = statutFormations.Select(statutFormation => GetStatutFormationDTO(statutFormation)); return statutFormationDTOs; @@ -605,9 +562,6 @@ namespace EPAServeur.Services statutFormations = await epContext.StatutFormation.ToListAsync(); - if (statutFormations == null) - return null; - statutFormationDTOs = statutFormations.Select(statutFormation => GetStatutFormationDTO(statutFormation)); return statutFormationDTOs; @@ -624,9 +578,6 @@ namespace EPAServeur.Services typeFormations = epContext.TypeFormation; - if (typeFormations == null) - return null; - typeFormationDTOs = typeFormations.Select(typeFormation => GetTypeFormationDTO(typeFormation)); return typeFormationDTOs; @@ -643,9 +594,6 @@ namespace EPAServeur.Services typeFormations = await epContext.TypeFormation.ToListAsync(); - if (typeFormations == null) - return null; - typeFormationDTOs = typeFormations.Select(typeFormation => GetTypeFormationDTO(typeFormation)); return typeFormationDTOs; @@ -664,9 +612,7 @@ namespace EPAServeur.Services Formation formation = new Formation(); formation = SetFormation(formation, formationDTO); - if (formation.Statut != null) - epContext.StatutFormation.Attach(formation.Statut); - + epContext.StatutFormation.Attach(formation.Statut); epContext.OrigineFormation.Attach(formation.Origine); epContext.ModeFormation.Attach(formation.ModeFormation); epContext.TypeFormation.Attach(formation.TypeFormation); @@ -684,15 +630,13 @@ namespace EPAServeur.Services /// public async Task AddFormationAsync(FormationDTO formationDTO) { - if (!IsFormationValide(formationDTO)) + if (!await IsFormationValideAsync(formationDTO)) throw new FormationInvalidException(); Formation formation = new Formation(); formation = SetFormation(formation, formationDTO); - if (formation.Statut != null) - epContext.StatutFormation.Attach(formation.Statut); - + epContext.StatutFormation.Attach(formation.Statut); epContext.OrigineFormation.Attach(formation.Origine); epContext.ModeFormation.Attach(formation.ModeFormation); epContext.TypeFormation.Attach(formation.TypeFormation); @@ -714,10 +658,10 @@ namespace EPAServeur.Services if (!IsFormationValide(formationDTO)) throw new FormationInvalidException(); - if (formationDTO == null && !formationDTO.Id.HasValue && formationDTO.Id.Value != idFormation) + if (!formationDTO.Id.HasValue || formationDTO.Id.Value != idFormation) throw new FormationIncompatibleIdException(); - Formation formation = epContext.Formation.Find(idFormation); + Formation formation = epContext.Formation.Find(idFormation.Value); if (formation == null) return null; @@ -740,10 +684,10 @@ namespace EPAServeur.Services if (!IsFormationValide(formationDTO)) throw new FormationInvalidException(); - if (formationDTO == null && !formationDTO.Id.HasValue && formationDTO.Id.Value != idFormation) + if (!formationDTO.Id.HasValue || formationDTO.Id.Value != idFormation) throw new FormationIncompatibleIdException(); - Formation formation = await epContext.Formation.FindAsync(idFormation); + Formation formation = await epContext.Formation.FindAsync(idFormation.Value); if (formation == null) return null; @@ -761,14 +705,17 @@ namespace EPAServeur.Services /// public FormationDTO DeleteFormationById(long? idFormation) { - Formation formation = epContext.Formation.Find(idFormation); + if (!idFormation.HasValue) + throw new FormationIncompatibleIdException(); + + + Formation formation = epContext.Formation.Find(idFormation.Value); if (formation == null) throw new FormationNotFoundException(); epContext.Remove(formation); - epContext.SaveChanges(); return GetFormationDTO(formation); @@ -781,7 +728,10 @@ namespace EPAServeur.Services /// public async Task DeleteFormationByIdAsync(long? idFormation) { - Formation formation = await epContext.Formation.FindAsync(idFormation); + if (!idFormation.HasValue) + throw new FormationIncompatibleIdException(); + + Formation formation = await epContext.Formation.FindAsync(idFormation.Value); if (formation == null) throw new FormationNotFoundException(); @@ -798,14 +748,40 @@ namespace EPAServeur.Services /// /// Vérifier si un objet FormationDTO est valide pour ajout ou mise à jour /// - /// Un objet FormationDTO est valide si aucune de ses propriétés n'est à null + /// Un objet FormationDTO est valide si aucune de ses propriétés n'est à null,si la date de début de la formation est inférieur à la date de fin et si le statut, l'origine,le mode et le type sont présents dans la base de données /// /// true si l'objet est valide, false sinon private bool IsFormationValide(FormationDTO formation) { - return !(formation == null || formation.IdAgence == null || formation.Intitule == null || formation.Organisme == null); + return !(formation == null || formation.Statut == null || formation.Mode == null || formation.Origine == null || formation.Type == null + || !formation.IdAgence.HasValue || formation.IdAgence == 0 + || string.IsNullOrWhiteSpace(formation.Intitule) || string.IsNullOrWhiteSpace(formation.Organisme) + || !formation.DateDebut.HasValue || !formation.DateFin.HasValue || formation.DateDebut.Value > formation.DateFin.Value + || !formation.Statut.Id.HasValue || !formation.Mode.Id.HasValue || !formation.Origine.Id.HasValue || !formation.Type.Id.HasValue + || !epContext.StatutFormation.Any(statut => statut.Id == formation.Statut.Id.Value && statut.Libelle == formation.Statut.Libelle) + || !epContext.ModeFormation.Any(mode => mode.Id == formation.Mode.Id.Value && mode.Libelle == formation.Mode.Libelle) + || !epContext.OrigineFormation.Any(origine => origine.Id == formation.Origine.Id.Value && origine.Libelle == formation.Origine.Libelle) + || !epContext.TypeFormation.Any(type => type.Id == formation.Type.Id.Value && type.Libelle == formation.Type.Libelle)); } + /// + /// Vérifier si un objet FormationDTO est valide pour ajout ou mise à jour + /// + /// Un objet FormationDTO est valide si aucune de ses propriétés n'est à null,si la date de début de la formation est inférieur à la date de fin et si le statut, l'origine,le mode et le type sont présents dans la base de données + /// + /// true si l'objet est valide, false sinon + private async Task IsFormationValideAsync(FormationDTO formation) + { + return !(formation == null || formation.Statut == null || formation.Mode == null || formation.Origine == null || formation.Type == null + || !formation.IdAgence.HasValue || formation.IdAgence == 0 + || string.IsNullOrWhiteSpace(formation.Intitule) || string.IsNullOrWhiteSpace(formation.Organisme) + || !formation.DateDebut.HasValue || !formation.DateFin.HasValue || formation.DateDebut.Value > formation.DateFin.Value + || !formation.Statut.Id.HasValue || !formation.Mode.Id.HasValue || !formation.Origine.Id.HasValue || !formation.Type.Id.HasValue + || ! await epContext.StatutFormation.AnyAsync(statut => statut.Id == formation.Statut.Id.Value && statut.Libelle == formation.Statut.Libelle) + || ! await epContext.ModeFormation.AnyAsync(mode => mode.Id == formation.Mode.Id.Value && mode.Libelle == formation.Mode.Libelle) + || ! await epContext.OrigineFormation.AnyAsync(origine => origine.Id == formation.Origine.Id.Value && origine.Libelle == formation.Origine.Libelle) + || ! await epContext.TypeFormation.AnyAsync(type => type.Id == formation.Type.Id.Value && type.Libelle == formation.Type.Libelle)); + } #region Object to DTO /// /// Récupère un objet FormationDTO en fonction d'un objet Formation @@ -936,8 +912,6 @@ namespace EPAServeur.Services /// private OrigineFormation GetOrigineFormation(OrigineFormationDTO origineFormationDTO) { - if (origineFormationDTO == null) - return null; OrigineFormation origineFormation = new OrigineFormation() { Id = origineFormationDTO.Id.Value, @@ -953,8 +927,6 @@ namespace EPAServeur.Services /// private StatutFormation GetStatutFormation(StatutFormationDTO statutFormationDTO) { - if (statutFormationDTO == null) - return null; StatutFormation statutFormation = new StatutFormation() { Id = statutFormationDTO.Id.Value, @@ -970,8 +942,6 @@ namespace EPAServeur.Services /// private ModeFormation GetModeFormation(ModeFormationDTO modeFormationDTO) { - if (modeFormationDTO == null) - return null; ModeFormation modeFormation = new ModeFormation() { Id = modeFormationDTO.Id.Value, @@ -987,8 +957,6 @@ namespace EPAServeur.Services /// private TypeFormation GetTypeFormation(TypeFormationDTO typeFormationDTO) { - if (typeFormationDTO == null) - return null; TypeFormation typeFormation = new TypeFormation() { Id = typeFormationDTO.Id.Value,