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] 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>();