diff --git a/Sources/API_LoL/API_LoL.csproj b/Sources/API_LoL/API_LoL.csproj index 5ada54d..a8c4de7 100644 --- a/Sources/API_LoL/API_LoL.csproj +++ b/Sources/API_LoL/API_LoL.csproj @@ -9,6 +9,8 @@ + + diff --git a/Sources/API_LoL/Controllers/ChampionsController.cs b/Sources/API_LoL/Controllers/ChampionsController.cs index ce8b87e..1b2b021 100644 --- a/Sources/API_LoL/Controllers/ChampionsController.cs +++ b/Sources/API_LoL/Controllers/ChampionsController.cs @@ -13,7 +13,9 @@ using System.Xml.Linq; namespace API_LoL.Controllers { - [Route("api/[controller]")] + //[Route("api/[controller]")] + [ApiVersion("1.0")] + [Route("api/v{version:apiVersion}/[controller]")] [ApiController] public class ChampionsController : ControllerBase { diff --git a/Sources/API_LoL/Controllers/ChampionsControllerVersioned.cs b/Sources/API_LoL/Controllers/ChampionsControllerVersioned.cs new file mode 100644 index 0000000..87598d9 --- /dev/null +++ b/Sources/API_LoL/Controllers/ChampionsControllerVersioned.cs @@ -0,0 +1,108 @@ +using Microsoft.AspNetCore.Mvc; +using Model; +using StubLib; +using DTO; +using DTO.Mapper; +using System.CodeDom.Compiler; + +namespace API_LoL.Controllers +{ + [ApiVersion("2.0")] + [ApiVersion("2.2")] + [Route("api/v{version:apiVersion}/versioned")] + [ApiController] + public class ChampionsControllerVersioned : ControllerBase + { + public ChampionsControllerVersioned(IDataManager Manager) + { + this.ChampionsManager = Manager.ChampionsMgr; + } + + private IChampionsManager ChampionsManager; + + // 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(); } + // } + //} + + + //// 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) + //{ + //} + + //// DELETE api//5 + //[HttpDelete("{id}")] + //public void Delete(int id) + //{ + //} + + + ///---------- Versioning ----------/// + [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 d4fb3b8..c192eac 100644 --- a/Sources/API_LoL/Program.cs +++ b/Sources/API_LoL/Program.cs @@ -1,24 +1,65 @@ +using API_LoL; +using Microsoft.AspNetCore.Mvc.ApiExplorer; +using Microsoft.AspNetCore.Mvc.Versioning; using Model; using StubLib; var builder = WebApplication.CreateBuilder(args); -// Add services to the container. -builder.Services.AddControllers(); +//Versioning + +///NOT WORKING WHEN CHANGING VERSIONS : +/// voir sur https://blog.christian-schou.dk/how-to-use-api-versioning-in-net-core-web-api/ rubrique "Configure SwaggerOptions" +/// (mais requiere l'injection de dépendance). +/// Sinon, code plus simple disponible par le prof + + + +// Add ApiExplorer to discover versions +builder.Services.AddVersionedApiExplorer(setup => +{ + setup.GroupNameFormat = "'v'VVV"; + setup.SubstituteApiVersionInUrl = true; +}); + // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); +builder.Services.ConfigureOptions(); + + +builder.Services.AddApiVersioning(o => o.ApiVersionReader = new UrlSegmentApiVersionReader()); + +// Add services to the container. +builder.Services.AddControllers(); + + + builder.Services.AddScoped(); + + + var app = builder.Build(); +var apiVersionDescriptionProvider = app.Services.GetRequiredService(); + + // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseSwagger(); - app.UseSwaggerUI(); + app.UseSwaggerUI(options => + { + + foreach (var description in apiVersionDescriptionProvider.ApiVersionDescriptions) + { + options.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json", + description.GroupName.ToUpperInvariant()); + } + }); } app.UseHttpsRedirection(); 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; + } + } +}