From 9d261f56b35cc46127c25528d6bc34aca3348dec Mon Sep 17 00:00:00 2001 From: "victor.gaborit" Date: Fri, 16 Feb 2024 08:51:33 +0100 Subject: [PATCH] =?UTF-8?q?changement=20du=20retour=20pour=20les=20m=C3=A9?= =?UTF-8?q?thodes=20POST=20et=20PUT=20dans=20UserController=20:=20Post=20c?= =?UTF-8?q?'est=20un=20pour=20envoyer=20un=20=C3=A9l=C3=A9ment=20=C3=A0=20?= =?UTF-8?q?l'API=20alors=20que=20PUT=20c'est=20pour=20modifier=20un=20?= =?UTF-8?q?=C3=A9l=C3=A9ment=20existant.=20meilleur=20utilisation=20des=20?= =?UTF-8?q?code=20de=20retour=20:=20POST=20renvoie=20CreateAtAction.=20Uti?= =?UTF-8?q?lisation=20des=20exception=20renvoyer=20par=20le=20service=20po?= =?UTF-8?q?ur=20envoyer=20les=20bon=20codes=20de=20retour=20(BadRequest=20?= =?UTF-8?q?ou=20NotFount=20ou=20personalis=C3=A9=20:=20GetUsers=20renvoie?= =?UTF-8?q?=20un=20204=20si=20le=20nombre=20d'item=20renvoy=C3=A9=20est=20?= =?UTF-8?q?de=200),=20utilisation=20du=20FromBody=20pour=20la=20methode=20?= =?UTF-8?q?PUT?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- API_SQLuedo/API/Controllers/UserController.cs | 57 ++++++++++++++++--- 1 file changed, 49 insertions(+), 8 deletions(-) diff --git a/API_SQLuedo/API/Controllers/UserController.cs b/API_SQLuedo/API/Controllers/UserController.cs index 08ac4c5..7b61bf7 100644 --- a/API_SQLuedo/API/Controllers/UserController.cs +++ b/API_SQLuedo/API/Controllers/UserController.cs @@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; +using Model.Business; using Model.DTO; using Services; @@ -22,38 +23,78 @@ namespace API.Controllers [HttpGet("users/{page}/{number}")] public IActionResult GetUsers(int page, int number) { + var nbUser = _userDataService.GetUsers(page, number).Count(); + if(nbUser == 0) + { + return StatusCode(204); + } return Ok(_userDataService.GetUsers(page, number)); } [HttpGet("user/id/{id}")] public IActionResult GetUserById(int id) { - return Ok(_userDataService.GetUserById(id)); + try + { + return Ok(_userDataService.GetUserById(id)); + } catch (ArgumentException ex) + { + return NotFound(); + } + } [HttpGet("user/username/{username}")] public IActionResult GetUserByUsername(string username) { - return Ok(_userDataService.GetUserByUsername(username)); + try + { + return Ok(_userDataService.GetUserByUsername(username)); + }catch (ArgumentException ex) + { + return NotFound(); + } + } [HttpDelete] public IActionResult DeleteUser(int id) { - return Ok(_userDataService.DeleteUser(id)); + var sucess = _userDataService.DeleteUser(id); + if(sucess) + { + return Ok(_userDataService.DeleteUser(id)); + } else + { + return NotFound(); + } + } [HttpPost] - public IActionResult UpdateUser(int id, UserDTO user) + public IActionResult UpdateUser(string username, string password, string email, bool isAdmin) { - return Ok(_userDataService.UpdateUser(id, user)); + + // return Ok(_userDataService.CreateUser(username, password, email, isAdmin)); + return CreatedAtAction(nameof(GetUsers), _userDataService.CreateUser(username, password, email, isAdmin)); } [HttpPut] - public IActionResult CreateUser(string username, string password, string email, bool isAdmin) + public IActionResult CreateUser(int id, [FromBody] UserDTO userDTO) { - // return Ok(_userDataService.CreateUser(username, password, email, isAdmin)); - return CreatedAtAction(nameof(GetUsers), _userDataService.CreateUser(username, password, email, isAdmin)); + if(id != userDTO.Id) + { + return BadRequest(); + } + if(!ModelState.IsValid) + { + return BadRequest(); + } + if(userDTO != null) + { + return Ok(_userDataService.UpdateUser(id, userDTO)); + } + return NotFound(); } } }