diff --git a/Sources/API_LoL/Controllers/ChampionsControllerVersioned.cs b/Sources/API_LoL/Controllers/ChampionsControllerVersioned.cs index e8b3a24..87598d9 100644 --- a/Sources/API_LoL/Controllers/ChampionsControllerVersioned.cs +++ b/Sources/API_LoL/Controllers/ChampionsControllerVersioned.cs @@ -22,84 +22,86 @@ namespace API_LoL.Controllers // GET api//5 - [HttpGet] - public async Task 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 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/ - [HttpPost] - public async Task Post(ChampionDTO champion) - { - if (champion == null) - { - return UnprocessableEntity(); - } - else - { - await ChampionsManager.AddItem(champion.ToChampion()); - return CreatedAtAction("Post", champion); - } - } + //// POST api/ + //[HttpPost] + //public async Task Post(ChampionDTO champion) + //{ + // if (champion == null) + // { + // return UnprocessableEntity(); + // } + // else + // { + // await ChampionsManager.AddItem(champion.ToChampion()); + // return CreatedAtAction("Post", champion); + // } + //} - // PUT api//5 - [HttpPut("{id}")] - public void Put(int id, [FromBody] string value) - { - } + //// PUT api//5 + //[HttpPut("{id}")] + //public void Put(int id, [FromBody] string value) + //{ + //} - // DELETE api//5 - [HttpDelete("{id}")] - public void Delete(int id) - { - } + //// DELETE api//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!"; } diff --git a/Sources/API_LoL/Program.cs b/Sources/API_LoL/Program.cs index b8f9f47..c192eac 100644 --- a/Sources/API_LoL/Program.cs +++ b/Sources/API_LoL/Program.cs @@ -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(); + builder.Services.AddApiVersioning(o => o.ApiVersionReader = new UrlSegmentApiVersionReader()); @@ -36,6 +39,9 @@ builder.Services.AddControllers(); builder.Services.AddScoped(); + + + var app = builder.Build(); var apiVersionDescriptionProvider = app.Services.GetRequiredService(); diff --git a/Sources/API_LoL/SwaggerOptions.cs b/Sources/API_LoL/SwaggerOptions.cs new file mode 100644 index 0000000..46ad759 --- /dev/null +++ b/Sources/API_LoL/SwaggerOptions.cs @@ -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 + { + private readonly IApiVersionDescriptionProvider _provider; + + public ConfigureSwaggerOptions(IApiVersionDescriptionProvider provider) + { + _provider = provider; + } + + /// + /// Configure each API discovered for Swagger Documentation + /// + /// + 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)); + } + } + + /// + /// Configure Swagger Options. Inherited from the Interface + /// + /// + /// + public void Configure(string name, SwaggerGenOptions options) + { + Configure(options); + } + + /// + /// Create information about the version of the API + /// + /// + /// Information about the API + 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; + } + } +}