Ajout des logs dans l'api formation

develop
jboinembalome 4 years ago
parent c6518a5f3a
commit 3ec0c7fdf4
  1. 282
      Controllers/FormationsApi.cs
  2. 40
      Exceptions/FormationIncompatibleIdException.cs
  3. 40
      Exceptions/FormationInvalidException.cs
  4. 40
      Exceptions/FormationNotFoundException.cs
  5. 40
      Exceptions/ModeFormationNotFoundException.cs
  6. 40
      Exceptions/OrigineFormationNotFoundException.cs
  7. 40
      Exceptions/StatutFormationNotFoundException.cs
  8. 40
      Exceptions/TypeFormationNotFoundException.cs
  9. 8
      IServices/IFormationService.cs
  10. 274
      Services/FormationService.cs

@ -18,6 +18,9 @@ using IO.Swagger.DTO;
using EPAServeur.IServices; using EPAServeur.IServices;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using EPAServeur.Exceptions;
using Microsoft.EntityFrameworkCore;
namespace IO.Swagger.Controllers namespace IO.Swagger.Controllers
{ {
@ -28,10 +31,12 @@ namespace IO.Swagger.Controllers
public class FormationsApiController : ControllerBase public class FormationsApiController : ControllerBase
{ {
private readonly IFormationService formationService; private readonly IFormationService formationService;
private readonly ILogger<FormationsApiController> logger;
public FormationsApiController(IFormationService _formationService) public FormationsApiController(IFormationService _formationService, ILogger<FormationsApiController> _logger)
{ {
formationService = _formationService; formationService = _formationService;
logger = _logger;
} }
/// <summary> /// <summary>
@ -49,7 +54,28 @@ namespace IO.Swagger.Controllers
[SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")]
public virtual async Task<IActionResult> AjouterFormation([FromBody] FormationDTO body) public virtual async Task<IActionResult> AjouterFormation([FromBody] FormationDTO body)
{ {
FormationDTO nouvelleFormation = await formationService.AddFormationAsync(body); logger.LogInformation("Ajout d'une nouvelle formation.");
FormationDTO nouvelleFormation = null;
try
{
nouvelleFormation = await formationService.AddFormationAsync(body);
}
catch (FormationInvalidException)
{
logger.LogWarning("Des données sont manquants, la nouvelle formation ne peut pas être ajoutée.");
}
catch (DbUpdateException)
{
logger.LogError("Une erreur est survenue dans la base de données durant l'ajout de la nouvelle formation.");
}
catch (Exception)
{
logger.LogError("Une erreur inconnue est survenue lors de l'ajout de la formation note.");
}
logger.LogInformation("Nouvelle formation ajoutée.");
return Created("", nouvelleFormation); return Created("", nouvelleFormation);
} }
@ -69,15 +95,38 @@ namespace IO.Swagger.Controllers
[SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")]
public virtual async Task<IActionResult> DeleteFormation([FromRoute][Required] long? idFormation) public virtual async Task<IActionResult> DeleteFormation([FromRoute][Required] long? idFormation)
{ {
if ( ! await formationService.DeleteFormationByIdAsync(idFormation)) try
{
logger.LogInformation("Suppresion de la formation {idFormation}.", idFormation);
FormationDTO formation = await formationService.DeleteFormationByIdAsync(idFormation);
}
catch (FormationNotFoundException)
{ {
logger.LogError("Impossible de supprimer la formation d'id {idFormation} car elle n'existe pas.", idFormation);
ErreurDTO erreur = new ErreurDTO() ErreurDTO erreur = new ErreurDTO()
{ {
Code = "404", Code = "404",
Message = "Aucune formation trouvée" Message = "Aucune formation trouvée"
}; };
return NotFound(erreur); return NotFound(erreur);
} }
catch (DbUpdateConcurrencyException)
{
logger.LogWarning("La formation {idFormation} n'a pas pu être supprimée car elle est prise par une autre ressource.", idFormation);
}
catch (DbUpdateException)
{
logger.LogError("Une erreur a eu lieu, la formation {idFormation} n'a pas pu être supprimée.", idFormation);
}
catch (Exception)
{
logger.LogError("Une erreur inconnue est survenue lors de la suppression de la formation {idFormation}.", idFormation);
}
logger.LogInformation("Formation {idFormation} supprimée avec succès.", idFormation);
return NoContent(); return NoContent();
} }
@ -105,8 +154,19 @@ namespace IO.Swagger.Controllers
[SwaggerResponse(statusCode: 404, type: typeof(ErreurDTO), description: "Ressource n&#x27;a pas été trouvée")] [SwaggerResponse(statusCode: 404, type: typeof(ErreurDTO), description: "Ressource n&#x27;a pas été trouvée")]
public virtual async Task<IActionResult> GetFormationAnnulees([FromQuery][Required()] bool? asc, [FromQuery][Required()] int? numPage, [FromQuery][Required()] int? parPAge, [FromQuery] int? idAgence, [FromQuery] string texte, [FromQuery] string tri) public virtual async Task<IActionResult> GetFormationAnnulees([FromQuery][Required()] bool? asc, [FromQuery][Required()] int? numPage, [FromQuery][Required()] int? parPAge, [FromQuery] int? idAgence, [FromQuery] string texte, [FromQuery] string tri)
{ {
//TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ... logger.LogInformation("Récupération de la liste des formations annulées.");
IEnumerable<FormationDTO> formations = await formationService.GetFormationAnnuleesAsync(asc, numPage, parPAge, idAgence, texte, tri);
IEnumerable<FormationDTO> formations = null;
try
{
formations = await formationService.GetFormationAnnuleesAsync(asc, numPage, parPAge, idAgence, texte, tri);
}
catch (Exception)
{
logger.LogError("Une erreur inconnue est survenue lors de la récupération de la liste des formations annulées.");
}
if (formations == null) if (formations == null)
{ {
ErreurDTO erreur = new ErreurDTO() ErreurDTO erreur = new ErreurDTO()
@ -114,9 +174,12 @@ namespace IO.Swagger.Controllers
Code = "404", Code = "404",
Message = "Aucune formation annulée" Message = "Aucune formation annulée"
}; };
return NotFound(erreur); return NotFound(erreur);
} }
logger.LogInformation("Liste des formations annulées récupérée.");
return Ok(formations); return Ok(formations);
} }
@ -138,16 +201,41 @@ namespace IO.Swagger.Controllers
[SwaggerResponse(statusCode: 404, type: typeof(ErreurDTO), description: "Ressource n&#x27;a pas été trouvée")] [SwaggerResponse(statusCode: 404, type: typeof(ErreurDTO), description: "Ressource n&#x27;a pas été trouvée")]
public virtual async Task<IActionResult> GetFormationById([FromRoute][Required] long? idFormation) public virtual async Task<IActionResult> GetFormationById([FromRoute][Required] long? idFormation)
{ {
FormationDTO formationDTO = await formationService.GetFormationByIdAsync(idFormation); logger.LogInformation("Récupération de la formation {idFormation}.", idFormation);
if (formationDTO == null)
FormationDTO formationDTO = null;
try
{
formationDTO = await formationService.GetFormationByIdAsync(idFormation);
}
catch (FormationNotFoundException)
{ {
logger.LogError("Aucune formation ne correspond à l'id {idFormation} recherchée.", idFormation);
ErreurDTO erreurDTO = new ErreurDTO() ErreurDTO erreurDTO = new ErreurDTO()
{ {
Code = "404", Code = "404",
Message = "La formation n'existe pas", Message = "La formation n'existe pas",
}; };
return NotFound(erreurDTO); return NotFound(erreurDTO);
} }
catch (DbUpdateConcurrencyException)
{
logger.LogWarning("La formation {idFormation} n'a pas pu être récupérée car elle est prise par une autre ressource.", idFormation);
}
catch (DbUpdateException)
{
logger.LogError("Une erreur a eu lieu, la formation {idFormation} n'a pas pu être récupérée.", idFormation);
}
catch (Exception)
{
logger.LogError("Une erreur inconnue est survenue lors de la récupération de la formation {idFormation}.", idFormation);
}
logger.LogInformation("Formation {idFormation} récupérée.", idFormation);
return Ok(formationDTO); return Ok(formationDTO);
} }
@ -174,8 +262,19 @@ namespace IO.Swagger.Controllers
[SwaggerResponse(statusCode: 404, type: typeof(ErreurDTO), description: "Ressource n&#x27;a pas été trouvée")] [SwaggerResponse(statusCode: 404, type: typeof(ErreurDTO), description: "Ressource n&#x27;a pas été trouvée")]
public virtual async Task<IActionResult> GetFormationRealisee([FromQuery][Required()] bool? asc, [FromQuery][Required()] int? numPage, [FromQuery][Required()] int? parPAge, [FromQuery] int? idAgence, [FromQuery] string texte, [FromQuery] string tri) public virtual async Task<IActionResult> GetFormationRealisee([FromQuery][Required()] bool? asc, [FromQuery][Required()] int? numPage, [FromQuery][Required()] int? parPAge, [FromQuery] int? idAgence, [FromQuery] string texte, [FromQuery] string tri)
{ {
//TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ... logger.LogInformation("Récupération de la liste des formations réalisées.");
IEnumerable<FormationDTO> formations = await formationService.GetFormationRealiseeAsync(asc, numPage, parPAge, idAgence, texte, tri);
IEnumerable<FormationDTO> formations = null;
try
{
formations = await formationService.GetFormationRealiseeAsync(asc, numPage, parPAge, idAgence, texte, tri);
}
catch (Exception)
{
logger.LogError("Une erreur inconnue est survenue lors de la récupération de la liste des formations réalisées.");
}
if (formations == null) if (formations == null)
{ {
ErreurDTO erreur = new ErreurDTO() ErreurDTO erreur = new ErreurDTO()
@ -183,9 +282,12 @@ namespace IO.Swagger.Controllers
Code = "404", Code = "404",
Message = "Aucune formation réalisée" Message = "Aucune formation réalisée"
}; };
return NotFound(erreur); return NotFound(erreur);
} }
logger.LogInformation("Liste des formations réalisées récupérée.");
return Ok(formations); return Ok(formations);
} }
@ -210,10 +312,21 @@ namespace IO.Swagger.Controllers
[SwaggerOperation("GetFormations")] [SwaggerOperation("GetFormations")]
[SwaggerResponse(statusCode: 200, type: typeof(List<FormationDetailsDTO>), description: "OK")] [SwaggerResponse(statusCode: 200, type: typeof(List<FormationDetailsDTO>), description: "OK")]
[SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")]
public virtual async Task<IActionResult> GetFormations([FromQuery][Required()]bool? asc, [FromQuery][Required()]int? numPage, [FromQuery][Required()]int? parPAge, [FromQuery]int? idAgence, [FromQuery]int? statutFormation, [FromQuery]string texte, [FromQuery]string tri) public virtual async Task<IActionResult> GetFormations([FromQuery][Required()] bool? asc, [FromQuery][Required()] int? numPage, [FromQuery][Required()] int? parPAge, [FromQuery] int? idAgence, [FromQuery] int? statutFormation, [FromQuery] string texte, [FromQuery] string tri)
{
logger.LogInformation("Récupération de la liste des formations.");
IEnumerable<FormationDTO> formations = null;
try
{
formations = await formationService.GetFormationsAsync(asc, numPage, parPAge, idAgence, statutFormation, texte, tri);
}
catch (Exception)
{ {
//TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ... logger.LogError("Une erreur inconnue est survenue lors de la récupération de la liste des formations.");
IEnumerable<FormationDTO> formations = await formationService.GetFormationsAsync(asc, numPage, parPAge, idAgence, texte, tri); }
if (formations == null) if (formations == null)
{ {
ErreurDTO erreur = new ErreurDTO() ErreurDTO erreur = new ErreurDTO()
@ -221,9 +334,12 @@ namespace IO.Swagger.Controllers
Code = "404", Code = "404",
Message = "Aucune formation" Message = "Aucune formation"
}; };
return NotFound(erreur); return NotFound(erreur);
} }
logger.LogInformation("Liste des formations récupérée.");
return Ok(formations); return Ok(formations);
} }
@ -242,8 +358,19 @@ namespace IO.Swagger.Controllers
[SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")]
public virtual async Task<IActionResult> GetModesFormation() public virtual async Task<IActionResult> GetModesFormation()
{ {
//TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ... logger.LogInformation("Récupération de la liste des modes de formation.");
IEnumerable<ModeFormationDTO> modeFormations = await formationService.GetModesFormationAsync();
IEnumerable<ModeFormationDTO> modeFormations = null;
try
{
modeFormations = await formationService.GetModesFormationAsync();
}
catch (Exception)
{
logger.LogError("Une erreur inconnue est survenue lors de la récupération de la liste des modes de formation.");
}
if (modeFormations == null) if (modeFormations == null)
{ {
ErreurDTO erreur = new ErreurDTO() ErreurDTO erreur = new ErreurDTO()
@ -251,9 +378,12 @@ namespace IO.Swagger.Controllers
Code = "404", Code = "404",
Message = "Aucun mode de formation" Message = "Aucun mode de formation"
}; };
return NotFound(erreur); return NotFound(erreur);
} }
logger.LogInformation("Liste des modes de formation récupérée.");
return Ok(modeFormations); return Ok(modeFormations);
} }
@ -272,8 +402,19 @@ namespace IO.Swagger.Controllers
[SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")]
public virtual async Task<IActionResult> GetOriginesFormation() public virtual async Task<IActionResult> GetOriginesFormation()
{ {
//TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ... logger.LogInformation("Récupération de la liste des origines de formation.");
IEnumerable<OrigineFormationDTO> origineFormations = await formationService.GetOriginesFormationAsync();
IEnumerable<OrigineFormationDTO> origineFormations = null;
try
{
origineFormations = await formationService.GetOriginesFormationAsync();
}
catch (Exception)
{
logger.LogError("Une erreur inconnue est survenue lors de la récupération de la liste des origines de formation.");
}
if (origineFormations == null) if (origineFormations == null)
{ {
ErreurDTO erreur = new ErreurDTO() ErreurDTO erreur = new ErreurDTO()
@ -284,6 +425,8 @@ namespace IO.Swagger.Controllers
return NotFound(erreur); return NotFound(erreur);
} }
logger.LogInformation("Liste des origines de formation récupérée.");
return Ok(origineFormations); return Ok(origineFormations);
} }
@ -310,8 +453,19 @@ namespace IO.Swagger.Controllers
[SwaggerResponse(statusCode: 404, type: typeof(ErreurDTO), description: "Ressource n&#x27;a pas été trouvée")] [SwaggerResponse(statusCode: 404, type: typeof(ErreurDTO), description: "Ressource n&#x27;a pas été trouvée")]
public virtual async Task<IActionResult> GetProchainesFormation([FromQuery][Required()] bool? asc, [FromQuery][Required()] int? numPage, [FromQuery][Required()] int? parPAge, [FromQuery] int? idAgence, [FromQuery] string texte, [FromQuery] string tri) public virtual async Task<IActionResult> GetProchainesFormation([FromQuery][Required()] bool? asc, [FromQuery][Required()] int? numPage, [FromQuery][Required()] int? parPAge, [FromQuery] int? idAgence, [FromQuery] string texte, [FromQuery] string tri)
{ {
//TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ... logger.LogInformation("Récupération de la liste des prochaines formations.");
IEnumerable<FormationDTO> formations = await formationService.GetProchainesFormationAsync(asc, numPage, parPAge, idAgence, texte, tri);
IEnumerable<FormationDTO> formations = null;
try
{
formations = await formationService.GetProchainesFormationAsync(asc, numPage, parPAge, idAgence, texte, tri);
}
catch (Exception)
{
logger.LogError("Une erreur inconnue est survenue lors de la récupération de la liste des prochaines formations.");
}
if (formations == null) if (formations == null)
{ {
ErreurDTO erreur = new ErreurDTO() ErreurDTO erreur = new ErreurDTO()
@ -319,9 +473,12 @@ namespace IO.Swagger.Controllers
Code = "404", Code = "404",
Message = "Aucune prochaine formation" Message = "Aucune prochaine formation"
}; };
return NotFound(erreur); return NotFound(erreur);
} }
logger.LogInformation("Liste des prochaines formations récupérée.");
return Ok(formations); return Ok(formations);
} }
@ -340,8 +497,19 @@ namespace IO.Swagger.Controllers
[SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")]
public virtual async Task<IActionResult> GetStatutsFormation() public virtual async Task<IActionResult> GetStatutsFormation()
{ {
//TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ... logger.LogInformation("Récupération de la liste des statuts de formation.");
IEnumerable<StatutFormationDTO> statutFormations = await formationService.GetStatutsFormationAsync();
IEnumerable<StatutFormationDTO> statutFormations = null;
try
{
statutFormations = await formationService.GetStatutsFormationAsync();
}
catch (Exception)
{
logger.LogError("Une erreur inconnue est survenue lors de la récupération de la liste des statuts de formation.");
}
if (statutFormations == null) if (statutFormations == null)
{ {
ErreurDTO erreur = new ErreurDTO() ErreurDTO erreur = new ErreurDTO()
@ -349,9 +517,12 @@ namespace IO.Swagger.Controllers
Code = "404", Code = "404",
Message = "Aucun statut de formation" Message = "Aucun statut de formation"
}; };
return NotFound(erreur); return NotFound(erreur);
} }
logger.LogInformation("Liste des statuts de formation récupérée.");
return Ok(statutFormations); return Ok(statutFormations);
} }
@ -370,8 +541,19 @@ namespace IO.Swagger.Controllers
[SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")]
public virtual async Task<IActionResult> GetTypesFormation() public virtual async Task<IActionResult> GetTypesFormation()
{ {
//TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ... logger.LogInformation("Récupération de la liste des types de formation.");
IEnumerable<TypeFormationDTO> typeFormations = await formationService.GetTypesFormationAsync();
IEnumerable<TypeFormationDTO> typeFormations = null;
try
{
typeFormations = await formationService.GetTypesFormationAsync();
}
catch (Exception)
{
logger.LogError("Une erreur inconnue est survenue lors de la récupération de la liste des types de formation.");
}
if (typeFormations == null) if (typeFormations == null)
{ {
ErreurDTO erreur = new ErreurDTO() ErreurDTO erreur = new ErreurDTO()
@ -379,9 +561,12 @@ namespace IO.Swagger.Controllers
Code = "404", Code = "404",
Message = "Aucun type de formation" Message = "Aucun type de formation"
}; };
return NotFound(erreur); return NotFound(erreur);
} }
logger.LogInformation("Liste des types de formation récupérée.");
return Ok(typeFormations); return Ok(typeFormations);
} }
@ -402,12 +587,61 @@ namespace IO.Swagger.Controllers
[SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")]
public virtual async Task<IActionResult> UpdateFormation([FromBody] FormationDTO body, [FromRoute][Required] long? idFormation) public virtual async Task<IActionResult> UpdateFormation([FromBody] FormationDTO body, [FromRoute][Required] long? idFormation)
{ {
FormationDTO formation = await formationService.UpdateFormationAsync(idFormation, body); logger.LogInformation("Mise à jour de la formation d'id {idFormation}.", idFormation);
FormationDTO formation = null;
try
{
formation = await formationService.UpdateFormationAsync(idFormation, body);
}
catch (FormationInvalidException)
{
logger.LogWarning("Des données sont manquants, la formation {idFormation} ne peut pas être mise à jour.", idFormation);
}
catch (FormationIncompatibleIdException)
{
logger.LogError("L'id de la formation à mettre à jour {body.Id} et l'id de la formation avec les nouvelles informations {idFormation} sont différents.", body.Id, idFormation);
}
catch (DbUpdateConcurrencyException)
{
logger.LogError("La formation {idFormation} n'a pas pu être mise à jour car elle est prise par une autre ressource.", idFormation);
}
catch (DbUpdateException)
{
logger.LogError("Une erreur est survenue dans la base de données lors de la mise à jour de la formation {idFormation}.", idFormation);
}
catch (Exception)
{
logger.LogError("Une erreur inconnue est survenue lors de la mise à jour de la formation {idFormation}.", idFormation);
}
if (formation == null) if (formation == null)
{ {
formation = formationService.AddFormation(body); try
{
formation = await formationService.AddFormationAsync(body);
}
catch (FormationInvalidException)
{
logger.LogWarning("Des données sont manquants, la nouvelle formation ne peut pas être ajoutée.");
}
catch (DbUpdateException)
{
logger.LogError("Une erreur est survenue dans la base de données durant l'ajout de la nouvelle formation.");
}
catch (Exception)
{
logger.LogError("Une erreur inconnue est survenue lors de l'ajout de la formation note.");
}
logger.LogInformation("Nouvelle formation ajoutée.");
return Created("", formation); return Created("", formation);
} }
logger.LogInformation("Update effectué avec succès");
return Ok(formation); return Ok(formation);
} }
} }

