diff --git a/EPAServeur/Controllers/NotesApi.cs b/EPAServeur/Controllers/NotesApi.cs index 97aef7c..b9b91cf 100644 --- a/EPAServeur/Controllers/NotesApi.cs +++ b/EPAServeur/Controllers/NotesApi.cs @@ -23,6 +23,7 @@ using Microsoft.Extensions.Logging; using IO.Swagger.ClientCollaborateur; using EPAServeur.Exceptions; using Microsoft.EntityFrameworkCore; +using System.Threading.Tasks; namespace IO.Swagger.Controllers { @@ -70,11 +71,11 @@ namespace IO.Swagger.Controllers [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "L’utilisateur souhaitant accéder à la ressource n’a pas les droits d’accès suffisants")] [SwaggerResponse(statusCode: 415, type: typeof(ErreurDTO), description: "L’opération ne peut pas être effectuée car certaines données sont manquantes")] [SwaggerResponse(statusCode: 500, type: typeof(ErreurDTO), description: "Une erreur est survenue sur le serveur")] - public virtual IActionResult AddNote([FromBody]DetailsNoteDTO body) + public virtual async Task AddNote([FromBody]DetailsNoteDTO body) { try { - body = noteService.AjouterNote(body); + body = await noteService.AjouterNoteAsync(body); } catch(ApiException e) { @@ -183,7 +184,7 @@ namespace IO.Swagger.Controllers { try { - noteService.SupprimerNote(idNote); + noteService.SupprimerNoteAsync(idNote); } catch(DbUpdateException e) { @@ -220,8 +221,6 @@ namespace IO.Swagger.Controllers //TODO: Uncomment the next line to return response 500 or use other options such as return this.NotFound(), return this.BadRequest(..), ... // return StatusCode(500, default(ErreurDTO)); - - throw new NotImplementedException(); } /// @@ -236,7 +235,7 @@ namespace IO.Swagger.Controllers /// Une erreur est survenue sur le serveur [HttpGet] [Route("/api/notes/{idNote}")] - [Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] + //[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] [ValidateModelState] [SwaggerOperation("GetNoteById")] [SwaggerResponse(statusCode: 200, type: typeof(DetailsNoteDTO), description: "OK")] @@ -244,8 +243,51 @@ namespace IO.Swagger.Controllers [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "L’utilisateur souhaitant accéder à la ressource n’a pas les droits d’accès suffisants")] [SwaggerResponse(statusCode: 404, type: typeof(ErreurDTO), description: "La ressource n'a pas été trouvée")] [SwaggerResponse(statusCode: 500, type: typeof(ErreurDTO), description: "Une erreur est survenue sur le serveur")] - public virtual IActionResult GetNoteById([FromRoute][Required]long? idNote) - { + public virtual async Task GetNoteById([FromRoute][Required]long? idNote) + { + DetailsNoteDTO note; + try + { + note = await noteService.GetNoteByIdAsync(idNote); + } + catch(ReferentNotFoundException e) + { + ErreurDTO erreur = new ErreurDTO() + { + Code = 404, + Message = e.Message, + }; + return NotFound(erreur); + } + catch (CollaborateurNotFoundException e) + { + ErreurDTO erreur = new ErreurDTO() + { + Code = 404, + Message = e.Message, + }; + return NotFound(erreur); + } + catch (NoteNotFoundException e) + { + ErreurDTO erreur = new ErreurDTO() + { + Code = 404, + Message = e.Message, + }; + return NotFound(erreur); + } + catch(Exception e) + { + logger.LogError(e.Message); + ErreurDTO erreur = new ErreurDTO() + { + Code = 500, + Message = "Une erreur inconnue est survenue", + }; + return StatusCode(500, erreur); + } + return Ok(note); //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(DetailsNoteDTO)); @@ -260,13 +302,6 @@ namespace IO.Swagger.Controllers //TODO: Uncomment the next line to return response 500 or use other options such as return this.NotFound(), return this.BadRequest(..), ... // return StatusCode(500, default(ErreurDTO)); - string exampleJson = null; - exampleJson = "{\n \"idAuteur\" : \"046b6c7f-0b8a-43b9-b35d-6489e6daee91\",\n \"dateCreation\" : \"2000-01-23T04:56:07.000+00:00\",\n \"dateMiseAjour\" : \"2000-01-23T04:56:07.000+00:00\",\n \"titre\" : \"titre\",\n \"texte\" : \"texte\",\n \"id\" : 0,\n \"collaborateur\" : {\n \"businessUnit\" : {\n \"agence\" : {\n \"bu\" : [ null, null ],\n \"id\" : 6,\n \"nom\" : \"nom\"\n },\n \"id\" : 0,\n \"nom\" : \"nom\"\n },\n \"mailApside\" : \"\",\n \"dateArrivee\" : \"2000-01-23T04:56:07.000+00:00\",\n \"id\" : \"046b6c7f-0b8a-43b9-b35d-6489e6daee91\",\n \"dateDepart\" : \"2000-01-23T04:56:07.000+00:00\",\n \"nom\" : \"nom\",\n \"prenom\" : \"prenom\"\n }\n}"; - - var example = exampleJson != null - ? JsonConvert.DeserializeObject(exampleJson) - : default(DetailsNoteDTO); //TODO: Change the data returned - return new ObjectResult(example); } /// @@ -285,7 +320,7 @@ namespace IO.Swagger.Controllers /// La ressource n'a pas été trouvée /// Une erreur est survenue sur le serveur [HttpGet] - [Route("/api/notes/{idAuteur}")] + [Route("/api/notes/auteur/{idAuteur}")] [Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] [ValidateModelState] [SwaggerOperation("GetNotesAuteur")] diff --git a/EPAServeur/IServices/INoteService.cs b/EPAServeur/IServices/INoteService.cs index 37324b3..2326e69 100644 --- a/EPAServeur/IServices/INoteService.cs +++ b/EPAServeur/IServices/INoteService.cs @@ -23,6 +23,8 @@ namespace EPAServeur.IServices Task AjouterNoteAsync(DetailsNoteDTO note); void SupprimerNote(long? idNote); void SupprimerNoteAsync(long? idNote); + DetailsNoteDTO GetNoteById(long? idNote); + Task GetNoteByIdAsync(long? idNote); diff --git a/EPAServeur/Services/NoteService.cs b/EPAServeur/Services/NoteService.cs index cb8fd2a..2ce9ca7 100644 --- a/EPAServeur/Services/NoteService.cs +++ b/EPAServeur/Services/NoteService.cs @@ -19,12 +19,7 @@ namespace EPAServeur.Services public class NoteService : INoteService { #region Variables - /* - /// - /// Service collaborateur pour récupérer les données collaborateurs - /// - private readonly ICollaborateurService collaborateurService; - */ + private readonly ICollaborateurApi collaborateurApi; @@ -32,11 +27,16 @@ namespace EPAServeur.Services /// Contexte pour interagir avec la base de données MySQL du serveur EP /// private readonly EpContext context; + + /// + /// Service collaborateur pour récupérer les données collaborateurs au format DTO + /// + private readonly ICollaborateurService collaborateurService; #endregion #region Constructeurs - public NoteService(ICollaborateurApi _collaborateurApi, EpContext _context) + public NoteService(ICollaborateurApi _collaborateurApi, ICollaborateurService _collaborateurService, EpContext _context) { - //collaborateurService = _collaborateurService; + collaborateurService = _collaborateurService; collaborateurApi = _collaborateurApi; context = _context; } @@ -84,6 +84,28 @@ namespace EPAServeur.Services context.SaveChanges(); } } + + /// + /// Récupérer une note en fonction de son id + /// + /// Id de la note à récupérer + /// L'objet DTO de la note correspondant à l'id passé en paramètre + public DetailsNoteDTO GetNoteById(long? idNote) + { + + Note note = context.Note.Find(idNote); + + //vérifier l'existance de la note + if (note == null) + throw new NoteNotFoundException(); + //vérifier l'existence de l'auteur + Collaborateur auteur = collaborateurApi.ChercherCollabId(note.IdAuteur); + if (auteur == null) + throw new ReferentNotFoundException("L'auteur de la note n'existe pas"); + + return NoteToDetailSDTO(note); + } + #endregion #region Services Async @@ -128,9 +150,30 @@ namespace EPAServeur.Services await context.SaveChangesAsync(); } } - #endregion + + + /// + /// Récupérer une note en fonction de son id de manière async + /// + /// Id de la note à récupérer + /// L'objet DTO de la note correspondant à l'id passé en paramètre + public async Task GetNoteByIdAsync(long? idNote) + { + Note note = await context.Note.FindAsync(idNote); + + //vérifier l'existance de la note + if (note == null) + throw new NoteNotFoundException(); + //vérifier l'existence de l'auteur + Collaborateur auteur = await collaborateurApi.ChercherCollabIdAsync(note.IdAuteur); + if (auteur == null) + throw new ReferentNotFoundException("L'auteur de la note n'existe pas"); + return await NoteToDetailSDTOAsync(note); + } + + #endregion /* #region Services @@ -358,6 +401,32 @@ namespace EPAServeur.Services /// Note transformer en DetailsNoteDTO private DetailsNoteDTO NoteToDetailSDTO(Note note) { + CollaborateurDTO collaborateur = collaborateurService.GetCollaborateurById(note.IdCollaborateur); + if (collaborateur == null) + throw new CollaborateurNotFoundException("Il est impossible de récupérer une note donc le collaborateur n'existe plus"); + DetailsNoteDTO details = new DetailsNoteDTO() + { + Id = note.IdNote, + DateCreation = note.DateCreation, + DateMiseAjour = note.DateMiseAJour, + Titre = note.Titre, + Texte = note.Texte, + IdAuteur = note.IdAuteur, + Collaborateur = collaborateur, + }; + return details; + } + + /// + /// Transformatino d'une note en DetailsNoteDTO de manière asynchrone + /// + /// Note à transformer + /// Note transformer en DetailsNoteDTO + private async Task NoteToDetailSDTOAsync(Note note) + { + CollaborateurDTO collaborateur = await collaborateurService.GetCollaborateurByIdAsync(note.IdCollaborateur); + if (collaborateur == null) + throw new CollaborateurNotFoundException("Il est impossible de récupérer une note donc le collaborateur n'existe plus"); DetailsNoteDTO details = new DetailsNoteDTO() { Id = note.IdNote, @@ -366,7 +435,7 @@ namespace EPAServeur.Services Titre = note.Titre, Texte = note.Texte, IdAuteur = note.IdAuteur, - //Collaborateur = collaborateurService.GetCollaborateurById(note.IdCollaborateur) + Collaborateur = collaborateur, }; return details; }