From cda46536eea1e947ccbcba75d85d3c2e7c02db32 Mon Sep 17 00:00:00 2001 From: jboinembalome Date: Tue, 8 Sep 2020 14:34:07 +0200 Subject: [PATCH 1/3] =?UTF-8?q?Impl=C3=A9mentation=20du=20service=20format?= =?UTF-8?q?ion=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. From 2f0b45406cb28c2ace5d9fe43c2d384643cb2277 Mon Sep 17 00:00:00 2001 From: jboinembalome Date: Wed, 9 Sep 2020 11:33:15 +0200 Subject: [PATCH 2/3] =?UTF-8?q?Impl=C3=A9mentation=20compl=C3=A8te=20du=20?= =?UTF-8?q?service=20formation=20et=20de=20l'api=20formation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Controllers/FormationsApi.cs | 320 ++++++++++++++------------------- IServices/IFormationService.cs | 16 +- Services/FormationService.cs | 196 +++++++++++++++----- 3 files changed, 302 insertions(+), 230 deletions(-) diff --git a/Controllers/FormationsApi.cs b/Controllers/FormationsApi.cs index 5278644..561eb2e 100644 --- a/Controllers/FormationsApi.cs +++ b/Controllers/FormationsApi.cs @@ -19,9 +19,11 @@ 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; namespace IO.Swagger.Controllers -{ +{ /// /// /// @@ -48,17 +50,19 @@ 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 IActionResult AjouterFormation([FromBody] FormationDTO body) { - switch (formationService.AddFormation(body)) - { - case 0: - return StatusCode(201); - case 1: - return Forbid(); - default: - return NotFound(); - } + FormationDTO nouvelleFormation = formationService.AddFormation(body); + + return Created("", nouvelleFormation); + //if (body.Id != null && body.Id > 0) + //{ + // return StatusCode(201, body); + //} + //else + //{ + // return NotFound(); + //} } /// @@ -74,33 +78,18 @@ namespace IO.Swagger.Controllers [ValidateModelState] [SwaggerOperation("DeleteFormation")] [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] - public virtual IActionResult DeleteFormation([FromRoute][Required]decimal? idFormation) - { - int id = 0; - - try - { - id = Convert.ToInt32(idFormation); - } - catch (Exception ex) + public virtual IActionResult DeleteFormation([FromRoute][Required] decimal? idFormation) + { + if (!formationService.DeleteFormationById(idFormation)) { - ErreurDTO erreurDTO = new ErreurDTO() + ErreurDTO erreur = new ErreurDTO() { - Code = "403", - Message = "Impossible de convertir le paramètre idFormation en int. " + ex.Message + Code = "404", + Message = "Aucune formation trouvée" }; - return StatusCode(403, erreurDTO); - } - - switch (formationService.DeleteFormationById(id)) - { - case 0: - return NoContent(); - case 1: - return Forbid(); - default: - return NotFound(); + return NotFound(erreur); } + return NoContent(); } /// @@ -124,23 +113,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 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, 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} ]"; + IEnumerable formations = formationService.GetFormationAnnulees(asc, numPage, parPAge, idAgence, texte, tri); + if (formations == null) + { + ErreurDTO erreur = new ErreurDTO() + { + Code = "404", + Message = "Aucune formation annulée" + }; + return NotFound(erreur); + } - // var example = exampleJson != null - // ? JsonConvert.DeserializeObject>(exampleJson) - // : default(List); //TODO: Change the data returned - //return new ObjectResult(example); + return Ok(formations); } /// @@ -159,33 +146,19 @@ 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]decimal? idFormation) + 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)); - FormationDTO formationDTO = formationService.GetFormationById(Convert.ToInt32(idFormation)); if (formationDTO == null) { ErreurDTO erreurDTO = new ErreurDTO() { Code = "404", - Message = "Le formateur n'existe pas", + Message = "La formation 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); } /// @@ -209,25 +182,24 @@ 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 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, 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} ]"; + IEnumerable formations = formationService.GetFormationRealisee(asc, numPage, parPAge, idAgence, texte, tri); + if (formations == null) + { + ErreurDTO erreur = new ErreurDTO() + { + Code = "404", + Message = "Aucune formation réalisée" + }; + return NotFound(erreur); + } - // var example = exampleJson != null - // ? JsonConvert.DeserializeObject>(exampleJson) - // : default(List); //TODO: Change the data returned - //return new ObjectResult(example); + return Ok(formations); } + /// /// /// @@ -248,20 +220,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 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, formationService.GetFormations()); + IEnumerable formations = formationService.GetFormations(asc, numPage, parPAge, idAgence, texte, tri); + if (formations == null) + { + ErreurDTO erreur = new ErreurDTO() + { + Code = "404", + Message = "Aucune formation" + }; + return NotFound(erreur); + } - //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} ]"; - - // var example = exampleJson != null - // ? JsonConvert.DeserializeObject>(exampleJson) - // : default(List); //TODO: Change the data returned - //return new ObjectResult(example); + return Ok(formations); } /// @@ -280,17 +253,18 @@ namespace IO.Swagger.Controllers 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, 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} ]"; + IEnumerable modeFormations = formationService.GetModesFormation(); + if (modeFormations == null) + { + ErreurDTO erreur = new ErreurDTO() + { + Code = "404", + Message = "Aucun mode de formation" + }; + return NotFound(erreur); + } - // var example = exampleJson != null - // ? JsonConvert.DeserializeObject>(exampleJson) - // : default(List); //TODO: Change the data returned - //return new ObjectResult(example); + return Ok(modeFormations); } /// @@ -309,17 +283,18 @@ namespace IO.Swagger.Controllers 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, 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} ]"; + IEnumerable origineFormations = formationService.GetOriginesFormation(); + if (origineFormations == null) + { + ErreurDTO erreur = new ErreurDTO() + { + Code = "404", + Message = "Aucune origine de formation" + }; + return NotFound(erreur); + } - // var example = exampleJson != null - // ? JsonConvert.DeserializeObject>(exampleJson) - // : default(List); //TODO: Change the data returned - //return new ObjectResult(example); + return Ok(origineFormations); } /// @@ -343,23 +318,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 IActionResult GetProchainesFormation([FromQuery][Required()] bool? asc, [FromQuery][Required()] int? numPage, [FromQuery][Required()] int? parPAge, [FromQuery] int? idAgence, [FromQuery] string texte, [FromQuery] string tri) + { //TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - // 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)); + IEnumerable formations = formationService.GetProchainesFormation(asc, numPage, parPAge, idAgence, texte, tri); + if (formations == null) + { + ErreurDTO erreur = new ErreurDTO() + { + Code = "404", + Message = "Aucune prochaine formation" + }; + return NotFound(erreur); + } - //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); + return Ok(formations); } /// @@ -378,17 +351,18 @@ namespace IO.Swagger.Controllers 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, 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} ]"; + IEnumerable statutFormations = formationService.GetStatutsFormation(); + if (statutFormations == null) + { + ErreurDTO erreur = new ErreurDTO() + { + Code = "404", + Message = "Aucun statut de formation" + }; + return NotFound(erreur); + } - // var example = exampleJson != null - // ? JsonConvert.DeserializeObject>(exampleJson) - // : default(List); //TODO: Change the data returned - //return new ObjectResult(example); + return Ok(statutFormations); } /// @@ -407,17 +381,18 @@ namespace IO.Swagger.Controllers 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, 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} ]"; + IEnumerable typeFormations = formationService.GetTypesFormation(); + if (typeFormations == null) + { + ErreurDTO erreur = new ErreurDTO() + { + Code = "404", + Message = "Aucun type de formation" + }; + return NotFound(erreur); + } - // var example = exampleJson != null - // ? JsonConvert.DeserializeObject>(exampleJson) - // : default(List); //TODO: Change the data returned - //return new ObjectResult(example); + return Ok(typeFormations); } /// @@ -435,47 +410,28 @@ 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]decimal? idFormation) + 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); - - //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)); - int id = 0; - try + FormationDTO formation = formationService.UpdateFormation(body); + if (formation == null) { - id = Convert.ToInt32(idFormation); + formation = formationService.AddFormation(body); + return Created("", formation); } - 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); + return Ok(formation); + + + //switch (formationService.UpdateFormation(body)) + //{ + // case 0: + // return Ok(); + // case 1: + // return StatusCode(201); + // case 2: + // return Forbid(); + // default: + // return NotFound(); + //} } } diff --git a/IServices/IFormationService.cs b/IServices/IFormationService.cs index 34b7dc7..90f5cd7 100644 --- a/IServices/IFormationService.cs +++ b/IServices/IFormationService.cs @@ -10,19 +10,19 @@ namespace EPAServeur.IServices { public interface IFormationService { - FormationDTO GetFormationById(int? id); + FormationDTO GetFormationById(decimal? id); - IEnumerable GetFormations(); - IEnumerable GetFormationAnnulees(); - IEnumerable GetFormationRealisee(); - IEnumerable GetProchainesFormation(); + IEnumerable GetFormations(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri); + IEnumerable GetFormationAnnulees(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); + IEnumerable GetProchainesFormation(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri); IEnumerable GetModesFormation(); IEnumerable GetOriginesFormation(); IEnumerable GetStatutsFormation(); IEnumerable GetTypesFormation(); - byte AddFormation(FormationDTO formationDTO); - byte UpdateFormation(FormationDTO formationDTO); - byte DeleteFormationById(int? id); + FormationDTO AddFormation(FormationDTO formationDTO); + FormationDTO UpdateFormation(FormationDTO formationDTO); + bool DeleteFormationById(decimal? id); } } diff --git a/Services/FormationService.cs b/Services/FormationService.cs index e3b2233..edb6b6e 100644 --- a/Services/FormationService.cs +++ b/Services/FormationService.cs @@ -4,6 +4,7 @@ using EPAServeur.Models.Formation; using IO.Swagger.ApiCollaborateur; using IO.Swagger.DTO; using IO.Swagger.ModelCollaborateur; +using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; using System; using System.Collections.Generic; @@ -22,9 +23,24 @@ namespace EPAServeur.Services epContext = _epContext; } - public FormationDTO GetFormationById(int? id) + public FormationDTO GetFormationById(decimal? id) { - Formation formation = epContext.Formation.FirstOrDefault(formation => formation.Id == id); + int idFormation = 0; + + try + { + idFormation = Convert.ToInt32(id); + } + catch (Exception ex) + { + return null; + } + + Formation formation = epContext.Formation.Include(formation => formation.Statut) + .Include(formation => formation.ModeFormation) + .Include(formation => formation.Origine) + .Include(formation => formation.TypeFormation) + .FirstOrDefault(formation => formation.Id == idFormation); if (formation == null) return null; @@ -32,36 +48,138 @@ namespace EPAServeur.Services return GetFormationDTO(formation); } - public IEnumerable GetFormations() + public IEnumerable GetFormations(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri) { - IEnumerable formations = epContext.Formation; - IEnumerable formationDTOs = formations.Select(formation => GetFormationDTO(formation)); + 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 = 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); + + if (formations == null) + return new List(); + + formationDTOs = formations.Where(formation => formation.Intitule.ToLower().Contains(texte)).Select(formation => GetFormationDTO(formation)); return formationDTOs; } - public IEnumerable GetFormationAnnulees() + public IEnumerable GetFormationAnnulees(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri) { - IEnumerable formations = epContext.Formation; - IEnumerable formationDTOs = formations.Where(formation => formation.Statut.Id == 4).Select(formation => GetFormationDTO(formation)); + 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 = 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); + else + formations = epContext.Formation.Where(formation => formation.Statut.Id == 4) + .Include(formation => formation.Statut) + .Include(formation => formation.ModeFormation) + .Include(formation => formation.Origine) + .Include(formation => formation.TypeFormation); + + if (formations == null) + return new List(); + + formationDTOs = formations.Where(formation => formation.Intitule.ToLower().Contains(texte)).Select(formation => GetFormationDTO(formation)); return formationDTOs; } - public IEnumerable GetFormationRealisee() + public IEnumerable GetFormationRealisee(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri) { - IEnumerable formations = epContext.Formation; - IEnumerable formationDTOs = formations.Where(formation => formation.Statut.Id == 3).Select(formation => GetFormationDTO(formation)); + 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 = 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 new List(); + + formationDTOs = formations.Where(formation => formation.Intitule.ToLower().Contains(texte)).Select(formation => GetFormationDTO(formation)); return formationDTOs; - } - public IEnumerable GetProchainesFormation() + public IEnumerable GetProchainesFormation(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri) { - 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); + 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 = 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)) + .Include(formation => formation.Statut) + .Include(formation => formation.ModeFormation) + .Include(formation => formation.Origine) + .Include(formation => formation.TypeFormation); + + if (formations == null) + return new List(); + + formationDTOs = formations.Where(formation => formation.Intitule.ToLower().Contains(texte)).Select(formation => GetFormationDTO(formation)); return formationDTOs; } @@ -98,7 +216,7 @@ namespace EPAServeur.Services return typeFormationDTOs; } - public byte AddFormation(FormationDTO formationDTO) + public FormationDTO AddFormation(FormationDTO formationDTO) { Formation formation = new Formation(); formation = SetFormation(formation, formationDTO); @@ -119,51 +237,49 @@ namespace EPAServeur.Services } catch (Exception) { - return 1; + return null; } - return 0; + return GetFormationDTO(formation); } - public byte UpdateFormation(FormationDTO formationDTO) + public FormationDTO 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 null; } - return 0; + formation = SetFormation(formation, formationDTO); + epContext.SaveChanges(); + + return GetFormationDTO(formation); } - public byte DeleteFormationById(int? id) + public bool DeleteFormationById(decimal? id) { - Formation formation = epContext.Formation.FirstOrDefault(formation => formation.Id == id); - - if (formation == null) - return 1; + int idFormation = 0; try { - epContext.Remove(formation); - epContext.SaveChanges(); + idFormation = Convert.ToInt32(id); } - catch (Exception) + catch (Exception ex) { - return 2; + return false; } - return 0; + Formation formation = epContext.Formation.FirstOrDefault(formation => formation.Id == idFormation); + if (formation == null) + return false; + + + epContext.Remove(formation); + epContext.SaveChanges(); + return true; } #region Object to DTO @@ -265,7 +381,7 @@ namespace EPAServeur.Services 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() From f60c4ddb83c04c32826d36605734e4c587f23b64 Mon Sep 17 00:00:00 2001 From: jboinembalome Date: Wed, 9 Sep 2020 15:47:27 +0200 Subject: [PATCH 3/3] Ajout des try/catch dans la classe FormationService --- Services/FormationService.cs | 365 ++++++++++++++++++++++++++++------- 1 file changed, 295 insertions(+), 70 deletions(-) diff --git a/Services/FormationService.cs b/Services/FormationService.cs index edb6b6e..89b14d9 100644 --- a/Services/FormationService.cs +++ b/Services/FormationService.cs @@ -1,28 +1,42 @@ 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; -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 { + #region Variables + private readonly EpContext epContext; + #endregion + + #region Contructeurs + + /// + /// Constructeur de la classe FormationService + /// + /// public FormationService(EpContext _epContext) { epContext = _epContext; } + #endregion + + #region Méthodes Service + + /// + /// Récupérer une formation par son id + /// + /// + /// public FormationDTO GetFormationById(decimal? id) { int idFormation = 0; @@ -33,7 +47,7 @@ namespace EPAServeur.Services } catch (Exception ex) { - return null; + throw; } Formation formation = epContext.Formation.Include(formation => formation.Statut) @@ -48,6 +62,16 @@ namespace EPAServeur.Services return GetFormationDTO(formation); } + /// + /// Récupérer la liste des formations + /// + /// 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 GetFormations(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri) { IEnumerable formations; @@ -62,16 +86,35 @@ namespace EPAServeur.Services int take = parPAge.Value; 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); + { + try + { + formations = epContext.Formation.Where(formation => formation.IdAgence == idAgence) + .Include(formation => formation.Statut) + .Include(formation => formation.ModeFormation) + .Include(formation => formation.Origine) + .Include(formation => formation.TypeFormation); + } + catch (Exception ex) + { + throw; + } + } else - formations = epContext.Formation.Include(formation => formation.Statut) - .Include(formation => formation.ModeFormation) - .Include(formation => formation.Origine) - .Include(formation => formation.TypeFormation); + { + try + { + 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 new List(); @@ -81,7 +124,16 @@ namespace EPAServeur.Services return formationDTOs; } - + /// + /// Récupérer les formations annulées + /// + /// 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 GetFormationAnnulees(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri) { IEnumerable formations; @@ -116,6 +168,16 @@ namespace EPAServeur.Services return formationDTOs; } + /// + /// Récupérer les formations réalisées + /// + /// 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 GetFormationRealisee(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri) { IEnumerable formations; @@ -130,17 +192,37 @@ namespace EPAServeur.Services int take = parPAge.Value; if (idAgence != null) - 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); + { + 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; + } + } + 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); + { + 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; + } + } + if (formations == null) return new List(); @@ -150,6 +232,16 @@ namespace EPAServeur.Services return formationDTOs; } + /// + /// Récupérer les formations plannifiées et replannifiées + /// + /// 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 GetProchainesFormation(bool? asc, int? numPage, int? parPAge, int? idAgence, string texte, string tri) { IEnumerable formations; @@ -164,17 +256,27 @@ namespace EPAServeur.Services int take = parPAge.Value; if (idAgence != null) - formations = epContext.Formation.Where(formation => formation.IdAgence == idAgence && (formation.Statut.Id == 1 || formation.Statut.Id == 2)) + 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; + } + else + { formations = 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); + .Include(formation => formation.Statut) + .Include(formation => formation.ModeFormation) + .Include(formation => formation.Origine) + .Include(formation => formation.TypeFormation); + } if (formations == null) return new List(); @@ -184,38 +286,103 @@ namespace EPAServeur.Services return formationDTOs; } + /// + /// Récupérer les modes de formation + /// + /// public IEnumerable GetModesFormation() { - IEnumerable modeFormations = epContext.ModeFormation; - IEnumerable modeFormationDTOs = modeFormations.Select(modeFormation => GetModeFormationDTO(modeFormation)); + IEnumerable modeFormations; + IEnumerable modeFormationDTOs; + try + { + modeFormations = epContext.ModeFormation; + } + catch (Exception ex) + { + throw; + } + + modeFormationDTOs = modeFormations.Select(modeFormation => GetModeFormationDTO(modeFormation)); return modeFormationDTOs; } + /// + /// Récupérer les origines de formation + /// + /// public IEnumerable GetOriginesFormation() { - IEnumerable origineFormations = epContext.OrigineFormation; - IEnumerable origineFormationDTOs = origineFormations.Select(origineFormation => GetOrigineFormationDTO(origineFormation)); + IEnumerable origineFormations; + IEnumerable origineFormationDTOs; + + try + { + origineFormations = epContext.OrigineFormation; + } + catch (Exception ex) + { + throw; + } + + origineFormationDTOs = origineFormations.Select(origineFormation => GetOrigineFormationDTO(origineFormation)); return origineFormationDTOs; } + /// + /// Récupérer les statuts de formation + /// + /// public IEnumerable GetStatutsFormation() { - IEnumerable statutFormations = epContext.StatutFormation; - IEnumerable statutFormationDTOs = statutFormations.Select(statutFormation => GetStatutFormationDTO(statutFormation)); + IEnumerable statutFormations; + IEnumerable statutFormationDTOs; + + try + { + statutFormations = epContext.StatutFormation; + } + catch (Exception ex) + { + + throw; + } + + statutFormationDTOs = statutFormations.Select(statutFormation => GetStatutFormationDTO(statutFormation)); return statutFormationDTOs; } + /// + /// Récupérer les types de formation + /// + /// public IEnumerable GetTypesFormation() { - IEnumerable typeFormations = epContext.TypeFormation; - IEnumerable typeFormationDTOs = typeFormations.Select(typeFormation => GetTypeFormationDTO(typeFormation)); + IEnumerable typeFormations; + IEnumerable typeFormationDTOs; + + try + { + typeFormations = epContext.TypeFormation; + } + catch (Exception ex) + { + throw; + } + + typeFormationDTOs = typeFormations.Select(typeFormation => GetTypeFormationDTO(typeFormation)); return typeFormationDTOs; } + /// + /// Ajouter une formation + /// + /// + /// public FormationDTO AddFormation(FormationDTO formationDTO) { Formation formation = new Formation(); @@ -235,14 +402,19 @@ namespace EPAServeur.Services { epContext.SaveChanges(); } - catch (Exception) + catch (Exception ex) { - return null; + throw; } return GetFormationDTO(formation); } + /// + /// Modifier une formation + /// + /// + /// public FormationDTO UpdateFormation(FormationDTO formationDTO) { Formation formation = epContext.Formation.FirstOrDefault(formation => formation.Id == formationDTO.Id); @@ -253,11 +425,23 @@ namespace EPAServeur.Services } formation = SetFormation(formation, formationDTO); - epContext.SaveChanges(); + try + { + epContext.SaveChanges(); + } + catch (Exception ex) + { + throw; + } return GetFormationDTO(formation); } + /// + /// Supprimer une formation + /// + /// + /// public bool DeleteFormationById(decimal? id) { int idFormation = 0; @@ -276,13 +460,30 @@ namespace EPAServeur.Services if (formation == null) return false; - epContext.Remove(formation); - epContext.SaveChanges(); + + try + { + epContext.SaveChanges(); + } + catch (Exception) + { + throw; + } + return true; } + #endregion + + #region Méthodes Privée + #region Object to DTO + /// + /// Récupère un objet FormationDTO en fonction d'un objet Formation + /// + /// + /// private FormationDTO GetFormationDTO(Formation formation) { FormationDTO formationDTO = new FormationDTO() @@ -302,21 +503,14 @@ namespace EPAServeur.Services 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; } + /// + /// Récupère un objet OrigineFormationDTO en fonction d'un objet OrigineFormation + /// + /// + /// private OrigineFormationDTO GetOrigineFormationDTO(OrigineFormation origineFormation) { if (origineFormation == null) @@ -329,6 +523,11 @@ namespace EPAServeur.Services return origineFormationDTO; } + /// + /// Récupère un objet StatutFormationDTO en fonction d'un objet StatutFormation + /// + /// + /// private StatutFormationDTO GetStatutFormationDTO(StatutFormation statutFormation) { if (statutFormation == null) @@ -341,6 +540,11 @@ namespace EPAServeur.Services return statutFormationDTO; } + /// + /// Récupère un objet ModeFormationDTO en fonction d'un objet ModeFormation + /// + /// + /// private ModeFormationDTO GetModeFormationDTO(ModeFormation modeFormation) { if (modeFormation == null) @@ -352,6 +556,11 @@ namespace EPAServeur.Services }; return modeFormationDTO; } + /// + /// Récupère un objet TypeFormationDTO en fonction d'un objet TypeFormation + /// + /// + /// private TypeFormationDTO GetTypeFormationDTO(TypeFormation typeFormation) { if (typeFormation == null) @@ -367,6 +576,13 @@ namespace EPAServeur.Services #endregion #region DTO to Object + + /// + /// Modifie un objet Formation en fonction d'un objet FormationDTO + /// + /// + /// + /// private Formation SetFormation(Formation formation, FormationDTO formationDTO) { formation.Intitule = formationDTO.Intitule; @@ -382,23 +598,14 @@ namespace EPAServeur.Services 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; } - + /// + /// Récupère un objet OrigineFormation en fonction d'un objet OrigineFormationDTO + /// + /// + /// private OrigineFormation GetOrigineFormation(OrigineFormationDTO origineFormationDTO) { if (origineFormationDTO == null) @@ -411,6 +618,11 @@ namespace EPAServeur.Services return origineFormation; } + /// + /// Récupère un objet StatutFormation en fonction d'un objet StatutFormationDTO + /// + /// + /// private StatutFormation GetStatutFormation(StatutFormationDTO statutFormationDTO) { if (statutFormationDTO == null) @@ -423,6 +635,11 @@ namespace EPAServeur.Services return statutFormation; } + /// + /// Récupère un objet ModeFormation en fonction d'un objet ModeFormationDTO + /// + /// + /// private ModeFormation GetModeFormation(ModeFormationDTO modeFormationDTO) { if (modeFormationDTO == null) @@ -434,6 +651,12 @@ namespace EPAServeur.Services }; return modeFormation; } + + /// + /// Récupère un objet TypeFormation en fonction d'un objet TypeFormationDTO + /// + /// + /// private TypeFormation GetTypeFormation(TypeFormationDTO typeFormationDTO) { if (typeFormationDTO == null) @@ -448,5 +671,7 @@ namespace EPAServeur.Services #endregion + #endregion + } }