API #1

Merged
nathan.boileau merged 29 commits from API into master 2 years ago

2
.gitignore vendored

@ -428,5 +428,7 @@ FodyWeavers.xsd
*.sln.iml *.sln.iml
*.db *.db
*.db-shm
*.db-wal
*Migrations *Migrations
*.sonarqube *.sonarqube

@ -1,37 +1,59 @@
using apiLOL; using apiLOL;
using apiLOL.Controllers; using apiLOL.Controllers;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using StubLib; using StubLib;
namespace TestUnitaire namespace TestUnitaire
{ {
public class TestAPILol public class TestAPILol
{ {
[Fact] [Theory]
public void Test1() [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<ControllerChampions>();
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 // Arrange
var data = new StubData(); var data = new StubData();
var controller = new ControllerChampions(new StubData()); var logger = new NullLogger<ControllerChampions>();
var champDTO = new ChampionDTO("Charles", "Charles est un champion de League of Legends"); var controller = new ControllerChampions(data, logger);
var champDTO = new ChampionDTO(name, bio);
// Act // Act
var result = controller.Post(champDTO); // Call method POST to add a champion
data.ChampionsMgr.AddItem(champDTO.ToModel()); var result = await controller.Post(champDTO);
var nbItem = data.ChampionsMgr.GetNbItems(); // Call method GET to get the champion
Task<int> nbItemTask = nbItem; var resultGet = await controller.GetChampion(name);
// Assert // Assert
Assert.IsType<OkResult>(result); // IS the champion added to the list, number of champions in the list + 1
// Verify that the champions is added to the stub Assert.Equal(name, champDTO.Name);
Assert.Equal(7, nbItemTask.Result); Assert.Equal(bio, champDTO.Bio);
} }
} }
} }

@ -4,14 +4,14 @@ namespace apiLOL
{ {
public static class ChampionMapper public static class ChampionMapper
{ {
public static ChampionDTO ToDTO(this Champion champion) public static ChampionDTO ToDTO(this Champion champion) => new ChampionDTO(champion.Name, champion.Bio);
{
return new ChampionDTO(champion.Name, champion.Bio);
}
public static Champion ToModel(this ChampionDTO championDTO) public static Champion ToModel(this ChampionDTO championDTO)
{ {
return new Champion(championDTO.Name); Champion champ = new Champion(championDTO.Name);
champ.Bio = championDTO.Bio;
return champ;
} }
} }

@ -0,0 +1,13 @@
namespace apiLOL
{
public class ChampionPageDTO
{
public IEnumerable<ChampionDTO> Data { get; set; }
public int Index { get; set; }
public int Count { get; set; }
public int TotalCount { get; set; }
}
}

@ -1,32 +1,45 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Model; 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 // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
namespace apiLOL.Controllers namespace apiLOL.Controllers
{ {
[ApiController] [ApiController]
[Route("api/[controller]")] [Route("api/v1/[controller]")]
[ApiVersion("1.0")]
public class ControllerChampions : Controller public class ControllerChampions : Controller
{ {
private readonly IDataManager data; private readonly IDataManager data;
private readonly ILogger _logger;
public ControllerChampions(IDataManager manager, ILogger<ControllerChampions> log)
public ControllerChampions(IDataManager manager)
{ {
data = manager; data = manager;
_logger = log;
} }
// GET: api/<ControllerLol> // GET: api/<ControllerLol>
[HttpGet] [HttpGet]
public async Task<IActionResult> Get() public async Task<IActionResult> Get([FromQuery]int index = 0, int count = 10)
{ {
var champs = (await data.ChampionsMgr.GetItems(0, await data.ChampionsMgr.GetNbItems())).Select(Model => Model.ToDTO()); //FromQuery permet de filtrer dans la collection de champions en fonction du nom
return Ok(champs); // 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}")] [Route("{name}")]
public async Task<IActionResult> GetChampion(string name) public async Task<IActionResult> GetChampion(string name)
{ {
var champs = (await data.ChampionsMgr.GetItemsByName(name,0,1)).First(); _logger.LogInformation($"methode GetChampion de ControllerChampions appelée avec le paramètre {name}");
return Ok(champs.ToDTO()); 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/<ControllerLol> // POST api/<ControllerLol>
[HttpPost] [HttpPost]
public IActionResult Post(ChampionDTO champDTO) public async Task<IActionResult> Post(ChampionDTO champDTO)
{
_logger.LogInformation($"methode Post de ControllerChampions appelée avec le paramètre {champDTO.Name}");
try
{ {
Champion tmp = champDTO.ToModel(); Champion tmp = champDTO.ToModel();
data.ChampionsMgr.AddItem(tmp); Champion champion = await data.ChampionsMgr.AddItem(tmp);
return Ok(); ChampionDTO dtoChamp = champion.ToDTO();
return CreatedAtAction(nameof(GetChampion), new { name = dtoChamp.Name }, dtoChamp);
}
catch
{
return BadRequest("le champion existe deja");
}
} }
// PUT api/<ControllerLol>/5 // PUT api/<ControllerLol>/5
[HttpPut("{id}")] [HttpPut("{name}")]
public void Put(int id, [FromBody] string value) public async Task<IActionResult> 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/<ControllerLol>/5 // DELETE api/<ControllerLol>/5
[HttpDelete("{id}")] [HttpDelete("{name}")]
public void Delete(int id) public async Task<IActionResult> 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");
}
} }
} }

@ -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<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> 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();
}
}
}

@ -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; }
}
}

@ -7,6 +7,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.2" /> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.2" /> <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.2" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.2" />

Loading…
Cancel
Save