changement du retour pour les méthodes POST et PUT dans UserController : Post c'est un pour envoyer un élément à l'API alors que PUT c'est pour modifier un élément existant. meilleur utilisation des code de retour : POST renvoie CreateAtAction. Utilisation des exception renvoyer par le service pour envoyer les bon codes de retour (BadRequest ou NotFount ou personalisé : GetUsers renvoie un 204 si le nombre d'item renvoyé est de 0), utilisation du FromBody pour la methode PUT

pull/23/head
Victor GABORIT 1 year ago
parent 0611a5ca3e
commit 9d261f56b3

@ -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();
}
}
}

Loading…
Cancel
Save