@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace EPAServeur.Exceptions
{
/// <summary>
/// Exception à jeter lorsque l'id de la formation avec les données à mettre à jour et l'id de la formation à mettre sont différents
/// </summary>
public class FormationIncompatibleIdException : Exception
{
/// <summary>
/// Initialise une nouvelle instance de la classe <see cref="FormationIncompatibleIdException"/> class.
/// </summary>
public FormationIncompatibleIdException()
{
}
/// <summary>
/// Initialise une nouvelle instance de la classe <see cref="FormationIncompatibleIdException"/> class.
/// </summary>
/// <param name="message"></param>
public FormationIncompatibleIdException(string message) : base(message)
{
}
/// <summary>
/// Initialise une nouvelle instance de la classe <see cref="FormationIncompatibleIdException"/> class.
/// </summary>
/// <param name="message"></param>
/// <param name="inner"></param>
public FormationIncompatibleIdException(string message, Exception inner) : base(message, inner)
{
}
}
}

@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace EPAServeur.Exceptions
{
/// <summary>
/// Exception à jeter lorsq'une formation est invalide
/// </summary>
public class FormationInvalidException : Exception
{
/// <summary>
/// Initialise une nouvelle instance de la classe <see cref="FormationInvalidException"/> class.
/// </summary>
public FormationInvalidException()
{
}
/// <summary>
/// Initialise une nouvelle instance de la classe <see cref="FormationInvalidException"/> class.
/// </summary>
/// <param name="message"></param>
public FormationInvalidException(string message) : base(message)
{
}
/// <summary>
/// Initialise une nouvelle instance de la classe <see cref="FormationInvalidException"/> class.
/// </summary>
/// <param name="message"></param>
/// <param name="inner"></param>
public FormationInvalidException(string message, Exception inner) : base(message, inner)
{
}
}
}

