From 2ab6d70fb13dc70adb3d825bc04fcedaaf1e45b9 Mon Sep 17 00:00:00 2001 From: etudiant Date: Sun, 12 Feb 2023 08:03:56 +0100 Subject: [PATCH] pagination --- .../Controllers/PartieController.cs | 37 ++++++++++++++++++ Sources/BowlingApi/bowling.db-shm | Bin 0 -> 32768 bytes Sources/BowlingApi/bowling.db-wal | 0 3 files changed, 37 insertions(+) create mode 100644 Sources/BowlingApi/bowling.db-shm create mode 100644 Sources/BowlingApi/bowling.db-wal diff --git a/Sources/BowlingApi/Controllers/PartieController.cs b/Sources/BowlingApi/Controllers/PartieController.cs index f8fbe59..e98711c 100644 --- a/Sources/BowlingApi/Controllers/PartieController.cs +++ b/Sources/BowlingApi/Controllers/PartieController.cs @@ -121,6 +121,43 @@ namespace BowlingApi.Controllers throw; } + }/// + /// 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 _partieService.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; + } } /// diff --git a/Sources/BowlingApi/bowling.db-shm b/Sources/BowlingApi/bowling.db-shm new file mode 100644 index 0000000000000000000000000000000000000000..fe9ac2845eca6fe6da8a63cd096d9cf9e24ece10 GIT binary patch literal 32768 zcmeIuAr62r3