You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Digitalisation_EPA_Serveur/Services/FormationService.cs

337 lines
13 KiB

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.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(int? id)
{
Formation formation = epContext.Formation.FirstOrDefault(formation => formation.Id == id);
if (formation == null)
return null;
return GetFormationDTO(formation);
}
public IEnumerable<FormationDTO> GetFormations()
{
IEnumerable<Formation> formations = epContext.Formation;
IEnumerable<FormationDTO> formationDTOs = formations.Select(formation => GetFormationDTO(formation));
return formationDTOs;
}
public IEnumerable<FormationDTO> GetFormationAnnulees()
{
IEnumerable<Formation> formations = epContext.Formation;
IEnumerable<FormationDTO> formationDTOs = formations.Where(formation => formation.Statut.Id == 4).Select(formation => GetFormationDTO(formation));
return formationDTOs;
}
public IEnumerable<FormationDTO> GetFormationRealisee()
{
IEnumerable<Formation> formations = epContext.Formation;
IEnumerable<FormationDTO> formationDTOs = formations.Where(formation => formation.Statut.Id == 3).Select(formation => GetFormationDTO(formation));
return formationDTOs;
}
public IEnumerable<FormationDTO> GetProchainesFormation()
{
IEnumerable<Formation> formations = epContext.Formation;
IEnumerable<FormationDTO> formationDTOs = formations.Where(formation => formation.Statut.Id == 1 && formation.Statut.Id == 2).Select(formation => GetFormationDTO(formation)).OrderBy(formation => formation.DateDebut);
return formationDTOs;
}
public IEnumerable<ModeFormationDTO> GetModesFormation()
{
IEnumerable<ModeFormation> modeFormations = epContext.ModeFormation;
IEnumerable<ModeFormationDTO> modeFormationDTOs = modeFormations.Select(modeFormation => GetModeFormationDTO(modeFormation));
return modeFormationDTOs;
}
public IEnumerable<OrigineFormationDTO> GetOriginesFormation()
{
IEnumerable<OrigineFormation> origineFormations = epContext.OrigineFormation;
IEnumerable<OrigineFormationDTO> origineFormationDTOs = origineFormations.Select(origineFormation => GetOrigineFormationDTO(origineFormation));
return origineFormationDTOs;
}
public IEnumerable<StatutFormationDTO> GetStatutsFormation()
{
IEnumerable<StatutFormation> statutFormations = epContext.StatutFormation;
IEnumerable<StatutFormationDTO> statutFormationDTOs = statutFormations.Select(statutFormation => GetStatutFormationDTO(statutFormation));
return statutFormationDTOs;
}
public IEnumerable<TypeFormationDTO> GetTypesFormation()
{
IEnumerable<TypeFormation> typeFormations = epContext.TypeFormation;
IEnumerable<TypeFormationDTO> typeFormationDTOs = typeFormations.Select(typeFormation => GetTypeFormationDTO(typeFormation));
return typeFormationDTOs;
}
public byte 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 1;
}
return 0;
}
public byte UpdateFormation(FormationDTO formationDTO)
{
Formation formation = epContext.Formation.FirstOrDefault(formation => formation.Id == formationDTO.Id);
if (formation == null)
return 1;
formation = SetFormation(formation, formationDTO);
try
{
epContext.SaveChanges();
}
catch (Exception)
{
return 2;
}
return 0;
}
public byte DeleteFormationById(int? id)
{
Formation formation = epContext.Formation.FirstOrDefault(formation => formation.Id == id);
if (formation == null)
return 1;
try
{
epContext.Remove(formation);
epContext.SaveChanges();
}
catch (Exception)
{
return 2;
}
return 0;
}
#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<ParticipationFormationDTO> 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<ParticipationFormationDTO> 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
}
}