From 79df883367c4679e9d3615c0c137da4a38279aa1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yana=C3=ABl=20GRETTE?= Date: Mon, 14 Sep 2020 16:16:53 +0200 Subject: [PATCH] =?UTF-8?q?D=C3=A9but=20d'ajout=20de=20logger=20pour=20les?= =?UTF-8?q?=20notes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Controllers/NotesApi.cs | 48 +++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/Controllers/NotesApi.cs b/Controllers/NotesApi.cs index 1422250..c6d5379 100644 --- a/Controllers/NotesApi.cs +++ b/Controllers/NotesApi.cs @@ -21,6 +21,7 @@ using IO.Swagger.DTO; using EPAServeur.Services; using EPAServeur.IServices; using EPAServeur.Models.Notes; +using Microsoft.Extensions.Logging; namespace IO.Swagger.Controllers { @@ -31,10 +32,12 @@ namespace IO.Swagger.Controllers public class NotesApiController : ControllerBase { private readonly INoteService noteService; + private readonly ILogger logger; - public NotesApiController(INoteService _noteService) + public NotesApiController(INoteService _noteService, ILogger _logger) { noteService = _noteService; + logger = _logger; } /// @@ -56,8 +59,10 @@ 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)); - if(!noteService.SupprimerNote(idNote)) + logger.LogInformation("Suppresion de la note {idNote}", idNote); + if (!noteService.SupprimerNote(idNote)) { + logger.LogError("Impossible de supprimer la note d'id {idNote} car elle n'existe pas", idNote); ErreurDTO erreur = new ErreurDTO() { Code = "404", @@ -65,6 +70,7 @@ namespace IO.Swagger.Controllers }; return NotFound(erreur); }; + logger.LogInformation("Note {idNote} supprimée avec sucès", idNote); return NoContent(); } @@ -88,9 +94,11 @@ 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)); + logger.LogInformation("Récupération de la note {idNote}", idNote); DetailsNoteDTO note = noteService.GetNoteById(idNote); if(note == null) { + logger.LogError("Aucune note ne correspond à l'id {idNote} cherché", idNote); ErreurDTO erreur = new ErreurDTO() { Code = "404", @@ -98,6 +106,7 @@ namespace IO.Swagger.Controllers }; return NotFound(erreur); } + logger.LogInformation("Note d'id {idNote} trouvée",idNote); return Ok(note); } @@ -121,18 +130,12 @@ namespace IO.Swagger.Controllers [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] public virtual IActionResult GetNotes([FromQuery][Required()]bool? asc, [FromQuery][Required()]int? numPage, [FromQuery][Required()]int? parPAge, [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 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 \"idCollaborateur\" : \"046b6c7f-0b8a-43b9-b35d-6489e6daee91\",\n \"dateMiseAjour\" : \"2000-01-23T04:56:07.000+00:00\",\n \"titre\" : \"titre\",\n \"id\" : 0,\n \"collaborateur\" : \"collaborateur\"\n}, {\n \"idCollaborateur\" : \"046b6c7f-0b8a-43b9-b35d-6489e6daee91\",\n \"dateMiseAjour\" : \"2000-01-23T04:56:07.000+00:00\",\n \"titre\" : \"titre\",\n \"id\" : 0,\n \"collaborateur\" : \"collaborateur\"\n} ]"; - - var example = exampleJson != null - ? JsonConvert.DeserializeObject>(exampleJson) - : default(List); //TODO: Change the data returned - return new ObjectResult(example); + //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(); } /// @@ -160,9 +163,11 @@ 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)); + logger.LogInformation("Récupération des notes de l'auteur d'id {idReferent}", idReferent); IEnumerable notes = noteService.GetNotesByAuteur(idReferent, asc, numPage, parPAge, texte, tri); if(notes == null) { + logger.LogError("Le référent {idReferent} n'a pas été trouvé", idReferent); ErreurDTO erreur = new ErreurDTO() { Code = "404", @@ -170,6 +175,7 @@ namespace IO.Swagger.Controllers }; return NotFound(erreur); } + logger.LogInformation("Liste des notes de l'auteur {idReferent} trouvée", idReferent); return Ok(notes); } @@ -199,9 +205,11 @@ 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)); + logger.LogInformation("Récupération des notes de l'auteur {idReferent} sur un collaborateur {idCollaborateur}", idReferent, idCollaborateur); IEnumerable notes = noteService.GetNotesByCollaborateur(idReferent, idCollaborateur, asc, numPage, parPAge, texte, tri); if (notes == null) { + logger.LogInformation("auteur {idReferent} ou collaborateur {idCollaborateur} introuvable", idReferent, idCollaborateur); ErreurDTO erreur = new ErreurDTO() { Code = "404", @@ -209,6 +217,7 @@ namespace IO.Swagger.Controllers }; return NotFound(erreur); } + logger.LogInformation("Liste des notes de l'auteur {idReferent} sur un collaborateur {idCollaborateur} trouvée", idReferent, idCollaborateur); return Ok(notes); } @@ -229,7 +238,9 @@ 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)); + logger.LogInformation("Ajout d'une nouvelle note"); Note nouvelleNote = noteService.AjouterNote(body); + logger.LogInformation("Nouvelle note ajoutée"); return Created("",nouvelleNote); } @@ -248,14 +259,19 @@ namespace IO.Swagger.Controllers [ValidateModelState] [SwaggerOperation("UpdateNote")] [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] - public virtual IActionResult UpdateNote([FromBody]DetailsNoteDTO body, [FromRoute][Required]long? idNote) + public virtual IActionResult UpdateNote([FromBody] DetailsNoteDTO body, [FromRoute][Required] long? idNote) { //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)); - + logger.LogInformation("Mise à jour de la note d'id {idNote}", idNote); Note note = noteService.UpdateNote(idNote, body); - if (note == null) + if (note == null) { + logger.LogInformation(""); note = noteService.AjouterNote(body); + logger.LogInformation("Ajout effectuer avec succès"); + } + else + logger.LogInformation("Update effectué avec succès"); return Ok(note); //TODO: Uncomment the next line to return response 201 or use other options such as return this.NotFound(), return this.BadRequest(..), ... // return StatusCode(201);