@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace EPAServeur.Exceptions
{
/// <summary>
/// Exception à jeter lorsqu'une formation n'a pas été trouvée
/// </summary>
public class FormationNotFoundException : Exception
{
/// <summary>
/// Initialise une nouvelle instance de la classe <see cref="FormationNotFoundException"/> class.
/// </summary>
public FormationNotFoundException()
{
}
/// <summary>
/// Initialise une nouvelle instance de la classe <see cref="FormationNotFoundException"/> class.
/// </summary>
/// <param name="message"></param>
public FormationNotFoundException(string message) : base(message)
{
}
/// <summary>
/// Initialise une nouvelle instance de la classe <see cref="FormationNotFoundException"/> class.
/// </summary>
/// <param name="message"></param>
/// <param name="inner"></param>
public FormationNotFoundException(string message, Exception inner) : base(message, inner)
{
}
}
}

@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace EPAServeur.Exceptions
{
/// <summary>
/// Exception à jeter lorsqu'un mode de formation n'a pas été trouvé
/// </summary>
public class ModeFormationNotFoundException : Exception
{
/// <summary>
/// Initialise une nouvelle instance de la classe <see cref="ModeFormationNotFoundException"/> class.
/// </summary>
public ModeFormationNotFoundException()
{
}
/// <summary>
/// Initialise une nouvelle instance de la classe <see cref="ModeFormationNotFoundException"/> class.
/// </summary>
/// <param name="message"></param>
public ModeFormationNotFoundException(string message) : base(message)
{
}
/// <summary>
/// Initialise une nouvelle instance de la classe <see cref="ModeFormationNotFoundException"/> class.
/// </summary>
/// <param name="message"></param>
/// <param name="inner"></param>
public ModeFormationNotFoundException(string message, Exception inner) : base(message, inner)
{
}
}
}

