From ad1fd3163e72a9bbddaad2ca34d6b7700e35b1f4 Mon Sep 17 00:00:00 2001 From: TekH Date: Fri, 25 Jul 2025 10:44:12 +0200 Subject: [PATCH] Refactoring (Controller): Aktualisierung zur Verwendung der aktuellen Version von TryGetUserId --- .../Controllers/ProfileController.cs | 4 +-- .../ProfileControlsTFController.cs | 23 +++-------------- .../Controllers/ProfileObjStateController.cs | 23 +++-------------- .../Controllers/UserController.cs | 25 ++++++++----------- 4 files changed, 20 insertions(+), 55 deletions(-) diff --git a/src/WorkFlow.API/Controllers/ProfileController.cs b/src/WorkFlow.API/Controllers/ProfileController.cs index c2dc053..c0a2ac0 100644 --- a/src/WorkFlow.API/Controllers/ProfileController.cs +++ b/src/WorkFlow.API/Controllers/ProfileController.cs @@ -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) diff --git a/src/WorkFlow.API/Controllers/ProfileControlsTFController.cs b/src/WorkFlow.API/Controllers/ProfileControlsTFController.cs index 856e67e..9b22360 100644 --- a/src/WorkFlow.API/Controllers/ProfileControlsTFController.cs +++ b/src/WorkFlow.API/Controllers/ProfileControlsTFController.cs @@ -35,15 +35,10 @@ public class ProfileControlsTFController : CRUDControllerBase pctf.UserId == userId ? await base.Delete(id) : Unauthorized(), diff --git a/src/WorkFlow.API/Controllers/ProfileObjStateController.cs b/src/WorkFlow.API/Controllers/ProfileObjStateController.cs index 93b94f1..a688559 100644 --- a/src/WorkFlow.API/Controllers/ProfileObjStateController.cs +++ b/src/WorkFlow.API/Controllers/ProfileObjStateController.cs @@ -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(), diff --git a/src/WorkFlow.API/Controllers/UserController.cs b/src/WorkFlow.API/Controllers/UserController.cs index bc76eeb..8cf9ebd 100644 --- a/src/WorkFlow.API/Controllers/UserController.cs +++ b/src/WorkFlow.API/Controllers/UserController.cs @@ -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) {