Refactoring (Controller): Aktualisierung zur Verwendung der aktuellen Version von TryGetUserId

This commit is contained in:
tekh 2025-07-25 10:44:12 +02:00
parent bed5fae01c
commit ad1fd3163e
4 changed files with 20 additions and 55 deletions

View File

@ -27,13 +27,13 @@ public class ProfileController : ControllerBase
{
try
{
if (!this.TryGetUserId(out var userId))
if (!User.TryGetUserId(out var userId))
{
_logger.LogError("Invalid user ID: Retrieved ID is null or not an integer.");
return Unauthorized("Failed to retrieve user identity.");
}
var profile = await _mediator.Send(new ReadProfile(UserId: userId ?? throw new InvalidOperationException("User ID is null")));
var profile = await _mediator.Send(new ReadProfile(UserId: userId));
return profile is null ? NotFound() : Ok(profile);
}
catch (Exception ex)

View File

@ -35,15 +35,10 @@ public class ProfileControlsTFController : CRUDControllerBase<IProfileControlsTF
{
try
{
if (!this.TryGetUserId(out int? id))
if (!User.TryGetUserId(out var id))
{
logger.LogError("Authorization failed: User ID claim not found.");
return StatusCode(StatusCodes.Status500InternalServerError, "Failed to retrieve user identity.");
}
else if (id is null)
{
logger.LogError("Invalid user ID: Retrieved ID is null or not an integer.");
return StatusCode(StatusCodes.Status500InternalServerError, "Invalid user ID.");
return Unauthorized("Failed to retrieve user identity.");
}
return await _service.ReadAsync(
@ -71,16 +66,11 @@ public class ProfileControlsTFController : CRUDControllerBase<IProfileControlsTF
{
try
{
if (!this.TryGetUserId(out int? id))
if (!User.TryGetUserId(out var id))
{
logger.LogError("Authorization failed: User ID claim not found.");
return StatusCode(StatusCodes.Status500InternalServerError, "Failed to retrieve user identity.");
}
else if (id is null)
{
logger.LogError("Invalid user ID: Retrieved ID is null or not an integer.");
return StatusCode(StatusCodes.Status500InternalServerError, "Invalid user ID.");
}
if (createDto.UserId != id)
return Unauthorized();
@ -100,16 +90,11 @@ public class ProfileControlsTFController : CRUDControllerBase<IProfileControlsTF
{
try
{
if (!this.TryGetUserId(out int? userId))
if (!User.TryGetUserId(out var userId))
{
logger.LogError("Authorization failed: User ID claim not found.");
return StatusCode(StatusCodes.Status500InternalServerError, "Failed to retrieve user identity.");
}
else if (userId is null)
{
logger.LogError("Invalid user ID: Retrieved ID is null or not an integer.");
return StatusCode(StatusCodes.Status500InternalServerError, "Invalid user ID.");
}
return await _service.ReadByIdAsync(id).ThenAsync(
SuccessAsync: async pctf => pctf.UserId == userId ? await base.Delete(id) : Unauthorized(),

View File

@ -35,15 +35,10 @@ namespace WorkFlow.API.Controllers
{
try
{
if (!this.TryGetUserId(out int? id))
if (!User.TryGetUserId(out var id))
{
logger.LogError("Authorization failed: User ID claim not found.");
return StatusCode(StatusCodes.Status500InternalServerError, "Failed to retrieve user identity.");
}
else if (id is null)
{
logger.LogError("Invalid user ID: Retrieved ID is null or not an integer.");
return StatusCode(StatusCodes.Status500InternalServerError, "Invalid user ID.");
return Unauthorized("Failed to retrieve user identity.");
}
return await _service.ReadAsync(
@ -70,16 +65,11 @@ namespace WorkFlow.API.Controllers
{
try
{
if (!this.TryGetUserId(out int? id))
if (!User.TryGetUserId(out var id))
{
logger.LogError("Authorization failed: User ID claim not found.");
return StatusCode(StatusCodes.Status500InternalServerError, "Failed to retrieve user identity.");
}
else if (id is null)
{
logger.LogError("Invalid user ID: Retrieved ID is null or not an integer.");
return StatusCode(StatusCodes.Status500InternalServerError, "Invalid user ID.");
}
if (createDto.UserId != id)
return Unauthorized();
@ -98,16 +88,11 @@ namespace WorkFlow.API.Controllers
{
try
{
if (!this.TryGetUserId(out int? userId))
if (!User.TryGetUserId(out var userId))
{
logger.LogError("Authorization failed: User ID claim not found.");
return StatusCode(StatusCodes.Status500InternalServerError, "Failed to retrieve user identity.");
}
else if (userId is null)
{
logger.LogError("Invalid user ID: Retrieved ID is null or not an integer.");
return StatusCode(StatusCodes.Status500InternalServerError, "Invalid user ID.");
}
return await _service.ReadByIdAsync(id).ThenAsync(
SuccessAsync: async pctf => pctf.UserId == userId ? await base.Delete(id) : Unauthorized(),

View File

@ -26,24 +26,19 @@ public class UserController : ControllerBase
{
try
{
if (!this.TryGetUserId(out int? id))
if (!User.TryGetUserId(out var 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.");
return Unauthorized("Failed to retrieve user identity.");
}
return await userService.ReadByIdAsync(id).ThenAsync(
Success: Ok,
Fail: IActionResult (msg, ntc) =>
{
logger.LogNotice(ntc);
return NotFound();
});
}
catch (Exception ex)
{