From fdcf26413cc4cf64f13f4b63ad79eee5e9c739be Mon Sep 17 00:00:00 2001 From: lebeaulato Date: Mon, 17 Mar 2025 16:46:19 +0100 Subject: [PATCH] routes api fait, ne sauvegardent pas encore --- WF_EF_Api/Shared/IUserService.cs | 4 +- WF_EF_Api/StubApi/UserService.cs | 25 ++- .../WfApi/Controllers/UsersController.cs | 161 +++++++++++++++++- 3 files changed, 178 insertions(+), 12 deletions(-) diff --git a/WF_EF_Api/Shared/IUserService.cs b/WF_EF_Api/Shared/IUserService.cs index d8028ee..29fe4bf 100644 --- a/WF_EF_Api/Shared/IUserService.cs +++ b/WF_EF_Api/Shared/IUserService.cs @@ -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 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. diff --git a/WF_EF_Api/StubApi/UserService.cs b/WF_EF_Api/StubApi/UserService.cs index 039d8ab..7fd167a 100644 --- a/WF_EF_Api/StubApi/UserService.cs +++ b/WF_EF_Api/StubApi/UserService.cs @@ -41,17 +41,19 @@ namespace StubApi public async Task CountUser() { - throw new NotImplementedException(); + return _users.Count; } public async Task ExistEmail(string email) { - throw new NotImplementedException(); + if (_users.FirstOrDefault(u => u.Email == email) == null) return false; + return true; } public async Task ExistUsername(string username) { - throw new NotImplementedException(); + if (_users.FirstOrDefault(u => u.Pseudo == username) == null) return false; + return true; } public async Task> GetAllUser() @@ -64,7 +66,7 @@ namespace StubApi return _users.FirstOrDefault(u => u.Pseudo == username).Password; } - public async Task GetLastUserId() + public async Task 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 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; } } } diff --git a/WF_EF_Api/WfApi/Controllers/UsersController.cs b/WF_EF_Api/WfApi/Controllers/UsersController.cs index 7fb0e56..a60300f 100644 --- a/WF_EF_Api/WfApi/Controllers/UsersController.cs +++ b/WF_EF_Api/WfApi/Controllers/UsersController.cs @@ -28,8 +28,8 @@ namespace WfApi.Controllers try { var result = _user.GetUserById(id); - - if(result.IsCompletedSuccessfully) + + if (result.IsCompletedSuccessfully) { return await Task.FromResult(Ok(result)); } @@ -46,7 +46,7 @@ namespace WfApi.Controllers [HttpGet("all")] // Indiquer que l'id est dans l'URL - public async Task GetAllUsers(int index =0, int count = 5) + public async Task 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 GetCountUser() + { + try + { + var result = _user.CountUser(); + + if (result.IsCompletedSuccessfully) + { + return await Task.FromResult(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 GetExistUsername(string username) + { + try + { + var result = _user.ExistUsername(username); + + if (result.IsCompletedSuccessfully) + { + return await Task.FromResult(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 GetExistEmail(string email) + { + try + { + var result = _user.ExistEmail(email); + + if (result.IsCompletedSuccessfully) + { + return await Task.FromResult(Ok(result)); + } + else + { + return NoContent(); + } + } + catch (Exception) + { + return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" }); + } + } + + //===================================== ROUTE PUT ===================================== + + + [HttpPut()] + public async Task 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 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 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." }); + } + } + } }