46 lines
1.7 KiB
C#
46 lines
1.7 KiB
C#
using DigitalData.Core.DTO;
|
|
using DigitalData.UserManager.Application.Contracts;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using WorkFlow.API.Attributes;
|
|
|
|
namespace WorkFlow.API.Controllers
|
|
{
|
|
[APIKeyAuth]
|
|
[Route("api/[controller]")]
|
|
[ApiController]
|
|
[Authorize]
|
|
public class UserController(ILogger<UserController> logger, IUserService userService) : ControllerBase
|
|
{
|
|
[HttpGet]
|
|
public async Task<IActionResult> 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(ex, "An unexpected error occurred while processing the request: {Message}", ex.Message);
|
|
return StatusCode(StatusCodes.Status500InternalServerError, "An internal server error occurred.");
|
|
}
|
|
}
|
|
}
|
|
} |