@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace EPAServeur.Exceptions
{
/// <summary>
/// Exception à jeter lorsqu'une origine de formation n'a pas été trouvée
/// </summary>
public class OrigineFormationNotFoundException : Exception
{
/// <summary>
/// Initialise une nouvelle instance de la classe <see cref="OrigineFormationNotFoundException"/> class.
/// </summary>
public OrigineFormationNotFoundException()
{
}
/// <summary>
/// Initialise une nouvelle instance de la classe <see cref="OrigineFormationNotFoundException"/> class.
/// </summary>
/// <param name="message"></param>
public OrigineFormationNotFoundException(string message) : base(message)
{
}
/// <summary>
/// Initialise une nouvelle instance de la classe <see cref="OrigineFormationNotFoundException"/> class.
/// </summary>
/// <param name="message"></param>
/// <param name="inner"></param>
public OrigineFormationNotFoundException(string message, Exception inner) : base(message, inner)
{
}
}
}

@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace EPAServeur.Exceptions
{
/// <summary>
/// Exception à jeter lorsqu'un statut de formation n'a pas été trouvé
/// </summary>
public class StatutFormationNotFoundException : Exception
{
/// <summary>
/// Initialise une nouvelle instance de la classe <see cref="StatutFormationNotFoundException"/> class.
/// </summary>
public StatutFormationNotFoundException()
{
}
/// <summary>
/// Initialise une nouvelle instance de la classe <see cref="StatutFormationNotFoundException"/> class.
/// </summary>
/// <param name="message"></param>
public StatutFormationNotFoundException(string message) : base(message)
{
}
/// <summary>
/// Initialise une nouvelle instance de la classe <see cref="StatutFormationNotFoundException"/> class.
/// </summary>
/// <param name="message"></param>
/// <param name="inner"></param>
public StatutFormationNotFoundException(string message, Exception inner) : base(message, inner)
{
}
}
}

