diff --git a/Sources/Bowling Api Gateway/Properties/launchSettings.json b/Sources/Bowling Api Gateway/Properties/launchSettings.json index 0db569b..899a6e2 100644 --- a/Sources/Bowling Api Gateway/Properties/launchSettings.json +++ b/Sources/Bowling Api Gateway/Properties/launchSettings.json @@ -1,9 +1,26 @@ { "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:47947", + "sslPort": 44397 + } + }, "profiles": { "Bowling Api Gateway": { "commandName": "Project", - "applicationUrl": "http://localhost:7003", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:7003", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } diff --git a/Sources/BowlingApi/BowlingApi.csproj b/Sources/BowlingApi/BowlingApi.csproj index 087a05a..5f5d64c 100644 --- a/Sources/BowlingApi/BowlingApi.csproj +++ b/Sources/BowlingApi/BowlingApi.csproj @@ -17,6 +17,7 @@ + diff --git a/Sources/BowlingApi/Controllers/JoueurController.cs b/Sources/BowlingApi/Controllers/JoueurController.cs index 75f6c23..dca3621 100644 --- a/Sources/BowlingApi/Controllers/JoueurController.cs +++ b/Sources/BowlingApi/Controllers/JoueurController.cs @@ -5,7 +5,8 @@ using Microsoft.AspNetCore.Mvc; namespace BowlingApi.Controllers; -[ApiVersion("1.0")] +[ApiVersion("1")] +[ApiVersion("2")] [ApiController] [Route("api/v{version:apiVersion}/[controller]")] public class JoueurController:Controller @@ -29,6 +30,7 @@ public class JoueurController:Controller [ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)] [ProducesResponseType(typeof(string), StatusCodes.Status404NotFound)] [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] + [MapToApiVersion("1")] public async Task Get() { try @@ -59,6 +61,7 @@ public class JoueurController:Controller [ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)] [ProducesResponseType(typeof(string), StatusCodes.Status404NotFound)] [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] + [MapToApiVersion("1")] public async Task Get(int page=1,int pageSize=10) { try @@ -85,6 +88,54 @@ public class JoueurController:Controller } } + + /// + /// Get player with pagination + /// Get : api/Joueur?page=1&pageSize=10 + /// + /// + /// + ///< param name="name"> + /// la liste des Joueurs + /// Retourne la liste des joueurs + /// Si la liste est vide + /// Si une erreur est survenue + [HttpGet("{page}/{pageSize}")] + [MapToApiVersion("2")] + [ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)] + [ProducesResponseType(typeof(string), StatusCodes.Status404NotFound)] + [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] + public async Task Get(int page=1,int pageSize=10, string name=null) + { + try + { + var result = await _joueurService.GetAll(); + if (result == null) + { + return NotFound(); + } + + if (name != null) + { + result = result.Where(x => x.Pseudo.ToLower().Contains(name.ToLower())); + } + var data = result.Skip((page - 1) * pageSize).Take(pageSize); + Response.Headers.Add("X-Pagination", Newtonsoft.Json.JsonConvert.SerializeObject(new + { + totalCount = result.Count(), + pageSize = pageSize, + currentPage = page, + totalPages = (int)Math.Ceiling(result.Count() / (double)pageSize) + })); + return Ok(data); + } + catch (Exception e) + { + return StatusCode(StatusCodes.Status500InternalServerError, e.Message); + throw; + } + } + /// /// Get a player by name /// GET: api/Joueur/Djon @@ -99,6 +150,7 @@ public class JoueurController:Controller [ProducesResponseType(typeof(string), StatusCodes.Status404NotFound)] [ProducesResponseType(typeof(string), StatusCodes.Status400BadRequest)] [ProducesResponseType(typeof(JoueurDTO), StatusCodes.Status200OK)] + [MapToApiVersion("2")] public async Task Get(string name) { try @@ -132,6 +184,7 @@ public class JoueurController:Controller [ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)] [ProducesResponseType(typeof(string), StatusCodes.Status400BadRequest)] [ProducesResponseType(typeof(JoueurDTO), StatusCodes.Status201Created)] + [MapToApiVersion("1")] public async Task> Post([FromBody] JoueurDTO joueur) { try @@ -166,6 +219,7 @@ public class JoueurController:Controller [ProducesResponseType(typeof(string), StatusCodes.Status404NotFound)] [ProducesResponseType(typeof(string), StatusCodes.Status400BadRequest)] [ProducesResponseType(typeof(JoueurDTO), StatusCodes.Status200OK)] + [MapToApiVersion("1")] public async Task> Put(long id,[FromBody] JoueurDTO joueur) { try @@ -186,4 +240,35 @@ public class JoueurController:Controller throw; } } + + /// + /// Supprimer un joueur par son id + /// DELETE: api/Joueur/5 + /// + /// + /// Retourne le joueur supprimé + /// Si le joueur n'existe pas + /// Si une erreur est survenue + [HttpDelete("{id}")] + [ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)] + [ProducesResponseType(typeof(string), StatusCodes.Status404NotFound)] + [ProducesResponseType(typeof(bool), StatusCodes.Status200OK)] + [MapToApiVersion("2")] + public async Task> Delete(long id) + { + try + { + var joueur = _joueurService.Delete(id); + if (joueur.Result == false) + { + return NotFound(); + } + return Ok(joueur); + } + catch (Exception e) + { + StatusCode(StatusCodes.Status500InternalServerError, e.Message); + throw; + } + } } \ No newline at end of file diff --git a/Sources/BowlingApi/Program.cs b/Sources/BowlingApi/Program.cs index 5564b8e..679859f 100644 --- a/Sources/BowlingApi/Program.cs +++ b/Sources/BowlingApi/Program.cs @@ -5,6 +5,7 @@ using BowlingRepository.Interface; using BowlingService; using BowlingService.Interfaces; using Mapper; +using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; var builder = WebApplication.CreateBuilder(args); @@ -19,7 +20,15 @@ builder.Services.AddSwaggerGen(c=> c.SwaggerDoc("v1", new() { Title = "APi Bowling APP", Version = "v1" }); c.SwaggerDoc("v2", new() { Title = "APi Bowling APP", Version = "v2" }); c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, "BowlingApi.xml")); + +}); + +builder.Services.AddVersionedApiExplorer(options => +{ + options.GroupNameFormat = "'v'VVV"; + options.SubstituteApiVersionInUrl = true; }); + builder.Services.AddAutoMapper(typeof(JoueurProfile)); builder.Services.AddScoped(); @@ -33,6 +42,13 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddApiVersioning(config => +{ + config.DefaultApiVersion = new ApiVersion(1, 0); + config.AssumeDefaultVersionWhenUnspecified = true; + config.ReportApiVersions = true; +}); + //configure Logger diff --git a/Sources/BowlingApi/bowling.db b/Sources/BowlingApi/bowling.db index e0616d7..3a3942f 100644 Binary files a/Sources/BowlingApi/bowling.db and b/Sources/BowlingApi/bowling.db differ diff --git a/Sources/BowlingService/Interfaces/IJoueurService.cs b/Sources/BowlingService/Interfaces/IJoueurService.cs index ba4f159..b11b58e 100644 --- a/Sources/BowlingService/Interfaces/IJoueurService.cs +++ b/Sources/BowlingService/Interfaces/IJoueurService.cs @@ -6,7 +6,7 @@ namespace BowlingService.Interfaces; public interface IJoueurService { Task Add(JoueurDTO data); - Task Delete(JoueurDTO data); + Task Delete(long data); Task Update(long id,JoueurDTO data); Task GetDataWithName(string name); Task> GetAll(); diff --git a/Sources/BowlingService/JoueurService.cs b/Sources/BowlingService/JoueurService.cs index 8a10c61..01eae6a 100644 --- a/Sources/BowlingService/JoueurService.cs +++ b/Sources/BowlingService/JoueurService.cs @@ -63,17 +63,17 @@ namespace BowlingService /// /// /// - public async Task Delete(JoueurDTO _joueur) + public async Task Delete(long _joueurId) { var result = false; try { - result = await _joueurRepository.Delete(_joueur.Id); - _logger.LogInformation("A player was deleted : {player}", _joueur.Pseudo); + result = await _joueurRepository.Delete(_joueurId); + _logger.LogInformation("A player was deleted : {player}", _joueurId); } catch (Exception ex) { - _logger.LogError(ex, "Error while deleting player : {player}", _joueur.Pseudo); + _logger.LogError(ex, "Error while deleting player : {player}", _joueurId); throw; } return result;