From 991401d4870512afc63fed67bf7fdd54cc89a0e1 Mon Sep 17 00:00:00 2001 From: jboinembalome Date: Fri, 11 Sep 2020 09:17:45 +0200 Subject: [PATCH] =?UTF-8?q?Cr=C3=A9ation=20du=20service=20Referent=20+=20i?= =?UTF-8?q?mpl=C3=A9mentation=20des=20m=C3=A9thodes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Controllers/ReferentsApi.cs | 150 ++++++++++++-------- IServices/IReferentService.cs | 20 +++ Services/ReferentService.cs | 253 ++++++++++++++++++++++++++++++++++ Startup.cs | 1 + 4 files changed, 365 insertions(+), 59 deletions(-) create mode 100644 IServices/IReferentService.cs create mode 100644 Services/ReferentService.cs diff --git a/Controllers/ReferentsApi.cs b/Controllers/ReferentsApi.cs index f4adbb0..af8dc61 100644 --- a/Controllers/ReferentsApi.cs +++ b/Controllers/ReferentsApi.cs @@ -11,53 +11,90 @@ using System; using System.Collections.Generic; using Microsoft.AspNetCore.Mvc; using Swashbuckle.AspNetCore.Annotations; -using Swashbuckle.AspNetCore.SwaggerGen; using Newtonsoft.Json; using System.ComponentModel.DataAnnotations; using IO.Swagger.Attributes; -using IO.Swagger.Security; -using Microsoft.AspNetCore.Authorization; using IO.Swagger.DTO; +using EPAServeur.IServices; +using System.Net; +using Microsoft.AspNetCore.Authorization; +using IO.Swagger.Security; +using System.Linq; namespace IO.Swagger.Controllers -{ +{ /// /// /// [ApiController] public class ReferentsApiController : ControllerBase - { + { + private readonly IReferentService referentService; + + public ReferentsApiController(IReferentService _referentService) + { + referentService = _referentService; + } + /// /// /// - /// Récupérer la liste des référents d'un collaborateur + /// Récupérer un referent par son id + /// id referent + /// OK + /// Acces interdit + /// Ressource n'a pas été trouvée + [HttpGet] + [Route("/api/referents/{idReferent}")] + ////[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] + [ValidateModelState] + [SwaggerOperation("GetReferentById")] + [SwaggerResponse(statusCode: 200, type: typeof(ReferentDTO), description: "OK")] + [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] + [SwaggerResponse(statusCode: 404, type: typeof(ErreurDTO), description: "Ressource n'a pas été trouvée")] + public virtual IActionResult GetReferentById([FromRoute][Required]Guid? idReferent) + { + ReferentDTO referentDTO = referentService.GetReferentById(idReferent); + if (referentDTO == null) + { + ErreurDTO erreurDTO = new ErreurDTO() + { + Code = "404", + Message = "Le référent n'existe pas", + }; + return NotFound(erreurDTO); + } + return Ok(referentDTO); + } + + /// + /// + /// + /// Récupérer la liste de tous les referents /// Préciser si les données sont dans l'ordre (true) ou dans l'ordre inverse (false) - /// id collaborateur /// Numéro de la page du tableau qui affiche les données /// Nombre d'éléments affiché sur chaque page du tableau + /// Liste des fonctions des collaborateurs que l'on veut récupérer + /// id de l'agence à laquelle sont rattachées les données à récupérer + /// id de la business unit à 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 /// OK /// Acces interdit - /// Ressource n'a pas été trouvée [HttpGet] - [Route("/api/referents/collaborateur/{idCollaborateur}")] - [Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] + [Route("/api/referents")] + //[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] [ValidateModelState] - [SwaggerOperation("GetReferentByCollaborateur")] + [SwaggerOperation("GetReferents")] [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 GetReferentByCollaborateur([FromQuery][Required()]bool? asc, [FromRoute][Required]Guid? idCollaborateur, [FromQuery][Required()]int? numPage, [FromQuery][Required()]int? parPAge, [FromQuery]string texte, [FromQuery]string tri) + public virtual IActionResult GetReferents([FromQuery][Required()]bool? asc, [FromQuery][Required()]int? numPage, [FromQuery][Required()]int? parPAge, [FromQuery]List fonctions, [FromQuery]long? idAgence, [FromQuery]long? idBU, [FromQuery]string texte, [FromQuery]string tri) { //TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ... // return StatusCode(200, default(List)); //TODO: Uncomment the next line to return response 403 or use other options such as return this.NotFound(), return this.BadRequest(..), ... // return StatusCode(403, default(ErreurDTO)); - - //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 \"mailApside\" : \"\",\n \"id\" : \"046b6c7f-0b8a-43b9-b35d-6489e6daee91\",\n \"nom\" : \"nom\",\n \"prenom\" : \"prenom\",\n \"collaborateurs\" : [ null, null ]\n}, {\n \"mailApside\" : \"\",\n \"id\" : \"046b6c7f-0b8a-43b9-b35d-6489e6daee91\",\n \"nom\" : \"nom\",\n \"prenom\" : \"prenom\",\n \"collaborateurs\" : [ null, null ]\n} ]"; @@ -70,73 +107,68 @@ namespace IO.Swagger.Controllers /// /// /// - /// Récupérer un referent par son id - /// id referent + /// Récupérer le référent actuel d'un collaborateur + /// id collaborateur /// OK /// Acces interdit /// Ressource n'a pas été trouvée [HttpGet] - [Route("/api/referents/{idReferent}")] - [Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] + [Route("/api/referents/actuel/collaborateur/{idCollaborateur}")] + //[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] [ValidateModelState] - [SwaggerOperation("GetReferentById")] + [SwaggerOperation("GetReferentActuelCollaborateur")] [SwaggerResponse(statusCode: 200, type: typeof(ReferentDTO), description: "OK")] [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] [SwaggerResponse(statusCode: 404, type: typeof(ErreurDTO), description: "Ressource n'a pas été trouvée")] - public virtual IActionResult GetReferentById([FromRoute][Required]Guid? idReferent) - { - //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(ReferentDTO)); - - //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 \"mailApside\" : \"\",\n \"id\" : \"046b6c7f-0b8a-43b9-b35d-6489e6daee91\",\n \"nom\" : \"nom\",\n \"prenom\" : \"prenom\",\n \"collaborateurs\" : [ null, null ]\n}"; - - var example = exampleJson != null - ? JsonConvert.DeserializeObject(exampleJson) - : default(ReferentDTO); //TODO: Change the data returned - return new ObjectResult(example); + public virtual IActionResult GetReferentActuelCollaborateur([FromRoute][Required] Guid? idCollaborateur) + { + ReferentDTO referentDTO = referentService.GetReferentActuelCollaborateur(idCollaborateur); + if (referentDTO == null) + { + ErreurDTO erreurDTO = new ErreurDTO() + { + Code = "404", + Message = "Aucun référent pour le collaborateur", + }; + return NotFound(erreurDTO); + } + return Ok(referentDTO); } /// /// /// - /// Récupérer la liste de tous les referents + /// Récupérer la liste des référents d'un collaborateur /// Préciser si les données sont dans l'ordre (true) ou dans l'ordre inverse (false) + /// id collaborateur /// Numéro de la page du tableau qui affiche les données /// Nombre d'éléments affiché sur chaque page du tableau - /// Liste des fonctions des collaborateurs que l'on veut récupérer - /// id de l'agence à laquelle sont rattachées les données à récupérer - /// id de la business unit à 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 /// OK /// Acces interdit + /// Ressource n'a pas été trouvée [HttpGet] - [Route("/api/referents")] - [Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] + [Route("/api/referents/collaborateur/{idCollaborateur}")] + //[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] [ValidateModelState] - [SwaggerOperation("GetReferents")] + [SwaggerOperation("GetReferentByCollaborateur")] [SwaggerResponse(statusCode: 200, type: typeof(List), description: "OK")] [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] - public virtual IActionResult GetReferents([FromQuery][Required()]bool? asc, [FromQuery][Required()]int? numPage, [FromQuery][Required()]int? parPAge, [FromQuery]List fonctions, [FromQuery]long? idAgence, [FromQuery]long? idBU, [FromQuery]string texte, [FromQuery]string tri) - { - //TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - // return StatusCode(200, default(List)); - - //TODO: Uncomment the next line to return response 403 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - // return StatusCode(403, default(ErreurDTO)); - string exampleJson = null; - exampleJson = "[ {\n \"mailApside\" : \"\",\n \"id\" : \"046b6c7f-0b8a-43b9-b35d-6489e6daee91\",\n \"nom\" : \"nom\",\n \"prenom\" : \"prenom\",\n \"collaborateurs\" : [ null, null ]\n}, {\n \"mailApside\" : \"\",\n \"id\" : \"046b6c7f-0b8a-43b9-b35d-6489e6daee91\",\n \"nom\" : \"nom\",\n \"prenom\" : \"prenom\",\n \"collaborateurs\" : [ null, null ]\n} ]"; - - var example = exampleJson != null - ? JsonConvert.DeserializeObject>(exampleJson) - : default(List); //TODO: Change the data returned - return new ObjectResult(example); + [SwaggerResponse(statusCode: 404, type: typeof(ErreurDTO), description: "Ressource n'a pas été trouvée")] + public virtual IActionResult GetReferentsByCollaborateur([FromQuery][Required()] bool? asc, [FromRoute][Required] Guid? idCollaborateur, [FromQuery][Required()] int? numPage, [FromQuery][Required()] int? parPAge, [FromQuery] string texte, [FromQuery] string tri) + { + IEnumerable referentDTO = referentService.GetReferentsByCollaborateur(asc,idCollaborateur,numPage,parPAge,texte,tri); + if (referentDTO.Count() == 0) + { + ErreurDTO erreurDTO = new ErreurDTO() + { + Code = "404", + Message = "Aucun référent pour le collaborateur", + }; + return NotFound(erreurDTO); + } + return Ok(referentDTO); } } } diff --git a/IServices/IReferentService.cs b/IServices/IReferentService.cs new file mode 100644 index 0000000..fe24257 --- /dev/null +++ b/IServices/IReferentService.cs @@ -0,0 +1,20 @@ +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 IReferentService + { + ReferentDTO GetReferentById(Guid? id); + ReferentDTO GetReferentActuelCollaborateur(Guid? idCollaborateur); + IEnumerable GetReferents(bool? asc, int? numPage, int? parPAge, List fonctions, long? idAgence, long? idBU, string texte, string tri); + + IEnumerable GetReferentsByCollaborateur(bool? asc, Guid? idCollaborateur, int? numPage, int? parPAge, string texte, string tri); + + } +} diff --git a/Services/ReferentService.cs b/Services/ReferentService.cs new file mode 100644 index 0000000..68b9892 --- /dev/null +++ b/Services/ReferentService.cs @@ -0,0 +1,253 @@ +using EPAServeur.IServices; +using IO.Swagger.ApiCollaborateur; +using IO.Swagger.DTO; +using IO.Swagger.ModelCollaborateur; +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EPAServeur.Services +{ + public class ReferentService : IReferentService + { + #region Variables + private readonly IReferentApi referentApi; + + #endregion + + #region Contructeurs + public ReferentService(IReferentApi _referentApi) + { + referentApi = _referentApi; + } + + #endregion + + #region Méthodes Service + + /// + /// Récupère un référent par son id + /// + /// + /// + public ReferentDTO GetReferentById(Guid? id) + { + Referent referent = referentApi.ChercherRefId(id); + if (referent == null) + return null; + + return GetReferentDTO(referent); + } + + /// + /// Récupère un référent en fonction d'un collaborateur + /// + /// + /// + public ReferentDTO GetReferentActuelCollaborateur(Guid? idCollaborateur) + { + Referent referent = referentApi.ChercherRefActuelCollabId(idCollaborateur); + + if (referent == null) + return null; + + return GetReferentDTO(referent); + } + + /// + /// Récupère la liste des référents pour une agence + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + public IEnumerable GetReferents(bool? asc, int? numPage, int? parPAge, List fonctions, long? idAgence, long? idBU, string texte, string tri) + { + IEnumerable referents = null ; // A changer + IEnumerable referentDTOs = null; // A changer + + if (texte == null) + texte = ""; + else + texte = texte.ToLower(); + + int skip = (numPage.Value - 1) * parPAge.Value; + int take = parPAge.Value; + + if (idBU != null) + { + + } + else + { + + } + + if (idAgence != null) + { + try + { + + } + catch (Exception ex) + { + throw; + } + } + else + { + try + { + + } + catch (Exception ex) + { + throw; + } + } + + + if (referents == null) + return new List(); + + referentDTOs = referents.Where(referent => (referent.Nom + " " + referent.Prenom).ToLower().Contains(texte) || (referent.Prenom + " " + referent.Nom).ToLower().Contains(texte)).Select(referent => GetReferentDTO(referent)); + + return referentDTOs; + } + + /// + /// Récupère la liste des référents pour un collaborateur + /// + /// + /// + /// + /// + /// + /// + /// + public IEnumerable GetReferentsByCollaborateur(bool? asc, Guid? idCollaborateur, int? numPage, int? parPAge, string texte, string tri) + { + IEnumerable referents; + IEnumerable referentDTOs; + + if (texte == null) + texte = ""; + else + texte = texte.ToLower(); + + int skip = (numPage.Value - 1) * parPAge.Value; + int take = parPAge.Value; + + if (idCollaborateur == null) + return new List(); + + + referents = referentApi.ChercherRefCollabId(idCollaborateur); + referentDTOs = referents.Where(referent => (referent.Nom + " " + referent.Prenom).ToLower().Contains(texte) || (referent.Prenom + " " + referent.Nom).ToLower().Contains(texte)).Select(referent => GetReferentDTO(referent)); + + return referentDTOs; + } + #endregion + + #region Méthodes Privées + + #region Object to DTO + + /// + /// Récupère un objet AgenceDTO en fonction d'un objet Agence + /// + /// + /// + private AgenceDTO GetAgenceDTO(Agence agence) + { + if (agence == null) + return null; + AgenceDTO agenceDTO = new AgenceDTO() + { + Id = agence.Id, + Nom = agence.Nom, + Bu = new List() + }; + agenceDTO.Bu = agence.Bus.Select(bu => new BusinessUnitDTO() + { + Id = bu.Id, + Nom = bu.Nom + }).ToList(); + return agenceDTO; + } + + /// + /// Récupère un objet BusinessUnitDTO en fonction d'un objet BU + /// + /// + /// + private BusinessUnitDTO GetBusinessUnitDTO(BU businessUnit) + { + if (businessUnit == null) + return null; + BusinessUnitDTO businessUnitDTO = new BusinessUnitDTO() + { + Id = businessUnit.Id, + Nom = businessUnit.Nom, + Agence = GetAgenceDTO(businessUnit.Agence) + }; + return businessUnitDTO; + } + + /// + /// Récupère un objet CollaborateurDTO en fonction d'un objet Collaborateur + /// + /// + /// + private CollaborateurDTO GetCollaborateurDTO(Collaborateur collaborateur) + { + CollaborateurDTO collaborateurDTO = new CollaborateurDTO() + { + Id = collaborateur.Id, + Prenom = collaborateur.Prenom, + Nom = collaborateur.Nom, + MailApside = collaborateur.MailApside, + DateArrivee = collaborateur.DateArrivee, + + }; + collaborateurDTO.BusinessUnit = GetBusinessUnitDTO(collaborateur.BusinessUnit); + collaborateurDTO.Referent = GetReferentDTO(collaborateur.Referent); + return collaborateurDTO; + } + + /// + /// Récupère un objet ReferentDTO en fonction d'un objet Referent + /// + /// + /// + private ReferentDTO GetReferentDTO(Referent referent) + { + if (referent == null) + return null; + ReferentDTO referentDTO = new ReferentDTO() + { + Id = referent.Id, + Prenom = referent.Prenom, + Nom = referent.Nom, + MailApside = referent.MailApside + }; + return referentDTO; + } + #endregion + + #region DTO to Object + + #endregion + + #endregion + + + + + } +} diff --git a/Startup.cs b/Startup.cs index 7290f43..28e0eea 100644 --- a/Startup.cs +++ b/Startup.cs @@ -58,6 +58,7 @@ namespace EPAServeur services.AddScoped(); services.AddScoped(); services.AddScoped(); + services.AddScoped(); }