From 3146d7a9c59d3ea14892a82554996cb5dd50030f Mon Sep 17 00:00:00 2001 From: jboinembalome Date: Thu, 25 Feb 2021 09:10:10 +0100 Subject: [PATCH] =?UTF-8?q?Int=C3=A9gration=20du=20service=20Participation?= =?UTF-8?q?FormationService=20dans=20ParticipationFormationAPI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ParticipationsFormationsApi.cs | 262 ++++++++++++------ 1 file changed, 181 insertions(+), 81 deletions(-) diff --git a/EPAServeur/Controllers/ParticipationsFormationsApi.cs b/EPAServeur/Controllers/ParticipationsFormationsApi.cs index 446cf7e..b581060 100644 --- a/EPAServeur/Controllers/ParticipationsFormationsApi.cs +++ b/EPAServeur/Controllers/ParticipationsFormationsApi.cs @@ -18,6 +18,14 @@ using IO.Swagger.Attributes; using IO.Swagger.Security; using Microsoft.AspNetCore.Authorization; using IO.Swagger.DTO; +using EPAServeur.IServices; +using Microsoft.Extensions.Logging; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Hosting; +using System.Threading.Tasks; +using EPAServeur.Exceptions; +using Microsoft.AspNetCore.Http; +using Microsoft.EntityFrameworkCore; namespace IO.Swagger.Controllers { @@ -26,7 +34,18 @@ namespace IO.Swagger.Controllers /// [ApiController] public class ParticipationsFormationsApiController : ControllerBase - { + { + private readonly IParticipationFormationService participationFormationService; + private readonly ILogger logger; + private readonly IWebHostEnvironment env; + + public ParticipationsFormationsApiController(IParticipationFormationService _participationFormationService, ILogger _logger, IWebHostEnvironment _env) + { + participationFormationService = _participationFormationService; + logger = _logger; + env = _env; + } + /// /// /// @@ -41,7 +60,7 @@ namespace IO.Swagger.Controllers /// Une erreur est survenue sur le serveur [HttpPut] [Route("/api/participationsformation/{idParticipationFormation}/evaluation")] - [Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] + //[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] [ValidateModelState] [SwaggerOperation("EvaluerFormation")] [SwaggerResponse(statusCode: 200, type: typeof(EvaluationDTO), description: "Evaluation envoyée avec succès")] @@ -50,32 +69,95 @@ namespace IO.Swagger.Controllers [SwaggerResponse(statusCode: 404, type: typeof(ErreurDTO), description: "La ressource n'a pas été trouvée")] [SwaggerResponse(statusCode: 415, type: typeof(ErreurDTO), description: "L’opération ne peut pas être effectuée car certaines données sont manquantes")] [SwaggerResponse(statusCode: 500, type: typeof(ErreurDTO), description: "Une erreur est survenue sur le serveur")] - public virtual IActionResult EvaluerFormation([FromBody]EvaluationDTO body, [FromRoute][Required]long? idParticipationFormation) - { - //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(EvaluationDTO)); - - //TODO: Uncomment the next line to return response 401 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - // return StatusCode(401, default(ErreurDTO)); - - //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)); - - //TODO: Uncomment the next line to return response 404 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - // return StatusCode(404, default(ErreurDTO)); - - //TODO: Uncomment the next line to return response 415 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - // return StatusCode(415, default(ErreurDTO)); - - //TODO: Uncomment the next line to return response 500 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - // return StatusCode(500, default(ErreurDTO)); - string exampleJson = null; - exampleJson = "{\n \"dateDebut\" : \"2000-01-23T04:56:07.000+00:00\",\n \"id\" : 0,\n \"estCertifiee\" : true,\n \"intitule\" : \"intitule\",\n \"saisies\" : [ {\n \"note\" : 6,\n \"texte\" : \"texte\",\n \"id\" : \"id\",\n \"champ\" : {\n \"ordre\" : 6,\n \"texte\" : \"texte\",\n \"section\" : \"section\",\n \"soussection\" : \"soussection\",\n \"id\" : 3,\n \"typeSaisie\" : \"Commentaire\"\n }\n }, {\n \"note\" : 6,\n \"texte\" : \"texte\",\n \"id\" : \"id\",\n \"champ\" : {\n \"ordre\" : 6,\n \"texte\" : \"texte\",\n \"section\" : \"section\",\n \"soussection\" : \"soussection\",\n \"id\" : 3,\n \"typeSaisie\" : \"Commentaire\"\n }\n } ]\n}"; - - var example = exampleJson != null - ? JsonConvert.DeserializeObject(exampleJson) - : default(EvaluationDTO); //TODO: Change the data returned - return new ObjectResult(example); + public virtual async Task EvaluerFormation([FromBody]EvaluationDTO body, [FromRoute][Required]long idParticipationFormation) + { + if (env.IsDevelopment()) + logger.LogInformation("Mise à jour de la participation à la formation d'id {idParticipationFormation}.", idParticipationFormation); + + try + { + body = await participationFormationService.EvaluerFormationAsync(idParticipationFormation, body); + } + catch (ParticipationFormationIncompatibleIdException e) + { + if (env.IsDevelopment()) + logger.LogInformation(e.Message); + + ErreurDTO erreur = new ErreurDTO() + { + Code = StatusCodes.Status415UnsupportedMediaType, + Message = e.Message, + }; + + return StatusCode(erreur.Code.Value, erreur.Message); + } + catch (ParticipationFormationInvalidException e) + { + if (env.IsDevelopment()) + logger.LogInformation(e.Message); + + ErreurDTO erreur = new ErreurDTO() + { + Code = StatusCodes.Status415UnsupportedMediaType, + Message = e.Message, + }; + + return StatusCode(erreur.Code.Value, erreur.Message); + } + catch (ParticipationFormationNotFoundException e) + { + if (env.IsDevelopment()) + logger.LogInformation(e.Message); + + ErreurDTO erreur = new ErreurDTO() + { + Code = StatusCodes.Status404NotFound, + Message = e.Message + }; + + return NotFound(erreur); + } + catch (DbUpdateConcurrencyException e) + { + logger.LogError(e.Message); + + ErreurDTO erreur = new ErreurDTO() + { + Code = StatusCodes.Status500InternalServerError, + Message = string.Format("La participation à la formation {0} n'a pas pu être mise à jour car elle est prise par une autre ressource.", idParticipationFormation) + }; + + return StatusCode(erreur.Code.Value, erreur); + } + catch (DbUpdateException e) + { + logger.LogError(e.Message); + + ErreurDTO erreur = new ErreurDTO() + { + Code = StatusCodes.Status500InternalServerError, + Message = "Une erreur est survenue sur le serveur lors de la mise à jour de la participation à la formation." + }; + + return StatusCode(erreur.Code.Value, erreur); + } + catch (Exception e) + { + logger.LogError(e.Message); + + ErreurDTO erreur = new ErreurDTO() + { + Code = StatusCodes.Status500InternalServerError, + Message = "Une erreur inconnue est survenue sur le serveur." + }; + + return StatusCode(erreur.Code.Value, erreur); + } + + if (env.IsDevelopment()) + logger.LogInformation("Update effectué avec succès"); + + return Ok(body); } /// @@ -90,7 +172,7 @@ namespace IO.Swagger.Controllers /// Une erreur est survenue sur le serveur [HttpGet] [Route("/api/participationsformation/{idParticipationFormation}/evaluation")] - [Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] + //[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] [ValidateModelState] [SwaggerOperation("GetEvaluationCollaborateur")] [SwaggerResponse(statusCode: 200, type: typeof(EvaluationDTO), description: "OK")] @@ -98,29 +180,47 @@ namespace IO.Swagger.Controllers [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "L’utilisateur souhaitant accéder à la ressource n’a pas les droits d’accès suffisants")] [SwaggerResponse(statusCode: 404, type: typeof(ErreurDTO), description: "La ressource n'a pas été trouvée")] [SwaggerResponse(statusCode: 500, type: typeof(ErreurDTO), description: "Une erreur est survenue sur le serveur")] - public virtual IActionResult GetEvaluationCollaborateur([FromRoute][Required]long? idParticipationFormation) - { - //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(EvaluationDTO)); - - //TODO: Uncomment the next line to return response 401 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - // return StatusCode(401, default(ErreurDTO)); - - //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)); - - //TODO: Uncomment the next line to return response 404 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - // return StatusCode(404, default(ErreurDTO)); - - //TODO: Uncomment the next line to return response 500 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - // return StatusCode(500, default(ErreurDTO)); - string exampleJson = null; - exampleJson = "{\n \"dateDebut\" : \"2000-01-23T04:56:07.000+00:00\",\n \"id\" : 0,\n \"estCertifiee\" : true,\n \"intitule\" : \"intitule\",\n \"saisies\" : [ {\n \"note\" : 6,\n \"texte\" : \"texte\",\n \"id\" : \"id\",\n \"champ\" : {\n \"ordre\" : 6,\n \"texte\" : \"texte\",\n \"section\" : \"section\",\n \"soussection\" : \"soussection\",\n \"id\" : 3,\n \"typeSaisie\" : \"Commentaire\"\n }\n }, {\n \"note\" : 6,\n \"texte\" : \"texte\",\n \"id\" : \"id\",\n \"champ\" : {\n \"ordre\" : 6,\n \"texte\" : \"texte\",\n \"section\" : \"section\",\n \"soussection\" : \"soussection\",\n \"id\" : 3,\n \"typeSaisie\" : \"Commentaire\"\n }\n } ]\n}"; - - var example = exampleJson != null - ? JsonConvert.DeserializeObject(exampleJson) - : default(EvaluationDTO); //TODO: Change the data returned - return new ObjectResult(example); + public virtual async Task GetEvaluationCollaborateur([FromRoute][Required]long idParticipationFormation) + { + if (env.IsDevelopment()) + logger.LogInformation("Récupération de la participation à la formation {idParticipationFormation}.", idParticipationFormation); + + EvaluationDTO evaluationDTO; + + try + { + evaluationDTO = await participationFormationService.GetEvaluationCollaborateurAsync(idParticipationFormation); + } + catch (ParticipationFormationNotFoundException e) + { + if (env.IsDevelopment()) + logger.LogInformation(e.Message); + + ErreurDTO erreurDTO = new ErreurDTO() + { + Code = StatusCodes.Status404NotFound, + Message = e.Message + }; + + return NotFound(erreurDTO); + } + catch (Exception e) + { + logger.LogError(e.Message); + + ErreurDTO erreur = new ErreurDTO() + { + Code = StatusCodes.Status500InternalServerError, + Message = "Une erreur inconnue est survenue sur le serveur." + }; + + return StatusCode(erreur.Code.Value, erreur); + } + + if (env.IsDevelopment()) + logger.LogInformation("Participation à la formation {idParticipationFormation} récupérée.", idParticipationFormation); + + return Ok(evaluationDTO); } /// @@ -128,11 +228,6 @@ namespace IO.Swagger.Controllers /// /// Récupérer la liste des participations de formation d’un collaborateur. /// Id du collaborateur - /// Indique si les données sont récupérées dans l'ordre croissant ou non - /// Numéro de la page du tableau à afficher - /// Nombre d’élément maximum à afficher dans le tableau - /// Texte permettant de filtrer les données - /// Colonne du tableau sur lequel le tri devra être effectué /// OK /// L'utilisateur souhaitant accéder à la ressource n'est pas authentifié /// L’utilisateur souhaitant accéder à la ressource n’a pas les droits d’accès suffisants @@ -140,7 +235,7 @@ namespace IO.Swagger.Controllers /// Une erreur est survenue sur le serveur [HttpGet] [Route("/api/participationsformation/{idCollaborateur}")] - [Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] + //[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] [ValidateModelState] [SwaggerOperation("GetParticipationByCollaborateur")] [SwaggerResponse(statusCode: 200, type: typeof(List), description: "OK")] @@ -148,29 +243,34 @@ namespace IO.Swagger.Controllers [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "L’utilisateur souhaitant accéder à la ressource n’a pas les droits d’accès suffisants")] [SwaggerResponse(statusCode: 404, type: typeof(ErreurDTO), description: "La ressource n'a pas été trouvée")] [SwaggerResponse(statusCode: 500, type: typeof(ErreurDTO), description: "Une erreur est survenue sur le serveur")] - public virtual IActionResult GetParticipationByCollaborateur([FromRoute][Required]Guid? idCollaborateur, [FromQuery]bool? asc, [FromQuery]int? numPage, [FromQuery][Range(5, 100)]int? parPAge, [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 401 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - // return StatusCode(401, default(ErreurDTO)); - - //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)); - - //TODO: Uncomment the next line to return response 404 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - // return StatusCode(404, default(ErreurDTO)); - - //TODO: Uncomment the next line to return response 500 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - // return StatusCode(500, default(ErreurDTO)); - string exampleJson = null; - exampleJson = "[ {\n \"estEvaluee\" : true,\n \"dateCreation\" : \"2000-01-23T04:56:07.000+00:00\",\n \"dateDebut\" : \"2000-01-23T04:56:07.000+00:00\",\n \"id\" : 7,\n \"intitule\" : \"intitule\"\n}, {\n \"estEvaluee\" : true,\n \"dateCreation\" : \"2000-01-23T04:56:07.000+00:00\",\n \"dateDebut\" : \"2000-01-23T04:56:07.000+00:00\",\n \"id\" : 7,\n \"intitule\" : \"intitule\"\n} ]"; - - var example = exampleJson != null - ? JsonConvert.DeserializeObject>(exampleJson) - : default(List); //TODO: Change the data returned - return new ObjectResult(example); + public virtual async Task GetParticipationByCollaborateur([FromRoute][Required]Guid idCollaborateur) + { + if (env.IsDevelopment()) + logger.LogInformation("Récupération de la liste des particicaptions aux formations d'un collaborateur."); + + IEnumerable participationFormationDTOs; + + try + { + participationFormationDTOs = await participationFormationService.GetParticipationByCollaborateurAsync(idCollaborateur); + } + catch (Exception e) + { + logger.LogError(e.Message); + + ErreurDTO erreur = new ErreurDTO() + { + Code = StatusCodes.Status500InternalServerError, + Message = "Une erreur inconnue est survenue sur le serveur." + }; + + return StatusCode(erreur.Code.Value, erreur); + } + + if (env.IsDevelopment()) + logger.LogInformation("Liste des particicaptions aux formations d'un collaborateur récupérée."); + + return Ok(participationFormationDTOs); } } }