From c405b64fa2f414ae6149dee6e51471e66e926f89 Mon Sep 17 00:00:00 2001 From: baollier1 Date: Fri, 3 Mar 2023 15:22:59 +0100 Subject: [PATCH] log --- .../apiLOL/Controllers/ControllerChampions.cs | 30 ++++++++++++------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/Sources/apiLOL/Controllers/ControllerChampions.cs b/Sources/apiLOL/Controllers/ControllerChampions.cs index 043a628..80aad2b 100644 --- a/Sources/apiLOL/Controllers/ControllerChampions.cs +++ b/Sources/apiLOL/Controllers/ControllerChampions.cs @@ -26,19 +26,23 @@ namespace apiLOL.Controllers // GET: api/ [HttpGet] - public async Task Get([FromQuery]int index = 0, int count = 10) + public async Task Get([FromQuery]int index = 0, int count = 10, [FromQuery]string name = "") { //FromQuery permet de filtrer dans la collection de champions en fonction du nom - // Possible de faire une classe PageRequest pour gérer les paramètres index et count - _logger.LogInformation($"methode Get de ControllerChampions appelée"); //Context dans les logs + + _logger.LogInformation($"methode Get de ControllerChampions appelée index:{index}, count: {count} et name:{name}"); int nbChampions = await data.ChampionsMgr.GetNbItems(); _logger.LogInformation($"Nombre de champions : {nbChampions}"); - var champs = (await data.ChampionsMgr.GetItems(index, count)).Select(Model => Model.ToDTO()); + Task> champs; + if(name.Equals(name)) + champs = (Task>)(await data.ChampionsMgr.GetItems(index, count)).Select(Model => Model.ToDTO()); + else + champs = (Task>)(await data.ChampionsMgr.GetItemsByName(name, index, count)).Select(Model => Model.ToDTO()); var page = new ChampionPageDTO { - Data = champs, + Data = (IEnumerable)champs, Index = index, Count = count, TotalCount = nbChampions @@ -58,10 +62,11 @@ namespace apiLOL.Controllers var champs = (await data.ChampionsMgr.GetItemsByName(name, 0, 1)); return Ok(champs.First().ToDTO()); } - catch + catch(Exception ex) { + _logger.LogInformation($"erreur methode Get de ControllerChampions: {ex}"); return BadRequest("erreur de nom de champion"); - } //Attraper l'excpetion et la logger + } } @@ -79,8 +84,9 @@ namespace apiLOL.Controllers ChampionDTO dtoChamp = champion.ToDTO(); return CreatedAtAction(nameof(GetChampion), new { name = dtoChamp.Name }, dtoChamp); } - catch + catch (Exception ex) { + _logger.LogInformation($"erreur methode Post de ControllerChampions: {ex}"); return BadRequest("le champion existe deja"); } } @@ -97,8 +103,9 @@ namespace apiLOL.Controllers champs.Bio = bio; return Ok(champs.ToDTO()); } - catch + catch (Exception ex) { + _logger.LogInformation($"erreur methode Put de ControllerChampions: {ex}"); return BadRequest("erreur de nom de champion"); } } @@ -112,11 +119,12 @@ namespace apiLOL.Controllers try { var champ = (await data.ChampionsMgr.GetItemsByName(name, 0, 1)).First(); - data.ChampionsMgr.DeleteItem(champ); // await + await data.ChampionsMgr.DeleteItem(champ); return Ok(champ.ToDTO()); } - catch + catch (Exception ex) { + _logger.LogInformation($"erreur methode Delete de ControllerChampions: {ex}"); return BadRequest("erreur de nom de champion"); } }