You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
650 lines
28 KiB
650 lines
28 KiB
using DTO;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Shared;
|
|
using System.Net;
|
|
namespace WfApi.Controllers
|
|
{
|
|
[ApiController]
|
|
[Route("api/v1/users")] //Version API
|
|
|
|
public class UsersController : ControllerBase
|
|
{
|
|
private readonly IUserService<UserDTO> _user;
|
|
|
|
private readonly ILogger<UsersController> _logger;
|
|
public UsersController(IUserService<UserDTO> userService, ILogger<UsersController> logger)
|
|
{
|
|
_user = userService;
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
//===================================== ROUTE GET =====================================
|
|
/// <summary>
|
|
/// Gets a user by their unique identifier.
|
|
/// </summary>
|
|
/// <param name="id">The unique identifier of the user</param>
|
|
/// <returns></returns>
|
|
/// <remarks>
|
|
/// ## **Sample request**:
|
|
///
|
|
/// GET /users/{id}
|
|
///
|
|
/// Where `{id}` is the unique identifier of the user you want to retrieve.
|
|
///
|
|
/// ## **Returns**
|
|
///
|
|
/// - **200 OK** : Returns the user data if the user with the given ID exists.
|
|
/// - **204 No Content** : No user found for the provided ID, or the operation failed.
|
|
/// - **500 Internal Server Error** : If there is an exception during the process.
|
|
///
|
|
/// ## **Error Handling**
|
|
/// - In case of an internal server error (e.g., database issues), a `500 Internal Server Error` will be returned with an error message.
|
|
/// </remarks>
|
|
/// <response code="200">Returns the user data corresponding to the provided ID</response>
|
|
/// <response code="204">No content if no user is found or the operation fails</response>
|
|
/// <response code="500">Internal server error in case of an exception</response>
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
[HttpGet("{id}")] // Indiquer que l'id est dans l'URL
|
|
public async Task<IActionResult> Get(int id)
|
|
{
|
|
try
|
|
{
|
|
var result = _user.GetUserById(id);
|
|
|
|
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" });
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Gets a list of users with pagination support.
|
|
/// </summary>
|
|
/// <param name="index">The index of the page to retrieve (default is 0)</param>
|
|
/// <param name="count">The number of users to retrieve per page (default is 5)</param>
|
|
/// <returns></returns>
|
|
/// <remarks>
|
|
/// ## **Sample request**:
|
|
///
|
|
/// GET /users/all?index=0&count=5
|
|
///
|
|
/// The `index` parameter specifies the page number to retrieve (starting from 0), and the `count` parameter specifies how many users to return per page.
|
|
///
|
|
/// ## **Returns**
|
|
///
|
|
/// - **200 OK** : Returns a list of users if the operation is successful.
|
|
/// - **204 No Content** : If no users are found or the operation fails.
|
|
/// - **500 Internal Server Error** : If there is an exception during the execution of the request.
|
|
///
|
|
/// ## **Error Handling**
|
|
/// - In case of an internal server error (e.g., database issues), a `500 Internal Server Error` is returned with an error message.
|
|
/// </remarks>
|
|
/// <response code="200">Returns a list of users</response>
|
|
/// <response code="204">No content if no users are found or the operation fails</response>
|
|
/// <response code="500">Internal server error in case of an exception</response>
|
|
[HttpGet("all")]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
public async Task<IActionResult> GetAllUsers(int index = 0, int count = 5)
|
|
{
|
|
try
|
|
{
|
|
var result = _user.GetSomeUser(index, count);
|
|
|
|
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" });
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Gets the hashed password for a given username.
|
|
/// </summary>
|
|
/// <param name="username">The username to retrieve the hashed password for</param>
|
|
/// <returns></returns>
|
|
/// <remarks>
|
|
/// ## **Sample request**:
|
|
///
|
|
/// GET /users/hashpassword?username=johndoe
|
|
///
|
|
/// The `username` parameter specifies the username for which to retrieve the hashed password.
|
|
///
|
|
/// ## **Returns**
|
|
///
|
|
/// - **200 OK** : Returns the hashed password for the user if the username exists.
|
|
/// - **400 Bad Request** : If no username is provided or if the username is invalid.
|
|
/// - **204 No Content** : If no hashed password is found for the provided username.
|
|
/// - **500 Internal Server Error** : If an exception occurs while processing the request.
|
|
///
|
|
/// ## **Error Handling**
|
|
/// - In case of a missing or invalid `username`, a `400 Bad Request` is returned with a relevant message.
|
|
/// - If there is an exception during the process (e.g., database errors), a `500 Internal Server Error` is returned.
|
|
/// </remarks>
|
|
/// <response code="200">Returns the hashed password for the provided username</response>
|
|
/// <response code="400">Bad request if no username is provided or invalid</response>
|
|
/// <response code="204">No content if no hashed password is found</response>
|
|
/// <response code="500">Internal server error in case of an exception</response>
|
|
[HttpGet("hashpassword")]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
public async Task<IActionResult> GetHashPassword([FromQuery] string username)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(username))
|
|
{
|
|
return BadRequest(new { message = "No user defined" });
|
|
}
|
|
|
|
try
|
|
{
|
|
var result = _user.GetHashPassword(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" });
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Gets a user by their username.
|
|
/// </summary>
|
|
/// <param name="username">The username to retrieve the user</param>
|
|
/// <returns></returns>
|
|
/// <remarks>
|
|
/// ## **Sample request**:
|
|
///
|
|
/// GET /users/username?username=johndoe
|
|
///
|
|
/// The `username` parameter specifies the username of the user you want to retrieve.
|
|
///
|
|
/// ## **Returns**
|
|
///
|
|
/// - **200 OK** : Returns the user data if the username exists.
|
|
/// - **400 Bad Request** : If no username is provided or if the username is invalid.
|
|
/// - **204 No Content** : If no user is found with the provided username.
|
|
/// - **500 Internal Server Error** : If an exception occurs while processing the request.
|
|
///
|
|
/// ## **Error Handling**
|
|
/// - In case of a missing or invalid `username`, a `400 Bad Request` is returned with a relevant message.
|
|
/// - If there is an exception during the process (e.g., database errors), a `500 Internal Server Error` is returned.
|
|
/// </remarks>
|
|
/// <response code="200">Returns the user data for the provided username</response>
|
|
/// <response code="400">Bad request if no username is provided or invalid</response>
|
|
/// <response code="204">No content if no user is found with the provided username</response>
|
|
/// <response code="500">Internal server error in case of an exception</response>
|
|
[HttpGet("username")]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
public async Task<IActionResult> GetUserByUsername([FromQuery] string username)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(username))
|
|
{
|
|
return BadRequest(new { message = "No user defined" });
|
|
}
|
|
|
|
try
|
|
{
|
|
var result = _user.GetUserByUsername(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" });
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Gets a user by their email address.
|
|
/// </summary>
|
|
/// <param name="email">The email address of the user</param>
|
|
/// <returns></returns>
|
|
/// <remarks>
|
|
/// ## **Sample request**:
|
|
///
|
|
/// GET /users/email?email=johndoe@example.com
|
|
///
|
|
/// The `email` parameter specifies the email address of the user you want to retrieve.
|
|
///
|
|
/// ## **Returns**
|
|
///
|
|
/// - **200 OK** : Returns the user data if the email exists.
|
|
/// - **400 Bad Request** : If no email is provided or if the email is invalid.
|
|
/// - **204 No Content** : If no user is found with the provided email.
|
|
/// - **500 Internal Server Error** : If an exception occurs while processing the request.
|
|
///
|
|
/// ## **Error Handling**
|
|
/// - In case of a missing or invalid `email`, a `400 Bad Request` is returned with a relevant message.
|
|
/// - If there is an exception during the process (e.g., database errors), a `500 Internal Server Error` is returned.
|
|
/// </remarks>
|
|
/// <response code="200">Returns the user data for the provided email</response>
|
|
/// <response code="400">Bad request if no email is provided or invalid</response>
|
|
/// <response code="204">No content if no user is found with the provided email</response>
|
|
/// <response code="500">Internal server error in case of an exception</response>
|
|
[HttpGet("email")]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
public async Task<IActionResult> GetUserByEmail([FromQuery] string email)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(email))
|
|
{
|
|
return BadRequest(new { message = "No user email defined" });
|
|
}
|
|
try
|
|
{
|
|
var result = _user.GetUserByEmail(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" });
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Gets the total number of users.
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
/// <remarks>
|
|
/// ## **Sample request**:
|
|
///
|
|
/// GET /users/count
|
|
///
|
|
/// This endpoint returns the total number of users present in the system.
|
|
///
|
|
/// ## **Returns**
|
|
///
|
|
/// - **200 OK** : Returns the total count of users.
|
|
/// - **204 No Content** : If no users are found or the operation fails.
|
|
/// - **500 Internal Server Error** : If an exception occurs while processing the request.
|
|
///
|
|
/// ## **Error Handling**
|
|
/// - In case of an exception during the process (e.g., database errors), a `500 Internal Server Error` is returned with a relevant message.
|
|
/// </remarks>
|
|
/// <response code="200">Returns the total count of users</response>
|
|
/// <response code="204">No content if the count could not be retrieved</response>
|
|
/// <response code="500">Internal server error in case of an exception</response>
|
|
[HttpGet("count")]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
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" });
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Checks if a user exists by their username.
|
|
/// </summary>
|
|
/// <param name="username">The username to check for existence</param>
|
|
/// <returns></returns>
|
|
/// <remarks>
|
|
/// ## **Sample request**:
|
|
///
|
|
/// GET /users/existusername?username=johndoe
|
|
///
|
|
/// The `username` parameter specifies the username to check if it exists in the system.
|
|
///
|
|
/// ## **Returns**
|
|
///
|
|
/// - **200 OK** : If the username exists, returns a success message.
|
|
/// - **400 Bad Request** : If no username is provided or if the username is invalid.
|
|
/// - **404 Not Found** : If the user with the specified username does not exist.
|
|
/// - **500 Internal Server Error** : If an exception occurs while processing the request.
|
|
///
|
|
/// ## **Error Handling**
|
|
/// - In case of a missing or invalid `username`, a `400 Bad Request` is returned with a relevant message.
|
|
/// - If the user does not exist, a `404 Not Found` response is returned.
|
|
/// - If there is an exception during the process (e.g., database errors), a `500 Internal Server Error` is returned with an appropriate message.
|
|
/// </remarks>
|
|
/// <response code="200">Returns a success message if the username exists</response>
|
|
/// <response code="400">Bad request if no username is provided or invalid</response>
|
|
/// <response code="404">Not found if the user with the provided username does not exist</response>
|
|
/// <response code="500">Internal server error in case of an exception</response>
|
|
[HttpGet("existusername")]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
public async Task<IActionResult> GetExistUsername([FromQuery] string username)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(username))
|
|
{
|
|
return BadRequest(new { message = "No user defined" });
|
|
}
|
|
try
|
|
{
|
|
var result = _user.ExistUsername(username);
|
|
|
|
if (result.IsCompletedSuccessfully)
|
|
{
|
|
return await Task.FromResult<IActionResult>(Ok(result));
|
|
}
|
|
else
|
|
{
|
|
return NotFound("User not found");
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" });
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Checks if a user exists by their email address.
|
|
/// </summary>
|
|
/// <param name="email">The email address to check for existence</param>
|
|
/// <returns></returns>
|
|
/// <remarks>
|
|
/// ## **Sample request**:
|
|
///
|
|
/// GET /users/existemail?email=johndoe@example.com
|
|
///
|
|
/// The `email` parameter specifies the email address to check if it exists in the system.
|
|
///
|
|
/// ## **Returns**
|
|
///
|
|
/// - **200 OK** : If the email exists, returns a success message.
|
|
/// - **400 Bad Request** : If no email is provided or if the email is invalid.
|
|
/// - **404 Not Found** : If the user with the specified email does not exist.
|
|
/// - **500 Internal Server Error** : If an exception occurs while processing the request.
|
|
///
|
|
/// ## **Error Handling**
|
|
/// - In case of a missing or invalid `email`, a `400 Bad Request` is returned with a relevant message.
|
|
/// - If the user does not exist, a `404 Not Found` response is returned.
|
|
/// - If there is an exception during the process (e.g., database errors), a `500 Internal Server Error` is returned with an appropriate message.
|
|
/// </remarks>
|
|
/// <response code="200">Returns a success message if the email exists</response>
|
|
/// <response code="400">Bad request if no email is provided or invalid</response>
|
|
/// <response code="404">Not found if the user with the provided email does not exist</response>
|
|
/// <response code="500">Internal server error in case of an exception</response>
|
|
[HttpGet("existemail")]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
public async Task<IActionResult> GetExistEmail([FromQuery] string email)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(email))
|
|
{
|
|
return BadRequest(new { message = "No user email defined" });
|
|
}
|
|
try
|
|
{
|
|
var result = _user.ExistEmail(email);
|
|
|
|
if (result.IsCompletedSuccessfully)
|
|
{
|
|
return await Task.FromResult<IActionResult>(Ok(result));
|
|
}
|
|
else
|
|
{
|
|
return NotFound("User email not found");
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" });
|
|
}
|
|
}
|
|
|
|
//===================================== ROUTE PUT =====================================
|
|
|
|
|
|
/// <summary>
|
|
/// Updates an existing user's data.
|
|
/// </summary>
|
|
/// <param name="id">The ID of the user to update</param>
|
|
/// <param name="updateduser">The updated user data</param>
|
|
/// <returns></returns>
|
|
/// <remarks>
|
|
/// ## **Sample request**:
|
|
///
|
|
/// PUT /users?id=1
|
|
/// Body:
|
|
/// {
|
|
/// "username": "newusername",
|
|
/// "email": "newemail@example.com",
|
|
/// "fullName": "New Name"
|
|
/// }
|
|
///
|
|
/// The `id` parameter specifies the user ID to be updated, and the body contains the updated user data.
|
|
///
|
|
/// ## **Returns**
|
|
///
|
|
/// - **200 OK** : If the user was successfully updated.
|
|
/// - **400 Bad Request** : If the provided user data is invalid or missing.
|
|
/// - **500 Internal Server Error** : If an error occurs while processing the update.
|
|
///
|
|
/// ## **Error Handling**
|
|
/// - If the `updateduser` object is `null`, a `400 Bad Request` is returned with a message indicating that player data is required.
|
|
/// - If an exception occurs during the process (e.g., database errors), a `500 Internal Server Error` is returned with an appropriate message.
|
|
/// </remarks>
|
|
/// <response code="200">Returns the updated user data</response>
|
|
/// <response code="400">Bad request if no user data is provided or invalid</response>
|
|
/// <response code="500">Internal server error in case of an exception</response>
|
|
[HttpPut()]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
public async Task<IActionResult> UpdateUser([FromQuery] int id, [FromBody] UserDTO updateduser)
|
|
{
|
|
try
|
|
{
|
|
if (updateduser == null)
|
|
{
|
|
return BadRequest(new { message = "Player data is required." });
|
|
}
|
|
|
|
var result = _user.UpdateUser(id,updateduser);
|
|
|
|
return Ok(result);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal server error." });
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//===================================== ROUTE POST =====================================
|
|
|
|
/// <summary>
|
|
/// Creates a new user in the system.
|
|
/// </summary>
|
|
/// <param name="newUser">The user data to create the new user</param>
|
|
/// <returns></returns>
|
|
/// <remarks>
|
|
/// ## **Sample request**:
|
|
///
|
|
/// POST /users
|
|
/// Body:
|
|
/// {
|
|
/// "id": 123,
|
|
/// "username": "newuser",
|
|
/// "email": "newuser@example.com",
|
|
/// "fullName": "New User"
|
|
/// }
|
|
///
|
|
/// The `newUser` parameter in the body contains the data of the new user to be created.
|
|
///
|
|
/// ## **Returns**
|
|
///
|
|
/// - **201 Created** : If the user was successfully created. The location of the created resource is returned in the response header.
|
|
/// - **400 Bad Request** : If the provided user data is invalid or missing.
|
|
/// - **409 Conflict** : If a user with the specified ID already exists.
|
|
/// - **500 Internal Server Error** : If an error occurs while processing the creation of the user.
|
|
///
|
|
/// ## **Error Handling**
|
|
/// - If the `newUser` object is `null`, a `400 Bad Request` is returned with a message indicating that user data is required.
|
|
/// - If the user already exists (based on `Id`), a `409 Conflict` is returned with a message indicating that a user with this ID already exists.
|
|
/// - If there is an exception during the process (e.g., database errors), a `500 Internal Server Error` is returned with an appropriate message.
|
|
/// </remarks>
|
|
/// <response code="201">Returns the created user and its location</response>
|
|
/// <response code="400">Bad request if no user data is provided or invalid</response>
|
|
/// <response code="409">Conflict if a user with the same ID already exists</response>
|
|
/// <response code="500">Internal server error in case of an exception</response>
|
|
[HttpPost]
|
|
[ProducesResponseType(StatusCodes.Status201Created)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(StatusCodes.Status409Conflict)]
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
public async Task<IActionResult> CreateUser([FromBody] UserDTO newUser)
|
|
{
|
|
try
|
|
{
|
|
if (newUser == null)
|
|
{
|
|
return BadRequest(new { message = "User data is required." });
|
|
}
|
|
|
|
var existingPlayer = _user.GetUserById(newUser.Id).Result;
|
|
if (existingPlayer != null)
|
|
{
|
|
return Conflict(new { message = "A user with this ID already exists." });
|
|
}
|
|
|
|
_user.AddUser(newUser);
|
|
|
|
return CreatedAtAction(nameof(GetAllUsers), new { id = newUser.Id }, newUser);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Erreur interne du serveur." });
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//===================================== ROUTE DELETE =====================================
|
|
|
|
/// <summary>
|
|
/// Deletes a player by their ID.
|
|
/// </summary>
|
|
/// <param name="id">The ID of the player to be deleted</param>
|
|
/// <returns></returns>
|
|
/// <remarks>
|
|
/// ## **Sample request**:
|
|
///
|
|
/// DELETE /api/v1/players?id=51
|
|
///
|
|
/// The `id` parameter specifies the ID of the player to be deleted.
|
|
///
|
|
/// ## **Returns**
|
|
///
|
|
/// - **200 OK** : If the player was successfully deleted, a success message is returned.
|
|
/// - **404 Not Found** : If no player with the given ID is found.
|
|
/// - **500 Internal Server Error** : If an error occurs while deleting the player.
|
|
///
|
|
/// ## **Error Handling**
|
|
/// - If no player is found with the specified `id`, a `404 Not Found` response is returned with a message "Player not found."
|
|
/// - If there is an exception during the process (e.g., database errors), a `500 Internal Server Error` response is returned with a message "Internal server error."
|
|
/// </remarks>
|
|
/// <response code="200">Returns a success message indicating the player was deleted</response>
|
|
/// <response code="404">Not found if no player with the specified ID is found</response>
|
|
/// <response code="500">Internal server error in case of an exception</response>
|
|
[HttpDelete] // /api/v1/players?id=51
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
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)
|
|
{
|
|
return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal server error." });
|
|
}
|
|
}
|
|
}
|
|
}
|