Début d'ajout de logger pour les notes

develop
Yanaël GRETTE 4 years ago
parent 64b4394994
commit 79df883367
  1. 40
      Controllers/NotesApi.cs

@ -21,6 +21,7 @@ using IO.Swagger.DTO;
using EPAServeur.Services; using EPAServeur.Services;
using EPAServeur.IServices; using EPAServeur.IServices;
using EPAServeur.Models.Notes; using EPAServeur.Models.Notes;
using Microsoft.Extensions.Logging;
namespace IO.Swagger.Controllers namespace IO.Swagger.Controllers
{ {
@ -31,10 +32,12 @@ namespace IO.Swagger.Controllers
public class NotesApiController : ControllerBase public class NotesApiController : ControllerBase
{ {
private readonly INoteService noteService; private readonly INoteService noteService;
private readonly ILogger<NotesApiController> logger;
public NotesApiController(INoteService _noteService) public NotesApiController(INoteService _noteService, ILogger<NotesApiController> _logger)
{ {
noteService = _noteService; noteService = _noteService;
logger = _logger;
} }
/// <summary> /// <summary>
@ -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(..), ... //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)); // 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() ErreurDTO erreur = new ErreurDTO()
{ {
Code = "404", Code = "404",
@ -65,6 +70,7 @@ namespace IO.Swagger.Controllers
}; };
return NotFound(erreur); return NotFound(erreur);
}; };
logger.LogInformation("Note {idNote} supprimée avec sucès", idNote);
return NoContent(); 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(..), ... //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)); // return StatusCode(403, default(ErreurDTO));
logger.LogInformation("Récupération de la note {idNote}", idNote);
DetailsNoteDTO note = noteService.GetNoteById(idNote); DetailsNoteDTO note = noteService.GetNoteById(idNote);
if(note == null) if(note == null)
{ {
logger.LogError("Aucune note ne correspond à l'id {idNote} cherché", idNote);
ErreurDTO erreur = new ErreurDTO() ErreurDTO erreur = new ErreurDTO()
{ {
Code = "404", Code = "404",
@ -98,6 +106,7 @@ namespace IO.Swagger.Controllers
}; };
return NotFound(erreur); return NotFound(erreur);
} }
logger.LogInformation("Note d'id {idNote} trouvée",idNote);
return Ok(note); return Ok(note);
} }
@ -126,13 +135,7 @@ 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(..), ... //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)); // return StatusCode(403, default(ErreurDTO));
string exampleJson = null; throw new NotImplementedException();
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<List<AffichageNoteDTO>>(exampleJson)
: default(List<AffichageNoteDTO>); //TODO: Change the data returned
return new ObjectResult(example);
} }
/// <summary> /// <summary>
@ -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(..), ... //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)); // return StatusCode(403, default(ErreurDTO));
logger.LogInformation("Récupération des notes de l'auteur d'id {idReferent}", idReferent);
IEnumerable<AffichageNoteDTO> notes = noteService.GetNotesByAuteur(idReferent, asc, numPage, parPAge, texte, tri); IEnumerable<AffichageNoteDTO> notes = noteService.GetNotesByAuteur(idReferent, asc, numPage, parPAge, texte, tri);
if(notes == null) if(notes == null)
{ {
logger.LogError("Le référent {idReferent} n'a pas été trouvé", idReferent);
ErreurDTO erreur = new ErreurDTO() ErreurDTO erreur = new ErreurDTO()
{ {
Code = "404", Code = "404",
@ -170,6 +175,7 @@ namespace IO.Swagger.Controllers
}; };
return NotFound(erreur); return NotFound(erreur);
} }
logger.LogInformation("Liste des notes de l'auteur {idReferent} trouvée", idReferent);
return Ok(notes); 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(..), ... //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)); // return StatusCode(403, default(ErreurDTO));
logger.LogInformation("Récupération des notes de l'auteur {idReferent} sur un collaborateur {idCollaborateur}", idReferent, idCollaborateur);
IEnumerable<AffichageNoteDTO> notes = noteService.GetNotesByCollaborateur(idReferent, idCollaborateur, asc, numPage, parPAge, texte, tri); IEnumerable<AffichageNoteDTO> notes = noteService.GetNotesByCollaborateur(idReferent, idCollaborateur, asc, numPage, parPAge, texte, tri);
if (notes == null) if (notes == null)
{ {
logger.LogInformation("auteur {idReferent} ou collaborateur {idCollaborateur} introuvable", idReferent, idCollaborateur);
ErreurDTO erreur = new ErreurDTO() ErreurDTO erreur = new ErreurDTO()
{ {
Code = "404", Code = "404",
@ -209,6 +217,7 @@ namespace IO.Swagger.Controllers
}; };
return NotFound(erreur); return NotFound(erreur);
} }
logger.LogInformation("Liste des notes de l'auteur {idReferent} sur un collaborateur {idCollaborateur} trouvée", idReferent, idCollaborateur);
return Ok(notes); 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(..), ... //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)); // return StatusCode(403, default(ErreurDTO));
logger.LogInformation("Ajout d'une nouvelle note");
Note nouvelleNote = noteService.AjouterNote(body); Note nouvelleNote = noteService.AjouterNote(body);
logger.LogInformation("Nouvelle note ajoutée");
return Created("",nouvelleNote); return Created("",nouvelleNote);
} }
@ -248,14 +259,19 @@ namespace IO.Swagger.Controllers
[ValidateModelState] [ValidateModelState]
[SwaggerOperation("UpdateNote")] [SwaggerOperation("UpdateNote")]
[SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] [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(..), ... //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)); // return StatusCode(403, default(ErreurDTO));
logger.LogInformation("Mise à jour de la note d'id {idNote}", idNote);
Note note = noteService.UpdateNote(idNote, body); Note note = noteService.UpdateNote(idNote, body);
if (note == null) if (note == null) {
logger.LogInformation("");
note = noteService.AjouterNote(body); note = noteService.AjouterNote(body);
logger.LogInformation("Ajout effectuer avec succès");
}
else
logger.LogInformation("Update effectué avec succès");
return Ok(note); return Ok(note);
//TODO: Uncomment the next line to return response 201 or use other options such as return this.NotFound(), return this.BadRequest(..), ... //TODO: Uncomment the next line to return response 201 or use other options such as return this.NotFound(), return this.BadRequest(..), ...
// return StatusCode(201); // return StatusCode(201);

Loading…
Cancel
Save