From 9c5346213e331d2e9d1a235815fd4d688f9b764b Mon Sep 17 00:00:00 2001 From: TekH Date: Mon, 9 Nov 2026 15:59:09 +0100 Subject: [PATCH] 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. --- .../Users/Queries/GetAuthorizedUsersQuery.cs | 22 ++++++++++++ .../Controllers/UsersController.cs | 36 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 src/core/DigitalData.TaskFlow.Application/Features/Users/Queries/GetAuthorizedUsersQuery.cs create mode 100644 src/presentation/DigitalData.TaskFlow.API/Controllers/UsersController.cs diff --git a/src/core/DigitalData.TaskFlow.Application/Features/Users/Queries/GetAuthorizedUsersQuery.cs b/src/core/DigitalData.TaskFlow.Application/Features/Users/Queries/GetAuthorizedUsersQuery.cs new file mode 100644 index 0000000..6c81a42 --- /dev/null +++ b/src/core/DigitalData.TaskFlow.Application/Features/Users/Queries/GetAuthorizedUsersQuery.cs @@ -0,0 +1,22 @@ +using DigitalData.TaskFlow.Application.Common.Interfaces; +using MediatR; + +namespace DigitalData.TaskFlow.Application.Features.Users.Queries; + +public sealed record GetAuthorizedUsersQuery : IRequest; + +public sealed record GetAuthorizedUsersResult(IEnumerable Meta, IEnumerable Wisagids); + +public sealed record GetAuthorizedUsersMeta(int? Code, string Message); + +public sealed class GetAuthorizedUsersQueryHandler(IUserRepository repo) : IRequestHandler +{ + public async Task 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)); + } +} diff --git a/src/presentation/DigitalData.TaskFlow.API/Controllers/UsersController.cs b/src/presentation/DigitalData.TaskFlow.API/Controllers/UsersController.cs new file mode 100644 index 0000000..a860daf --- /dev/null +++ b/src/presentation/DigitalData.TaskFlow.API/Controllers/UsersController.cs @@ -0,0 +1,36 @@ +using DigitalData.TaskFlow.Application.Features.Users.Queries; +using MediatR; +using Microsoft.AspNetCore.Mvc; + +namespace DigitalData.TaskFlow.API.Controllers; + +/// +/// +/// +/// +[ApiController] +[Route("api/users")] +public sealed class UsersController(ISender sender) : ControllerBase +{ + /// + /// + /// + /// + /// + [HttpGet("authorized")] + [ProducesResponseType(typeof(GetAuthorizedUsersResult), StatusCodes.Status200OK)] + [ProducesResponseType(typeof(GetAuthorizedUsersResult), StatusCodes.Status401Unauthorized)] + [ProducesResponseType(typeof(GetAuthorizedUsersResult), StatusCodes.Status500InternalServerError)] + public async Task 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) + }; + } +}