From 9d75faaeef9b1b64a9004005791d0bf06ebde236 Mon Sep 17 00:00:00 2001 From: etudiant Date: Sun, 12 Feb 2023 14:09:21 +0100 Subject: [PATCH] dp --- Sources/BowlingApi/BowlingApi.csproj | 1 - .../Controllers/PartieController.cs | 61 ++++++++++++++++++ Sources/BowlingApi/bowling.db-shm | Bin 0 -> 32768 bytes Sources/BowlingApi/bowling.db-wal | 0 .../Interfaces/IPartieService.cs | 1 + Sources/BowlingService/PartieService.cs | 21 ++++++ 6 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 Sources/BowlingApi/bowling.db-shm create mode 100644 Sources/BowlingApi/bowling.db-wal diff --git a/Sources/BowlingApi/BowlingApi.csproj b/Sources/BowlingApi/BowlingApi.csproj index 2605b8f..ffe323b 100644 --- a/Sources/BowlingApi/BowlingApi.csproj +++ b/Sources/BowlingApi/BowlingApi.csproj @@ -11,7 +11,6 @@ - 4 bin\Debug\net6.0\BowlingApi.xml diff --git a/Sources/BowlingApi/Controllers/PartieController.cs b/Sources/BowlingApi/Controllers/PartieController.cs index d50407e..03f6d46 100644 --- a/Sources/BowlingApi/Controllers/PartieController.cs +++ b/Sources/BowlingApi/Controllers/PartieController.cs @@ -121,6 +121,67 @@ namespace BowlingApi.Controllers } + [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; + } + } + /// + /// Supprimer une partie par son id + /// DELETE: api/parti/5 + /// + /// + /// Retourne la partie supprimé + /// Si la partie 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 parti = _partieService.Delete(id); + if (parti.Result == false) + { + return NotFound(); + } + return Ok(parti); + } + catch (Exception e) + { + StatusCode(StatusCodes.Status500InternalServerError, e.Message); + throw; + } + } + + /// /// Modification partie /// PUT: api/parti/5 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 Add(PartieDTO _partie); public Task Delete(PartieDTO _partie); + public Task Delete(long id); public Task Update(PartieDTO _partie); public Task> GetAll(); public Task> GetAllWithDate(DateTime date); diff --git a/Sources/BowlingService/PartieService.cs b/Sources/BowlingService/PartieService.cs index b805d02..9e92659 100644 --- a/Sources/BowlingService/PartieService.cs +++ b/Sources/BowlingService/PartieService.cs @@ -152,6 +152,27 @@ public async Task Add(PartieDTO _partie) return result; } + + public async Task Delete(long _partiid) + { + var result = false; + try + { + result = await _IpartieRepository.Delete(_partiid); + + if (result) + _logger.LogInformation("A parti was deleted : {player}", _partiid); + else + _logger.LogWarning("A parti not found : {player}", _partiid); + + } + catch (Exception ex) + { + _logger.LogError(ex, "Error while deleting player : {player}", _partiid); + throw; + } + return result; + } } }