diff --git a/Controllers/FormationsApi.cs b/Controllers/FormationsApi.cs index ba84147..6c58dde 100644 --- a/Controllers/FormationsApi.cs +++ b/Controllers/FormationsApi.cs @@ -11,16 +11,16 @@ using System; using System.Collections.Generic; using Microsoft.AspNetCore.Mvc; using Swashbuckle.AspNetCore.Annotations; -using Swashbuckle.AspNetCore.SwaggerGen; using Newtonsoft.Json; using System.ComponentModel.DataAnnotations; using IO.Swagger.Attributes; -using IO.Swagger.Security; -using Microsoft.AspNetCore.Authorization; using IO.Swagger.DTO; using EPAServeur.IServices; -using System.Reflection.Metadata.Ecma335; -using System.Runtime.InteropServices.WindowsRuntime; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.Extensions.Logging; +using EPAServeur.Exceptions; +using Microsoft.EntityFrameworkCore; namespace IO.Swagger.Controllers { @@ -31,10 +31,12 @@ namespace IO.Swagger.Controllers public class FormationsApiController : ControllerBase { private readonly IFormationService formationService; + private readonly ILogger logger; - public FormationsApiController(IFormationService _formationService) + public FormationsApiController(IFormationService _formationService, ILogger _logger) { formationService = _formationService; + logger = _logger; } /// @@ -50,19 +52,32 @@ namespace IO.Swagger.Controllers [ValidateModelState] [SwaggerOperation("AjouterFormation")] [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] - public virtual IActionResult AjouterFormation([FromBody] FormationDTO body) + public virtual async Task AjouterFormation([FromBody] FormationDTO body) { - FormationDTO nouvelleFormation = formationService.AddFormation(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); - //if (body.Id != null && body.Id > 0) - //{ - // return StatusCode(201, body); - //} - //else - //{ - // return NotFound(); - //} } /// @@ -78,17 +93,41 @@ namespace IO.Swagger.Controllers [ValidateModelState] [SwaggerOperation("DeleteFormation")] [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] - public virtual IActionResult DeleteFormation([FromRoute][Required] long? idFormation) + public virtual async Task DeleteFormation([FromRoute][Required] long? idFormation) { - if (!formationService.DeleteFormationById(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() { Code = "404", Message = "Aucune formation trouvée" }; + 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(); } @@ -113,10 +152,21 @@ namespace IO.Swagger.Controllers [SwaggerResponse(statusCode: 200, type: typeof(List), description: "OK")] [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] [SwaggerResponse(statusCode: 404, type: typeof(ErreurDTO), description: "Ressource n'a pas été trouvée")] - public virtual 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 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(..), ... - IEnumerable formations = formationService.GetFormationAnnulees(asc, numPage, parPAge, idAgence, texte, tri); + logger.LogInformation("Récupération de la liste des formations annulées."); + + IEnumerable 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) { ErreurDTO erreur = new ErreurDTO() @@ -124,9 +174,12 @@ namespace IO.Swagger.Controllers Code = "404", Message = "Aucune formation annulée" }; + return NotFound(erreur); } + logger.LogInformation("Liste des formations annulées récupérée."); + return Ok(formations); } @@ -146,18 +199,43 @@ namespace IO.Swagger.Controllers [SwaggerResponse(statusCode: 200, type: typeof(FormationDTO), description: "OK")] [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] [SwaggerResponse(statusCode: 404, type: typeof(ErreurDTO), description: "Ressource n'a pas été trouvée")] - public virtual IActionResult GetFormationById([FromRoute][Required] long? idFormation) + public virtual async Task GetFormationById([FromRoute][Required] long? idFormation) { - FormationDTO formationDTO = formationService.GetFormationById(Convert.ToInt32(idFormation)); - if (formationDTO == null) + logger.LogInformation("Récupération de la formation {idFormation}.", idFormation); + + 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() { Code = "404", Message = "La formation n'existe pas", }; + 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); } @@ -182,10 +260,21 @@ namespace IO.Swagger.Controllers [SwaggerResponse(statusCode: 200, type: typeof(List), description: "OK")] [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] [SwaggerResponse(statusCode: 404, type: typeof(ErreurDTO), description: "Ressource n'a pas été trouvée")] - public virtual 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 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(..), ... - IEnumerable formations = formationService.GetFormationRealisee(asc, numPage, parPAge, idAgence, texte, tri); + logger.LogInformation("Récupération de la liste des formations réalisées."); + + IEnumerable 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) { ErreurDTO erreur = new ErreurDTO() @@ -193,9 +282,12 @@ namespace IO.Swagger.Controllers Code = "404", Message = "Aucune formation réalisée" }; + return NotFound(erreur); } + logger.LogInformation("Liste des formations réalisées récupérée."); + return Ok(formations); } @@ -220,10 +312,21 @@ namespace IO.Swagger.Controllers [SwaggerOperation("GetFormations")] [SwaggerResponse(statusCode: 200, type: typeof(List), description: "OK")] [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] - public virtual 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 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) { - //TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - IEnumerable formations = formationService.GetFormations(asc, numPage, parPAge, idAgence, texte, tri); + logger.LogInformation("Récupération de la liste des formations."); + + IEnumerable formations = null; + + try + { + formations = await formationService.GetFormationsAsync(asc, numPage, parPAge, idAgence, statutFormation, texte, tri); + } + catch (Exception) + { + logger.LogError("Une erreur inconnue est survenue lors de la récupération de la liste des formations."); + } + if (formations == null) { ErreurDTO erreur = new ErreurDTO() @@ -231,9 +334,12 @@ namespace IO.Swagger.Controllers Code = "404", Message = "Aucune formation" }; + return NotFound(erreur); } + logger.LogInformation("Liste des formations récupérée."); + return Ok(formations); } @@ -250,10 +356,21 @@ namespace IO.Swagger.Controllers [SwaggerOperation("GetModesFormation")] [SwaggerResponse(statusCode: 200, type: typeof(List), description: "OK")] [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] - public virtual IActionResult GetModesFormation() + public virtual async Task GetModesFormation() { - //TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - IEnumerable modeFormations = formationService.GetModesFormation(); + logger.LogInformation("Récupération de la liste des modes de formation."); + + IEnumerable 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) { ErreurDTO erreur = new ErreurDTO() @@ -261,9 +378,12 @@ namespace IO.Swagger.Controllers Code = "404", Message = "Aucun mode de formation" }; + return NotFound(erreur); } + logger.LogInformation("Liste des modes de formation récupérée."); + return Ok(modeFormations); } @@ -280,10 +400,21 @@ namespace IO.Swagger.Controllers [SwaggerOperation("GetOriginesFormation")] [SwaggerResponse(statusCode: 200, type: typeof(List), description: "OK")] [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] - public virtual IActionResult GetOriginesFormation() + public virtual async Task GetOriginesFormation() { - //TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - IEnumerable origineFormations = formationService.GetOriginesFormation(); + logger.LogInformation("Récupération de la liste des origines de formation."); + + IEnumerable 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) { ErreurDTO erreur = new ErreurDTO() @@ -294,6 +425,8 @@ namespace IO.Swagger.Controllers return NotFound(erreur); } + logger.LogInformation("Liste des origines de formation récupérée."); + return Ok(origineFormations); } @@ -318,10 +451,21 @@ namespace IO.Swagger.Controllers [SwaggerResponse(statusCode: 200, type: typeof(List), description: "OK")] [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] [SwaggerResponse(statusCode: 404, type: typeof(ErreurDTO), description: "Ressource n'a pas été trouvée")] - public virtual 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 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(..), ... - IEnumerable formations = formationService.GetProchainesFormation(asc, numPage, parPAge, idAgence, texte, tri); + logger.LogInformation("Récupération de la liste des prochaines formations."); + + IEnumerable 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) { ErreurDTO erreur = new ErreurDTO() @@ -329,9 +473,12 @@ namespace IO.Swagger.Controllers Code = "404", Message = "Aucune prochaine formation" }; + return NotFound(erreur); } + logger.LogInformation("Liste des prochaines formations récupérée."); + return Ok(formations); } @@ -348,10 +495,21 @@ namespace IO.Swagger.Controllers [SwaggerOperation("GetStatutsFormation")] [SwaggerResponse(statusCode: 200, type: typeof(List), description: "OK")] [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] - public virtual IActionResult GetStatutsFormation() + public virtual async Task GetStatutsFormation() { - //TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - IEnumerable statutFormations = formationService.GetStatutsFormation(); + logger.LogInformation("Récupération de la liste des statuts de formation."); + + IEnumerable 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) { ErreurDTO erreur = new ErreurDTO() @@ -359,9 +517,12 @@ namespace IO.Swagger.Controllers Code = "404", Message = "Aucun statut de formation" }; + return NotFound(erreur); } + logger.LogInformation("Liste des statuts de formation récupérée."); + return Ok(statutFormations); } @@ -378,10 +539,21 @@ namespace IO.Swagger.Controllers [SwaggerOperation("GetTypesFormation")] [SwaggerResponse(statusCode: 200, type: typeof(List), description: "OK")] [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] - public virtual IActionResult GetTypesFormation() + public virtual async Task GetTypesFormation() { - //TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - IEnumerable typeFormations = formationService.GetTypesFormation(); + logger.LogInformation("Récupération de la liste des types de formation."); + + IEnumerable 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) { ErreurDTO erreur = new ErreurDTO() @@ -389,9 +561,12 @@ namespace IO.Swagger.Controllers Code = "404", Message = "Aucun type de formation" }; + return NotFound(erreur); } + logger.LogInformation("Liste des types de formation récupérée."); + return Ok(typeFormations); } @@ -410,29 +585,64 @@ namespace IO.Swagger.Controllers [ValidateModelState] [SwaggerOperation("UpdateFormation")] [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] - public virtual IActionResult UpdateFormation([FromBody] FormationDTO body, [FromRoute][Required] long? idFormation) + public virtual async Task UpdateFormation([FromBody] FormationDTO body, [FromRoute][Required] long? idFormation) { - FormationDTO formation = formationService.UpdateFormation(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) { - 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 Ok(formation); + logger.LogInformation("Update effectué avec succès"); - //switch (formationService.UpdateFormation(body)) - //{ - // case 0: - // return Ok(); - // case 1: - // return StatusCode(201); - // case 2: - // return Forbid(); - // default: - // return NotFound(); - //} - + return Ok(formation); } } } diff --git a/Controllers/ReferentsApi.cs b/Controllers/ReferentsApi.cs index af8dc61..cf5c6ad 100644 --- a/Controllers/ReferentsApi.cs +++ b/Controllers/ReferentsApi.cs @@ -20,6 +20,10 @@ using System.Net; using Microsoft.AspNetCore.Authorization; using IO.Swagger.Security; using System.Linq; +using System.Threading.Tasks; +using Microsoft.Extensions.Logging; +using IO.Swagger.ClientCollaborateur; +using EPAServeur.Exceptions; namespace IO.Swagger.Controllers { @@ -30,10 +34,12 @@ namespace IO.Swagger.Controllers public class ReferentsApiController : ControllerBase { private readonly IReferentService referentService; + private readonly ILogger logger; - public ReferentsApiController(IReferentService _referentService) + public ReferentsApiController(IReferentService _referentService, ILogger _logger) { referentService = _referentService; + logger = _logger; } /// @@ -52,18 +58,38 @@ namespace IO.Swagger.Controllers [SwaggerResponse(statusCode: 200, type: typeof(ReferentDTO), description: "OK")] [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] [SwaggerResponse(statusCode: 404, type: typeof(ErreurDTO), description: "Ressource n'a pas été trouvée")] - public virtual IActionResult GetReferentById([FromRoute][Required]Guid? idReferent) + public virtual async Task GetReferentById([FromRoute][Required]Guid? idReferent) { - ReferentDTO referentDTO = referentService.GetReferentById(idReferent); - if (referentDTO == null) + logger.LogInformation("Récupération du référent {idReferent}.", idReferent); + + ReferentDTO referentDTO = null; + + try + { + referentDTO = await referentService.GetReferentByIdAsync(idReferent); + } + catch (ApiException) + { + logger.LogError("Une erreur est survenue lors de la communication avec le service Collaborateur pour récupérer le référent par son id {idReferent}.", idReferent); + } + catch (ReferentNotFoundException) { + logger.LogError("Le référent {idReferent} est introuvable.", idReferent); ErreurDTO erreurDTO = new ErreurDTO() { Code = "404", Message = "Le référent n'existe pas", }; + return NotFound(erreurDTO); } + catch (Exception) + { + logger.LogError("Une erreur inconnue est survenue lors de la récupération du référent {idReferent}.", idReferent); + } + + logger.LogInformation("Référent {idReferent} récupéré.", idReferent); + return Ok(referentDTO); } @@ -89,19 +115,9 @@ namespace IO.Swagger.Controllers [SwaggerResponse(statusCode: 200, type: typeof(List), description: "OK")] [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] public virtual IActionResult GetReferents([FromQuery][Required()]bool? asc, [FromQuery][Required()]int? numPage, [FromQuery][Required()]int? parPAge, [FromQuery]List fonctions, [FromQuery]long? idAgence, [FromQuery]long? idBU, [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(..), ... - // return StatusCode(200, default(List)); - - //TODO: Uncomment the next line to return response 403 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - // return StatusCode(403, default(ErreurDTO)); - string exampleJson = null; - exampleJson = "[ {\n \"mailApside\" : \"\",\n \"id\" : \"046b6c7f-0b8a-43b9-b35d-6489e6daee91\",\n \"nom\" : \"nom\",\n \"prenom\" : \"prenom\",\n \"collaborateurs\" : [ null, null ]\n}, {\n \"mailApside\" : \"\",\n \"id\" : \"046b6c7f-0b8a-43b9-b35d-6489e6daee91\",\n \"nom\" : \"nom\",\n \"prenom\" : \"prenom\",\n \"collaborateurs\" : [ null, null ]\n} ]"; - - var example = exampleJson != null - ? JsonConvert.DeserializeObject>(exampleJson) - : default(List); //TODO: Change the data returned - return new ObjectResult(example); + { + //IEnumerable referentDTOs = referentService.GetReferents(asc,numPage,parPAge,fonctions,idAgence,idBU,texte,tri); + return NoContent(); } /// @@ -120,18 +136,38 @@ namespace IO.Swagger.Controllers [SwaggerResponse(statusCode: 200, type: typeof(ReferentDTO), description: "OK")] [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] [SwaggerResponse(statusCode: 404, type: typeof(ErreurDTO), description: "Ressource n'a pas été trouvée")] - public virtual IActionResult GetReferentActuelCollaborateur([FromRoute][Required] Guid? idCollaborateur) + public virtual async Task GetReferentActuelCollaborateur([FromRoute][Required] Guid? idCollaborateur) { - ReferentDTO referentDTO = referentService.GetReferentActuelCollaborateur(idCollaborateur); - if (referentDTO == null) + logger.LogInformation("Récupération du référent actuel du collaborateur {idCollaborateur}.", idCollaborateur); + + ReferentDTO referentDTO = null; + + try + { + referentDTO = await referentService.GetReferentActuelCollaborateurAsync(idCollaborateur); + } + catch (ApiException) + { + logger.LogError("Une erreur est survenue lors de la communication avec le service Collaborateur pour récupérer le référent actuel du collaborateur {idCollaborateur}.", idCollaborateur); + } + catch (ReferentNotFoundException) { + logger.LogError("Le référent actuel du collaborateur {idCollaborateur} est introuvable.", idCollaborateur); ErreurDTO erreurDTO = new ErreurDTO() { Code = "404", - Message = "Aucun référent pour le collaborateur", + Message = "Aucun référent actuel pour le collaborateur", }; + return NotFound(erreurDTO); } + catch (Exception) + { + logger.LogError("Une erreur inconnue est survenue lors de la récupération du référent actuel du collaborateur {idCollaborateur}.", idCollaborateur); + } + + logger.LogInformation("Référent actuel du collaborateur {idCollaborateur} récupéré.", idCollaborateur); + return Ok(referentDTO); } @@ -156,19 +192,51 @@ namespace IO.Swagger.Controllers [SwaggerResponse(statusCode: 200, type: typeof(List), description: "OK")] [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] [SwaggerResponse(statusCode: 404, type: typeof(ErreurDTO), description: "Ressource n'a pas été trouvée")] - public virtual IActionResult GetReferentsByCollaborateur([FromQuery][Required()] bool? asc, [FromRoute][Required] Guid? idCollaborateur, [FromQuery][Required()] int? numPage, [FromQuery][Required()] int? parPAge, [FromQuery] string texte, [FromQuery] string tri) + public virtual async Task GetReferentsByCollaborateur([FromQuery][Required()] bool? asc, [FromRoute][Required] Guid? idCollaborateur, [FromQuery][Required()] int? numPage, [FromQuery][Required()] int? parPAge, [FromQuery] string texte, [FromQuery] string tri) { - IEnumerable referentDTO = referentService.GetReferentsByCollaborateur(asc,idCollaborateur,numPage,parPAge,texte,tri); - if (referentDTO.Count() == 0) + logger.LogInformation("Récupération de la liste des référents du collaborateur {idCollaborateur}.", idCollaborateur); + + IEnumerable referentDTOs = null; + + try + { + referentDTOs = await referentService.GetReferentsByCollaborateurAsync(asc, idCollaborateur, numPage, parPAge, texte, tri); + } + catch (ApiException) + { + logger.LogError("Une erreur est survenue lors de la communication avec le service collaborateur lors de la récupération de la liste des référents du collaborateur {idReferent}.", idCollaborateur); + } + catch (CollaborateurNotFoundException) { + ErreurDTO erreurDTO = new ErreurDTO() + { + Code = "404", + Message = "Le collaborateur n'existe pas", + }; + + return NotFound(erreurDTO); + } + catch (Exception) + { + logger.LogError("Une erreur inconnue est survenue lors de la récupération des référents du collaborateur {idCollaborateur}.", idCollaborateur); + } + + if (referentDTOs.Count() == 0) + { + logger.LogInformation("Aucun référent pour le collaborateur {idCollaborateur}.", idCollaborateur); + ErreurDTO erreurDTO = new ErreurDTO() { Code = "404", Message = "Aucun référent pour le collaborateur", }; + return NotFound(erreurDTO); } - return Ok(referentDTO); + + logger.LogInformation("Liste des référents du collaborateur {idCollaborateur} récupérée", idCollaborateur); + + return Ok(referentDTOs); } } } diff --git a/DTO/FormationDetailsDTO.cs b/DTO/FormationDetailsDTO.cs index d125cb6..50e979f 100644 --- a/DTO/FormationDetailsDTO.cs +++ b/DTO/FormationDetailsDTO.cs @@ -66,6 +66,13 @@ namespace IO.Swagger.DTO [DataMember(Name="nbPartitipants")] public int? NbPartitipants { get; set; } + /// + /// Gets or Sets Origine + /// + [Required] + [DataMember(Name = "origine")] + public OrigineFormationDTO Origine { get; set; } + /// /// Gets or Sets Mode /// diff --git a/Exceptions/FormationIncompatibleIdException.cs b/Exceptions/FormationIncompatibleIdException.cs new file mode 100644 index 0000000..2fc7201 --- /dev/null +++ b/Exceptions/FormationIncompatibleIdException.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace EPAServeur.Exceptions +{ + /// + /// Exception à jeter lorsque l'id de la formation avec les données à mettre à jour et l'id de la formation à mettre sont différents + /// + public class FormationIncompatibleIdException : Exception + { + /// + /// Initialise une nouvelle instance de la classe class. + /// + public FormationIncompatibleIdException() + { + + } + + /// + /// Initialise une nouvelle instance de la classe class. + /// + /// + public FormationIncompatibleIdException(string message) : base(message) + { + + } + + /// + /// Initialise une nouvelle instance de la classe class. + /// + /// + /// + public FormationIncompatibleIdException(string message, Exception inner) : base(message, inner) + { + + } + } +} diff --git a/Exceptions/FormationInvalidException.cs b/Exceptions/FormationInvalidException.cs new file mode 100644 index 0000000..0d5e444 --- /dev/null +++ b/Exceptions/FormationInvalidException.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace EPAServeur.Exceptions +{ + /// + /// Exception à jeter lorsq'une formation est invalide + /// + public class FormationInvalidException : Exception + { + /// + /// Initialise une nouvelle instance de la classe class. + /// + public FormationInvalidException() + { + + } + + /// + /// Initialise une nouvelle instance de la classe class. + /// + /// + public FormationInvalidException(string message) : base(message) + { + + } + + /// + /// Initialise une nouvelle instance de la classe class. + /// + /// + /// + public FormationInvalidException(string message, Exception inner) : base(message, inner) + { + + } + } +} diff --git a/Exceptions/FormationNotFoundException.cs b/Exceptions/FormationNotFoundException.cs new file mode 100644 index 0000000..93fdcc9 --- /dev/null +++ b/Exceptions/FormationNotFoundException.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace EPAServeur.Exceptions +{ + /// + /// Exception à jeter lorsqu'une formation n'a pas été trouvée + /// + public class FormationNotFoundException : Exception + { + /// + /// Initialise une nouvelle instance de la classe class. + /// + public FormationNotFoundException() + { + + } + + /// + /// Initialise une nouvelle instance de la classe class. + /// + /// + public FormationNotFoundException(string message) : base(message) + { + + } + + /// + /// Initialise une nouvelle instance de la classe class. + /// + /// + /// + public FormationNotFoundException(string message, Exception inner) : base(message, inner) + { + + } + } +} diff --git a/Exceptions/ModeFormationNotFoundException.cs b/Exceptions/ModeFormationNotFoundException.cs new file mode 100644 index 0000000..c3c1f4a --- /dev/null +++ b/Exceptions/ModeFormationNotFoundException.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace EPAServeur.Exceptions +{ + /// + /// Exception à jeter lorsqu'un mode de formation n'a pas été trouvé + /// + public class ModeFormationNotFoundException : Exception + { + /// + /// Initialise une nouvelle instance de la classe class. + /// + public ModeFormationNotFoundException() + { + + } + + /// + /// Initialise une nouvelle instance de la classe class. + /// + /// + public ModeFormationNotFoundException(string message) : base(message) + { + + } + + /// + /// Initialise une nouvelle instance de la classe class. + /// + /// + /// + public ModeFormationNotFoundException(string message, Exception inner) : base(message, inner) + { + + } + } +} diff --git a/Exceptions/OrigineFormationNotFoundException.cs b/Exceptions/OrigineFormationNotFoundException.cs new file mode 100644 index 0000000..26438b6 --- /dev/null +++ b/Exceptions/OrigineFormationNotFoundException.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace EPAServeur.Exceptions +{ + /// + /// Exception à jeter lorsqu'une origine de formation n'a pas été trouvée + /// + public class OrigineFormationNotFoundException : Exception + { + /// + /// Initialise une nouvelle instance de la classe class. + /// + public OrigineFormationNotFoundException() + { + + } + + /// + /// Initialise une nouvelle instance de la classe class. + /// + /// + public OrigineFormationNotFoundException(string message) : base(message) + { + + } + + /// + /// Initialise une nouvelle instance de la classe class. + /// + /// + /// + public OrigineFormationNotFoundException(string message, Exception inner) : base(message, inner) + { + + } + } +} diff --git a/Exceptions/StatutFormationNotFoundException.cs b/Exceptions/StatutFormationNotFoundException.cs new file mode 100644 index 0000000..66c7167 --- /dev/null +++ b/Exceptions/StatutFormationNotFoundException.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace EPAServeur.Exceptions +{ + /// + /// Exception à jeter lorsqu'un statut de formation n'a pas été trouvé + /// + public class StatutFormationNotFoundException : Exception + { + /// + /// Initialise une nouvelle instance de la classe class. + /// + public StatutFormationNotFoundException() + { + + } + + /// + /// Initialise une nouvelle instance de la classe class. + /// + /// + public StatutFormationNotFoundException(string message) : base(message) + { + + } + + /// + /// Initialise une nouvelle instance de la classe class. + /// + /// + /// + public StatutFormationNotFoundException(string message, Exception inner) : base(message, inner) + { + + } + } +} diff --git a/Exceptions/TypeFormationNotFoundException.cs b/Exceptions/TypeFormationNotFoundException.cs new file mode 100644 index 0000000..54f1de8 --- /dev/null +++ b/Exceptions/TypeFormationNotFoundException.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace EPAServeur.Exceptions +{ + /// + /// Exception à jeter lorsqu'un type de formation n'a pas été trouvé + /// + public class TypeFormationNotFoundException : Exception + { + /// + /// Initialise une nouvelle instance de la classe class. + /// + public TypeFormationNotFoundException() + { + + } + + /// + /// Initialise une nouvelle instance de la classe class. + /// + /// + public TypeFormationNotFoundException(string message) : base(message) + { + + } + + /// + /// Initialise une nouvelle instance de la classe class. + /// + /// + /// + public TypeFormationNotFoundException(string message, Exception inner) : base(message, inner) + { + + } + } +} diff --git a/IServices/IFormationService.cs b/IServices/IFormationService.cs index dbe8278..6b455c8 100644 --- a/IServices/IFormationService.cs +++ b/IServices/IFormationService.cs @@ -10,19 +10,31 @@ namespace EPAServeur.IServices { public interface IFormationService { - FormationDTO GetFormationById(long? id); - - IEnumerable GetFormations(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri); + FormationDTO GetFormationById(long? idFormation); + Task GetFormationByIdAsync(long? idFormation); + IEnumerable GetFormations(bool? asc, int? numPage, int? parPAge, int? idAgence, int? statutFormation, string texte, string tri); + Task> GetFormationsAsync(bool? asc, int? numPage, int? parPAge, int? idAgence, int? statutFormation, string texte, string tri); IEnumerable GetFormationAnnulees(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri); + Task> GetFormationAnnuleesAsync(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri); IEnumerable GetFormationRealisee(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri); + Task> GetFormationRealiseeAsync(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri); IEnumerable GetProchainesFormation(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri); + Task> GetProchainesFormationAsync(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri); IEnumerable GetModesFormation(); + Task> GetModesFormationAsync(); IEnumerable GetOriginesFormation(); + Task> GetOriginesFormationAsync(); IEnumerable GetStatutsFormation(); + Task> GetStatutsFormationAsync(); IEnumerable GetTypesFormation(); + Task> GetTypesFormationAsync(); FormationDTO AddFormation(FormationDTO formationDTO); - FormationDTO UpdateFormation(FormationDTO formationDTO); - bool DeleteFormationById(long? id); + Task AddFormationAsync(FormationDTO formationDTO); + FormationDTO UpdateFormation(long? idFormation, FormationDTO formationDTO); + Task UpdateFormationAsync(long? idFormation, FormationDTO formationDTO); + FormationDTO DeleteFormationById(long? idFormation); + Task DeleteFormationByIdAsync(long? idFormation); + } } diff --git a/IServices/IReferentService.cs b/IServices/IReferentService.cs index fe24257..8cb68cb 100644 --- a/IServices/IReferentService.cs +++ b/IServices/IReferentService.cs @@ -10,11 +10,15 @@ namespace EPAServeur.IServices { public interface IReferentService { - ReferentDTO GetReferentById(Guid? id); + ReferentDTO GetReferentById(Guid? idReferent); + Task GetReferentByIdAsync(Guid? idReferent); ReferentDTO GetReferentActuelCollaborateur(Guid? idCollaborateur); + Task GetReferentActuelCollaborateurAsync(Guid? idCollaborateur); IEnumerable GetReferents(bool? asc, int? numPage, int? parPAge, List fonctions, long? idAgence, long? idBU, string texte, string tri); + Task> GetReferentsAsync(bool? asc, int? numPage, int? parPAge, List fonctions, long? idAgence, long? idBU, string texte, string tri); IEnumerable GetReferentsByCollaborateur(bool? asc, Guid? idCollaborateur, int? numPage, int? parPAge, string texte, string tri); + Task> GetReferentsByCollaborateurAsync(bool? asc, Guid? idCollaborateur, int? numPage, int? parPAge, string texte, string tri); } } diff --git a/Services/FormationService.cs b/Services/FormationService.cs index 775bf3d..f164303 100644 --- a/Services/FormationService.cs +++ b/Services/FormationService.cs @@ -1,4 +1,5 @@ using EPAServeur.Context; +using EPAServeur.Exceptions; using EPAServeur.IServices; using EPAServeur.Models.Formation; using IO.Swagger.DTO; @@ -6,6 +7,7 @@ using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; namespace EPAServeur.Services { @@ -35,18 +37,37 @@ namespace EPAServeur.Services /// /// Récupérer une formation par son id /// - /// + /// /// - public FormationDTO GetFormationById(long? id) + public FormationDTO GetFormationById(long? idFormation) { Formation formation = epContext.Formation.Include(formation => formation.Statut) .Include(formation => formation.ModeFormation) .Include(formation => formation.Origine) .Include(formation => formation.TypeFormation) - .FirstOrDefault(formation => formation.Id == id); + .FirstOrDefault(formation => formation.Id == idFormation); if (formation == null) - return null; + throw new FormationNotFoundException(); + + return GetFormationDTO(formation); + } + + /// + /// Récupérer une formation par son id de manière asynchrone + /// + /// + /// + public async Task GetFormationByIdAsync(long? idFormation) + { + Formation formation = await epContext.Formation.Include(formation => formation.Statut) + .Include(formation => formation.ModeFormation) + .Include(formation => formation.Origine) + .Include(formation => formation.TypeFormation) + .FirstOrDefaultAsync(formation => formation.Id == idFormation); + + if (formation == null) + throw new FormationNotFoundException(); return GetFormationDTO(formation); } @@ -61,7 +82,7 @@ namespace EPAServeur.Services /// Texte permettant d'identifier l'objet rechercher /// Colonne du tableau sur lequel le tri s'effectue /// - public IEnumerable GetFormations(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri) + public IEnumerable GetFormations(bool? asc, int? numPage, int? parPAge, int? idAgence, int? statutFormation, string texte, string tri) { IEnumerable formations; IEnumerable formationDTOs; @@ -74,39 +95,113 @@ 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) + 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 + { + formations = epContext.Formation.Include(formation => formation.Statut) .Include(formation => formation.ModeFormation) .Include(formation => formation.Origine) .Include(formation => formation.TypeFormation); - } - catch (Exception ex) - { - throw; - } + } + + + if (formations == null) + return null; + + formationDTOs = formations.Where(formation => formation.Intitule.ToLower().Contains(texte)).Select(formation => GetFormationDTO(formation)); + + return formationDTOs; + } + + /// + /// Récupérer la liste des formations de manière asynchrone + /// + /// Préciser si les données sont dans l'ordre (true) ou dans l'ordre inverse (false) + /// Numéro de la page du tableau qui affiche les données + /// Nombre d'éléments affiché sur chaque page du tableau + /// id de l'agence à laquelle sont rattachées les données à récupérer + /// Texte permettant d'identifier l'objet rechercher + /// Colonne du tableau sur lequel le tri s'effectue + /// + public async Task> GetFormationsAsync(bool? asc, int? numPage, int? parPAge, int? idAgence, int? statutFormation, string texte, string tri) + { + IEnumerable formations; + IEnumerable formationDTOs; + + if (texte == null) + texte = ""; + else + texte = texte.ToLower(); + + int skip = (numPage.Value - 1) * parPAge.Value; + int take = parPAge.Value; + + if (statutFormation != null && idAgence != null) + { + formations = await epContext.Formation + .Include(formation => formation.Statut) + .Include(formation => formation.ModeFormation) + .Include(formation => formation.Origine) + .Include(formation => formation.TypeFormation) + .Where(formation => formation.Statut.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 = epContext.Formation.Include(formation => formation.Statut) - .Include(formation => formation.ModeFormation) - .Include(formation => formation.Origine) - .Include(formation => formation.TypeFormation); - } - 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(); + } if (formations == null) - return new List(); + return null; formationDTOs = formations.Where(formation => formation.Intitule.ToLower().Contains(texte)).Select(formation => GetFormationDTO(formation)); @@ -150,7 +245,51 @@ namespace EPAServeur.Services .Include(formation => formation.TypeFormation); if (formations == null) - return new List(); + return null; + + formationDTOs = formations.Where(formation => formation.Intitule.ToLower().Contains(texte)).Select(formation => GetFormationDTO(formation)); + + return formationDTOs; + } + + /// + /// Récupérer les formations annulées de manière asynchrone + /// + /// Préciser si les données sont dans l'ordre (true) ou dans l'ordre inverse (false) + /// Numéro de la page du tableau qui affiche les données + /// Nombre d'éléments affiché sur chaque page du tableau + /// id de l'agence à laquelle sont rattachées les données à récupérer + /// Texte permettant d'identifier l'objet rechercher + /// Colonne du tableau sur lequel le tri s'effectue + /// + public async Task> GetFormationAnnuleesAsync(bool? asc, int? numPage, int? parPAge, 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 = await 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).ToListAsync(); + else + formations = await epContext.Formation.Where(formation => formation.Statut.Id == 4) + .Include(formation => formation.Statut) + .Include(formation => formation.ModeFormation) + .Include(formation => formation.Origine) + .Include(formation => formation.TypeFormation).ToListAsync(); + + if (formations == null) + return null; formationDTOs = formations.Where(formation => formation.Intitule.ToLower().Contains(texte)).Select(formation => GetFormationDTO(formation)); @@ -182,39 +321,80 @@ 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 + { + + 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 null; + + formationDTOs = formations.Where(formation => formation.Intitule.ToLower().Contains(texte)).Select(formation => GetFormationDTO(formation)); + + return formationDTOs; + } + + /// + /// Récupérer les formations réalisées de manière asynchrone + /// + /// Préciser si les données sont dans l'ordre (true) ou dans l'ordre inverse (false) + /// Numéro de la page du tableau qui affiche les données + /// Nombre d'éléments affiché sur chaque page du tableau + /// id de l'agence à laquelle sont rattachées les données à récupérer + /// Texte permettant d'identifier l'objet rechercher + /// Colonne du tableau sur lequel le tri s'effectue + /// + public async Task> GetFormationRealiseeAsync(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 = 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 = 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 = 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(); } if (formations == null) - return new List(); + return null; formationDTOs = formations.Where(formation => formation.Intitule.ToLower().Contains(texte)).Select(formation => GetFormationDTO(formation)); @@ -245,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)) @@ -268,7 +443,7 @@ namespace EPAServeur.Services } if (formations == null) - return new List(); + return null; formationDTOs = formations.Where(formation => formation.Intitule.ToLower().Contains(texte)).Select(formation => GetFormationDTO(formation)); @@ -276,22 +451,87 @@ namespace EPAServeur.Services } /// - /// Récupérer les modes de formation + /// Récupérer les formations plannifiées et replannifiées de manère asynchrone /// + /// Préciser si les données sont dans l'ordre (true) ou dans l'ordre inverse (false) + /// Numéro de la page du tableau qui affiche les données + /// Nombre d'éléments affiché sur chaque page du tableau + /// id de l'agence à laquelle sont rattachées les données à récupérer + /// Texte permettant d'identifier l'objet rechercher + /// Colonne du tableau sur lequel le tri s'effectue /// - public IEnumerable GetModesFormation() + public async Task> GetProchainesFormationAsync(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri) { - IEnumerable modeFormations; - IEnumerable modeFormationDTOs; - try + 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) { - modeFormations = epContext.ModeFormation; + 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) + + else { - throw; + formations = await 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).ToListAsync(); } + if (formations == null) + return null; + + formationDTOs = formations.Where(formation => formation.Intitule.ToLower().Contains(texte)).Select(formation => GetFormationDTO(formation)); + + return formationDTOs; + } + + /// + /// Récupérer les modes de formation + /// + /// + public IEnumerable GetModesFormation() + { + IEnumerable modeFormations; + IEnumerable modeFormationDTOs; + + modeFormations = epContext.ModeFormation; + + if (modeFormations == null) + return null; + + modeFormationDTOs = modeFormations.Select(modeFormation => GetModeFormationDTO(modeFormation)); + + return modeFormationDTOs; + } + + /// + /// Récupérer les modes de formation de manière asynchrone + /// + /// + public async Task> GetModesFormationAsync() + { + IEnumerable modeFormations; + IEnumerable modeFormationDTOs; + modeFormations = await epContext.ModeFormation.ToListAsync(); + + if (modeFormations == null) + return null; + modeFormationDTOs = modeFormations.Select(modeFormation => GetModeFormationDTO(modeFormation)); return modeFormationDTOs; @@ -306,14 +546,29 @@ namespace EPAServeur.Services IEnumerable origineFormations; IEnumerable origineFormationDTOs; - try - { - origineFormations = epContext.OrigineFormation; - } - catch (Exception ex) - { - throw; - } + origineFormations = epContext.OrigineFormation; + + if (origineFormations == null) + return null; + + origineFormationDTOs = origineFormations.Select(origineFormation => GetOrigineFormationDTO(origineFormation)); + + return origineFormationDTOs; + } + + /// + /// Récupérer les origines de formation de manière asynchrone + /// + /// + public async Task> GetOriginesFormationAsync() + { + IEnumerable origineFormations; + IEnumerable origineFormationDTOs; + + origineFormations = await epContext.OrigineFormation.ToListAsync(); + + if (origineFormations == null) + return null; origineFormationDTOs = origineFormations.Select(origineFormation => GetOrigineFormationDTO(origineFormation)); @@ -329,15 +584,29 @@ namespace EPAServeur.Services IEnumerable statutFormations; IEnumerable statutFormationDTOs; - try - { - statutFormations = epContext.StatutFormation; - } - catch (Exception ex) - { + statutFormations = epContext.StatutFormation; - throw; - } + if (statutFormations == null) + return null; + + statutFormationDTOs = statutFormations.Select(statutFormation => GetStatutFormationDTO(statutFormation)); + + return statutFormationDTOs; + } + + /// + /// Récupérer les statuts de formation de manière asynchrone + /// + /// + public async Task> GetStatutsFormationAsync() + { + IEnumerable statutFormations; + IEnumerable statutFormationDTOs; + + statutFormations = await epContext.StatutFormation.ToListAsync(); + + if (statutFormations == null) + return null; statutFormationDTOs = statutFormations.Select(statutFormation => GetStatutFormationDTO(statutFormation)); @@ -353,14 +622,29 @@ namespace EPAServeur.Services IEnumerable typeFormations; IEnumerable typeFormationDTOs; - try - { - typeFormations = epContext.TypeFormation; - } - catch (Exception ex) - { - throw; - } + typeFormations = epContext.TypeFormation; + + if (typeFormations == null) + return null; + + typeFormationDTOs = typeFormations.Select(typeFormation => GetTypeFormationDTO(typeFormation)); + + return typeFormationDTOs; + } + + /// + /// Récupérer les types de formation de manière asynchrone + /// + /// + public async Task> GetTypesFormationAsync() + { + IEnumerable typeFormations; + IEnumerable typeFormationDTOs; + + typeFormations = await epContext.TypeFormation.ToListAsync(); + + if (typeFormations == null) + return null; typeFormationDTOs = typeFormations.Select(typeFormation => GetTypeFormationDTO(typeFormation)); @@ -374,27 +658,47 @@ namespace EPAServeur.Services /// 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); + } + + /// + /// Ajouter une formation de manière asynchrone + /// + /// + /// + public async Task 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); + + await epContext.SaveChangesAsync(); return GetFormationDTO(formation); } @@ -402,26 +706,50 @@ namespace EPAServeur.Services /// /// Modifier une formation /// + /// /// /// - public FormationDTO UpdateFormation(FormationDTO formationDTO) + public FormationDTO UpdateFormation(long? idFormation, FormationDTO formationDTO) { - Formation formation = epContext.Formation.FirstOrDefault(formation => formation.Id == formationDTO.Id); + 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); + } + + /// + /// Modifier une formation de manière asynchrone + /// + /// + /// + /// + public async Task UpdateFormationAsync(long? idFormation, FormationDTO formationDTO) + { + if (!IsFormationValide(formationDTO)) + throw new FormationInvalidException(); + + if (formationDTO == null && !formationDTO.Id.HasValue && formationDTO.Id.Value != idFormation) + throw new FormationIncompatibleIdException(); + + Formation formation = await epContext.Formation.FindAsync(idFormation); + + if (formation == null) + return null; + + formation = SetFormation(formation, formationDTO); + await epContext.SaveChangesAsync(); return GetFormationDTO(formation); } @@ -429,32 +757,54 @@ namespace EPAServeur.Services /// /// Supprimer une formation /// - /// + /// /// - public bool DeleteFormationById(long? id) + public FormationDTO DeleteFormationById(long? idFormation) { - Formation formation = epContext.Formation.FirstOrDefault(formation => formation.Id == id); + 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); } + /// + /// Supprimer une formation de manière asynchrone + /// + /// + /// + public async Task DeleteFormationByIdAsync(long? idFormation) + { + Formation formation = await epContext.Formation.FindAsync(idFormation); + + if (formation == null) + throw new FormationNotFoundException(); + + epContext.Remove(formation); + + await epContext.SaveChangesAsync(); + + return GetFormationDTO(formation); + } #endregion #region Méthodes Privée + /// + /// Vérifier si un objet FormationDTO est valide pour ajout ou mise à jour + /// + /// Un objet FormationDTO est valide si aucune de ses propriétés n'est à null + /// + /// true si l'objet est valide, false sinon + private bool IsFormationValide(FormationDTO formation) + { + return !(formation == null || formation.IdAgence == null || formation.Intitule == null || formation.Organisme == null); + } #region Object to DTO /// diff --git a/Services/ReferentService.cs b/Services/ReferentService.cs index 68b9892..dcabe51 100644 --- a/Services/ReferentService.cs +++ b/Services/ReferentService.cs @@ -1,10 +1,12 @@ -using EPAServeur.IServices; +using EPAServeur.Exceptions; +using EPAServeur.IServices; using IO.Swagger.ApiCollaborateur; using IO.Swagger.DTO; using IO.Swagger.ModelCollaborateur; using System; using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; namespace EPAServeur.Services { @@ -12,13 +14,14 @@ namespace EPAServeur.Services { #region Variables private readonly IReferentApi referentApi; - + private readonly ICollaborateurApi collaborateurApi; #endregion #region Contructeurs - public ReferentService(IReferentApi _referentApi) + public ReferentService(IReferentApi _referentApi, ICollaborateurApi _collaborateurApi) { referentApi = _referentApi; + collaborateurApi = _collaborateurApi; } #endregion @@ -28,13 +31,29 @@ namespace EPAServeur.Services /// /// Récupère un référent par son id /// - /// + /// /// - public ReferentDTO GetReferentById(Guid? id) + public ReferentDTO GetReferentById(Guid? idReferent) { - Referent referent = referentApi.ChercherRefId(id); + Referent referent = referentApi.ChercherRefId(idReferent); + if (referent == null) - return null; + throw new ReferentNotFoundException(); + + return GetReferentDTO(referent); + } + + /// + /// Récupère un référent par son id de manière asynchrone + /// + /// + /// + public async Task GetReferentByIdAsync(Guid? idReferent) + { + Referent referent = await referentApi.ChercherRefIdAsync(idReferent); + + if (referent == null) + throw new ReferentNotFoundException(); return GetReferentDTO(referent); } @@ -49,27 +68,181 @@ namespace EPAServeur.Services Referent referent = referentApi.ChercherRefActuelCollabId(idCollaborateur); if (referent == null) - return null; + throw new ReferentNotFoundException(); return GetReferentDTO(referent); } - /// - /// Récupère la liste des référents pour une agence - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// + /// + /// Récupère un référent en fonction d'un collaborateur de manière asynchrone + /// + /// + /// + public async Task GetReferentActuelCollaborateurAsync(Guid? idCollaborateur) + { + Referent referent = await referentApi.ChercherRefActuelCollabIdAsync(idCollaborateur); + + if (referent == null) + throw new ReferentNotFoundException(); + + return GetReferentDTO(referent); + } + + /// + /// Récupère la liste des référents pour une agence + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// public IEnumerable GetReferents(bool? asc, int? numPage, int? parPAge, List fonctions, long? idAgence, long? idBU, string texte, string tri) { - IEnumerable referents = null ; // A changer - IEnumerable referentDTOs = null; // A changer + throw new NotImplementedException(); + + ////IEnumerable collaborateurs; // A changer (Sera utilisé pour récupérer la liste des référents par fonction + //IEnumerable referents = null; // A changer + //IEnumerable referentDTOs = null; // A changer + ////List ids = fonctions.Select(guid => (Guid?)Guid.Parse(guid)).ToList(); + + ////collaborateurs = collaborateurApi.ChercherCollab(ids); + //if (texte == null) + // texte = ""; + //else + // texte = texte.ToLower(); + + //int skip = (numPage.Value - 1) * parPAge.Value; + //int take = parPAge.Value; + + //if (idBU != null) + //{ + + //} + //else + //{ + + //} + + //if (idAgence != null) + //{ + // try + // { + + // } + // catch (Exception ex) + // { + // throw; + // } + //} + //else + //{ + // try + // { + + // } + // catch (Exception ex) + // { + // throw; + // } + //} + + + //if (referents == null) + // return new List(); + + //referentDTOs = referents.Where(referent => (referent.Nom + " " + referent.Prenom).ToLower().Contains(texte) || (referent.Prenom + " " + referent.Nom).ToLower().Contains(texte)).Select(referent => GetReferentDTO(referent)); + + //return referentDTOs; + } + + /// + /// Récupère la liste des référents pour une agence de manière asynchrone + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + + public async Task> GetReferentsAsync(bool? asc, int? numPage, int? parPAge, List fonctions, long? idAgence, long? idBU, string texte, string tri) + { + throw new NotImplementedException(); + + //IEnumerable referents = null ; // A changer + //IEnumerable referentDTOs = null; // A changer + + //if (texte == null) + // texte = ""; + //else + // texte = texte.ToLower(); + + //int skip = (numPage.Value - 1) * parPAge.Value; + //int take = parPAge.Value; + + //if (idBU != null) + //{ + + //} + //else + //{ + + //} + + //if (idAgence != null) + //{ + // try + // { + + // } + // catch (Exception ex) + // { + // throw; + // } + //} + //else + //{ + // try + // { + + // } + // catch (Exception ex) + // { + // throw; + // } + //} + + + //if (referents == null) + // return new List(); + + //referentDTOs = referents.Where(referent => (referent.Nom + " " + referent.Prenom).ToLower().Contains(texte) || (referent.Prenom + " " + referent.Nom).ToLower().Contains(texte)).Select(referent => GetReferentDTO(referent)); + + //return referentDTOs; + } + + + /// + /// Récupère la liste des référents pour un collaborateur + /// + /// + /// + /// + /// + /// + /// + /// + public IEnumerable GetReferentsByCollaborateur(bool? asc, Guid? idCollaborateur, int? numPage, int? parPAge, string texte, string tri) + { + IEnumerable referents; + IEnumerable referentDTOs; if (texte == null) texte = ""; @@ -79,38 +252,15 @@ namespace EPAServeur.Services int skip = (numPage.Value - 1) * parPAge.Value; int take = parPAge.Value; - if (idBU != null) - { - - } - else - { + if (idCollaborateur == null) + return new List(); - } + Collaborateur collaborateur = collaborateurApi.ChercherCollabId(idCollaborateur); - if (idAgence != null) - { - try - { - - } - catch (Exception ex) - { - throw; - } - } - else - { - try - { - - } - catch (Exception ex) - { - throw; - } - } + if (collaborateur == null) + throw new CollaborateurNotFoundException(); + referents = referentApi.ChercherRefCollabId(idCollaborateur); if (referents == null) return new List(); @@ -121,7 +271,7 @@ namespace EPAServeur.Services } /// - /// Récupère la liste des référents pour un collaborateur + /// Récupère la liste des référents pour un collaborateur de manère asynchrone /// /// /// @@ -130,7 +280,7 @@ namespace EPAServeur.Services /// /// /// - public IEnumerable GetReferentsByCollaborateur(bool? asc, Guid? idCollaborateur, int? numPage, int? parPAge, string texte, string tri) + public async Task> GetReferentsByCollaborateurAsync(bool? asc, Guid? idCollaborateur, int? numPage, int? parPAge, string texte, string tri) { IEnumerable referents; IEnumerable referentDTOs; @@ -146,12 +296,21 @@ namespace EPAServeur.Services if (idCollaborateur == null) return new List(); + Collaborateur collaborateur = await collaborateurApi.ChercherCollabIdAsync(idCollaborateur); + + if (collaborateur == null) + throw new CollaborateurNotFoundException(); + + referents = await referentApi.ChercherRefCollabIdAsync(idCollaborateur); + + if (referents == null) + return new List(); - referents = referentApi.ChercherRefCollabId(idCollaborateur); referentDTOs = referents.Where(referent => (referent.Nom + " " + referent.Prenom).ToLower().Contains(texte) || (referent.Prenom + " " + referent.Nom).ToLower().Contains(texte)).Select(referent => GetReferentDTO(referent)); return referentDTOs; } + #endregion #region Méthodes Privées @@ -238,6 +397,25 @@ namespace EPAServeur.Services }; return referentDTO; } + + /// + /// Récupère un objet ReferentDTO en fonction d'un objet Collaborateur + /// + /// + /// + private ReferentDTO GetReferentDTO(Collaborateur collaborateur) + { + if (collaborateur == null) + return null; + ReferentDTO referentDTO = new ReferentDTO() + { + Id = collaborateur.Id, + Prenom = collaborateur.Prenom, + Nom = collaborateur.Nom, + MailApside = collaborateur.MailApside + }; + return referentDTO; + } #endregion #region DTO to Object