add : V2 of ControllerChampions
continuous-integration/drone/push Build is passing Details

API2
Nathan BOILEAU 2 years ago
parent 428501e7fa
commit d476aff59f

@ -6,13 +6,14 @@ using Model;
namespace apiLOL.Controllers namespace apiLOL.Controllers
{ {
[ApiController] [ApiController]
[Route("api/v1/[controller]")] [Route("api/v1/[controller]")]
[ApiVersion("1.0")] [ApiVersion("1.0")]
public class ControllerChampions : Controller public class ControllerChampions : Controller
{ {
private readonly IDataManager data; private readonly IDataManager data;
// EFdata manager qui implémente l'interface IDataManager // EFdata manager qui implémente l'interface IDataManager
// coté client : Refaire un APIdata manager qui implémente l'interface IDataManager // coté client : Refaire un APIdata manager qui implémente l'interface IDataManager
private readonly ILogger _logger; private readonly ILogger _logger;
@ -26,20 +27,23 @@ namespace apiLOL.Controllers
// GET: api/<ControllerLol> // GET: api/<ControllerLol>
[HttpGet] [HttpGet]
public async Task<IActionResult> Get([FromQuery]int index = 0, int count = 10, string? name = "") public async Task<IActionResult> Get([FromQuery] int index = 0, int count = 10, string? name = "")
{ {
//FromQuery permet de filtrer dans la collection de champions en fonction du nom //FromQuery permet de filtrer dans la collection de champions en fonction du nom
_logger.LogInformation($"methode Get de ControllerChampions appelée index:{index}, count: {count} et name:{name}"); _logger.LogInformation(
$"methode Get de ControllerChampions appelée index:{index}, count: {count} et name:{name}");
int nbChampions = await data.ChampionsMgr.GetNbItems(); int nbChampions = await data.ChampionsMgr.GetNbItems();
_logger.LogInformation($"Nombre de champions : {nbChampions}"); _logger.LogInformation($"Nombre de champions : {nbChampions}");
//var champs = (await data.ChampionsMgr.GetItems(index, count)).Where(Model => Model.Name.Contains(name)).Select(Model => Model.ToDTO()); //var champs = (await data.ChampionsMgr.GetItems(index, count)).Where(Model => Model.Name.Contains(name)).Select(Model => Model.ToDTO());
var champs = (await data.ChampionsMgr.GetItems(index, await data.ChampionsMgr.GetNbItems())).Where(Model => Model.Name.Contains(name)).Skip(index * count).Take(count).Select(Model => Model.ToDTO()); var champs = (await data.ChampionsMgr.GetItems(index, await data.ChampionsMgr.GetNbItems()))
.Where(Model => Model.Name.Contains(name)).Skip(index * count).Take(count)
.Select(Model => Model.ToDTO());
var page = new ChampionPageDTO var page = new ChampionPageDTO
{ {
Data = (IEnumerable<ChampionDTO>)champs, Data = (IEnumerable<ChampionDTO>) champs,
Index = index, Index = index,
Count = count, Count = count,
TotalCount = nbChampions TotalCount = nbChampions
@ -59,11 +63,11 @@ namespace apiLOL.Controllers
var champs = (await data.ChampionsMgr.GetItemsByName(name, 0, 1)); var champs = (await data.ChampionsMgr.GetItemsByName(name, 0, 1));
return Ok(champs.First().ToDTO()); return Ok(champs.First().ToDTO());
} }
catch(Exception ex) catch (Exception ex)
{ {
_logger.LogError($"erreur methode Get de ControllerChampions: {ex}"); _logger.LogError($"erreur methode Get de ControllerChampions: {ex}");
return BadRequest("erreur de nom de champion"); return BadRequest("erreur de nom de champion");
} }
} }
@ -77,7 +81,7 @@ namespace apiLOL.Controllers
Champion tmp = champDTO.ToModel(); Champion tmp = champDTO.ToModel();
Champion champion = await data.ChampionsMgr.AddItem(tmp); Champion champion = await data.ChampionsMgr.AddItem(tmp);
ChampionDTO dtoChamp = champion.ToDTO(); ChampionDTO dtoChamp = champion.ToDTO();
return CreatedAtAction(nameof(GetChampion), new { name = dtoChamp.Name }, dtoChamp); return CreatedAtAction(nameof(GetChampion), new {name = dtoChamp.Name}, dtoChamp);
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -90,7 +94,8 @@ namespace apiLOL.Controllers
[HttpPut("{name}")] [HttpPut("{name}")]
public async Task<IActionResult> Put(string name, string bio) 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}"); _logger.LogInformation(
$"methode Put de ControllerChampions appelée avec le paramètre name: {name} et bio: {bio}");
try try
{ {
@ -125,4 +130,45 @@ namespace apiLOL.Controllers
} }
} }
[ApiController]
[Route("api/v2/[controller]")]
[ApiVersion("2.0")]
public class ControllerChampionsSecondVersion : Controller
{
private readonly ILogger _logger;
public ControllerChampionsSecondVersion(ILogger<ControllerChampions> log)
{
_logger = log;
}
// GET api/<ControllerSkins>/5
[HttpGet()]
public string Get(int id)
{
return "Version 2 of GET";
}
// POST api/<ControllerSkins>
[HttpPost]
public void Post([FromBody] string value)
{
}
// PUT api/<ControllerSkins>/5
[HttpPut("{id}")]
public void Put(int id, [FromBody] string value)
{
}
// DELETE api/<ControllerSkins>/5
[HttpDelete("{id}")]
public void Delete(int id)
{
}
}
} }

@ -1,3 +1,5 @@
using Microsoft.AspNetCore.Mvc.Versioning;
using Microsoft.AspNetCore.Mvc;
using Model; using Model;
using StubLib; using StubLib;
@ -10,18 +12,27 @@ builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(); builder.Services.AddSwaggerGen();
builder.Services.AddSingleton<IDataManager,StubData>(); builder.Services.AddSingleton<IDataManager, StubData>();
// Scoped et Transient sont des types de cycle de vie // Scoped et Transient sont des types de cycle de vie
// Transient : une instance a chaque contruction d'objet // Transient : une instance a chaque contruction d'objet
// Scoped : une instance par requete client // Scoped : une instance par requete client
// Configure API versioning
builder.Services.AddApiVersioning(options =>
{
options.ReportApiVersions = true;
options.AssumeDefaultVersionWhenUnspecified = true;
options.DefaultApiVersion = new ApiVersion(1, 0);
options.ApiVersionReader = new HeaderApiVersionReader("api-version");
});
var app = builder.Build(); var app = builder.Build();
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment()) if (app.Environment.IsDevelopment())
{ {
app.UseSwagger(); app.UseSwagger();
app.UseSwaggerUI(); app.UseSwaggerUI();
} }
app.UseHttpsRedirection(); app.UseHttpsRedirection();

Loading…
Cancel
Save