From a8e8b85b0a40df8f2dc7934581750d8802635fe2 Mon Sep 17 00:00:00 2001 From: jboinembalome Date: Thu, 14 Jan 2021 10:57:34 +0100 Subject: [PATCH] MAJ du service formation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Modification des exceptions en ajoutant des messages directement de le service. Création de méthodes privées afin d'éviter les redondances de code. Utilisation de l'interface IQueryable afin d'améliorer les performances du serveur web en utilisant les ressources de la BDD. --- EPAServeur/Services/FormationService.cs | 817 ++++++++---------------- 1 file changed, 265 insertions(+), 552 deletions(-) diff --git a/EPAServeur/Services/FormationService.cs b/EPAServeur/Services/FormationService.cs index 378fc7a..1ef4934 100644 --- a/EPAServeur/Services/FormationService.cs +++ b/EPAServeur/Services/FormationService.cs @@ -15,8 +15,36 @@ namespace EPAServeur.Services { #region Variables + /// + /// Accès et gestion de la base de données + /// private readonly EpContext epContext; + /// + /// Nombre d'éléments min à afficher par page + /// + private readonly int minParPage = 5; + + /// + /// Nom d'éléments max à affichar par page + /// + private readonly int maxParPage = 100; + + /// + /// Nombre d'éléments à afficher par défaut par page + /// + private readonly int defaultParPage = 15; + + /// + /// Numéro de page min à afficher par défaut + /// + private readonly int defaultNumPage = 1; + + /// + /// Ordonnancement par défaut + /// + private readonly bool defaultAsc = true; + #endregion #region Contructeurs @@ -34,31 +62,12 @@ namespace EPAServeur.Services #region Méthodes Service - /// - /// Récupérer une formation par son id - /// - /// - /// - public FormationDTO GetFormationById(long? idFormation) - { - Formation formation = epContext.Formation.Include(formation => formation.Statut) - .Include(formation => formation.ModeFormation) - .Include(formation => formation.Origine) - .Include(formation => formation.TypeFormation) - .FirstOrDefault(formation => formation.IdFormation == idFormation); - - if (formation == null) - throw new FormationNotFoundException(); - - return GetFormationDTO(formation); - } - /// /// Récupérer une formation par son id de manière asynchrone /// /// /// - public async Task GetFormationByIdAsync(long? idFormation) + public async Task GetFormationByIdAsync(long idFormation) { Formation formation = await epContext.Formation.Include(formation => formation.Statut) .Include(formation => formation.ModeFormation) @@ -67,425 +76,85 @@ namespace EPAServeur.Services .FirstOrDefaultAsync(formation => formation.IdFormation == idFormation); if (formation == null) - throw new FormationNotFoundException(); + throw new FormationNotFoundException(string.Format("Aucune formation trouvée avec l'id suivant: {0}.",idFormation)); return GetFormationDTO(formation); } /// - /// Récupérer la liste des formations + /// Récupérer la liste des formations de manière asynchrone /// /// Préciser si les données sont dans l'ordre (true) ou dans l'ordre inverse (false) /// Numéro de la page du tableau qui affiche les données - /// Nombre d'éléments affiché sur chaque page du tableau + /// Nombre d'éléments affiché sur chaque page du tableau /// id de l'agence à laquelle sont rattachées les données à récupérer /// 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, long? idAgence, int? statutFormation, string texte, string tri) + public async Task> GetFormationsAsync(long? idAgence, List idStatuts, bool? asc, int? numPage, int? parPage, string texte, string tri, DateTime? dateDebut, DateTime? dateFin) { + IQueryable query; IEnumerable formations; IEnumerable formationDTOs; - if (texte == null) - texte = ""; - else - texte = texte.ToLower(); - - int skip = (numPage.Value - 1) * parPAge.Value; - int take = parPAge.Value; - - if (statutFormation != null && idAgence != null) - { - formations = epContext.Formation + query = epContext.Formation .Include(formation => formation.Statut) .Include(formation => formation.ModeFormation) .Include(formation => formation.Origine) - .Include(formation => formation.TypeFormation) - .Where(formation => formation.Statut.IdStatutFormation == statutFormation && formation.IdAgence == idAgence).Skip(skip).Take(take); - } - else if (statutFormation != null && idAgence == null) - { - formations = epContext.Formation - .Include(formation => formation.Statut) - .Include(formation => formation.ModeFormation) - .Include(formation => formation.Origine) - .Include(formation => formation.TypeFormation) - .Where(formation => formation.Statut.IdStatutFormation == statutFormation).Skip(skip).Take(take); - } - else if (idAgence != null) - { - formations = epContext.Formation.Where(formation => formation.IdAgence == idAgence) - .Include(formation => formation.Statut) - .Include(formation => formation.ModeFormation) - .Include(formation => formation.Origine) - .Include(formation => formation.TypeFormation).Skip(skip).Take(take); - } - else - { - formations = epContext.Formation.Include(formation => formation.Statut) - .Include(formation => formation.ModeFormation) - .Include(formation => formation.Origine) - .Include(formation => formation.TypeFormation).Skip(skip).Take(take); - } + .Include(formation => formation.TypeFormation); - formationDTOs = formations.Where(formation => formation.Intitule.ToLower().Contains(texte)).Select(formation => GetFormationDTO(formation)); - - return formationDTOs; - } - - /// - /// Récupérer la liste des formations de manière asynchrone - /// - /// Préciser si les données sont dans l'ordre (true) ou dans l'ordre inverse (false) - /// Numéro de la page du tableau qui affiche les données - /// Nombre d'éléments affiché sur chaque page du tableau - /// id de l'agence à laquelle sont rattachées les données à récupérer - /// 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, long? idAgence, int? statutFormation, string texte, string tri) - { - IEnumerable formations; - IEnumerable formationDTOs; - - if (texte == null) - texte = ""; - else - texte = texte.ToLower(); - - int skip = (numPage.Value - 1) * parPAge.Value; - int take = parPAge.Value; - - if (statutFormation != null && idAgence != null) - { - formations = await epContext.Formation - .Include(formation => formation.Statut) - .Include(formation => formation.ModeFormation) - .Include(formation => formation.Origine) - .Include(formation => formation.TypeFormation) - .Where(formation => formation.Statut.IdStatutFormation == statutFormation && formation.IdAgence == idAgence).Skip(skip).Take(take).ToListAsync(); - } - else if (statutFormation != null && idAgence == null) - { - formations = await epContext.Formation - .Include(formation => formation.Statut) - .Include(formation => formation.ModeFormation) - .Include(formation => formation.Origine) - .Include(formation => formation.TypeFormation) - .Where(formation => formation.Statut.IdStatutFormation == statutFormation).Skip(skip).Take(take).ToListAsync(); - } - else if (idAgence != null) - { - - formations = await epContext.Formation - .Include(formation => formation.Statut) - .Include(formation => formation.ModeFormation) - .Include(formation => formation.Origine) - .Include(formation => formation.TypeFormation).Where(formation => formation.IdAgence == idAgence).Skip(skip).Take(take).ToListAsync(); - - } - else - { - - formations = await epContext.Formation.Include(formation => formation.Statut) - .Include(formation => formation.ModeFormation) - .Include(formation => formation.Origine) - .Include(formation => formation.TypeFormation).Skip(skip).Take(take).ToListAsync(); - - } - - formationDTOs = formations.Where(formation => formation.Intitule.ToLower().Contains(texte)).Select(formation => GetFormationDTO(formation)); - - return formationDTOs; - } - - /// - /// Récupérer les formations annulées - /// - /// Préciser si les données sont dans l'ordre (true) ou dans l'ordre inverse (false) - /// Numéro de la page du tableau qui affiche les données - /// Nombre d'éléments affiché sur chaque page du tableau - /// id de l'agence à laquelle sont rattachées les données à récupérer - /// 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, long? idAgence, string texte, string tri) - { - IEnumerable formations; - IEnumerable formationDTOs; - - if (texte == null) - texte = ""; - else - texte = texte.ToLower(); + query = IdStatutsFilter(query, idStatuts); - int skip = (numPage.Value - 1) * parPAge.Value; - int take = parPAge.Value; + query = IdAgenceFilter(query, idAgence); - if (idAgence != null) - formations = epContext.Formation.Where(formation => formation.IdAgence == idAgence && formation.Statut.IdStatutFormation == 4) - .Include(formation => formation.Statut) - .Include(formation => formation.ModeFormation) - .Include(formation => formation.Origine) - .Include(formation => formation.TypeFormation).Skip(skip).Take(take); - else - formations = epContext.Formation.Where(formation => formation.Statut.IdStatutFormation == 4) - .Include(formation => formation.Statut) - .Include(formation => formation.ModeFormation) - .Include(formation => formation.Origine) - .Include(formation => formation.TypeFormation).Skip(skip).Take(take); - - formationDTOs = formations.Where(formation => formation.Intitule.ToLower().Contains(texte)).Select(formation => GetFormationDTO(formation)); - - return formationDTOs; - } - - /// - /// Récupérer les formations annulées de manière asynchrone - /// - /// Préciser si les données sont dans l'ordre (true) ou dans l'ordre inverse (false) - /// Numéro de la page du tableau qui affiche les données - /// Nombre d'éléments affiché sur chaque page du tableau - /// id de l'agence à laquelle sont rattachées les données à récupérer - /// 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, long? idAgence, string texte, string tri) - { - IEnumerable formations; - IEnumerable formationDTOs; - - if (texte == null) - texte = ""; - else - texte = texte.ToLower(); - - int skip = (numPage.Value - 1) * parPAge.Value; - int take = parPAge.Value; - - if (idAgence != null) - formations = await epContext.Formation.Where(formation => formation.IdAgence == idAgence && formation.Statut.IdStatutFormation == 4) - .Include(formation => formation.Statut) - .Include(formation => formation.ModeFormation) - .Include(formation => formation.Origine) - .Include(formation => formation.TypeFormation).Skip(skip).Take(take).ToListAsync(); - else - formations = await epContext.Formation.Where(formation => formation.Statut.IdStatutFormation == 4) - .Include(formation => formation.Statut) - .Include(formation => formation.ModeFormation) - .Include(formation => formation.Origine) - .Include(formation => formation.TypeFormation).Skip(skip).Take(take).ToListAsync(); - - formationDTOs = formations.Where(formation => formation.Intitule.ToLower().Contains(texte)).Select(formation => GetFormationDTO(formation)); - - return formationDTOs; - } - - /// - /// Récupérer les formations réalisées - /// - /// Préciser si les données sont dans l'ordre (true) ou dans l'ordre inverse (false) - /// Numéro de la page du tableau qui affiche les données - /// Nombre d'éléments affiché sur chaque page du tableau - /// id de l'agence à laquelle sont rattachées les données à récupérer - /// Texte permettant d'identifier l'objet rechercher - /// Colonne du tableau sur lequel le tri s'effectue - /// - public IEnumerable GetFormationRealisees(bool? asc, int? numPage, int? parPAge, long? idAgence, string texte, string tri) - { - IEnumerable formations; - IEnumerable formationDTOs; - - if (texte == null) - texte = ""; - else - texte = texte.ToLower(); - - int skip = (numPage.Value - 1) * parPAge.Value; - int take = parPAge.Value; - - if (idAgence != null) - { - - formations = epContext.Formation.Where(formation => formation.IdAgence == idAgence && formation.Statut.IdStatutFormation == 3) - .Include(formation => formation.Statut) - .Include(formation => formation.ModeFormation) - .Include(formation => formation.Origine) - .Include(formation => formation.TypeFormation).Skip(skip).Take(take); - } - - else - { - - formations = epContext.Formation.Where(formation => formation.Statut.IdStatutFormation == 3) - .Include(formation => formation.Statut) - .Include(formation => formation.ModeFormation) - .Include(formation => formation.Origine) - .Include(formation => formation.TypeFormation).Skip(skip).Take(take); - - } - - formationDTOs = formations.Where(formation => formation.Intitule.ToLower().Contains(texte)).Select(formation => GetFormationDTO(formation)); - - return formationDTOs; - } - - /// - /// Récupérer les formations réalisées de manière asynchrone - /// - /// Préciser si les données sont dans l'ordre (true) ou dans l'ordre inverse (false) - /// Numéro de la page du tableau qui affiche les données - /// Nombre d'éléments affiché sur chaque page du tableau - /// id de l'agence à laquelle sont rattachées les données à récupérer - /// Texte permettant d'identifier l'objet rechercher - /// Colonne du tableau sur lequel le tri s'effectue - /// - public async Task> GetFormationRealiseesAsync(bool? asc, int? numPage, int? parPAge, long? idAgence, string texte, string tri) - { - IEnumerable formations; - IEnumerable formationDTOs; - - if (texte == null) - texte = ""; - else - texte = texte.ToLower(); - - int skip = (numPage.Value - 1) * parPAge.Value; - int take = parPAge.Value; - - if (idAgence != null) - { - - formations = await epContext.Formation.Where(formation => formation.IdAgence == idAgence && formation.Statut.IdStatutFormation == 3) - .Include(formation => formation.Statut) - .Include(formation => formation.ModeFormation) - .Include(formation => formation.Origine) - .Include(formation => formation.TypeFormation).Skip(skip).Take(take).ToListAsync(); - } - - else - { - - formations = await epContext.Formation.Where(formation => formation.Statut.IdStatutFormation == 3) - .Include(formation => formation.Statut) - .Include(formation => formation.ModeFormation) - .Include(formation => formation.Origine) - .Include(formation => formation.TypeFormation).Skip(skip).Take(take).ToListAsync(); - } - - formationDTOs = formations.Where(formation => formation.Intitule.ToLower().Contains(texte)).Select(formation => GetFormationDTO(formation)); - - return formationDTOs; - } - - /// - /// Récupérer les formations plannifiées et replannifiées - /// - /// Préciser si les données sont dans l'ordre (true) ou dans l'ordre inverse (false) - /// Numéro de la page du tableau qui affiche les données - /// Nombre d'éléments affiché sur chaque page du tableau - /// id de l'agence à laquelle sont rattachées les données à récupérer - /// 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, long? idAgence, string texte, string tri) - { - IEnumerable formations; - IEnumerable formationDTOs; + query = IntituleFilter(query, texte); - if (texte == null) - texte = ""; - else - texte = texte.ToLower(); + query = DateFilter(query, dateDebut, dateFin); - int skip = (numPage.Value - 1) * parPAge.Value; - int take = parPAge.Value; + query = OrderByColumn(query, asc, tri); - if (idAgence != null) - { - formations = epContext.Formation.Where(formation => formation.IdAgence == idAgence && (formation.Statut.IdStatutFormation == 1 || formation.Statut.IdStatutFormation == 2)) - .Include(formation => formation.Statut) - .Include(formation => formation.ModeFormation) - .Include(formation => formation.Origine) - .Include(formation => formation.TypeFormation).Skip(skip).Take(take); + query = SkipAndTake(query, parPage, numPage); - } - else - { - formations = epContext.Formation.Where(formation => (formation.Statut.IdStatutFormation == 1 || formation.Statut.IdStatutFormation == 2)) - .Include(formation => formation.Statut) - .Include(formation => formation.ModeFormation) - .Include(formation => formation.Origine) - .Include(formation => formation.TypeFormation).Skip(skip).Take(take); - } + formations = await query.ToListAsync(); - formationDTOs = formations.Where(formation => formation.Intitule.ToLower().Contains(texte)).Select(formation => GetFormationDTO(formation)); + formationDTOs = formations.Select(formation => GetFormationDTO(formation)); return formationDTOs; } /// - /// Récupérer les formations plannifiées et replannifiées de manère asynchrone + /// Récupérer le nombre total de formations de manière asynchrone /// - /// Préciser si les données sont dans l'ordre (true) ou dans l'ordre inverse (false) /// Numéro de la page du tableau qui affiche les données - /// Nombre d'éléments affiché sur chaque page du tableau + /// Nombre d'éléments affiché sur chaque page du tableau /// id de l'agence à laquelle sont rattachées les données à récupérer /// 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, long? idAgence, string texte, string tri) + public async Task GetFormationsCountAsync(long? idAgence, List idStatuts, int? numPage, int? parPage, string texte, DateTime? dateDebut, DateTime? dateFin) { - IEnumerable formations; - IEnumerable formationDTOs; + IQueryable query; + long count; - if (texte == null) - texte = ""; - else - texte = texte.ToLower(); - - int skip = (numPage.Value - 1) * parPAge.Value; - int take = parPAge.Value; - - if (idAgence != null) - { - formations = await epContext.Formation.Where(formation => formation.IdAgence == idAgence && (formation.Statut.IdStatutFormation == 1 || formation.Statut.IdStatutFormation == 2)) - .Include(formation => formation.Statut) - .Include(formation => formation.ModeFormation) - .Include(formation => formation.Origine) - .Include(formation => formation.TypeFormation).Skip(skip).Take(take).ToListAsync(); - } + query = epContext.Formation + .Include(formation => formation.Statut) + .Include(formation => formation.ModeFormation) + .Include(formation => formation.Origine) + .Include(formation => formation.TypeFormation); - else - { - formations = await epContext.Formation.Where(formation => (formation.Statut.IdStatutFormation == 1 || formation.Statut.IdStatutFormation == 2)) - .Include(formation => formation.Statut) - .Include(formation => formation.ModeFormation) - .Include(formation => formation.Origine) - .Include(formation => formation.TypeFormation).Skip(skip).Take(take).ToListAsync(); - } + query = IntituleFilter(query, texte); - formationDTOs = formations.Where(formation => formation.Intitule.ToLower().Contains(texte)).Select(formation => GetFormationDTO(formation)); + query = IdStatutsFilter(query, idStatuts); - return formationDTOs; - } + query = IdAgenceFilter(query, idAgence); - /// - /// Récupérer les modes de formation - /// - /// - public IEnumerable GetModesFormation() - { - IEnumerable modeFormations; - IEnumerable modeFormationDTOs; + query = DateFilter(query, dateDebut, dateFin); - modeFormations = epContext.ModeFormation; + query = SkipAndTake(query, parPage, numPage); - modeFormationDTOs = modeFormations.Select(modeFormation => GetModeFormationDTO(modeFormation)); + count = await query.CountAsync(); - return modeFormationDTOs; + return count; } /// @@ -496,6 +165,7 @@ namespace EPAServeur.Services { IEnumerable modeFormations; IEnumerable modeFormationDTOs; + modeFormations = await epContext.ModeFormation.ToListAsync(); modeFormationDTOs = modeFormations.Select(modeFormation => GetModeFormationDTO(modeFormation)); @@ -503,22 +173,6 @@ namespace EPAServeur.Services return modeFormationDTOs; } - /// - /// Récupérer les origines de formation - /// - /// - public IEnumerable GetOriginesFormation() - { - IEnumerable origineFormations; - IEnumerable origineFormationDTOs; - - origineFormations = epContext.OrigineFormation; - - origineFormationDTOs = origineFormations.Select(origineFormation => GetOrigineFormationDTO(origineFormation)); - - return origineFormationDTOs; - } - /// /// Récupérer les origines de formation de manière asynchrone /// @@ -535,22 +189,6 @@ namespace EPAServeur.Services return origineFormationDTOs; } - /// - /// Récupérer les statuts de formation - /// - /// - public IEnumerable GetStatutsFormation() - { - IEnumerable statutFormations; - IEnumerable statutFormationDTOs; - - statutFormations = epContext.StatutFormation; - - statutFormationDTOs = statutFormations.Select(statutFormation => GetStatutFormationDTO(statutFormation)); - - return statutFormationDTOs; - } - /// /// Récupérer les statuts de formation de manière asynchrone /// @@ -567,22 +205,6 @@ namespace EPAServeur.Services return statutFormationDTOs; } - /// - /// Récupérer les types de formation - /// - /// - public IEnumerable GetTypesFormation() - { - IEnumerable typeFormations; - IEnumerable typeFormationDTOs; - - typeFormations = epContext.TypeFormation; - - typeFormationDTOs = typeFormations.Select(typeFormation => GetTypeFormationDTO(typeFormation)); - - return typeFormationDTOs; - } - /// /// Récupérer les types de formation de manière asynchrone /// @@ -599,30 +221,6 @@ namespace EPAServeur.Services return typeFormationDTOs; } - /// - /// Ajouter une formation - /// - /// - /// - public FormationDTO AddFormation(FormationDTO formationDTO) - { - if (!IsFormationValide(formationDTO)) - throw new FormationInvalidException(); - - Formation formation = new Formation(); - formation = SetFormation(formation, formationDTO); - - epContext.StatutFormation.Attach(formation.Statut); - epContext.OrigineFormation.Attach(formation.Origine); - epContext.ModeFormation.Attach(formation.ModeFormation); - epContext.TypeFormation.Attach(formation.TypeFormation); - epContext.Add(formation); - - epContext.SaveChanges(); - - return GetFormationDTO(formation); - } - /// /// Ajouter une formation de manière asynchrone /// @@ -630,8 +228,7 @@ namespace EPAServeur.Services /// public async Task AddFormationAsync(FormationDTO formationDTO) { - if (!await IsFormationValideAsync(formationDTO)) - throw new FormationInvalidException(); + IsFormationValide(formationDTO); Formation formation = new Formation(); formation = SetFormation(formation, formationDTO); @@ -648,143 +245,259 @@ namespace EPAServeur.Services } /// - /// Modifier une formation + /// Modifier une formation de manière asynchrone /// /// /// /// - public FormationDTO UpdateFormation(long? idFormation, FormationDTO formationDTO) + public async Task UpdateFormationAsync(long idFormation, FormationDTO formationDTO) { - if (!IsFormationValide(formationDTO)) - throw new FormationInvalidException(); - if (!formationDTO.Id.HasValue || formationDTO.Id.Value != idFormation) - throw new FormationIncompatibleIdException(); + throw new FormationIncompatibleIdException("L'id de la formation a mettre à jour et la formation a mettre à jour sont incompatble."); + + IsFormationValide(formationDTO); - Formation formation = epContext.Formation.Find(idFormation.Value); + Formation formation = await epContext.Formation.FindAsync(idFormation); if (formation == null) - return null; + throw new FormationNotFoundException(string.Format("Aucune formation trouvée avec l'id suivant: {0}.", idFormation)); - formation = SetFormation(formation, formationDTO); - epContext.SaveChanges(); + formation = SetFormation(formation, formationDTO); + await epContext.SaveChangesAsync(); return GetFormationDTO(formation); } /// - /// Modifier une formation de manière asynchrone + /// Supprimer une formation de manière asynchrone /// /// - /// /// - public async Task UpdateFormationAsync(long? idFormation, FormationDTO formationDTO) + public async Task DeleteFormationByIdAsync(long idFormation) { - if (!IsFormationValide(formationDTO)) - throw new FormationInvalidException(); - - if (!formationDTO.Id.HasValue || formationDTO.Id.Value != idFormation) - throw new FormationIncompatibleIdException(); - - Formation formation = await epContext.Formation.FindAsync(idFormation.Value); + Formation formation = await epContext.Formation.FindAsync(idFormation); if (formation == null) - return null; + throw new FormationNotFoundException(string.Format("Aucune formation trouvée avec l'id suivant: {0}.", idFormation)); + + epContext.Remove(formation); - formation = SetFormation(formation, formationDTO); await epContext.SaveChangesAsync(); return GetFormationDTO(formation); } + #endregion + #region Méthodes Privée /// - /// Supprimer une formation + /// Vérifier si un objet FormationDTO est valide pour ajout ou mise à jour /// - /// - /// - public FormationDTO DeleteFormationById(long? idFormation) + /// 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 void IsFormationValide(FormationDTO formation) { - if (!idFormation.HasValue) - throw new FormationIncompatibleIdException(); + // Vérifier que la formation n'est pas null + if (formation == null) + throw new FormationInvalidException("Aucune formation n'a été reçue"); + // Vérfier que la formation a bien un statut de formation + if (formation.Statut == null || !formation.Statut.Id.HasValue) + throw new FormationInvalidException("Impossible d'enregistrer une formation sans statut de formation."); - Formation formation = epContext.Formation.Find(idFormation.Value); + // Vérfier que la formation a bien un mode de formation + if (formation.Mode == null || !formation.Mode.Id.HasValue) + throw new FormationInvalidException("Impossible d'enregistrer une formation sans mode de formation."); - if (formation == null) - throw new FormationNotFoundException(); + // Vérfier que la formation a bien une origine de formation + if (formation.Origine == null || !formation.Origine.Id.HasValue) + throw new FormationInvalidException("Impossible d'enregistrer une formation sans origine de formation."); - epContext.Remove(formation); + // Vérfier que la formation a bien un type de formation + if (formation.Type == null || !formation.Type.Id.HasValue) + throw new FormationInvalidException("Impossible d'enregistrer une formation sans type de formation."); - epContext.SaveChanges(); + // Vérfier que la formation a bien une agence + if (!formation.IdAgence.HasValue || formation.IdAgence == 0) + throw new FormationInvalidException("Impossible d'enregistrer une formation sans mode de formation."); - return GetFormationDTO(formation); + // Vérifier que la formation a bien un intitulé + if (string.IsNullOrWhiteSpace(formation.Intitule)) + throw new FormationInvalidException("L'intitulé de la formation doit contenir au moins 1 caractère."); + + // Vérifier que la formation a bien un organisme + if (string.IsNullOrWhiteSpace(formation.Organisme)) + throw new FormationInvalidException("L'organisme de la formation doit contenir au moins 1 caractère."); + + // Vérifier que la formation a bien une date de début + if (!formation.DateDebut.HasValue) + throw new FormationInvalidException("Impossible d'enregistrer une formation sans date de début de formation."); + + // Vérifier que la formation a bien une date de fin + if (!formation.DateFin.HasValue) + throw new FormationInvalidException("Impossible d'enregistrer une formation sans date de fin de formation."); + + // Vérifier que la formation a bien une date de début inférieure à la date de fin + if (formation.DateDebut.Value > formation.DateFin.Value) + throw new FormationInvalidException("La date de début de la formation est supérieure à la date de fin."); + + // Vérfier que le statut de la formation est présent dans la BDD + if (!epContext.StatutFormation.Any(statut => statut.IdStatutFormation == formation.Statut.Id.Value && statut.Libelle == formation.Statut.Libelle)) + throw new FormationInvalidException("Le statut de la formation n'existe pas."); + + // Vérfier que le mode de la formation est présent dans la BDD + if (!epContext.ModeFormation.Any(mode => mode.IdModeFormation == formation.Mode.Id.Value && mode.Libelle == formation.Mode.Libelle)) + throw new FormationInvalidException("Le mode de la formation n'existe pas."); + + // Vérfier que l'orgine de la formation est présente dans la BDD + if (!epContext.OrigineFormation.Any(origine => origine.IdOrigineFormation == formation.Origine.Id.Value && origine.Libelle == formation.Origine.Libelle)) + throw new FormationInvalidException("L'origine de la formation n'existe pas."); + + // Vérfier que le type de la formation est présent dans la BDD + if (!epContext.TypeFormation.Any(type => type.IdTypeFormation == formation.Type.Id.Value && type.Libelle == formation.Type.Libelle)) + throw new FormationInvalidException("Le type de la formation n'existe pas."); } /// - /// Supprimer une formation de manière asynchrone + /// Ajouter un ordonnancement croissant ou décroissant sur colonne /// - /// + /// + /// /// - public async Task DeleteFormationByIdAsync(long? idFormation) + private IQueryable OrderByColumn(IQueryable query, bool? asc, string columnName) { - if (!idFormation.HasValue) - throw new FormationIncompatibleIdException(); - - Formation formation = await epContext.Formation.FindAsync(idFormation.Value); + if (!asc.HasValue) + asc = defaultAsc; - if (formation == null) - throw new FormationNotFoundException(); + switch (columnName) + { + case "intitule": + if (asc.Value) + return query.OrderBy(p => p.Intitule); + else + return query.OrderByDescending(p => p.Intitule); + case "statut": + if (asc.Value) + return query.OrderBy(p => p.Statut.Libelle); + else + return query.OrderByDescending(p => p.Statut.Libelle); + case "origine": + if (asc.Value) + return query.OrderBy(p => p.Origine.Libelle); + else + return query.OrderByDescending(p => p.Origine.Libelle); + case "date": + if (asc.Value) + return query.OrderBy(p => p.DateDebut); + else + return query.OrderByDescending(p => p.DateDebut); + case "certification": + if (asc.Value) + return query.OrderBy(p => p.EstCertifiee); + else + return query.OrderByDescending(p => p.EstCertifiee); + //(A faire lorsque la gestion des participations aux formations sera terminée) + //case "participants": + // return query; + default: + if (asc.Value) + return query.OrderBy(p => p.Intitule); + else + return query.OrderByDescending(p => p.Intitule); + } + } - epContext.Remove(formation); + /// + /// Ajouter un filtre pour récupérer les formations en fonction de plusieurs identifiants de statut de formation + /// + /// + /// + /// + private IQueryable IdStatutsFilter(IQueryable query, List idStatuts) + { + if (idStatuts != null && idStatuts.Count > 0) + return query.Where(formation => idStatuts.Contains(formation.Statut.IdStatutFormation)); + else + return query; + } - await epContext.SaveChangesAsync(); + /// + /// Ajouter un filtre pour récupérer les formations en fonction de l'identifiant d'une agence + /// + /// + /// + /// + private IQueryable IdAgenceFilter(IQueryable query, long? idAgence) + { + if (idAgence != null) + return query.Where(formation => formation.IdAgence == idAgence); + else + return query; + } - return GetFormationDTO(formation); + /// + /// Ajouter un filtre pour récupérer les formations en fonction d'un intitulé + /// + /// + /// + /// + private IQueryable IntituleFilter(IQueryable query, string intitule) + { + if (!string.IsNullOrWhiteSpace(intitule)) + return query.Where(formation => formation.Intitule.ToLower().Contains(intitule.ToLower())); + else + return query; } - #endregion - #region Méthodes Privée /// - /// Vérifier si un objet FormationDTO est valide pour ajout ou mise à jour + /// Ajouter un filtre pour récupérer les formations en fonction d'un intervalle de date /// - /// 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) + /// + /// + /// + /// + private IQueryable DateFilter(IQueryable query, DateTime? dateDebut, DateTime? dateFin) { - 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.IdStatutFormation == formation.Statut.Id.Value && statut.Libelle == formation.Statut.Libelle) - || !epContext.ModeFormation.Any(mode => mode.IdModeFormation == formation.Mode.Id.Value && mode.Libelle == formation.Mode.Libelle) - || !epContext.OrigineFormation.Any(origine => origine.IdOrigineFormation == formation.Origine.Id.Value && origine.Libelle == formation.Origine.Libelle) - || !epContext.TypeFormation.Any(type => type.IdTypeFormation == formation.Type.Id.Value && type.Libelle == formation.Type.Libelle)); + if (dateDebut.HasValue && dateFin.HasValue) + return query.Where(formation => formation.DateDebut >= dateDebut.Value && formation.DateFin <= dateFin.Value.AddDays(1)); + else if (!dateDebut.HasValue && dateFin.HasValue) + return query.Where(formation => formation.DateFin <= dateFin.Value.AddDays(1)); + else if (dateDebut.HasValue && !dateFin.HasValue) + return query.Where(formation => formation.DateDebut >= dateDebut.Value); + else + return query; + } /// - /// Vérifier si un objet FormationDTO est valide pour ajout ou mise à jour + /// Ajouter une pagination /// - /// 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) + /// + /// + /// + /// + private IQueryable SkipAndTake(IQueryable query, int? parPage, int? numPage) { - 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.IdStatutFormation == formation.Statut.Id.Value && statut.Libelle == formation.Statut.Libelle) - || ! await epContext.ModeFormation.AnyAsync(mode => mode.IdModeFormation == formation.Mode.Id.Value && mode.Libelle == formation.Mode.Libelle) - || ! await epContext.OrigineFormation.AnyAsync(origine => origine.IdOrigineFormation == formation.Origine.Id.Value && origine.Libelle == formation.Origine.Libelle) - || ! await epContext.TypeFormation.AnyAsync(type => type.IdTypeFormation == formation.Type.Id.Value && type.Libelle == formation.Type.Libelle)); + int skip, take; + + if (!parPage.HasValue || parPage.Value < minParPage || parPage.Value > maxParPage) + parPage = defaultParPage; + + if (!numPage.HasValue || numPage.Value <= 0) + numPage = defaultNumPage; + + + skip = (numPage.Value - 1) * parPage.Value; + take = parPage.Value; + + return query.Skip(skip).Take(take); } + + #region Object to DTO /// - /// Récupère un objet FormationDTO en fonction d'un objet Formation + /// Récuperer un objet FormationDTO en fonction d'un objet Formation /// /// /// @@ -811,7 +524,7 @@ namespace EPAServeur.Services } /// - /// Récupère un objet OrigineFormationDTO en fonction d'un objet OrigineFormation + /// Récuperer un objet OrigineFormationDTO en fonction d'un objet OrigineFormation /// /// /// @@ -828,7 +541,7 @@ namespace EPAServeur.Services } /// - /// Récupère un objet StatutFormationDTO en fonction d'un objet StatutFormation + /// Récuperer un objet StatutFormationDTO en fonction d'un objet StatutFormation /// /// /// @@ -845,7 +558,7 @@ namespace EPAServeur.Services } /// - /// Récupère un objet ModeFormationDTO en fonction d'un objet ModeFormation + /// Récuperer un objet ModeFormationDTO en fonction d'un objet ModeFormation /// /// /// @@ -861,7 +574,7 @@ namespace EPAServeur.Services return modeFormationDTO; } /// - /// Récupère un objet TypeFormationDTO en fonction d'un objet TypeFormation + /// Récuperer un objet TypeFormationDTO en fonction d'un objet TypeFormation /// /// /// @@ -882,7 +595,7 @@ namespace EPAServeur.Services #region DTO to Object /// - /// Modifie un objet Formation en fonction d'un objet FormationDTO + /// Modifier un objet Formation en fonction d'un objet FormationDTO /// /// /// @@ -906,7 +619,7 @@ namespace EPAServeur.Services } /// - /// Récupère un objet OrigineFormation en fonction d'un objet OrigineFormationDTO + /// Récuperer un objet OrigineFormation en fonction d'un objet OrigineFormationDTO /// /// /// @@ -921,7 +634,7 @@ namespace EPAServeur.Services } /// - /// Récupère un objet StatutFormation en fonction d'un objet StatutFormationDTO + /// Récuperer un objet StatutFormation en fonction d'un objet StatutFormationDTO /// /// /// @@ -936,7 +649,7 @@ namespace EPAServeur.Services } /// - /// Récupère un objet ModeFormation en fonction d'un objet ModeFormationDTO + /// Récuperer un objet ModeFormation en fonction d'un objet ModeFormationDTO /// /// /// @@ -951,7 +664,7 @@ namespace EPAServeur.Services } /// - /// Récupère un objet TypeFormation en fonction d'un objet TypeFormationDTO + /// Récuperer un objet TypeFormation en fonction d'un objet TypeFormationDTO /// /// ///