@ -1,4 +1,5 @@
using EPAServeur.Context ;
using EPAServeur.Exceptions ;
using EPAServeur.IServices ;
using EPAServeur.Models.Formation ;
using IO.Swagger.DTO ;
@ -47,7 +48,7 @@ namespace EPAServeur.Services
. FirstOrDefault ( formation = > formation . Id = = idFormation ) ;
if ( formation = = null )
return null ;
throw new FormationNotFoundException ( ) ;
return GetFormationDTO ( formation ) ;
}
@ -66,7 +67,7 @@ namespace EPAServeur.Services
. FirstOrDefaultAsync ( formation = > formation . Id = = idFormation ) ;
if ( formation = = null )
return null ;
throw new FormationNotFoundException ( ) ;
return GetFormationDTO ( formation ) ;
}
@ -81,7 +82,7 @@ namespace EPAServeur.Services
/// <param name="texte">Texte permettant d'identifier l'objet rechercher</param>
/// <param name="tri">Colonne du tableau sur lequel le tri s'effectue</param>
/// <returns></returns>
public IEnumerable < FormationDTO > GetFormations ( bool? asc , int? numPage , int? parPAge , int? idAgence , string texte , string tri )
public IEnumerable < FormationDTO > GetFormations ( bool? asc , int? numPage , int? parPAge , int? idAgence , int? statutFormation , string texte , string tri )
{
IEnumerable < Formation > formations ;
IEnumerable < FormationDTO > formationDTOs ;
@ -94,34 +95,38 @@ namespace EPAServeur.Services
int skip = ( numPage . Value - 1 ) * parPAge . Value ;
int take = parPAge . Value ;
if ( idAgence ! = null )
if ( statutFormation ! = null & & idAgence ! = null )
{
try
{
formations = epContext . Formation . Where ( formation = > formation . IdAgence = = idAgence )
. Include ( formation = > formation . Statut )
. Include ( formation = > formation . ModeFormation )
. Include ( formation = > formation . Origine )
. Include ( formation = > formation . TypeFormation ) ;
}
catch ( Exception ex )
{
throw ;
}
formations = epContext . Formation
. Include ( formation = > formation . Statut )
. Include ( formation = > formation . ModeFormation )
. Include ( formation = > formation . Origine )
. Include ( formation = > formation . TypeFormation )
. Where ( formation = > formation . Statut . Id = = statutFormation & & formation . IdAgence = = idAgence ) ;
}
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 . Id = = statutFormation ) ;
}
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 ) ;
}
else
{
try
{
formations = epContext . Formation . Include ( formation = > formation . Statut )
. Include ( formation = > formation . ModeFormation )
. Include ( formation = > formation . Origine )
. Include ( formation = > formation . TypeFormation ) ;
}
catch ( Exception ex )
{
throw ;
}
formations = epContext . Formation . Include ( formation = > formation . Statut )
. Include ( formation = > formation . ModeFormation )
. Include ( formation = > formation . Origine )
. Include ( formation = > formation . TypeFormation ) ;
}
@ -143,7 +148,7 @@ namespace EPAServeur.Services
/// <param name="texte">Texte permettant d'identifier l'objet rechercher</param>
/// <param name="tri">Colonne du tableau sur lequel le tri s'effectue</param>
/// <returns></returns>
public async Task < IEnumerable < FormationDTO > > GetFormationsAsync ( bool? asc , int? numPage , int? parPAge , int? idAgence , string texte , string tri )
public async Task < IEnumerable < FormationDTO > > GetFormationsAsync ( bool? asc , int? numPage , int? parPAge , int? idAgence , int? statutFormation , string texte , string tri )
{
IEnumerable < Formation > formations ;
IEnumerable < FormationDTO > formationDTOs ;
@ -156,34 +161,42 @@ namespace EPAServeur.Services
int skip = ( numPage . Value - 1 ) * parPAge . Value ;
int take = parPAge . Value ;
if ( idAgence ! = null )
if ( statutFormation ! = null & & idAgence ! = null )
{
try
{
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 ) . ToListAsync ( ) ;
}
catch ( Exception ex )
{
throw ;
}
formations = await epContext . Formation
. Include ( formation = > formation . Statut )
. Include ( formation = > formation . ModeFormation )
. Include ( formation = > formation . Origine )
. Include ( formation = > formation . TypeFormation )
. Where ( formation = > formation . Statut . Id = = statutFormation & & formation . IdAgence = = idAgence ) . 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 . Id = = statutFormation ) . 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 ) . ToListAsync ( ) ;
}
else
{
try
{
formations = await epContext . Formation . Include ( formation = > formation . Statut )
. Include ( formation = > formation . ModeFormation )
. Include ( formation = > formation . Origine )
. Include ( formation = > formation . TypeFormation ) . ToListAsync ( ) ;
}
catch ( Exception ex )
{
throw ;
}
formations = await epContext . Formation . Include ( formation = > formation . Statut )
. Include ( formation = > formation . ModeFormation )
. Include ( formation = > formation . Origine )
. Include ( formation = > formation . TypeFormation ) . ToListAsync ( ) ;
}
@ -308,34 +321,23 @@ namespace EPAServeur.Services
if ( idAgence ! = null )
{
try
{
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 ) ;
}
catch ( Exception ex )
{
throw ;
}
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
{
try
{
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 ) ;
}
catch ( Exception ex )
{
throw ;
}
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 ) ;
}
@ -372,34 +374,22 @@ namespace EPAServeur.Services
if ( idAgence ! = null )
{
try
{
formations = await 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 ) . ToListAsync ( ) ;
}
catch ( Exception ex )
{
throw ;
}
formations = await 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 ) . ToListAsync ( ) ;
}
else
{
try
{
formations = await epContext . Formation . Where ( formation = > formation . Statut . Id = = 3 )
. Include ( formation = > formation . Statut )
. Include ( formation = > formation . ModeFormation )
. Include ( formation = > formation . Origine )
. Include ( formation = > formation . TypeFormation ) . ToListAsync ( ) ;
}
catch ( Exception ex )
{
throw ;
}
formations = await epContext . Formation . Where ( formation = > formation . Statut . Id = = 3 )
. Include ( formation = > formation . Statut )
. Include ( formation = > formation . ModeFormation )
. Include ( formation = > formation . Origine )
. Include ( formation = > formation . TypeFormation ) . ToListAsync ( ) ;
}
@ -435,19 +425,14 @@ namespace EPAServeur.Services
int take = parPAge . Value ;
if ( idAgence ! = null )
try
{
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 ) ;
}
catch ( Exception ex )
{
throw ;
}
{
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 ) )
@ -489,18 +474,14 @@ namespace EPAServeur.Services
int take = parPAge . Value ;
if ( idAgence ! = null )
try
{
formations = await 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 ) . ToListAsync ( ) ;
}
catch ( Exception ex )
{
throw ;
}
{
formations = await 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 ) . ToListAsync ( ) ;
}
else
{
@ -527,14 +508,8 @@ namespace EPAServeur.Services
{
IEnumerable < ModeFormation > modeFormations ;
IEnumerable < ModeFormationDTO > modeFormationDTOs ;
try
{
modeFormations = epContext . ModeFormation ;
}
catch ( Exception ex )
{
throw ;
}
modeFormations = epContext . ModeFormation ;
if ( modeFormations = = null )
return null ;
@ -552,14 +527,7 @@ namespace EPAServeur.Services
{
IEnumerable < ModeFormation > modeFormations ;
IEnumerable < ModeFormationDTO > modeFormationDTOs ;
try
{
modeFormations = await epContext . ModeFormation . ToListAsync ( ) ;
}
catch ( Exception ex )
{
throw ;
}
modeFormations = await epContext . ModeFormation . ToListAsync ( ) ;
if ( modeFormations = = null )
return null ;
@ -578,14 +546,7 @@ namespace EPAServeur.Services
IEnumerable < OrigineFormation > origineFormations ;
IEnumerable < OrigineFormationDTO > origineFormationDTOs ;
try
{
origineFormations = epContext . OrigineFormation ;
}
catch ( Exception ex )
{
throw ;
}
origineFormations = epContext . OrigineFormation ;
if ( origineFormations = = null )
return null ;
@ -604,14 +565,7 @@ namespace EPAServeur.Services
IEnumerable < OrigineFormation > origineFormations ;
IEnumerable < OrigineFormationDTO > origineFormationDTOs ;
try
{
origineFormations = await epContext . OrigineFormation . ToListAsync ( ) ;
}
catch ( Exception ex )
{
throw ;
}
origineFormations = await epContext . OrigineFormation . ToListAsync ( ) ;
if ( origineFormations = = null )
return null ;
@ -630,15 +584,7 @@ namespace EPAServeur.Services
IEnumerable < StatutFormation > statutFormations ;
IEnumerable < StatutFormationDTO > statutFormationDTOs ;
try
{
statutFormations = epContext . StatutFormation ;
}
catch ( Exception ex )
{
throw ;
}
statutFormations = epContext . StatutFormation ;
if ( statutFormations = = null )
return null ;
@ -657,15 +603,7 @@ namespace EPAServeur.Services
IEnumerable < StatutFormation > statutFormations ;
IEnumerable < StatutFormationDTO > statutFormationDTOs ;
try
{
statutFormations = await epContext . StatutFormation . ToListAsync ( ) ;
}
catch ( Exception ex )
{
throw ;
}
statutFormations = await epContext . StatutFormation . ToListAsync ( ) ;
if ( statutFormations = = null )
return null ;
@ -684,14 +622,7 @@ namespace EPAServeur.Services
IEnumerable < TypeFormation > typeFormations ;
IEnumerable < TypeFormationDTO > typeFormationDTOs ;
try
{
typeFormations = epContext . TypeFormation ;
}
catch ( Exception ex )
{
throw ;
}
typeFormations = epContext . TypeFormation ;
if ( typeFormations = = null )
return null ;
@ -710,14 +641,7 @@ namespace EPAServeur.Services
IEnumerable < TypeFormation > typeFormations ;
IEnumerable < TypeFormationDTO > typeFormationDTOs ;
try
{
typeFormations = await epContext . TypeFormation . ToListAsync ( ) ;
}
catch ( Exception ex )
{
throw ;
}
typeFormations = await epContext . TypeFormation . ToListAsync ( ) ;
if ( typeFormations = = null )
return null ;
@ -734,27 +658,21 @@ namespace EPAServeur.Services
/// <returns></returns>
public FormationDTO AddFormation ( FormationDTO formationDTO )
{
if ( ! IsFormationValide ( formationDTO ) )
throw new FormationInvalidException ( ) ;
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 ex )
{
throw ;
}
epContext . SaveChanges ( ) ;
return GetFormationDTO ( formation ) ;
}
@ -766,27 +684,21 @@ namespace EPAServeur.Services
/// <returns></returns>
public async Task < FormationDTO > AddFormationAsync ( FormationDTO formationDTO )
{
if ( ! IsFormationValide ( formationDTO ) )
throw new FormationInvalidException ( ) ;
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
{
await epContext . SaveChangesAsync ( ) ;
}
catch ( Exception ex )
{
throw ;
}
await epContext . SaveChangesAsync ( ) ;
return GetFormationDTO ( formation ) ;
}
@ -799,27 +711,20 @@ namespace EPAServeur.Services
/// <returns></returns>
public FormationDTO UpdateFormation ( long? idFormation , FormationDTO formationDTO )
{
if ( formationDTO = = null & & ! formationDTO . Id . HasValue & & formationDTO . Id . Value ! = idFormation )
{
return null ;
}
if ( ! IsFormationValide ( formationDTO ) )
throw new FormationInvalidException ( ) ;
if ( formationDTO = = null & & ! formationDTO . Id . HasValue & & formationDTO . Id . Value ! = idFormation )
throw new FormationIncompatibleIdException ( ) ;
Formation formation = epContext . Formation . Find ( idFormation ) ;
if ( formation = = null )
{
return null ;
}
formation = SetFormation ( formation , formationDTO ) ;
try
{
epContext . SaveChanges ( ) ;
}
catch ( Exception ex )
{
throw ;
}
epContext . SaveChanges ( ) ;
return GetFormationDTO ( formation ) ;
}
@ -832,27 +737,19 @@ namespace EPAServeur.Services
/// <returns></returns>
public async Task < FormationDTO > UpdateFormationAsync ( long? idFormation , FormationDTO formationDTO )
{
if ( ! IsFormationValide ( formationDTO ) )
throw new FormationInvalidException ( ) ;
if ( formationDTO = = null & & ! formationDTO . Id . HasValue & & formationDTO . Id . Value ! = idFormation )
{
return null ;
}
throw new FormationIncompatibleIdException ( ) ;
Formation formation = await epContext . Formation . FindAsync ( idFormation ) ;
if ( formation = = null )
{
return null ;
}
formation = SetFormation ( formation , formationDTO ) ;
try
{
await epContext . SaveChangesAsync ( ) ;
}
catch ( Exception ex )
{
throw ;
}
await epContext . SaveChangesAsync ( ) ;
return GetFormationDTO ( formation ) ;
}
@ -862,25 +759,19 @@ namespace EPAServeur.Services
/// </summary>
/// <param name="idFormation"></param>
/// <returns></returns>
public bool DeleteFormationById ( long? idFormation )
public FormationDTO DeleteFormationById ( long? idFormation )
{
Formation formation = epContext . Formation . Find ( idFormation ) ;
if ( formation = = null )
return false ;
throw new FormationNotFoundException ( ) ;
epContext . Remove ( formation ) ;
try
{
epContext . SaveChanges ( ) ;
}
catch ( Exception )
{
throw ;
}
return true ;
epContext . SaveChanges ( ) ;
return GetFormationDTO ( formation ) ;
}
/// <summary>
@ -888,29 +779,32 @@ namespace EPAServeur.Services
/// </summary>
/// <param name="idFormation"></param>
/// <returns></returns>
public async Task < bool > DeleteFormationByIdAsync ( long? idFormation )
public async Task < FormationDTO > DeleteFormationByIdAsync ( long? idFormation )
{
Formation formation = await epContext . Formation . FindAsync ( idFormation ) ;
if ( formation = = null )
return false ;
throw new FormationNotFoundException ( ) ;
epContext . Remove ( formation ) ;
try
{
await epContext . SaveChangesAsync ( ) ;
}
catch ( Exception )
{
throw ;
}
await epContext . SaveChangesAsync ( ) ;
return true ;
return GetFormationDTO ( formation ) ;
}
# endregion
#region Méthodes Privée
/// <summary>
/// Vérifier si un objet FormationDTO est valide pour ajout ou mise à jour
/// </summary>
/// <remarks> Un objet FormationDTO est valide si aucune de ses propriétés n'est à null</remarks>
/// <param name="formation"></param>
/// <returns>true si l'objet est valide, false sinon</returns>
private bool IsFormationValide ( FormationDTO formation )
{
return ! ( formation = = null | | formation . IdAgence = = null | | formation . Intitule = = null | | formation . Organisme = = null ) ;
}
#region Object to DTO
/// <summary>