From cda46536eea1e947ccbcba75d85d3c2e7c02db32 Mon Sep 17 00:00:00 2001 From: jboinembalome Date: Tue, 8 Sep 2020 14:34:07 +0200 Subject: [PATCH] =?UTF-8?q?Impl=C3=A9mentation=20du=20service=20formation?= =?UTF-8?q?=20et=20de=20l'api=20formation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Controllers/FormationsApi.cs | 276 +++++++++++++++++---------- IServices/IFormationService.cs | 28 +++ Services/FormationService.cs | 336 +++++++++++++++++++++++++++++++++ Startup.cs | 2 +- 4 files changed, 537 insertions(+), 105 deletions(-) create mode 100644 IServices/IFormationService.cs create mode 100644 Services/FormationService.cs diff --git a/Controllers/FormationsApi.cs b/Controllers/FormationsApi.cs index 6a21879..5278644 100644 --- a/Controllers/FormationsApi.cs +++ b/Controllers/FormationsApi.cs @@ -18,6 +18,7 @@ using IO.Swagger.Attributes; using IO.Swagger.Security; using Microsoft.AspNetCore.Authorization; using IO.Swagger.DTO; +using EPAServeur.IServices; namespace IO.Swagger.Controllers { @@ -26,7 +27,14 @@ namespace IO.Swagger.Controllers /// [ApiController] public class FormationsApiController : ControllerBase - { + { + private readonly IFormationService formationService; + + public FormationsApiController(IFormationService _formationService) + { + formationService = _formationService; + } + /// /// /// @@ -36,19 +44,21 @@ namespace IO.Swagger.Controllers /// Acces interdit [HttpPost] [Route("/api/formations")] - [Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] + //[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] [ValidateModelState] [SwaggerOperation("AjouterFormation")] [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] public virtual IActionResult AjouterFormation([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); - - //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)); - - throw new NotImplementedException(); + { + switch (formationService.AddFormation(body)) + { + case 0: + return StatusCode(201); + case 1: + return Forbid(); + default: + return NotFound(); + } } /// @@ -60,19 +70,37 @@ namespace IO.Swagger.Controllers /// Acces interdit [HttpDelete] [Route("/api/formations/{idFormation}/supprimer")] - [Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] + //[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] [ValidateModelState] [SwaggerOperation("DeleteFormation")] [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] public virtual IActionResult DeleteFormation([FromRoute][Required]decimal? 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 403 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - // return StatusCode(403, default(ErreurDTO)); - - throw new NotImplementedException(); + { + int id = 0; + + try + { + id = Convert.ToInt32(idFormation); + } + catch (Exception ex) + { + ErreurDTO erreurDTO = new ErreurDTO() + { + Code = "403", + Message = "Impossible de convertir le paramètre idFormation en int. " + ex.Message + }; + return StatusCode(403, erreurDTO); + } + + switch (formationService.DeleteFormationById(id)) + { + case 0: + return NoContent(); + case 1: + return Forbid(); + default: + return NotFound(); + } } /// @@ -90,29 +118,29 @@ namespace IO.Swagger.Controllers /// Ressource n'a pas été trouvée [HttpGet] [Route("/api/formations/annulees")] - [Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] + //[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] [ValidateModelState] [SwaggerOperation("GetFormationAnnulees")] [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) - { + { //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)); + return StatusCode(200, formationService.GetFormationAnnulees()); //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)); - string exampleJson = null; - exampleJson = "[ {\n \"mode\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 5\n },\n \"dateDebut\" : \"2000-01-23T04:56:07.000+00:00\",\n \"estCertifie\" : true,\n \"id\" : 0,\n \"dateFin\" : \"2000-01-23T04:56:07.000+00:00\",\n \"type\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 5\n },\n \"intitule\" : \"intitule\",\n \"statut\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 6\n },\n \"nbPartitipants\" : 1\n}, {\n \"mode\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 5\n },\n \"dateDebut\" : \"2000-01-23T04:56:07.000+00:00\",\n \"estCertifie\" : true,\n \"id\" : 0,\n \"dateFin\" : \"2000-01-23T04:56:07.000+00:00\",\n \"type\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 5\n },\n \"intitule\" : \"intitule\",\n \"statut\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 6\n },\n \"nbPartitipants\" : 1\n} ]"; - - var example = exampleJson != null - ? JsonConvert.DeserializeObject>(exampleJson) - : default(List); //TODO: Change the data returned - return new ObjectResult(example); + //string exampleJson = null; + //exampleJson = "[ {\n \"mode\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 5\n },\n \"dateDebut\" : \"2000-01-23T04:56:07.000+00:00\",\n \"estCertifie\" : true,\n \"id\" : 0,\n \"dateFin\" : \"2000-01-23T04:56:07.000+00:00\",\n \"type\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 5\n },\n \"intitule\" : \"intitule\",\n \"statut\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 6\n },\n \"nbPartitipants\" : 1\n}, {\n \"mode\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 5\n },\n \"dateDebut\" : \"2000-01-23T04:56:07.000+00:00\",\n \"estCertifie\" : true,\n \"id\" : 0,\n \"dateFin\" : \"2000-01-23T04:56:07.000+00:00\",\n \"type\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 5\n },\n \"intitule\" : \"intitule\",\n \"statut\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 6\n },\n \"nbPartitipants\" : 1\n} ]"; + + // var example = exampleJson != null + // ? JsonConvert.DeserializeObject>(exampleJson) + // : default(List); //TODO: Change the data returned + //return new ObjectResult(example); } /// @@ -125,29 +153,39 @@ namespace IO.Swagger.Controllers /// Ressource n'a pas été trouvée [HttpGet] [Route("/api/formations/{idFormation}")] - [Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] + //[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] [ValidateModelState] [SwaggerOperation("GetFormationById")] [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]decimal? 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 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)); - string exampleJson = null; - exampleJson = "{\n \"heure\" : 1.4658129805029452,\n \"participantsFormation\" : [ {\n \"date\" : \"2000-01-23T04:56:07.000+00:00\",\n \"idCollaborateur\" : \"046b6c7f-0b8a-43b9-b35d-6489e6daee91\",\n \"estEvaluee\" : true,\n \"dateCreation\" : \"2000-01-23T04:56:07.000+00:00\",\n \"id\" : 5,\n \"formation\" : \"formation\",\n \"collaborateur\" : \"collaborateur\",\n \"statut\" : \"statut\"\n }, {\n \"date\" : \"2000-01-23T04:56:07.000+00:00\",\n \"idCollaborateur\" : \"046b6c7f-0b8a-43b9-b35d-6489e6daee91\",\n \"estEvaluee\" : true,\n \"dateCreation\" : \"2000-01-23T04:56:07.000+00:00\",\n \"id\" : 5,\n \"formation\" : \"formation\",\n \"collaborateur\" : \"collaborateur\",\n \"statut\" : \"statut\"\n } ],\n \"organisme\" : \"organisme\",\n \"origine\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 6\n },\n \"type\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 5\n },\n \"intitule\" : \"intitule\",\n \"mode\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 5\n },\n \"jour\" : 5.962133916683182,\n \"dateDebut\" : \"2000-01-23T04:56:07.000+00:00\",\n \"estCertifie\" : true,\n \"id\" : 0,\n \"dateFin\" : \"2000-01-23T04:56:07.000+00:00\",\n \"statut\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 6\n }\n}"; - - var example = exampleJson != null - ? JsonConvert.DeserializeObject(exampleJson) - : default(FormationDTO); //TODO: Change the data returned - return new ObjectResult(example); + FormationDTO formationDTO = formationService.GetFormationById(Convert.ToInt32(idFormation)); + if (formationDTO == null) + { + ErreurDTO erreurDTO = new ErreurDTO() + { + Code = "404", + Message = "Le formateur n'existe pas", + }; + return NotFound(erreurDTO); + } + return Ok(formationDTO); + + //string exampleJson = null; + //exampleJson = "{\n \"heure\" : 1.4658129805029452,\n \"participantsFormation\" : [ {\n \"date\" : \"2000-01-23T04:56:07.000+00:00\",\n \"idCollaborateur\" : \"046b6c7f-0b8a-43b9-b35d-6489e6daee91\",\n \"estEvaluee\" : true,\n \"dateCreation\" : \"2000-01-23T04:56:07.000+00:00\",\n \"id\" : 5,\n \"formation\" : \"formation\",\n \"collaborateur\" : \"collaborateur\",\n \"statut\" : \"statut\"\n }, {\n \"date\" : \"2000-01-23T04:56:07.000+00:00\",\n \"idCollaborateur\" : \"046b6c7f-0b8a-43b9-b35d-6489e6daee91\",\n \"estEvaluee\" : true,\n \"dateCreation\" : \"2000-01-23T04:56:07.000+00:00\",\n \"id\" : 5,\n \"formation\" : \"formation\",\n \"collaborateur\" : \"collaborateur\",\n \"statut\" : \"statut\"\n } ],\n \"organisme\" : \"organisme\",\n \"origine\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 6\n },\n \"type\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 5\n },\n \"intitule\" : \"intitule\",\n \"mode\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 5\n },\n \"jour\" : 5.962133916683182,\n \"dateDebut\" : \"2000-01-23T04:56:07.000+00:00\",\n \"estCertifie\" : true,\n \"id\" : 0,\n \"dateFin\" : \"2000-01-23T04:56:07.000+00:00\",\n \"statut\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 6\n }\n}"; + + // var example = exampleJson != null + // ? JsonConvert.DeserializeObject(exampleJson) + // : default(FormationDTO); //TODO: Change the data returned + //return new ObjectResult(example); } /// @@ -165,29 +203,29 @@ namespace IO.Swagger.Controllers /// Ressource n'a pas été trouvée [HttpGet] [Route("/api/formations/realisees")] - [Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] + //[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] [ValidateModelState] [SwaggerOperation("GetFormationRealisee")] [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) - { + { //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)); + return StatusCode(200, formationService.GetFormationRealisee()); //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)); - string exampleJson = null; - exampleJson = "[ {\n \"mode\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 5\n },\n \"dateDebut\" : \"2000-01-23T04:56:07.000+00:00\",\n \"estCertifie\" : true,\n \"id\" : 0,\n \"dateFin\" : \"2000-01-23T04:56:07.000+00:00\",\n \"type\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 5\n },\n \"intitule\" : \"intitule\",\n \"statut\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 6\n },\n \"nbPartitipants\" : 1\n}, {\n \"mode\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 5\n },\n \"dateDebut\" : \"2000-01-23T04:56:07.000+00:00\",\n \"estCertifie\" : true,\n \"id\" : 0,\n \"dateFin\" : \"2000-01-23T04:56:07.000+00:00\",\n \"type\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 5\n },\n \"intitule\" : \"intitule\",\n \"statut\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 6\n },\n \"nbPartitipants\" : 1\n} ]"; - - var example = exampleJson != null - ? JsonConvert.DeserializeObject>(exampleJson) - : default(List); //TODO: Change the data returned - return new ObjectResult(example); + //string exampleJson = null; + //exampleJson = "[ {\n \"mode\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 5\n },\n \"dateDebut\" : \"2000-01-23T04:56:07.000+00:00\",\n \"estCertifie\" : true,\n \"id\" : 0,\n \"dateFin\" : \"2000-01-23T04:56:07.000+00:00\",\n \"type\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 5\n },\n \"intitule\" : \"intitule\",\n \"statut\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 6\n },\n \"nbPartitipants\" : 1\n}, {\n \"mode\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 5\n },\n \"dateDebut\" : \"2000-01-23T04:56:07.000+00:00\",\n \"estCertifie\" : true,\n \"id\" : 0,\n \"dateFin\" : \"2000-01-23T04:56:07.000+00:00\",\n \"type\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 5\n },\n \"intitule\" : \"intitule\",\n \"statut\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 6\n },\n \"nbPartitipants\" : 1\n} ]"; + + // var example = exampleJson != null + // ? JsonConvert.DeserializeObject>(exampleJson) + // : default(List); //TODO: Change the data returned + //return new ObjectResult(example); } /// @@ -205,25 +243,25 @@ namespace IO.Swagger.Controllers /// Acces interdit [HttpGet] [Route("/api/formations")] - [Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] + //[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] [ValidateModelState] [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 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) + { //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)); + return StatusCode(200, formationService.GetFormations()); //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 \"mode\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 5\n },\n \"dateDebut\" : \"2000-01-23T04:56:07.000+00:00\",\n \"estCertifie\" : true,\n \"id\" : 0,\n \"dateFin\" : \"2000-01-23T04:56:07.000+00:00\",\n \"type\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 5\n },\n \"intitule\" : \"intitule\",\n \"statut\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 6\n },\n \"nbPartitipants\" : 1\n}, {\n \"mode\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 5\n },\n \"dateDebut\" : \"2000-01-23T04:56:07.000+00:00\",\n \"estCertifie\" : true,\n \"id\" : 0,\n \"dateFin\" : \"2000-01-23T04:56:07.000+00:00\",\n \"type\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 5\n },\n \"intitule\" : \"intitule\",\n \"statut\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 6\n },\n \"nbPartitipants\" : 1\n} ]"; + //string exampleJson = null; + //exampleJson = "[ {\n \"mode\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 5\n },\n \"dateDebut\" : \"2000-01-23T04:56:07.000+00:00\",\n \"estCertifie\" : true,\n \"id\" : 0,\n \"dateFin\" : \"2000-01-23T04:56:07.000+00:00\",\n \"type\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 5\n },\n \"intitule\" : \"intitule\",\n \"statut\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 6\n },\n \"nbPartitipants\" : 1\n}, {\n \"mode\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 5\n },\n \"dateDebut\" : \"2000-01-23T04:56:07.000+00:00\",\n \"estCertifie\" : true,\n \"id\" : 0,\n \"dateFin\" : \"2000-01-23T04:56:07.000+00:00\",\n \"type\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 5\n },\n \"intitule\" : \"intitule\",\n \"statut\" : {\n \"libelle\" : \"libelle\",\n \"id\" : 6\n },\n \"nbPartitipants\" : 1\n} ]"; - var example = exampleJson != null - ? JsonConvert.DeserializeObject>(exampleJson) - : default(List); //TODO: Change the data returned - return new ObjectResult(example); + // var example = exampleJson != null + // ? JsonConvert.DeserializeObject>(exampleJson) + // : default(List); //TODO: Change the data returned + //return new ObjectResult(example); } /// @@ -234,25 +272,25 @@ namespace IO.Swagger.Controllers /// Acces interdit [HttpGet] [Route("/api/modesFormation")] - [Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] + //[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] [ValidateModelState] [SwaggerOperation("GetModesFormation")] [SwaggerResponse(statusCode: 200, type: typeof(List), description: "OK")] [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] 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)); + return StatusCode(200, formationService.GetModesFormation()); //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 \"libelle\" : \"libelle\",\n \"id\" : 5\n}, {\n \"libelle\" : \"libelle\",\n \"id\" : 5\n} ]"; - - var example = exampleJson != null - ? JsonConvert.DeserializeObject>(exampleJson) - : default(List); //TODO: Change the data returned - return new ObjectResult(example); + //string exampleJson = null; + //exampleJson = "[ {\n \"libelle\" : \"libelle\",\n \"id\" : 5\n}, {\n \"libelle\" : \"libelle\",\n \"id\" : 5\n} ]"; + + // var example = exampleJson != null + // ? JsonConvert.DeserializeObject>(exampleJson) + // : default(List); //TODO: Change the data returned + //return new ObjectResult(example); } /// @@ -263,25 +301,25 @@ namespace IO.Swagger.Controllers /// Acces interdit [HttpGet] [Route("/api/originesFormation")] - [Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] + //[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] [ValidateModelState] [SwaggerOperation("GetOriginesFormation")] [SwaggerResponse(statusCode: 200, type: typeof(List), description: "OK")] [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] 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)); + return StatusCode(200, formationService.GetOriginesFormation()); //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 \"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); + //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); } /// @@ -299,7 +337,7 @@ namespace IO.Swagger.Controllers /// Ressource n'a pas été trouvée [HttpGet] [Route("/api/formations/prochaines")] - [Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] + //[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] [ValidateModelState] [SwaggerOperation("GetProchainesFormation")] [SwaggerResponse(statusCode: 200, type: typeof(List), description: "OK")] @@ -332,25 +370,25 @@ namespace IO.Swagger.Controllers /// Acces interdit [HttpGet] [Route("/api/statutsFormation")] - [Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] + //[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] [ValidateModelState] [SwaggerOperation("GetStatutsFormation")] [SwaggerResponse(statusCode: 200, type: typeof(List), description: "OK")] [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] 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)); + return StatusCode(200, formationService.GetStatutsFormation()); //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 \"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); + //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); } /// @@ -361,25 +399,25 @@ namespace IO.Swagger.Controllers /// Acces interdit [HttpGet] [Route("/api/typesFormation")] - [Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] + //[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] [ValidateModelState] [SwaggerOperation("GetTypesFormation")] [SwaggerResponse(statusCode: 200, type: typeof(List), description: "OK")] [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] 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)); + return StatusCode(200, formationService.GetTypesFormation()); //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 \"libelle\" : \"libelle\",\n \"id\" : 5\n}, {\n \"libelle\" : \"libelle\",\n \"id\" : 5\n} ]"; - - var example = exampleJson != null - ? JsonConvert.DeserializeObject>(exampleJson) - : default(List); //TODO: Change the data returned - return new ObjectResult(example); + //string exampleJson = null; + //exampleJson = "[ {\n \"libelle\" : \"libelle\",\n \"id\" : 5\n}, {\n \"libelle\" : \"libelle\",\n \"id\" : 5\n} ]"; + + // var example = exampleJson != null + // ? JsonConvert.DeserializeObject>(exampleJson) + // : default(List); //TODO: Change the data returned + //return new ObjectResult(example); } /// @@ -393,12 +431,12 @@ namespace IO.Swagger.Controllers /// Acces interdit [HttpPut] [Route("/api/formations/{idFormation}/update")] - [Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] + //[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] [ValidateModelState] [SwaggerOperation("UpdateFormation")] [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] public virtual IActionResult UpdateFormation([FromBody]FormationDTO body, [FromRoute][Required]decimal? 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); @@ -407,8 +445,38 @@ namespace IO.Swagger.Controllers //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)); + int id = 0; + try + { + id = Convert.ToInt32(idFormation); + } + catch (Exception ex) + { + ErreurDTO erreurDTO = new ErreurDTO() + { + Code = "403", + Message = "Impossible de convertir le paramètre idFormation en int. " + ex.Message + }; + return StatusCode(403, erreurDTO); + } + + + switch (formationService.DeleteFormationById(id)) + { + case 0: + return StatusCode(201); + case 1: + return StatusCode(200); + case 2: + return StatusCode(403); + default: + return NotFound(); + } + + //TODO: Uncomment the next line to return response 201 or use other options such as return this.NotFound(), return this.BadRequest(..), ... + formationService.UpdateFormation(body); + return StatusCode(201); - throw new NotImplementedException(); } } } diff --git a/IServices/IFormationService.cs b/IServices/IFormationService.cs new file mode 100644 index 0000000..34b7dc7 --- /dev/null +++ b/IServices/IFormationService.cs @@ -0,0 +1,28 @@ +using EPAServeur.Context; +using IO.Swagger.DTO; +using IO.Swagger.ModelCollaborateur; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace EPAServeur.IServices +{ + public interface IFormationService + { + FormationDTO GetFormationById(int? id); + + IEnumerable GetFormations(); + IEnumerable GetFormationAnnulees(); + IEnumerable GetFormationRealisee(); + IEnumerable GetProchainesFormation(); + IEnumerable GetModesFormation(); + IEnumerable GetOriginesFormation(); + IEnumerable GetStatutsFormation(); + IEnumerable GetTypesFormation(); + + byte AddFormation(FormationDTO formationDTO); + byte UpdateFormation(FormationDTO formationDTO); + byte DeleteFormationById(int? id); + } +} diff --git a/Services/FormationService.cs b/Services/FormationService.cs new file mode 100644 index 0000000..e3b2233 --- /dev/null +++ b/Services/FormationService.cs @@ -0,0 +1,336 @@ +using EPAServeur.Context; +using EPAServeur.IServices; +using EPAServeur.Models.Formation; +using IO.Swagger.ApiCollaborateur; +using IO.Swagger.DTO; +using IO.Swagger.ModelCollaborateur; +using Microsoft.EntityFrameworkCore.ChangeTracking; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices.ComTypes; +using System.Threading.Tasks; + +namespace EPAServeur.Services +{ + public class FormationService : IFormationService + { + private readonly EpContext epContext; + + public FormationService(EpContext _epContext) + { + epContext = _epContext; + } + + public FormationDTO GetFormationById(int? id) + { + Formation formation = epContext.Formation.FirstOrDefault(formation => formation.Id == id); + + if (formation == null) + return null; + + return GetFormationDTO(formation); + } + + public IEnumerable GetFormations() + { + IEnumerable formations = epContext.Formation; + IEnumerable formationDTOs = formations.Select(formation => GetFormationDTO(formation)); + + return formationDTOs; + } + + + public IEnumerable GetFormationAnnulees() + { + IEnumerable formations = epContext.Formation; + IEnumerable formationDTOs = formations.Where(formation => formation.Statut.Id == 4).Select(formation => GetFormationDTO(formation)); + + return formationDTOs; + } + + public IEnumerable GetFormationRealisee() + { + IEnumerable formations = epContext.Formation; + IEnumerable formationDTOs = formations.Where(formation => formation.Statut.Id == 3).Select(formation => GetFormationDTO(formation)); + + return formationDTOs; + + } + + public IEnumerable GetProchainesFormation() + { + IEnumerable formations = epContext.Formation; + IEnumerable formationDTOs = formations.Where(formation => formation.Statut.Id == 1 && formation.Statut.Id == 2).Select(formation => GetFormationDTO(formation)).OrderBy(formation => formation.DateDebut); + + return formationDTOs; + } + + public IEnumerable GetModesFormation() + { + IEnumerable modeFormations = epContext.ModeFormation; + IEnumerable modeFormationDTOs = modeFormations.Select(modeFormation => GetModeFormationDTO(modeFormation)); + + return modeFormationDTOs; + } + + public IEnumerable GetOriginesFormation() + { + IEnumerable origineFormations = epContext.OrigineFormation; + IEnumerable origineFormationDTOs = origineFormations.Select(origineFormation => GetOrigineFormationDTO(origineFormation)); + + return origineFormationDTOs; + } + + public IEnumerable GetStatutsFormation() + { + IEnumerable statutFormations = epContext.StatutFormation; + IEnumerable statutFormationDTOs = statutFormations.Select(statutFormation => GetStatutFormationDTO(statutFormation)); + + return statutFormationDTOs; + } + + public IEnumerable GetTypesFormation() + { + IEnumerable typeFormations = epContext.TypeFormation; + IEnumerable typeFormationDTOs = typeFormations.Select(typeFormation => GetTypeFormationDTO(typeFormation)); + + return typeFormationDTOs; + } + + public byte AddFormation(FormationDTO formationDTO) + { + 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) + { + return 1; + } + + return 0; + } + + public byte UpdateFormation(FormationDTO formationDTO) + { + Formation formation = epContext.Formation.FirstOrDefault(formation => formation.Id == formationDTO.Id); + + if (formation == null) + return 1; + + formation = SetFormation(formation, formationDTO); + try + { + epContext.SaveChanges(); + } + catch (Exception) + { + return 2; + } + + return 0; + } + + public byte DeleteFormationById(int? id) + { + Formation formation = epContext.Formation.FirstOrDefault(formation => formation.Id == id); + + if (formation == null) + return 1; + + try + { + epContext.Remove(formation); + epContext.SaveChanges(); + } + catch (Exception) + { + return 2; + } + + return 0; + + } + + #region Object to DTO + private FormationDTO GetFormationDTO(Formation formation) + { + FormationDTO formationDTO = new FormationDTO() + { + Id = formation.Id, + Intitule = formation.Intitule, + IdAgence = formation.IdAgence, + DateDebut = formation.DateDebut, + DateFin = formation.DateFin, + Heure = formation.Heure, + Jour = formation.Jour, + Organisme = formation.Organisme, + EstCertifie = formation.EstCertifiee, + Origine = GetOrigineFormationDTO(formation.Origine), + Statut = GetStatutFormationDTO(formation.Statut), + Mode = GetModeFormationDTO(formation.ModeFormation), + Type = GetTypeFormationDTO(formation.TypeFormation) + }; + + //List participationFormationDTOs = epContext.ParticipationFormation.Where(participationFormation => participationFormation.Formation.Id == formation.Id) + // .Select(participationFormation => new ParticipationFormationDTO() + // { + // Id = participationFormation.Id, + // DateCreation = participationFormation.DateCreation, + // Formation = participationFormation.Formation.Intitule, + // Date = participationFormation.Formation.DateDebut, + // Statut = participationFormation.Formation.Statut.Libelle, + // EstEvaluee = participationFormation.EstEvaluee, + // }).ToList(); + + //formationDTO.ParticipantsFormation = participationFormationDTOs; + return formationDTO; + } + + private OrigineFormationDTO GetOrigineFormationDTO(OrigineFormation origineFormation) + { + if (origineFormation == null) + return null; + OrigineFormationDTO origineFormationDTO = new OrigineFormationDTO() + { + Id = origineFormation.Id, + Libelle = origineFormation.Libelle + }; + return origineFormationDTO; + } + + private StatutFormationDTO GetStatutFormationDTO(StatutFormation statutFormation) + { + if (statutFormation == null) + return null; + StatutFormationDTO statutFormationDTO = new StatutFormationDTO() + { + Id = statutFormation.Id, + Libelle = statutFormation.Libelle + }; + return statutFormationDTO; + } + + private ModeFormationDTO GetModeFormationDTO(ModeFormation modeFormation) + { + if (modeFormation == null) + return null; + ModeFormationDTO modeFormationDTO = new ModeFormationDTO() + { + Id = modeFormation.Id, + Libelle = modeFormation.Libelle + }; + return modeFormationDTO; + } + private TypeFormationDTO GetTypeFormationDTO(TypeFormation typeFormation) + { + if (typeFormation == null) + return null; + TypeFormationDTO typeFormationDTO = new TypeFormationDTO() + { + Id = typeFormation.Id, + Libelle = typeFormation.Libelle + }; + return typeFormationDTO; + } + + #endregion + + #region DTO to Object + private Formation SetFormation(Formation formation, FormationDTO formationDTO) + { + formation.Intitule = formationDTO.Intitule; + formation.IdAgence = formationDTO.IdAgence.Value; + formation.DateDebut = formationDTO.DateDebut.Value; + formation.DateFin = formationDTO.DateFin.Value; + formation.Heure = Convert.ToInt32(formationDTO.Heure.Value); + formation.Jour = Convert.ToInt32(formationDTO.Jour.Value); + formation.Organisme = formationDTO.Organisme; + formation.EstCertifiee = formationDTO.EstCertifie.Value; + formation.Origine = GetOrigineFormation(formationDTO.Origine); + formation.Statut = GetStatutFormation(formationDTO.Statut); + formation.ModeFormation = GetModeFormation(formationDTO.Mode); + formation.TypeFormation = GetTypeFormation(formationDTO.Type); + + + //List participationFormationDTOs = epContext.ParticipationFormation.Where(participationFormation => participationFormation.Formation.Id == formation.Id) + // .Select(participationFormation => new ParticipationFormationDTO() + // { + // Id = participationFormation.Id, + // DateCreation = participationFormation.DateCreation, + // Formation = participationFormation.Formation.Intitule, + // Date = participationFormation.Formation.DateDebut, + // Statut = participationFormation.Formation.Statut.Libelle, + // EstEvaluee = participationFormation.EstEvaluee, + // }).ToList(); + + //formationDTO.ParticipantsFormation = participationFormationDTOs; + return formation; + } + + + private OrigineFormation GetOrigineFormation(OrigineFormationDTO origineFormationDTO) + { + if (origineFormationDTO == null) + return null; + OrigineFormation origineFormation = new OrigineFormation() + { + Id = origineFormationDTO.Id.Value, + Libelle = origineFormationDTO.Libelle + }; + return origineFormation; + } + + private StatutFormation GetStatutFormation(StatutFormationDTO statutFormationDTO) + { + if (statutFormationDTO == null) + return null; + StatutFormation statutFormation = new StatutFormation() + { + Id = statutFormationDTO.Id.Value, + Libelle = statutFormationDTO.Libelle + }; + return statutFormation; + } + + private ModeFormation GetModeFormation(ModeFormationDTO modeFormationDTO) + { + if (modeFormationDTO == null) + return null; + ModeFormation modeFormation = new ModeFormation() + { + Id = modeFormationDTO.Id.Value, + Libelle = modeFormationDTO.Libelle + }; + return modeFormation; + } + private TypeFormation GetTypeFormation(TypeFormationDTO typeFormationDTO) + { + if (typeFormationDTO == null) + return null; + TypeFormation typeFormation = new TypeFormation() + { + Id = typeFormationDTO.Id.Value, + Libelle = typeFormationDTO.Libelle + }; + return typeFormation; + } + + #endregion + + } +} diff --git a/Startup.cs b/Startup.cs index 3e5b0c6..cab797a 100644 --- a/Startup.cs +++ b/Startup.cs @@ -53,7 +53,7 @@ namespace EPAServeur //Services services.AddScoped(); - + services.AddScoped(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.