merge master
continuous-integration/drone/push Build is passing Details

pull/2/head
nathan boileau 2 years ago
commit 5268427070

@ -39,7 +39,7 @@ steps:
- dotnet sonarscanner end /d:sonar.login=$${PLUGIN_SONAR_TOKEN}
secrets: [ SECRET_SONAR_LOGIN ]
settings:
# accessible en ligne de commande par ${PLUGIN_SONAR_HOST}
# accessible en ligne de commande par ${PLUGIN_SONAR_HOST}
sonar_host: https://codefirst.iut.uca.fr/sonar/
# accessible en ligne de commande par ${PLUGIN_SONAR_TOKEN}
sonar_token:

@ -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<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
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<ControllerChampions>();
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<int> 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<OkResult>(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);
}
}
}

@ -11,6 +11,6 @@
public string Name { get; set; }
public string Bio { get; set; }
public string Bio { get; set; }
}
}

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

@ -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,33 +1,46 @@
using System.Security.Cryptography;
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<ControllerChampions> log)
{
data = manager;
_logger = log;
}
// GET: api/<ControllerLol>
[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());
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);
}
@ -36,33 +49,71 @@ namespace apiLOL.Controllers
[Route("{name}")]
public async Task<IActionResult> 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/<ControllerLol>
[HttpPost]
public IActionResult Post(ChampionDTO champDTO)
public async Task<IActionResult> 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}");
//return CreatedAtAction(nameof(GetChampion), new { Id = 1 },
// await data.ChampionsMgr.AddItem(champDTO.ToModel()));
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/<ControllerLol>/5
[HttpPut("{id}")]
public void Put(int id, [FromBody] string value)
[HttpPut("{name}")]
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
[HttpDelete("{id}")]
public void Delete(int id)
[HttpDelete("{name}")]
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>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.2" />

Loading…
Cancel
Save