using EPAServeur.Context; using EPAServeur.IServices; using EPAServeur.Models.Formation; using IO.Swagger.ApiCollaborateur; using IO.Swagger.DTO; using IO.Swagger.ModelCollaborateur; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices.ComTypes; using System.Threading.Tasks; namespace EPAServeur.Services { public class FormationService : IFormationService { private readonly EpContext epContext; public FormationService(EpContext _epContext) { epContext = _epContext; } public FormationDTO GetFormationById(decimal? id) { int idFormation = 0; try { idFormation = Convert.ToInt32(id); } catch (Exception ex) { return null; } Formation formation = epContext.Formation.Include(formation => formation.Statut) .Include(formation => formation.ModeFormation) .Include(formation => formation.Origine) .Include(formation => formation.TypeFormation) .FirstOrDefault(formation => formation.Id == idFormation); if (formation == null) return null; return GetFormationDTO(formation); } public IEnumerable GetFormations(bool? asc, int? numPage, int? parPAge, int? 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) .Include(formation => formation.Statut) .Include(formation => formation.ModeFormation) .Include(formation => formation.Origine) .Include(formation => formation.TypeFormation); else formations = epContext.Formation.Include(formation => formation.Statut) .Include(formation => formation.ModeFormation) .Include(formation => formation.Origine) .Include(formation => formation.TypeFormation); if (formations == null) return new List(); formationDTOs = formations.Where(formation => formation.Intitule.ToLower().Contains(texte)).Select(formation => GetFormationDTO(formation)); return formationDTOs; } public IEnumerable GetFormationAnnulees(bool? asc, int? numPage, int? parPAge, int? 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.Id == 4) .Include(formation => formation.Statut) .Include(formation => formation.ModeFormation) .Include(formation => formation.Origine) .Include(formation => formation.TypeFormation); else formations = epContext.Formation.Where(formation => formation.Statut.Id == 4) .Include(formation => formation.Statut) .Include(formation => formation.ModeFormation) .Include(formation => formation.Origine) .Include(formation => formation.TypeFormation); if (formations == null) return new List(); formationDTOs = formations.Where(formation => formation.Intitule.ToLower().Contains(texte)).Select(formation => GetFormationDTO(formation)); return formationDTOs; } public IEnumerable GetFormationRealisee(bool? asc, int? numPage, int? parPAge, int? 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.Id == 3) .Include(formation => formation.Statut) .Include(formation => formation.ModeFormation) .Include(formation => formation.Origine) .Include(formation => formation.TypeFormation); else formations = epContext.Formation.Where(formation => formation.Statut.Id == 3) .Include(formation => formation.Statut) .Include(formation => formation.ModeFormation) .Include(formation => formation.Origine) .Include(formation => formation.TypeFormation); if (formations == null) return new List(); formationDTOs = formations.Where(formation => formation.Intitule.ToLower().Contains(texte)).Select(formation => GetFormationDTO(formation)); return formationDTOs; } public IEnumerable GetProchainesFormation(bool? asc, int? numPage, int? parPAge, int? 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.Id == 1 || formation.Statut.Id == 2)) .Include(formation => formation.Statut) .Include(formation => formation.ModeFormation) .Include(formation => formation.Origine) .Include(formation => formation.TypeFormation); else formations = epContext.Formation.Where(formation => (formation.Statut.Id == 1 || formation.Statut.Id == 2)) .Include(formation => formation.Statut) .Include(formation => formation.ModeFormation) .Include(formation => formation.Origine) .Include(formation => formation.TypeFormation); if (formations == null) return new List(); formationDTOs = formations.Where(formation => formation.Intitule.ToLower().Contains(texte)).Select(formation => GetFormationDTO(formation)); return formationDTOs; } public IEnumerable GetModesFormation() { IEnumerable modeFormations = epContext.ModeFormation; IEnumerable modeFormationDTOs = modeFormations.Select(modeFormation => GetModeFormationDTO(modeFormation)); return modeFormationDTOs; } public IEnumerable GetOriginesFormation() { IEnumerable origineFormations = epContext.OrigineFormation; IEnumerable origineFormationDTOs = origineFormations.Select(origineFormation => GetOrigineFormationDTO(origineFormation)); return origineFormationDTOs; } public IEnumerable GetStatutsFormation() { IEnumerable statutFormations = epContext.StatutFormation; IEnumerable statutFormationDTOs = statutFormations.Select(statutFormation => GetStatutFormationDTO(statutFormation)); return statutFormationDTOs; } public IEnumerable GetTypesFormation() { IEnumerable typeFormations = epContext.TypeFormation; IEnumerable typeFormationDTOs = typeFormations.Select(typeFormation => GetTypeFormationDTO(typeFormation)); return typeFormationDTOs; } public FormationDTO AddFormation(FormationDTO formationDTO) { Formation formation = new Formation(); formation = SetFormation(formation, formationDTO); if (formation.Statut != null) { epContext.StatutFormation.Attach(formation.Statut); } epContext.OrigineFormation.Attach(formation.Origine); epContext.ModeFormation.Attach(formation.ModeFormation); epContext.TypeFormation.Attach(formation.TypeFormation); epContext.Add(formation); try { epContext.SaveChanges(); } catch (Exception) { return null; } return GetFormationDTO(formation); } public FormationDTO UpdateFormation(FormationDTO formationDTO) { Formation formation = epContext.Formation.FirstOrDefault(formation => formation.Id == formationDTO.Id); if (formation == null) { return null; } formation = SetFormation(formation, formationDTO); epContext.SaveChanges(); return GetFormationDTO(formation); } public bool DeleteFormationById(decimal? id) { int idFormation = 0; try { idFormation = Convert.ToInt32(id); } catch (Exception ex) { return false; } Formation formation = epContext.Formation.FirstOrDefault(formation => formation.Id == idFormation); if (formation == null) return false; epContext.Remove(formation); epContext.SaveChanges(); return true; } #region Object to DTO private FormationDTO GetFormationDTO(Formation formation) { FormationDTO formationDTO = new FormationDTO() { Id = formation.Id, Intitule = formation.Intitule, IdAgence = formation.IdAgence, DateDebut = formation.DateDebut, DateFin = formation.DateFin, Heure = formation.Heure, Jour = formation.Jour, Organisme = formation.Organisme, EstCertifie = formation.EstCertifiee, Origine = GetOrigineFormationDTO(formation.Origine), Statut = GetStatutFormationDTO(formation.Statut), Mode = GetModeFormationDTO(formation.ModeFormation), Type = GetTypeFormationDTO(formation.TypeFormation) }; //List participationFormationDTOs = epContext.ParticipationFormation.Where(participationFormation => participationFormation.Formation.Id == formation.Id) // .Select(participationFormation => new ParticipationFormationDTO() // { // Id = participationFormation.Id, // DateCreation = participationFormation.DateCreation, // Formation = participationFormation.Formation.Intitule, // Date = participationFormation.Formation.DateDebut, // Statut = participationFormation.Formation.Statut.Libelle, // EstEvaluee = participationFormation.EstEvaluee, // }).ToList(); //formationDTO.ParticipantsFormation = participationFormationDTOs; return formationDTO; } private OrigineFormationDTO GetOrigineFormationDTO(OrigineFormation origineFormation) { if (origineFormation == null) return null; OrigineFormationDTO origineFormationDTO = new OrigineFormationDTO() { Id = origineFormation.Id, Libelle = origineFormation.Libelle }; return origineFormationDTO; } private StatutFormationDTO GetStatutFormationDTO(StatutFormation statutFormation) { if (statutFormation == null) return null; StatutFormationDTO statutFormationDTO = new StatutFormationDTO() { Id = statutFormation.Id, Libelle = statutFormation.Libelle }; return statutFormationDTO; } private ModeFormationDTO GetModeFormationDTO(ModeFormation modeFormation) { if (modeFormation == null) return null; ModeFormationDTO modeFormationDTO = new ModeFormationDTO() { Id = modeFormation.Id, Libelle = modeFormation.Libelle }; return modeFormationDTO; } private TypeFormationDTO GetTypeFormationDTO(TypeFormation typeFormation) { if (typeFormation == null) return null; TypeFormationDTO typeFormationDTO = new TypeFormationDTO() { Id = typeFormation.Id, Libelle = typeFormation.Libelle }; return typeFormationDTO; } #endregion #region DTO to Object private Formation SetFormation(Formation formation, FormationDTO formationDTO) { formation.Intitule = formationDTO.Intitule; formation.IdAgence = formationDTO.IdAgence.Value; formation.DateDebut = formationDTO.DateDebut.Value; formation.DateFin = formationDTO.DateFin.Value; formation.Heure = Convert.ToInt32(formationDTO.Heure.Value); formation.Jour = Convert.ToInt32(formationDTO.Jour.Value); formation.Organisme = formationDTO.Organisme; formation.EstCertifiee = formationDTO.EstCertifie.Value; formation.Origine = GetOrigineFormation(formationDTO.Origine); formation.Statut = GetStatutFormation(formationDTO.Statut); formation.ModeFormation = GetModeFormation(formationDTO.Mode); formation.TypeFormation = GetTypeFormation(formationDTO.Type); //List participationFormationDTOs = epContext.ParticipationFormation.Where(participationFormation => participationFormation.Formation.Id == formation.Id) // .Select(participationFormation => new ParticipationFormationDTO() // { // Id = participationFormation.Id, // DateCreation = participationFormation.DateCreation, // Formation = participationFormation.Formation.Intitule, // Date = participationFormation.Formation.DateDebut, // Statut = participationFormation.Formation.Statut.Libelle, // EstEvaluee = participationFormation.EstEvaluee, // }).ToList(); //formationDTO.ParticipantsFormation = participationFormationDTOs; return formation; } private OrigineFormation GetOrigineFormation(OrigineFormationDTO origineFormationDTO) { if (origineFormationDTO == null) return null; OrigineFormation origineFormation = new OrigineFormation() { Id = origineFormationDTO.Id.Value, Libelle = origineFormationDTO.Libelle }; return origineFormation; } private StatutFormation GetStatutFormation(StatutFormationDTO statutFormationDTO) { if (statutFormationDTO == null) return null; StatutFormation statutFormation = new StatutFormation() { Id = statutFormationDTO.Id.Value, Libelle = statutFormationDTO.Libelle }; return statutFormation; } private ModeFormation GetModeFormation(ModeFormationDTO modeFormationDTO) { if (modeFormationDTO == null) return null; ModeFormation modeFormation = new ModeFormation() { Id = modeFormationDTO.Id.Value, Libelle = modeFormationDTO.Libelle }; return modeFormation; } private TypeFormation GetTypeFormation(TypeFormationDTO typeFormationDTO) { if (typeFormationDTO == null) return null; TypeFormation typeFormation = new TypeFormation() { Id = typeFormationDTO.Id.Value, Libelle = typeFormationDTO.Libelle }; return typeFormation; } #endregion } }