From 555e1dd4a7269abe52716163f142aba0baddffbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Victor=20P=C3=A9rez=20Ngounou?= Date: Thu, 2 Feb 2023 13:32:05 +0100 Subject: [PATCH 1/3] fix error --- .../Controllers/JoueurController.cs | 30 +++++++++++++----- Sources/BowlingApi/Program.cs | 17 ++++++++-- Sources/BowlingApi/bowling.db | Bin 45056 -> 45056 bytes Sources/BowlingRepository/JoueurRepository.cs | 3 +- .../Interfaces/IJoueurService.cs | 2 +- Sources/BowlingService/JoueurService.cs | 4 +-- .../BowlingAPITest/TestJoueurController.cs | 6 ++-- 7 files changed, 45 insertions(+), 17 deletions(-) diff --git a/Sources/BowlingApi/Controllers/JoueurController.cs b/Sources/BowlingApi/Controllers/JoueurController.cs index 3aafde3..ffec152 100644 --- a/Sources/BowlingApi/Controllers/JoueurController.cs +++ b/Sources/BowlingApi/Controllers/JoueurController.cs @@ -7,6 +7,7 @@ namespace BowlingApi.Controllers; [ApiController] [Route("api/[controller]")] + public class JoueurController:Controller { private IJoueurService _joueurService; @@ -16,8 +17,17 @@ public class JoueurController:Controller _joueurService = joueurService; } - // GET: api/Joueur + /// + /// Get all Players + /// + /// la liste des Joueurs + /// Retourne la liste des joueurs + /// Si la liste est vide + /// Si une erreur est survenue [HttpGet] + [ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)] + [ProducesResponseType(typeof(string), StatusCodes.Status404NotFound)] + [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] public async Task Get() { try @@ -36,7 +46,12 @@ public class JoueurController:Controller } } - // GET: api/Joueur/Djon + /// + /// Get a player by name + /// GET: api/Joueur/Djon + /// + /// + /// [HttpGet("{name}")] public async Task Get(string name) { @@ -80,21 +95,20 @@ public class JoueurController:Controller } } - [HttpPut("{name}")] - public async Task> Put(string name,[FromBody] JoueurDTO joueur) + [HttpPut("{id}")] + public async Task> Put(long id,[FromBody] JoueurDTO joueur) { try { if(joueur == null) return BadRequest("Le joueur est obligatoire"); - var updateJoueur = _joueurService.Update(joueur); - if (updateJoueur.Result == null) + var updateJoueur = _joueurService.Update(id,joueur); + if (updateJoueur.Result == false) { return NotFound(); } - - return Ok(updateJoueur); + return Ok(joueur); } catch (Exception e) { diff --git a/Sources/BowlingApi/Program.cs b/Sources/BowlingApi/Program.cs index 4173911..e357277 100644 --- a/Sources/BowlingApi/Program.cs +++ b/Sources/BowlingApi/Program.cs @@ -16,7 +16,10 @@ var builder = WebApplication.CreateBuilder(args); builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); -builder.Services.AddSwaggerGen(); +builder.Services.AddSwaggerGen(c=> +{ + c.SwaggerDoc("v1", new() { Title = "APi Bowling APP", Version = "v1" }); +}); builder.Services.AddAutoMapper(typeof(JoueurProfile)); builder.Services.AddScoped(); @@ -43,9 +46,19 @@ var app = builder.Build(); if (app.Environment.IsDevelopment()) { app.UseSwagger(); - app.UseSwaggerUI(); + app.UseSwaggerUI(c => + { + c.SwaggerEndpoint("/swagger/v1/swagger.json", "API du projet Bowling APP v1"); + + }); } +app.UseRouting(); +app.UseEndpoints(endpoint=> +{ + endpoint.MapControllers(); +}); + app.UseHttpsRedirection(); app.UseAuthorization(); diff --git a/Sources/BowlingApi/bowling.db b/Sources/BowlingApi/bowling.db index b257dc97c3c1a54a3fd0845c3301fcc914f52b80..79e15c686e381079b47f7b694de5709ed2856546 100644 GIT binary patch delta 53 zcmZp8z|`=7X#;xzBjaX{fQ9@5{9hSBm`9SKEHk+zzi9H#cpEMbMivH9hOGS5oX!8^ G83O>u#t@7E delta 39 vcmZp8z|`=7X#;xzBg1BnfQ9@MCkRa58E?bI$;iSW&JY GetJoueur(long id) { - return await _context.Joueurs.FindAsync(id); + var data= await _context.Joueurs.FindAsync(id); + return data; } public async Task> GetAllJoueur() diff --git a/Sources/BowlingService/Interfaces/IJoueurService.cs b/Sources/BowlingService/Interfaces/IJoueurService.cs index 06dd825..ba4f159 100644 --- a/Sources/BowlingService/Interfaces/IJoueurService.cs +++ b/Sources/BowlingService/Interfaces/IJoueurService.cs @@ -7,7 +7,7 @@ public interface IJoueurService { Task Add(JoueurDTO data); Task Delete(JoueurDTO data); - Task Update(JoueurDTO data); + Task Update(long id,JoueurDTO data); Task GetDataWithName(string name); Task> GetAll(); } \ No newline at end of file diff --git a/Sources/BowlingService/JoueurService.cs b/Sources/BowlingService/JoueurService.cs index 0b8eaec..8a10c61 100644 --- a/Sources/BowlingService/JoueurService.cs +++ b/Sources/BowlingService/JoueurService.cs @@ -123,12 +123,12 @@ namespace BowlingService return _joueur; } - public async Task Update(JoueurDTO _joueur) + public async Task Update(long id,JoueurDTO _joueur) { bool result = false; try { - JoueurEntity entity = _joueurRepository.GetJoueur(_joueur.Id).Result; + JoueurEntity entity = _joueurRepository.GetJoueur(id).Result; if (entity!= null) { entity.Pseudo = _joueur.Pseudo; diff --git a/Sources/Tests/BowlingAPITest/TestJoueurController.cs b/Sources/Tests/BowlingAPITest/TestJoueurController.cs index 2c948cb..614ce35 100644 --- a/Sources/Tests/BowlingAPITest/TestJoueurController.cs +++ b/Sources/Tests/BowlingAPITest/TestJoueurController.cs @@ -129,7 +129,7 @@ public class TestController var joueurController = new JoueurController(null); // Act - var result = await joueurController.Put(null, null); + var result = await joueurController.Put(0, null); // Assert result.Should().BeOfType>(); @@ -145,11 +145,11 @@ public class TestController // Arrange var joueur = new JoueurDTO { Id = 1, Pseudo = "John Doe" }; var joueurServiceMock = new Mock(); - joueurServiceMock.Setup(x => x.Update(joueur)).ReturnsAsync(true); + joueurServiceMock.Setup(x => x.Update(joueur.Id,joueur)).ReturnsAsync(true); var joueurController = new JoueurController(joueurServiceMock.Object); // Act - var result = await joueurController.Put(joueur.Pseudo, joueur); + var result = await joueurController.Put(joueur.Id, joueur); // Assert result.Should().BeOfType>(); From b61dff9eb411c1d18aee33ed8916feaad037d3ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Victor=20P=C3=A9rez=20Ngounou?= Date: Fri, 3 Feb 2023 21:10:33 +0100 Subject: [PATCH 2/3] Mise en place de la pagination --- Sources/BowlingApi/BowlingApi.csproj | 5 ++ .../Controllers/JoueurController.cs | 75 +++++++++++++++++- Sources/BowlingApi/Program.cs | 1 + .../BowlingApi/Properties/launchSettings.json | 2 - Sources/BowlingApi/bowling.db | Bin 45056 -> 45056 bytes 5 files changed, 79 insertions(+), 4 deletions(-) diff --git a/Sources/BowlingApi/BowlingApi.csproj b/Sources/BowlingApi/BowlingApi.csproj index 7f0352f..89ec037 100644 --- a/Sources/BowlingApi/BowlingApi.csproj +++ b/Sources/BowlingApi/BowlingApi.csproj @@ -9,6 +9,11 @@ + + + 4 + bin\Debug\net6.0\BowlingApi.xml + diff --git a/Sources/BowlingApi/Controllers/JoueurController.cs b/Sources/BowlingApi/Controllers/JoueurController.cs index ffec152..1af99b7 100644 --- a/Sources/BowlingApi/Controllers/JoueurController.cs +++ b/Sources/BowlingApi/Controllers/JoueurController.cs @@ -19,6 +19,7 @@ public class JoueurController:Controller /// /// Get all Players + /// GET: api/joueur /// /// la liste des Joueurs /// Retourne la liste des joueurs @@ -46,13 +47,59 @@ public class JoueurController:Controller } } + /// + /// Get player with pagination + /// Get : api/Joueur?page=1&pageSize=10 + /// + /// la liste des Joueurs + /// Retourne la liste des joueurs + /// Si la liste est vide + /// Si une erreur est survenue + + [HttpGet("{page}/{pageSize}")] + [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) + { + try + { + var result = await _joueurService.GetAll(); + if (result == null) + { + return NotFound(); + } + 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 /// /// - /// + /// Retourne le joueur + /// Si le nom du joueur est null + /// Si le joueur n'existe pas + /// Si une erreur est survenue [HttpGet("{name}")] + [ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)] + [ProducesResponseType(typeof(string), StatusCodes.Status404NotFound)] + [ProducesResponseType(typeof(string), StatusCodes.Status400BadRequest)] + [ProducesResponseType(typeof(JoueurDTO), StatusCodes.Status200OK)] public async Task Get(string name) { try @@ -74,8 +121,18 @@ public class JoueurController:Controller } } - // POST: api/Joueur + /// + /// Creer un joueur + /// POST: api/Joueur + /// + /// + /// Retourne le joueur créé + /// Si le joueur est null + /// Si une erreur est survenue [HttpPost] + [ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)] + [ProducesResponseType(typeof(string), StatusCodes.Status400BadRequest)] + [ProducesResponseType(typeof(JoueurDTO), StatusCodes.Status201Created)] public async Task> Post([FromBody] JoueurDTO joueur) { try @@ -95,7 +152,21 @@ public class JoueurController:Controller } } + /// + /// Modifier un joueur + /// PUT: api/Joueur/5 + /// + /// + /// + /// Retourne le joueur modifié + /// Si le joueur est null + /// Si le joueur n'existe pas + /// Si une erreur est survenue [HttpPut("{id}")] + [ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)] + [ProducesResponseType(typeof(string), StatusCodes.Status404NotFound)] + [ProducesResponseType(typeof(string), StatusCodes.Status400BadRequest)] + [ProducesResponseType(typeof(JoueurDTO), StatusCodes.Status200OK)] public async Task> Put(long id,[FromBody] JoueurDTO joueur) { try diff --git a/Sources/BowlingApi/Program.cs b/Sources/BowlingApi/Program.cs index e357277..59e1f46 100644 --- a/Sources/BowlingApi/Program.cs +++ b/Sources/BowlingApi/Program.cs @@ -19,6 +19,7 @@ builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(c=> { c.SwaggerDoc("v1", new() { Title = "APi Bowling APP", Version = "v1" }); + c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, "BowlingApi.xml")); }); builder.Services.AddAutoMapper(typeof(JoueurProfile)); builder.Services.AddScoped(); diff --git a/Sources/BowlingApi/Properties/launchSettings.json b/Sources/BowlingApi/Properties/launchSettings.json index fc48bb7..cfd4403 100644 --- a/Sources/BowlingApi/Properties/launchSettings.json +++ b/Sources/BowlingApi/Properties/launchSettings.json @@ -29,8 +29,6 @@ }, "RestFull": { "commandName": "Project", - "dotnetRunMessages": true, - "launchBrowser": false, "applicationUrl": "https://localhost:5001", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" diff --git a/Sources/BowlingApi/bowling.db b/Sources/BowlingApi/bowling.db index 79e15c686e381079b47f7b694de5709ed2856546..f7bb14f5c8310183fb04b29ebf850d7a5889d1e8 100644 GIT binary patch delta 56 zcmZp8z|`=7X#-;bC;wLlHvYAY{A)LJ1uW+0;bLTAkYq^AODRe#OfJbUn!GdKhLeMlg+Y`d MD?fGfzj&qq0O9-)-v9sr From e266ef33b3cf6bcb70feab74539e6e2aa60f570d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Victor=20P=C3=A9rez=20Ngounou?= Date: Sat, 4 Feb 2023 10:14:05 +0100 Subject: [PATCH 3/3] puch --- Sources/BowlingApi/Program.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/Sources/BowlingApi/Program.cs b/Sources/BowlingApi/Program.cs index 59e1f46..614bcf6 100644 --- a/Sources/BowlingApi/Program.cs +++ b/Sources/BowlingApi/Program.cs @@ -1,11 +1,8 @@ -using AutoMapper; using BowlingEF.Context; -using BowlingLib.Model; using BowlingRepository; using BowlingRepository.Interface; using BowlingService; using BowlingService.Interfaces; -using Business; using Mapper; using Microsoft.EntityFrameworkCore;