diff --git a/WorkFlow.API/Controllers/ControllerExtensions.cs b/WorkFlow.API/Controllers/ControllerExtensions.cs index 342f51a..bf23c09 100644 --- a/WorkFlow.API/Controllers/ControllerExtensions.cs +++ b/WorkFlow.API/Controllers/ControllerExtensions.cs @@ -5,7 +5,7 @@ namespace WorkFlow.API.Controllers { public static class ControllerExtensions { - public static bool? TryGetUserId(this ControllerBase controller, out int id) + public static bool TryGetUserId(this ControllerBase controller, out int? id) { var value = controller.User.FindFirstValue(ClaimTypes.NameIdentifier); @@ -22,7 +22,7 @@ namespace WorkFlow.API.Controllers } else { - id = default; + id = null; return false; } } diff --git a/WorkFlow.API/Controllers/UserController.cs b/WorkFlow.API/Controllers/UserController.cs new file mode 100644 index 0000000..0ea9646 --- /dev/null +++ b/WorkFlow.API/Controllers/UserController.cs @@ -0,0 +1,44 @@ +using DigitalData.Core.DTO; +using DigitalData.UserManager.Application.Contracts; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace WorkFlow.API.Controllers +{ + [Route("api/[controller]")] + [ApiController] + [Authorize] + public class UserController(ILogger logger, IUserService userService) : ControllerBase + { + [HttpGet] + public async Task GetAsync() + { + try + { + if (!this.TryGetUserId(out int? id)) + { + logger.LogError("Authorization failed: User ID claim not found."); + return StatusCode(StatusCodes.Status500InternalServerError, "Failed to retrieve user identity."); + } + else if(id is int id_int) + return await userService.ReadByIdAsync(id_int).ThenAsync( + Success: Ok, + Fail: IActionResult (msg, ntc) => + { + logger.LogNotice(ntc); + return NotFound(); + }); + else + { + logger.LogError("Invalid user ID: Retrieved ID is null or not an integer."); + return StatusCode(StatusCodes.Status500InternalServerError, "Invalid user ID."); + } + } + catch(Exception ex) + { + logger.LogError(exception: ex, "{message}", ex.Message); + return StatusCode(StatusCodes.Status500InternalServerError); + } + } + } +} \ No newline at end of file