@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace EPAServeur.Exceptions
{
/// <summary>
/// Exception à jeter lorsqu'un type de formation n'a pas été trouvé
/// </summary>
public class TypeFormationNotFoundException : Exception
{
/// <summary>
/// Initialise une nouvelle instance de la classe <see cref="TypeFormationNotFoundException"/> class.
/// </summary>
public TypeFormationNotFoundException()
{
}
/// <summary>
/// Initialise une nouvelle instance de la classe <see cref="TypeFormationNotFoundException"/> class.
/// </summary>
/// <param name="message"></param>
public TypeFormationNotFoundException(string message) : base(message)
{
}
/// <summary>
/// Initialise une nouvelle instance de la classe <see cref="TypeFormationNotFoundException"/> class.
/// </summary>
/// <param name="message"></param>
/// <param name="inner"></param>
public TypeFormationNotFoundException(string message, Exception inner) : base(message, inner)
{
}
}
}

@ -12,8 +12,8 @@ namespace EPAServeur.IServices
{ {
FormationDTO GetFormationById(long? idFormation); FormationDTO GetFormationById(long? idFormation);
Task<FormationDTO> GetFormationByIdAsync(long? idFormation); Task<FormationDTO> GetFormationByIdAsync(long? idFormation);
IEnumerable<FormationDTO> GetFormations(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri); IEnumerable<FormationDTO> GetFormations(bool? asc, int? numPage, int? parPAge, int? idAgence, int? statutFormation, string texte, string tri);
Task<IEnumerable<FormationDTO>> GetFormationsAsync(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri); Task<IEnumerable<FormationDTO>> GetFormationsAsync(bool? asc, int? numPage, int? parPAge, int? idAgence, int? statutFormation, string texte, string tri);
IEnumerable<FormationDTO> GetFormationAnnulees(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri); IEnumerable<FormationDTO> GetFormationAnnulees(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri);
Task<IEnumerable<FormationDTO>> GetFormationAnnuleesAsync(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri); Task<IEnumerable<FormationDTO>> GetFormationAnnuleesAsync(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri);
IEnumerable<FormationDTO> GetFormationRealisee(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri); IEnumerable<FormationDTO> GetFormationRealisee(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri);
@ -33,8 +33,8 @@ namespace EPAServeur.IServices
Task<FormationDTO> AddFormationAsync(FormationDTO formationDTO); Task<FormationDTO> AddFormationAsync(FormationDTO formationDTO);
FormationDTO UpdateFormation(long? idFormation, FormationDTO formationDTO); FormationDTO UpdateFormation(long? idFormation, FormationDTO formationDTO);
Task<FormationDTO> UpdateFormationAsync(long? idFormation, FormationDTO formationDTO); Task<FormationDTO> UpdateFormationAsync(long? idFormation, FormationDTO formationDTO);
bool DeleteFormationById(long? idFormation); FormationDTO DeleteFormationById(long? idFormation);
Task<bool> DeleteFormationByIdAsync(long? idFormation); Task<FormationDTO> DeleteFormationByIdAsync(long? idFormation);
} }
} }

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

Loading…
Cancel
Save