Début d'ajout de logger pour les notes

develop
Yanaël GRETTE 4 years ago
parent 64b4394994
commit 79df883367
  1. 48
      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<NotesApiController> logger;
public NotesApiController(INoteService _noteService)
public NotesApiController(INoteService _noteService, ILogger<NotesApiController> _logger)
{
noteService = _noteService;
logger = _logger;
}
/// <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(..), ...
// 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<AffichageNoteDTO>));
//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<AffichageNoteDTO>));
//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<List<AffichageNoteDTO>>(exampleJson)
: default(List<AffichageNoteDTO>); //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();
}
/// <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(..), ...
// 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);
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<AffichageNoteDTO> 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);

Loading…
Cancel
Save