diff --git a/EPAServeur/Controllers/FormationsApi.cs b/EPAServeur/Controllers/FormationsApi.cs index 38166bb..f2ed436 100644 --- a/EPAServeur/Controllers/FormationsApi.cs +++ b/EPAServeur/Controllers/FormationsApi.cs @@ -18,15 +18,35 @@ using IO.Swagger.Attributes; using IO.Swagger.Security; using Microsoft.AspNetCore.Authorization; using IO.Swagger.DTO; +using System.ComponentModel; +using EPAServeur.IServices; +using Microsoft.Extensions.Logging; +using System.Threading.Tasks; +using EPAServeur.Exceptions; +using Microsoft.EntityFrameworkCore; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Hosting; +using Microsoft.AspNetCore.Http; namespace IO.Swagger.Controllers -{ +{ /// /// /// [ApiController] public class FormationsApiController : ControllerBase - { + { + private readonly IFormationService formationService; + private readonly ILogger logger; + private readonly IWebHostEnvironment env; + + public FormationsApiController(IFormationService _formationService, ILogger _logger, IWebHostEnvironment _env) + { + formationService = _formationService; + logger = _logger; + env = _env; + } + /// /// /// @@ -47,29 +67,57 @@ 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: 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 AddFormation([FromBody]FormationDTO body) - { - //TODO: Uncomment the next line to return response 201 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - // return StatusCode(201, default(FormationDTO)); - - //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 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 \"heure\" : 1,\n \"participations\" : [ {\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 } ],\n \"organisme\" : \"organisme\",\n \"origine\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 2\n },\n \"estCertifiee\" : true,\n \"type\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 6\n },\n \"intitule\" : \"intitule\",\n \"mode\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 1\n },\n \"jour\" : 1,\n \"dateDebut\" : \"2000-01-23T04:56:07.000+00:00\",\n \"estRealisee\" : true,\n \"id\" : 3,\n \"dateFin\" : \"2000-01-23T04:56:07.000+00:00\",\n \"idAgence\" : 7,\n \"statut\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 4\n }\n}"; - - var example = exampleJson != null - ? JsonConvert.DeserializeObject(exampleJson) - : default(FormationDTO); //TODO: Change the data returned - return new ObjectResult(example); + public virtual async Task AddFormation([FromBody] FormationDTO body) + { + if (env.IsDevelopment()) + logger.LogInformation("Ajout d'une nouvelle formation."); + + try + { + body = await formationService.AddFormationAsync(body); + } + catch (FormationInvalidException e) + { + if (env.IsDevelopment()) + logger.LogInformation(e.Message); + + ErreurDTO erreur = new ErreurDTO() + { + Code = StatusCodes.Status500InternalServerError, + Message = e.Message, + }; + + return StatusCode(erreur.Code.Value, erreur.Message); + } + catch (DbUpdateException e) + { + logger.LogError(e.Message); + + ErreurDTO erreur = new ErreurDTO + { + Code = StatusCodes.Status500InternalServerError, + Message = "Une erreur est survenue sur le serveur lors de l'ajout de 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("Nouvelle formation ajoutée."); + + return Created("", body); } /// @@ -91,24 +139,69 @@ 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 DeleteFormation([FromRoute][Required]long? idFormation) - { - //TODO: Uncomment the next line to return response 204 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - // return StatusCode(204); - - //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)); - - throw new NotImplementedException(); + public virtual async Task DeleteFormation([FromRoute][Required] long idFormation) + { + try + { + if (env.IsDevelopment()) + logger.LogInformation("Suppresion de la formation {idFormation}.", idFormation); + + FormationDTO formation = await formationService.DeleteFormationByIdAsync(idFormation); + } + catch (FormationNotFoundException 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 formation {0} n'a pas pu être supprimée car elle est prise par une autre ressource.", idFormation) + }; + + 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 suppression de 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("Formation {idFormation} supprimée avec succès.", idFormation); + + return NoContent(); } /// @@ -131,29 +224,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 GetFormationById([FromRoute][Required]long? idFormation) - { - //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(FormationDTO)); - - //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 \"heure\" : 1,\n \"participations\" : [ {\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 } ],\n \"organisme\" : \"organisme\",\n \"origine\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 2\n },\n \"estCertifiee\" : true,\n \"type\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 6\n },\n \"intitule\" : \"intitule\",\n \"mode\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 1\n },\n \"jour\" : 1,\n \"dateDebut\" : \"2000-01-23T04:56:07.000+00:00\",\n \"estRealisee\" : true,\n \"id\" : 3,\n \"dateFin\" : \"2000-01-23T04:56:07.000+00:00\",\n \"idAgence\" : 7,\n \"statut\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 4\n }\n}"; - - var example = exampleJson != null - ? JsonConvert.DeserializeObject(exampleJson) - : default(FormationDTO); //TODO: Change the data returned - return new ObjectResult(example); + public virtual async Task GetFormationById([FromRoute][Required] long idFormation) + { + if (env.IsDevelopment()) + logger.LogInformation("Récupération de la formation {idFormation}.", idFormation); + + FormationDTO formationDTO = null; + + try + { + formationDTO = await formationService.GetFormationByIdAsync(idFormation); + } + catch (FormationNotFoundException 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("Formation {idFormation} récupérée.", idFormation); + + return Ok(formationDTO); } /// @@ -182,26 +293,34 @@ namespace IO.Swagger.Controllers [SwaggerResponse(statusCode: 401, type: typeof(ErreurDTO), description: "L'utilisateur souhaitant accéder à la ressource n'est pas authentifié")] [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: 500, type: typeof(ErreurDTO), description: "Une erreur est survenue sur le serveur")] - public virtual IActionResult GetFormations([FromQuery]long? idAgence, [FromQuery]List idStatuts, [FromQuery]bool? asc, [FromQuery]int? numPage, [FromQuery][Range(5, 100)]int? parPAge, [FromQuery]string texte, [FromQuery]string tri, [FromQuery]DateTime? dateDebut, [FromQuery]DateTime? dateFin) - { - //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 500 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - // return StatusCode(500, default(ErreurDTO)); - string exampleJson = null; - exampleJson = "[ {\n \"nbParticipations\" : 6,\n \"dateDebut\" : \"2000-01-23T04:56:07.000+00:00\",\n \"organisme\" : \"organisme\",\n \"id\" : 0,\n \"dateFin\" : \"2000-01-23T04:56:07.000+00:00\",\n \"origine\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 2\n },\n \"estCertifiee\" : true,\n \"intitule\" : \"intitule\",\n \"statut\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 4\n }\n}, {\n \"nbParticipations\" : 6,\n \"dateDebut\" : \"2000-01-23T04:56:07.000+00:00\",\n \"organisme\" : \"organisme\",\n \"id\" : 0,\n \"dateFin\" : \"2000-01-23T04:56:07.000+00:00\",\n \"origine\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 2\n },\n \"estCertifiee\" : true,\n \"intitule\" : \"intitule\",\n \"statut\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 4\n }\n} ]"; - - var example = exampleJson != null - ? JsonConvert.DeserializeObject>(exampleJson) - : default(List); //TODO: Change the data returned - return new ObjectResult(example); + public virtual async Task GetFormations([FromQuery] long? idAgence, [FromQuery] List idStatuts, [FromQuery] bool? asc, [FromQuery] int? numPage, [FromQuery][Range(5, 100)][DefaultValue(15)] int? parPAge, [FromQuery] string texte, [FromQuery] string tri, [FromQuery] DateTime? dateDebut, [FromQuery] DateTime? dateFin) + { + if (env.IsDevelopment()) + logger.LogInformation("Récupération de la liste des formations."); + + IEnumerable formations = null; + + try + { + formations = await formationService.GetFormationsAsync(idAgence, idStatuts, asc, numPage, parPAge, texte, tri, dateDebut, dateFin); + } + 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 formations récupérée."); + + return Ok(formations); } /// @@ -223,33 +342,42 @@ namespace IO.Swagger.Controllers /// Une erreur est survenue sur le serveur [HttpGet] [Route("/api/formations/count")] - [Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] + //[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] [ValidateModelState] [SwaggerOperation("GetFormationsCount")] [SwaggerResponse(statusCode: 200, type: typeof(long?), description: "OK")] [SwaggerResponse(statusCode: 401, type: typeof(ErreurDTO), description: "L'utilisateur souhaitant accéder à la ressource n'est pas authentifié")] [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: 500, type: typeof(ErreurDTO), description: "Une erreur est survenue sur le serveur")] - public virtual IActionResult GetFormationsCount([FromQuery]long? idAgence, [FromQuery]List idStatuts, [FromQuery]bool? asc, [FromQuery]int? numPage, [FromQuery][Range(5, 100)]int? parPAge, [FromQuery]string texte, [FromQuery]string tri, [FromQuery]DateTime? dateDebut, [FromQuery]DateTime? dateFin) - { - //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(long?)); - - //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 500 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - // return StatusCode(500, default(ErreurDTO)); - string exampleJson = null; - exampleJson = "0"; - - var example = exampleJson != null - ? JsonConvert.DeserializeObject(exampleJson) - : default(long?); //TODO: Change the data returned - return new ObjectResult(example); + public virtual async Task GetFormationsCount([FromQuery] long? idAgence, [FromQuery] List idStatuts, [FromQuery] int? numPage, [FromQuery][Range(5, 100)][DefaultValue(15)] int? parPAge, [FromQuery] string texte, [FromQuery] DateTime? dateDebut, [FromQuery] DateTime? dateFin) + { + if (env.IsDevelopment()) + logger.LogInformation("Récupération du nombre total de formations."); + + long count = 0; + + try + { + count = await formationService.GetFormationsCountAsync(idAgence, idStatuts, numPage, parPAge, texte, dateDebut, dateFin); + } + 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("Nombre total de formations récupéré."); + + return Ok(count); + } /// @@ -269,26 +397,34 @@ namespace IO.Swagger.Controllers [SwaggerResponse(statusCode: 401, type: typeof(ErreurDTO), description: "L'utilisateur souhaitant accéder à la ressource n'est pas authentifié")] [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: 500, type: typeof(ErreurDTO), description: "Une erreur est survenue sur le serveur")] - public virtual IActionResult GetModesFormation() - { - //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 500 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - // return StatusCode(500, default(ErreurDTO)); - string exampleJson = null; - exampleJson = "[ {\n \"libelle\" : \"libelle\",\n \"id\" : 1\n}, {\n \"libelle\" : \"libelle\",\n \"id\" : 1\n} ]"; - - var example = exampleJson != null - ? JsonConvert.DeserializeObject>(exampleJson) - : default(List); //TODO: Change the data returned - return new ObjectResult(example); + public virtual async Task GetModesFormation() + { + if (env.IsDevelopment()) + logger.LogInformation("Récupération de la liste des modes de formation."); + + IEnumerable modeFormations = null; + + try + { + modeFormations = await formationService.GetModesFormationAsync(); + } + 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 modes de formation récupérée."); + + return Ok(modeFormations); } /// @@ -308,26 +444,34 @@ namespace IO.Swagger.Controllers [SwaggerResponse(statusCode: 401, type: typeof(ErreurDTO), description: "L'utilisateur souhaitant accéder à la ressource n'est pas authentifié")] [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: 500, type: typeof(ErreurDTO), description: "Une erreur est survenue sur le serveur")] - public virtual IActionResult GetOriginesFormation() - { - //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 500 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - // return StatusCode(500, default(ErreurDTO)); - string exampleJson = null; - exampleJson = "[ {\n \"libelle\" : \"libelle\",\n \"id\" : 2\n}, {\n \"libelle\" : \"libelle\",\n \"id\" : 2\n} ]"; - - var example = exampleJson != null - ? JsonConvert.DeserializeObject>(exampleJson) - : default(List); //TODO: Change the data returned - return new ObjectResult(example); + public virtual async Task GetOriginesFormation() + { + if (env.IsDevelopment()) + logger.LogInformation("Récupération de la liste des origines de formation."); + + IEnumerable origineFormations = null; + + try + { + origineFormations = await formationService.GetOriginesFormationAsync(); + } + 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 origines de formation récupérée."); + + return Ok(origineFormations); } /// @@ -347,26 +491,34 @@ namespace IO.Swagger.Controllers [SwaggerResponse(statusCode: 401, type: typeof(ErreurDTO), description: "L'utilisateur souhaitant accéder à la ressource n'est pas authentifié")] [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: 500, type: typeof(ErreurDTO), description: "Une erreur est survenue sur le serveur")] - public virtual IActionResult GetStatutsFormation() - { - //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 500 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - // return StatusCode(500, default(ErreurDTO)); - string exampleJson = null; - exampleJson = "[ {\n \"libelle\" : \"libelle\",\n \"id\" : 4\n}, {\n \"libelle\" : \"libelle\",\n \"id\" : 4\n} ]"; - - var example = exampleJson != null - ? JsonConvert.DeserializeObject>(exampleJson) - : default(List); //TODO: Change the data returned - return new ObjectResult(example); + public virtual async Task GetStatutsFormation() + { + if (env.IsDevelopment()) + logger.LogInformation("Récupération de la liste des statuts de formation."); + + IEnumerable statutFormations = null; + + try + { + statutFormations = await formationService.GetStatutsFormationAsync(); + } + 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 statuts de formation récupérée."); + + return Ok(statutFormations); } /// @@ -386,26 +538,34 @@ namespace IO.Swagger.Controllers [SwaggerResponse(statusCode: 401, type: typeof(ErreurDTO), description: "L'utilisateur souhaitant accéder à la ressource n'est pas authentifié")] [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: 500, type: typeof(ErreurDTO), description: "Une erreur est survenue sur le serveur")] - public virtual IActionResult GetTypesFormation() - { - //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 500 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - // return StatusCode(500, default(ErreurDTO)); - string exampleJson = null; - exampleJson = "[ {\n \"libelle\" : \"libelle\",\n \"id\" : 6\n}, {\n \"libelle\" : \"libelle\",\n \"id\" : 6\n} ]"; - - var example = exampleJson != null - ? JsonConvert.DeserializeObject>(exampleJson) - : default(List); //TODO: Change the data returned - return new ObjectResult(example); + public virtual async Task GetTypesFormation() + { + if (env.IsDevelopment()) + logger.LogInformation("Récupération de la liste des types de formation."); + + IEnumerable typeFormations = null; + + try + { + typeFormations = await formationService.GetTypesFormationAsync(); + } + 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 types de formation récupérée."); + + return Ok(typeFormations); } /// @@ -430,27 +590,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 UpdateFormation([FromBody]FormationDTO body, [FromRoute][Required]long? idFormation) - { - //TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - // return StatusCode(200); - - //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)); - - throw new NotImplementedException(); + public virtual async Task UpdateFormation([FromBody] FormationDTO body, [FromRoute][Required] long idFormation) + { + if (env.IsDevelopment()) + logger.LogInformation("Mise à jour de la formation d'id {idFormation}.", idFormation); + + try + { + body = await formationService.UpdateFormationAsync(idFormation, body); + } + catch (FormationIncompatibleIdException 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 (FormationInvalidException 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 (FormationNotFoundException 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 formation {0} n'a pas pu être supprimée car elle est prise par une autre ressource.", idFormation) + }; + + 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 suppression de 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); } } }