refactor: ProfileControlsTFController mit benutzerdefinierter GetAsync-Methode erweitert
- Neue GetAsync-Methode hinzugefügt, um komplexe Filter mit optionalen Parametern zu unterstützen - Verbesserte Fehlerprotokollierung und Validierung für die Extraktion der Benutzeridentität - Benutzer-ID in den Service-Schichtenoperationen integriert - Basismethode GetAll entfernt, um eine bessere Kontrolle über das Datenabrufen zu gewährleisten
This commit is contained in:
parent
eb45c6aefa
commit
6d25f8d3bd
@ -1,4 +1,5 @@
|
|||||||
using DigitalData.Core.API;
|
using DigitalData.Core.API;
|
||||||
|
using DigitalData.Core.DTO;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using WorkFlow.Application.Contracts;
|
using WorkFlow.Application.Contracts;
|
||||||
@ -12,5 +13,44 @@ namespace WorkFlow.API.Controllers
|
|||||||
[Authorize]
|
[Authorize]
|
||||||
public class ProfileControlsTFController(ILogger<ProfileControlsTFController> logger, IProfileControlsTFService service) : CRUDControllerBaseWithErrorHandling<IProfileControlsTFService, ProfileControlsTFCreateDto, ProfileControlsTFDto, ProfileControlsTFUpdateDto, ProfileControlsTF, int>(logger, service)
|
public class ProfileControlsTFController(ILogger<ProfileControlsTFController> logger, IProfileControlsTFService service) : CRUDControllerBaseWithErrorHandling<IProfileControlsTFService, ProfileControlsTFCreateDto, ProfileControlsTFDto, ProfileControlsTFUpdateDto, ProfileControlsTF, int>(logger, service)
|
||||||
{
|
{
|
||||||
|
[NonAction]
|
||||||
|
public override Task<IActionResult> GetAll() => base.GetAll();
|
||||||
|
|
||||||
|
[HttpGet]
|
||||||
|
public async Task<IActionResult> GetAsync(
|
||||||
|
bool withProfile = true, bool withUser = false,
|
||||||
|
int? profileId = null, int? objId = null, bool? profileActive = null)
|
||||||
|
{
|
||||||
|
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 null)
|
||||||
|
{
|
||||||
|
logger.LogError("Invalid user ID: Retrieved ID is null or not an integer.");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "Invalid user ID.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return await _service.ReadAsync(
|
||||||
|
withProfile: withProfile, withUser: withUser,
|
||||||
|
userId: id,
|
||||||
|
profileId: profileId, objId: objId, profileActive: profileActive)
|
||||||
|
.ThenAsync(
|
||||||
|
Success: pctf => pctf.Any() ? Ok(pctf) : NotFound(),
|
||||||
|
Fail: IActionResult (msg, ntc) =>
|
||||||
|
{
|
||||||
|
logger.LogNotice(ntc);
|
||||||
|
return NotFound();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
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.");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -34,10 +34,10 @@ namespace WorkFlow.API.Controllers
|
|||||||
return StatusCode(StatusCodes.Status500InternalServerError, "Invalid user ID.");
|
return StatusCode(StatusCodes.Status500InternalServerError, "Invalid user ID.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch(Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
logger.LogError(exception: ex, "{message}", ex.Message);
|
logger.LogError(ex, "An unexpected error occurred while processing the request: {Message}", ex.Message);
|
||||||
return StatusCode(StatusCodes.Status500InternalServerError);
|
return StatusCode(StatusCodes.Status500InternalServerError, "An internal server error occurred.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user