- Enabled XML documentation generation for improved API documentation. - Updated API descriptions and added detailed endpoint information for better clarity. - Introduced Swagger annotations for authentication and user management endpoints. - Enhanced response types and added pagination information in user-related responses. - Included contact and license information in the API metadata for better transparency.
113 lines
4.2 KiB
C#
113 lines
4.2 KiB
C#
using Asp.Versioning;
|
|
using MediatR;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using OpenIddict.Validation.AspNetCore;
|
|
using Swashbuckle.AspNetCore.Annotations;
|
|
using IamService.API.Application.Common;
|
|
using IamService.API.Application.Queries.Users;
|
|
|
|
namespace IamService.API.Controllers;
|
|
|
|
/// <summary>
|
|
/// EN: Users management controller.
|
|
/// VI: Controller quản lý users.
|
|
/// </summary>
|
|
[ApiController]
|
|
[ApiVersion("1.0")]
|
|
[Route("api/v{version:apiVersion}/users")]
|
|
[Authorize(AuthenticationSchemes = OpenIddictValidationAspNetCoreDefaults.AuthenticationScheme)]
|
|
[SwaggerTag("User management endpoints - requires authentication")]
|
|
public class UsersController : ControllerBase
|
|
{
|
|
private readonly IMediator _mediator;
|
|
private readonly ILogger<UsersController> _logger;
|
|
|
|
public UsersController(
|
|
IMediator mediator,
|
|
ILogger<UsersController> logger)
|
|
{
|
|
_mediator = mediator;
|
|
_logger = logger;
|
|
}
|
|
|
|
/// <summary>
|
|
/// EN: Get all users with pagination.
|
|
/// VI: Lấy tất cả users với phân trang.
|
|
/// </summary>
|
|
/// <param name="pageNumber">Page number (1-based)</param>
|
|
/// <param name="pageSize">Number of items per page</param>
|
|
/// <param name="cancellationToken">Cancellation token</param>
|
|
/// <returns>Paginated list of users</returns>
|
|
[HttpGet]
|
|
[SwaggerOperation(
|
|
Summary = "Get all users",
|
|
Description = "Retrieves a paginated list of all users. Requires authentication.",
|
|
OperationId = "GetUsers")]
|
|
[SwaggerResponse(StatusCodes.Status200OK, "Successfully retrieved users", typeof(ApiResponse<IEnumerable<UserDto>>))]
|
|
[SwaggerResponse(StatusCodes.Status401Unauthorized, "Authentication required")]
|
|
[ProducesResponseType(typeof(ApiResponse<IEnumerable<UserDto>>), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
|
public async Task<IActionResult> GetUsers(
|
|
[FromQuery, SwaggerParameter("Page number (1-based)", Required = false)] int pageNumber = 1,
|
|
[FromQuery, SwaggerParameter("Number of items per page", Required = false)] int pageSize = 10,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
var query = new GetUsersQuery(pageNumber, pageSize);
|
|
var result = await _mediator.Send(query, cancellationToken);
|
|
|
|
return Ok(new ApiResponse<IEnumerable<UserDto>>
|
|
{
|
|
Success = true,
|
|
Data = result.Users.Select(u => new UserDto
|
|
{
|
|
Id = u.Id,
|
|
Email = u.Email ?? string.Empty,
|
|
FirstName = u.FirstName,
|
|
LastName = u.LastName,
|
|
FullName = u.FullName,
|
|
Status = u.Status,
|
|
CreatedAt = u.CreatedAt,
|
|
LastLoginAt = u.LastLoginAt
|
|
}),
|
|
Pagination = new PaginationInfo
|
|
{
|
|
PageNumber = result.PageNumber,
|
|
PageSize = result.PageSize,
|
|
TotalCount = result.TotalCount
|
|
}
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// EN: Get current user info.
|
|
/// VI: Lấy thông tin user hiện tại.
|
|
/// </summary>
|
|
/// <returns>Current user information</returns>
|
|
[HttpGet("me")]
|
|
[SwaggerOperation(
|
|
Summary = "Get current user",
|
|
Description = "Retrieves information about the currently authenticated user.",
|
|
OperationId = "GetCurrentUser")]
|
|
[SwaggerResponse(StatusCodes.Status200OK, "Successfully retrieved current user", typeof(ApiResponse<CurrentUserDto>))]
|
|
[SwaggerResponse(StatusCodes.Status401Unauthorized, "Authentication required")]
|
|
[ProducesResponseType(typeof(ApiResponse<CurrentUserDto>), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
|
public IActionResult GetCurrentUser()
|
|
{
|
|
var userId = User.FindFirst("sub")?.Value;
|
|
var email = User.FindFirst("email")?.Value;
|
|
var name = User.FindFirst("name")?.Value;
|
|
var roles = User.FindAll("role").Select(c => c.Value);
|
|
|
|
return Ok(ApiResponse<CurrentUserDto>.Ok(new CurrentUserDto
|
|
{
|
|
Id = userId ?? string.Empty,
|
|
Email = email,
|
|
Name = name,
|
|
Roles = roles
|
|
}));
|
|
}
|
|
}
|
|
|