routes api fait, ne sauvegardent pas encore

pull/4/head
lebeaulato 1 month ago
parent 0a91cae33c
commit fdcf26413c

@ -35,11 +35,11 @@ namespace Shared
// Updates the details of an existing user identified by 'userId'.
// 'userId' is the ID of the user to be updated, and 'user' contains the new user data.
Task UpdateUser(int userId, TUser user);
Task<TUser> UpdateUser(int userId, TUser user, TUser existingUser);
// Removes a user from the system based on their unique identifier ('userId').
// 'userId' is the unique identifier of the user to be removed.
Task RemoveUser(int userId);
Task RemoveUser(TUser user);
// Retrieves the hashed password for a given username.
// 'username' is the username for which the password hash is to be retrieved.

@ -41,17 +41,19 @@ namespace StubApi
public async Task<int> CountUser()
{
throw new NotImplementedException();
return _users.Count;
}
public async Task<bool> ExistEmail(string email)
{
throw new NotImplementedException();
if (_users.FirstOrDefault(u => u.Email == email) == null) return false;
return true;
}
public async Task<bool> ExistUsername(string username)
{
throw new NotImplementedException();
if (_users.FirstOrDefault(u => u.Pseudo == username) == null) return false;
return true;
}
public async Task<PaginationResult<UserDTO>> GetAllUser()
@ -64,7 +66,7 @@ namespace StubApi
return _users.FirstOrDefault(u => u.Pseudo == username).Password;
}
public async Task<int> GetLastUserId()
public async Task<int> GetLastUserId() // JE VOIS PAS L'INTERET
{
throw new NotImplementedException();
}
@ -90,7 +92,7 @@ namespace StubApi
return _users.FirstOrDefault(u => u.Pseudo == username);
}
public async Task RemoveUser(int userId)
public async Task RemoveUser(UserDTO user)
{
throw new NotImplementedException();
}
@ -100,9 +102,18 @@ namespace StubApi
throw new NotImplementedException();
}
public async Task UpdateUser(int userId, UserDTO user)
public async Task<UserDTO> UpdateUser(int userId, UserDTO user, UserDTO existingUser)
{
throw new NotImplementedException();
// Update users properties
existingUser.Pseudo = user.Pseudo;
existingUser.Password = user.Password;
existingUser.Email = user.Email;
existingUser.Years = user.Years;
existingUser.ImageProfil = user.ImageProfil;
return existingUser;
}
}
}

@ -28,8 +28,8 @@ namespace WfApi.Controllers
try
{
var result = _user.GetUserById(id);
if(result.IsCompletedSuccessfully)
if (result.IsCompletedSuccessfully)
{
return await Task.FromResult<IActionResult>(Ok(result));
}
@ -46,7 +46,7 @@ namespace WfApi.Controllers
[HttpGet("all")] // Indiquer que l'id est dans l'URL
public async Task<IActionResult> GetAllUsers(int index =0, int count = 5)
public async Task<IActionResult> GetAllUsers(int index = 0, int count = 5)
{
try
{
@ -135,6 +135,161 @@ namespace WfApi.Controllers
}
[HttpGet("countuser")] // Indiquer que l'id est dans l'URL
public async Task<IActionResult> GetCountUser()
{
try
{
var result = _user.CountUser();
if (result.IsCompletedSuccessfully)
{
return await Task.FromResult<IActionResult>(Ok(result));
}
else
{
return NoContent();
}
}
catch (Exception)
{
return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" });
}
}
[HttpGet("existusername/{username}")] // Indiquer que l'id est dans l'URL
public async Task<IActionResult> GetExistUsername(string username)
{
try
{
var result = _user.ExistUsername(username);
if (result.IsCompletedSuccessfully)
{
return await Task.FromResult<IActionResult>(Ok(result));
}
else
{
return NoContent();
}
}
catch (Exception)
{
return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" });
}
}
[HttpGet("existemail/{email}")] // Indiquer que l'id est dans l'URL
public async Task<IActionResult> GetExistEmail(string email)
{
try
{
var result = _user.ExistEmail(email);
if (result.IsCompletedSuccessfully)
{
return await Task.FromResult<IActionResult>(Ok(result));
}
else
{
return NoContent();
}
}
catch (Exception)
{
return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" });
}
}
//===================================== ROUTE PUT =====================================
[HttpPut()]
public async Task<IActionResult> UpdateUser([FromQuery] int id, [FromBody] UserDTO updateduser)
{
try
{
if (updateduser == null)
{
return BadRequest(new { message = "Player data is required." });
}
var existingUser = _user.GetUserById(id).Result;
if (existingUser == null)
{
return NotFound(new { message = "Player not found." });
}
var result = _user.UpdateUser(id,updateduser,existingUser);
return Ok(result);
}
catch (Exception ex)
{
return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal server error." });
}
}
//===================================== ROUTE POST =====================================
[HttpPost]
public async Task<IActionResult> CreatePlayer([FromBody] UserDTO newUser)
{
try
{
if (newUser == null)
{
return BadRequest(new { message = "Les données du joueur sont requises." });
}
var existingPlayer = _user.GetUserById(newUser.Id).Result;
if (existingPlayer != null)
{
return Conflict(new { message = "Un utilisateur avec cet ID existe déjà." });
}
_user.AddUser(newUser);
return CreatedAtAction(nameof(GetAllUsers), new { id = newUser.Id }, newUser);
}
catch (Exception ex)
{
return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Erreur interne du serveur." });
}
}
//===================================== ROUTE DELETE =====================================
[HttpDelete] // /api/v1/players?id=51
public async Task<IActionResult> DeletePlayer([FromQuery] int id)
{
try
{
var existingPlayer = _user.GetUserById(id).Result;
if (existingPlayer == null)
{
return NotFound(new { message = "Player not found." });
}
_user.RemoveUser(existingPlayer);
return Ok(new { message = $"Player {id} deleted successfully." });
}
catch (Exception ex)
{
return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal server error." });
}
}
}
}

Loading…
Cancel
Save