diff --git a/.gitignore b/.gitignore index 5913f3d..1132ce9 100644 --- a/.gitignore +++ b/.gitignore @@ -428,5 +428,7 @@ FodyWeavers.xsd *.sln.iml *.db +*.db-shm +*.db-wal *Migrations *.sonarqube \ No newline at end of file diff --git a/Sources/TestUnitaire/TestAPILol.cs b/Sources/TestUnitaire/TestAPILol.cs index 43a6d08..e819972 100644 --- a/Sources/TestUnitaire/TestAPILol.cs +++ b/Sources/TestUnitaire/TestAPILol.cs @@ -1,37 +1,59 @@ using apiLOL; using apiLOL.Controllers; using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; using StubLib; namespace TestUnitaire { public class TestAPILol { - [Fact] - public void Test1() + [Theory] + [InlineData("Beatrice", "sdfsdfd")] + [InlineData("Maurice", "Ahri est un champion de League of Legends")] + [InlineData("Loupiotte", "Akali est un champion de League of Legends")] + public async Task TestPostChampion(string name, string bio) { + // Arrange + var data = new StubData(); + var logger = new NullLogger(); + var controller = new ControllerChampions(data, logger); + var champDTO = new ChampionDTO(name, bio); + + // Act + var nbInListBefore = data.ChampionsMgr.GetNbItems().Result; + var result = await controller.Post(champDTO); + var nbInListAfter = data.ChampionsMgr.GetNbItems().Result; + // Assert + // IS the champion added to the list, number of champions in the list + 1 + Assert.Equal(nbInListBefore + 1, nbInListAfter); } - [Fact] - public void TestPostChampion() + + [Theory] + [InlineData("Beatrice", "Aatrox est un champion de League of Legends")] + [InlineData("Maurice", "Ahri est un champion de League of Legends")] + [InlineData("Loupiotte", "Akali est un champion de League of Legends")] + public async Task TestGetChampion(string name, string bio) { // Arrange var data = new StubData(); - var controller = new ControllerChampions(new StubData()); - var champDTO = new ChampionDTO("Charles", "Charles est un champion de League of Legends"); + var logger = new NullLogger(); + var controller = new ControllerChampions(data, logger); + var champDTO = new ChampionDTO(name, bio); // Act - var result = controller.Post(champDTO); - data.ChampionsMgr.AddItem(champDTO.ToModel()); - var nbItem = data.ChampionsMgr.GetNbItems(); - Task nbItemTask = nbItem; + // Call method POST to add a champion + var result = await controller.Post(champDTO); + // Call method GET to get the champion + var resultGet = await controller.GetChampion(name); // Assert - Assert.IsType(result); - // Verify that the champions is added to the stub - Assert.Equal(7, nbItemTask.Result); - + // IS the champion added to the list, number of champions in the list + 1 + Assert.Equal(name, champDTO.Name); + Assert.Equal(bio, champDTO.Bio); } } } \ No newline at end of file diff --git a/Sources/apiLOL/ChampionDTO.cs b/Sources/apiLOL/ChampionDTO.cs index 253af0a..f015a14 100644 --- a/Sources/apiLOL/ChampionDTO.cs +++ b/Sources/apiLOL/ChampionDTO.cs @@ -11,6 +11,6 @@ public string Name { get; set; } - public string Bio { get; set; } + public string Bio { get; set; } } } diff --git a/Sources/apiLOL/ChampionMapper.cs b/Sources/apiLOL/ChampionMapper.cs index 564b52b..a7009ab 100644 --- a/Sources/apiLOL/ChampionMapper.cs +++ b/Sources/apiLOL/ChampionMapper.cs @@ -4,15 +4,15 @@ namespace apiLOL { public static class ChampionMapper { - public static ChampionDTO ToDTO(this Champion champion) - { - return new ChampionDTO(champion.Name, champion.Bio); - } + public static ChampionDTO ToDTO(this Champion champion) => new ChampionDTO(champion.Name, champion.Bio); + public static Champion ToModel(this ChampionDTO championDTO) { - return new Champion(championDTO.Name); - } + Champion champ = new Champion(championDTO.Name); + champ.Bio = championDTO.Bio; + return champ; + } } } diff --git a/Sources/apiLOL/ChampionPageDTO.cs b/Sources/apiLOL/ChampionPageDTO.cs new file mode 100644 index 0000000..5fff791 --- /dev/null +++ b/Sources/apiLOL/ChampionPageDTO.cs @@ -0,0 +1,13 @@ +namespace apiLOL +{ + public class ChampionPageDTO + { + public IEnumerable Data { get; set; } + + public int Index { get; set; } + + public int Count { get; set; } + + public int TotalCount { get; set; } + } +} diff --git a/Sources/apiLOL/Controllers/ControllerChampions.cs b/Sources/apiLOL/Controllers/ControllerChampions.cs index 00e2396..18bb600 100644 --- a/Sources/apiLOL/Controllers/ControllerChampions.cs +++ b/Sources/apiLOL/Controllers/ControllerChampions.cs @@ -1,32 +1,45 @@ using Microsoft.AspNetCore.Mvc; using Model; -using StubLib; -using System.Xml.Linq; -using static StubLib.StubData; // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 namespace apiLOL.Controllers { [ApiController] - [Route("api/[controller]")] + [Route("api/v1/[controller]")] + [ApiVersion("1.0")] + public class ControllerChampions : Controller { private readonly IDataManager data; - - + private readonly ILogger _logger; - public ControllerChampions(IDataManager manager) + public ControllerChampions(IDataManager manager, ILogger log) { data = manager; + _logger = log; } // GET: api/ [HttpGet] - public async Task Get() + public async Task Get([FromQuery]int index = 0, int count = 10) { - var champs = (await data.ChampionsMgr.GetItems(0, await data.ChampionsMgr.GetNbItems())).Select(Model => Model.ToDTO()); - return Ok(champs); + //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"); + int nbChampions = await data.ChampionsMgr.GetNbItems(); + _logger.LogInformation($"Nombre de champions : {nbChampions}"); + + var champs = (await data.ChampionsMgr.GetItems(index, count)).Select(Model => Model.ToDTO()); + + var page = new ChampionPageDTO + { + Data = champs, + Index = index, + Count = count, + TotalCount = nbChampions + }; + return Ok(page); } @@ -35,30 +48,71 @@ namespace apiLOL.Controllers [Route("{name}")] public async Task GetChampion(string name) { - var champs = (await data.ChampionsMgr.GetItemsByName(name,0,1)).First(); - return Ok(champs.ToDTO()); + _logger.LogInformation($"methode GetChampion de ControllerChampions appelée avec le paramètre {name}"); + try + { + var champs = (await data.ChampionsMgr.GetItemsByName(name, 0, 1)); + return Ok(champs.First().ToDTO()); + } + catch + { + return BadRequest("erreur de nom de champion"); + } } // POST api/ [HttpPost] - public IActionResult Post(ChampionDTO champDTO) + public async Task Post(ChampionDTO champDTO) { - Champion tmp = champDTO.ToModel(); - data.ChampionsMgr.AddItem(tmp); - return Ok(); + _logger.LogInformation($"methode Post de ControllerChampions appelée avec le paramètre {champDTO.Name}"); + + + try + { + Champion tmp = champDTO.ToModel(); + Champion champion = await data.ChampionsMgr.AddItem(tmp); + ChampionDTO dtoChamp = champion.ToDTO(); + return CreatedAtAction(nameof(GetChampion), new { name = dtoChamp.Name }, dtoChamp); + } + catch + { + return BadRequest("le champion existe deja"); + } } // PUT api//5 - [HttpPut("{id}")] - public void Put(int id, [FromBody] string value) + [HttpPut("{name}")] + public async Task Put(string name, string bio) { + _logger.LogInformation($"methode Put de ControllerChampions appelée avec le paramètre name: {name} et bio: {bio}"); + + try + { + var champs = (await data.ChampionsMgr.GetItemsByName(name, 0, 1)).First(); + champs.Bio = bio; + return Ok(champs.ToDTO()); + } + catch + { + return BadRequest("erreur de nom de champion"); + } } // DELETE api//5 - [HttpDelete("{id}")] - public void Delete(int id) + [HttpDelete("{name}")] + public async Task Delete(String name) { + try + { + var champ = (await data.ChampionsMgr.GetItemsByName(name, 0, 1)).First(); + data.ChampionsMgr.DeleteItem(champ); + return Ok(champ.ToDTO()); + } + catch + { + return BadRequest("erreur de nom de champion"); + } } } diff --git a/Sources/apiLOL/Controllers/WeatherForecastController.cs b/Sources/apiLOL/Controllers/WeatherForecastController.cs deleted file mode 100644 index c8e4063..0000000 --- a/Sources/apiLOL/Controllers/WeatherForecastController.cs +++ /dev/null @@ -1,33 +0,0 @@ -using Microsoft.AspNetCore.Mvc; - -namespace apiLOL.Controllers -{ - [ApiController] - [Route("[controller]")] - public class WeatherForecastController : ControllerBase - { - private static readonly string[] Summaries = new[] - { - "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" - }; - - private readonly ILogger _logger; - - public WeatherForecastController(ILogger logger) - { - _logger = logger; - } - - [HttpGet(Name = "GetWeatherForecast")] - public IEnumerable Get() - { - return Enumerable.Range(1, 5).Select(index => new WeatherForecast - { - Date = DateTime.Now.AddDays(index), - TemperatureC = Random.Shared.Next(-20, 55), - Summary = Summaries[Random.Shared.Next(Summaries.Length)] - }) - .ToArray(); - } - } -} \ No newline at end of file diff --git a/Sources/apiLOL/WeatherForecast.cs b/Sources/apiLOL/WeatherForecast.cs deleted file mode 100644 index e380cd6..0000000 --- a/Sources/apiLOL/WeatherForecast.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace apiLOL -{ - public class WeatherForecast - { - public DateTime Date { get; set; } - - public int TemperatureC { get; set; } - - public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); - - public string? Summary { get; set; } - } -} \ No newline at end of file diff --git a/Sources/apiLOL/apiLOL.csproj b/Sources/apiLOL/apiLOL.csproj index 82733d5..b90f766 100644 --- a/Sources/apiLOL/apiLOL.csproj +++ b/Sources/apiLOL/apiLOL.csproj @@ -7,6 +7,7 @@ +