Add GetAuthorizedUsersQuery and UsersController
Introduced the `GetAuthorizedUsersQuery` feature to retrieve
authorized users, including the query, result, metadata, and
handler implementation. The handler interacts with the
`IUserRepository` to fetch user data.
Added a new `UsersController` with an `[HttpGet("authorized")]`
endpoint to expose the query via a RESTful API. The endpoint
returns appropriate HTTP status codes (`200 OK`, `401
Unauthorized`, `500 Internal Server Error`) based on the
result metadata.
Included necessary `using` directives for MediatR, ASP.NET
Core, and application features.
This commit is contained in:
@@ -0,0 +1,22 @@
|
|||||||
|
using DigitalData.TaskFlow.Application.Common.Interfaces;
|
||||||
|
using MediatR;
|
||||||
|
|
||||||
|
namespace DigitalData.TaskFlow.Application.Features.Users.Queries;
|
||||||
|
|
||||||
|
public sealed record GetAuthorizedUsersQuery : IRequest<GetAuthorizedUsersResult>;
|
||||||
|
|
||||||
|
public sealed record GetAuthorizedUsersResult(IEnumerable<GetAuthorizedUsersMeta> Meta, IEnumerable<int> Wisagids);
|
||||||
|
|
||||||
|
public sealed record GetAuthorizedUsersMeta(int? Code, string Message);
|
||||||
|
|
||||||
|
public sealed class GetAuthorizedUsersQueryHandler(IUserRepository repo) : IRequestHandler<GetAuthorizedUsersQuery, GetAuthorizedUsersResult>
|
||||||
|
{
|
||||||
|
public async Task<GetAuthorizedUsersResult> Handle(GetAuthorizedUsersQuery request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var users = await repo.GetAuthorizedAsync(cancellationToken);
|
||||||
|
|
||||||
|
return new GetAuthorizedUsersResult(
|
||||||
|
Meta: [new GetAuthorizedUsersMeta(Code: 0, Message: "OK")],
|
||||||
|
Wisagids: users.Select(x => x.WisagId));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
using DigitalData.TaskFlow.Application.Features.Users.Queries;
|
||||||
|
using MediatR;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
namespace DigitalData.TaskFlow.API.Controllers;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="sender"></param>
|
||||||
|
[ApiController]
|
||||||
|
[Route("api/users")]
|
||||||
|
public sealed class UsersController(ISender sender) : ControllerBase
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpGet("authorized")]
|
||||||
|
[ProducesResponseType(typeof(GetAuthorizedUsersResult), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(typeof(GetAuthorizedUsersResult), StatusCodes.Status401Unauthorized)]
|
||||||
|
[ProducesResponseType(typeof(GetAuthorizedUsersResult), StatusCodes.Status500InternalServerError)]
|
||||||
|
public async Task<IActionResult> GetAuthorizedAsync(CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var result = await sender.Send(new GetAuthorizedUsersQuery(), cancellationToken);
|
||||||
|
var code = result.Meta.FirstOrDefault()?.Code;
|
||||||
|
|
||||||
|
return code switch
|
||||||
|
{
|
||||||
|
0 => Ok(result),
|
||||||
|
4 => Unauthorized(result),
|
||||||
|
_ => StatusCode(StatusCodes.Status500InternalServerError, result)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user