diff --git a/Sources/BowlingApi/BowlingApi.csproj b/Sources/BowlingApi/BowlingApi.csproj index 491442d..87fe556 100644 --- a/Sources/BowlingApi/BowlingApi.csproj +++ b/Sources/BowlingApi/BowlingApi.csproj @@ -10,8 +10,12 @@ + + 4 + bin\Debug\net6.0\BowlingApi.xml + - + @@ -37,4 +41,5 @@ + diff --git a/Sources/BowlingApi/Controllers/JoueurController.cs b/Sources/BowlingApi/Controllers/JoueurController.cs index 3aafde3..1af99b7 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,18 @@ public class JoueurController:Controller _joueurService = joueurService; } - // GET: api/Joueur + /// + /// Get all Players + /// GET: api/joueur + /// + /// 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,8 +47,59 @@ public class JoueurController:Controller } } - // GET: api/Joueur/Djon + /// + /// 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 @@ -59,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 @@ -80,21 +152,34 @@ public class JoueurController:Controller } } - [HttpPut("{name}")] - public async Task> Put(string name,[FromBody] JoueurDTO joueur) + /// + /// 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 { 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..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; @@ -16,7 +13,11 @@ 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" }); + c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, "BowlingApi.xml")); +}); builder.Services.AddAutoMapper(typeof(JoueurProfile)); builder.Services.AddScoped(); @@ -43,9 +44,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/BowlingRepository/JoueurRepository.cs b/Sources/BowlingRepository/JoueurRepository.cs index 7bedefe..738712f 100644 --- a/Sources/BowlingRepository/JoueurRepository.cs +++ b/Sources/BowlingRepository/JoueurRepository.cs @@ -35,7 +35,8 @@ public class JoueurRepository:IJoueurRepository public async Task 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>();