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:
2026-11-09 15:59:09 +01:00
parent 6542d804f8
commit 9c5346213e
2 changed files with 58 additions and 0 deletions

View File

@@ -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));
}
}

View File

@@ -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)
};
}
}