From 2f716d22dd400c70ccb15411dae5ef563aa8fe26 Mon Sep 17 00:00:00 2001 From: jboinembalome Date: Fri, 18 Sep 2020 10:03:05 +0200 Subject: [PATCH] =?UTF-8?q?Ajout=20des=20m=C3=A9thodes=20asynchrones?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Controllers/ReferentsApi.cs | 29 ++---- IServices/IReferentService.cs | 4 + Services/ReferentService.cs | 183 +++++++++++++++++++++++++++++++--- 3 files changed, 183 insertions(+), 33 deletions(-) diff --git a/Controllers/ReferentsApi.cs b/Controllers/ReferentsApi.cs index af8dc61..764b287 100644 --- a/Controllers/ReferentsApi.cs +++ b/Controllers/ReferentsApi.cs @@ -20,6 +20,7 @@ using System.Net; using Microsoft.AspNetCore.Authorization; using IO.Swagger.Security; using System.Linq; +using System.Threading.Tasks; namespace IO.Swagger.Controllers { @@ -52,9 +53,9 @@ namespace IO.Swagger.Controllers [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) + public virtual async Task GetReferentById([FromRoute][Required]Guid? idReferent) { - ReferentDTO referentDTO = referentService.GetReferentById(idReferent); + ReferentDTO referentDTO = await referentService.GetReferentByIdAsync(idReferent); if (referentDTO == null) { ErreurDTO erreurDTO = new ErreurDTO() @@ -89,19 +90,9 @@ namespace IO.Swagger.Controllers [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); + { + //IEnumerable referentDTOs = referentService.GetReferents(asc,numPage,parPAge,fonctions,idAgence,idBU,texte,tri); + return Ok(); } /// @@ -120,9 +111,9 @@ namespace IO.Swagger.Controllers [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 GetReferentActuelCollaborateur([FromRoute][Required] Guid? idCollaborateur) + public virtual async Task GetReferentActuelCollaborateur([FromRoute][Required] Guid? idCollaborateur) { - ReferentDTO referentDTO = referentService.GetReferentActuelCollaborateur(idCollaborateur); + ReferentDTO referentDTO = await referentService.GetReferentActuelCollaborateurAsync(idCollaborateur); if (referentDTO == null) { ErreurDTO erreurDTO = new ErreurDTO() @@ -156,9 +147,9 @@ 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 GetReferentsByCollaborateur([FromQuery][Required()] bool? asc, [FromRoute][Required] Guid? idCollaborateur, [FromQuery][Required()] int? numPage, [FromQuery][Required()] int? parPAge, [FromQuery] string texte, [FromQuery] string tri) + public virtual async Task 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); + IEnumerable referentDTO = await referentService.GetReferentsByCollaborateurAsync(asc,idCollaborateur,numPage,parPAge,texte,tri); if (referentDTO.Count() == 0) { ErreurDTO erreurDTO = new ErreurDTO() diff --git a/IServices/IReferentService.cs b/IServices/IReferentService.cs index f54ce36..8cb68cb 100644 --- a/IServices/IReferentService.cs +++ b/IServices/IReferentService.cs @@ -11,10 +11,14 @@ namespace EPAServeur.IServices public interface IReferentService { ReferentDTO GetReferentById(Guid? idReferent); + Task GetReferentByIdAsync(Guid? idReferent); ReferentDTO GetReferentActuelCollaborateur(Guid? idCollaborateur); + Task GetReferentActuelCollaborateurAsync(Guid? idCollaborateur); IEnumerable GetReferents(bool? asc, int? numPage, int? parPAge, List fonctions, long? idAgence, long? idBU, string texte, string tri); + Task> GetReferentsAsync(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); + Task> GetReferentsByCollaborateurAsync(bool? asc, Guid? idCollaborateur, int? numPage, int? parPAge, string texte, string tri); } } diff --git a/Services/ReferentService.cs b/Services/ReferentService.cs index b9563a9..765e9da 100644 --- a/Services/ReferentService.cs +++ b/Services/ReferentService.cs @@ -5,6 +5,7 @@ using IO.Swagger.ModelCollaborateur; using System; using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; namespace EPAServeur.Services { @@ -12,13 +13,14 @@ namespace EPAServeur.Services { #region Variables private readonly IReferentApi referentApi; - + private readonly ICollaborateurApi collaborateurApi; #endregion #region Contructeurs - public ReferentService(IReferentApi _referentApi) + public ReferentService(IReferentApi _referentApi, ICollaborateurApi _collaborateurApi) { referentApi = _referentApi; + collaborateurApi = _collaborateurApi; } #endregion @@ -39,6 +41,20 @@ namespace EPAServeur.Services return GetReferentDTO(referent); } + /// + /// Récupère un référent par son id de manière asynchrone + /// + /// + /// + public async Task GetReferentByIdAsync(Guid? idReferent) + { + Referent referent = await referentApi.ChercherRefIdAsync(idReferent); + if (referent == null) + return null; + + return GetReferentDTO(referent); + } + /// /// Récupère un référent en fonction d'un collaborateur /// @@ -54,19 +70,104 @@ namespace EPAServeur.Services return GetReferentDTO(referent); } - /// - /// Récupère la liste des référents pour une agence - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// + /// + /// Récupère un référent en fonction d'un collaborateur de manière asynchrone + /// + /// + /// + public async Task GetReferentActuelCollaborateurAsync(Guid? idCollaborateur) + { + Referent referent = await referentApi.ChercherRefActuelCollabIdAsync(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 collaborateurs; // A changer (Sera utilisé pour récupérer la liste des référents par fonction + IEnumerable referents = null; // A changer + IEnumerable referentDTOs = null; // A changer + //List ids = fonctions.Select(guid => (Guid?)Guid.Parse(guid)).ToList(); + + //collaborateurs = collaborateurApi.ChercherCollab(ids); + 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 une agence de manière asynchrone + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + + public async Task> GetReferentsAsync(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 @@ -120,6 +221,7 @@ namespace EPAServeur.Services return referentDTOs; } + /// /// Récupère la liste des référents pour un collaborateur /// @@ -152,6 +254,40 @@ namespace EPAServeur.Services return referentDTOs; } + + /// + /// Récupère la liste des référents pour un collaborateur de manère asynchrone + /// + /// + /// + /// + /// + /// + /// + /// + public async Task> GetReferentsByCollaborateurAsync(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 = await referentApi.ChercherRefCollabIdAsync(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 @@ -238,6 +374,25 @@ namespace EPAServeur.Services }; return referentDTO; } + + /// + /// Récupère un objet ReferentDTO en fonction d'un objet Collaborateur + /// + /// + /// + private ReferentDTO GetReferentDTO(Collaborateur collaborateur) + { + if (collaborateur == null) + return null; + ReferentDTO referentDTO = new ReferentDTO() + { + Id = collaborateur.Id, + Prenom = collaborateur.Prenom, + Nom = collaborateur.Nom, + MailApside = collaborateur.MailApside + }; + return referentDTO; + } #endregion #region DTO to Object