🔖 fin du versionning ! 3 versions differentes : V1, V2, V2.2 !
continuous-integration/drone/push Build is passing Details

pull/21/head
Pierre Ferreira 2 years ago
parent f7a6e77554
commit 0260d4afd0

@ -22,84 +22,86 @@ namespace API_LoL.Controllers
// GET api/<ChampionController>/5
[HttpGet]
public async Task<IActionResult> Get(String? name = null, String? skill = null, String? characteristic = null, int index = 0, int size = 10)
{
if (size - index > 10)
{
return BadRequest();
}
if (!string.IsNullOrEmpty(name))
{
var list = await ChampionsManager.GetItemsByName(name, index, size);
if (list.Count() != 0)
{
return Ok(list.Select(champion => champion?.ToDTO()));
}
else { return NoContent(); }
}
else if (!string.IsNullOrEmpty(skill))
{
var list = await ChampionsManager.GetItemsBySkill(skill, index, size);
if (list.Count() != 0)
{
return Ok(list.Select(champion => champion?.ToDTO()));
}
else { return NoContent(); }
}
else if (!string.IsNullOrEmpty(characteristic))
{
var list = await ChampionsManager.GetItems(index, size);
if (list.Count() != 0)
{
return Ok(list.Select(champion => champion?.ToDTO()));
}
else { return NoContent(); }
}
else
{
var list = await ChampionsManager.GetItems(index, size);
if (list.Count() != 0)
{
return Ok(list.Select(champion => champion?.ToDTO()));
}
else { return NoContent(); }
}
}
//[HttpGet]
//public async Task<IActionResult> Get(String? name = null, String? skill = null, String? characteristic = null, int index = 0, int size = 10)
//{
// if (size - index > 10)
// {
// return BadRequest();
// }
// if (!string.IsNullOrEmpty(name))
// {
// var list = await ChampionsManager.GetItemsByName(name, index, size);
// if (list.Count() != 0)
// {
// return Ok(list.Select(champion => champion?.ToDTO()));
// }
// else { return NoContent(); }
// }
// else if (!string.IsNullOrEmpty(skill))
// {
// var list = await ChampionsManager.GetItemsBySkill(skill, index, size);
// if (list.Count() != 0)
// {
// return Ok(list.Select(champion => champion?.ToDTO()));
// }
// else { return NoContent(); }
// }
// else if (!string.IsNullOrEmpty(characteristic))
// {
// var list = await ChampionsManager.GetItems(index, size);
// if (list.Count() != 0)
// {
// return Ok(list.Select(champion => champion?.ToDTO()));
// }
// else { return NoContent(); }
// }
// else
// {
// var list = await ChampionsManager.GetItems(index, size);
// if (list.Count() != 0)
// {
// return Ok(list.Select(champion => champion?.ToDTO()));
// }
// else { return NoContent(); }
// }
//}
// POST api/<ChampionController>
[HttpPost]
public async Task<IActionResult> Post(ChampionDTO champion)
{
if (champion == null)
{
return UnprocessableEntity();
}
else
{
await ChampionsManager.AddItem(champion.ToChampion());
return CreatedAtAction("Post", champion);
}
}
//// POST api/<ChampionController>
//[HttpPost]
//public async Task<IActionResult> Post(ChampionDTO champion)
//{
// if (champion == null)
// {
// return UnprocessableEntity();
// }
// else
// {
// await ChampionsManager.AddItem(champion.ToChampion());
// return CreatedAtAction("Post", champion);
// }
//}
// PUT api/<ChampionController>/5
[HttpPut("{id}")]
public void Put(int id, [FromBody] string value)
{
}
//// PUT api/<ChampionController>/5
//[HttpPut("{id}")]
//public void Put(int id, [FromBody] string value)
//{
//}
// DELETE api/<ChampionController>/5
[HttpDelete("{id}")]
public void Delete(int id)
{
}
//// DELETE api/<ChampionController>/5
//[HttpDelete("{id}")]
//public void Delete(int id)
//{
//}
///---------- Versioning ----------///
[HttpGet]
[HttpGet, MapToApiVersion("2.0")]
public string GetThatOnlySayHello() => "Hello v2.0!";
//! FIXME : not working, mais avec la version 2.0 ca marche !
[HttpGet, MapToApiVersion("2.2")]
public string GetThatOnlySayHelloV2() => "Hello but i'm from v2.2!";
}

@ -1,3 +1,4 @@
using API_LoL;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.AspNetCore.Mvc.Versioning;
using Model;
@ -26,6 +27,8 @@ builder.Services.AddVersionedApiExplorer(setup =>
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.ConfigureOptions<ConfigureSwaggerOptions>();
builder.Services.AddApiVersioning(o => o.ApiVersionReader = new UrlSegmentApiVersionReader());
@ -36,6 +39,9 @@ builder.Services.AddControllers();
builder.Services.AddScoped<IDataManager,StubData>();
var app = builder.Build();
var apiVersionDescriptionProvider = app.Services.GetRequiredService<IApiVersionDescriptionProvider>();

@ -0,0 +1,61 @@
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
namespace API_LoL
{
public class ConfigureSwaggerOptions : IConfigureNamedOptions<SwaggerGenOptions>
{
private readonly IApiVersionDescriptionProvider _provider;
public ConfigureSwaggerOptions(IApiVersionDescriptionProvider provider)
{
_provider = provider;
}
/// <summary>
/// Configure each API discovered for Swagger Documentation
/// </summary>
/// <param name="options"></param>
public void Configure(SwaggerGenOptions options)
{
// add swagger document for every API version discovered
foreach (var description in _provider.ApiVersionDescriptions)
{
options.SwaggerDoc(description.GroupName, CreateVersionInfo(description));
}
}
/// <summary>
/// Configure Swagger Options. Inherited from the Interface
/// </summary>
/// <param name="name"></param>
/// <param name="options"></param>
public void Configure(string name, SwaggerGenOptions options)
{
Configure(options);
}
/// <summary>
/// Create information about the version of the API
/// </summary>
/// <param name="description"></param>
/// <returns>Information about the API</returns>
private OpenApiInfo CreateVersionInfo(ApiVersionDescription desc)
{
var info = new OpenApiInfo()
{
Title = ".NET Core (.NET 6) Web API",
Version = desc.ApiVersion.ToString()
};
if (desc.IsDeprecated)
{
info.Description += " This API version has been deprecated. Please use one of the new APIs available from the explorer.";
}
return info;
}
}
}
Loading…
Cancel
Save