diff --git a/WorkFlow.API/Controllers/ProfileControlsTFController.cs b/WorkFlow.API/Controllers/ProfileControlsTFController.cs index 82b63df..e245bbb 100644 --- a/WorkFlow.API/Controllers/ProfileControlsTFController.cs +++ b/WorkFlow.API/Controllers/ProfileControlsTFController.cs @@ -11,11 +11,14 @@ namespace WorkFlow.API.Controllers [Route("api/[controller]")] [ApiController] [Authorize] - public class ProfileControlsTFController(ILogger logger, IProfileControlsTFService service) : CRUDControllerBaseWithErrorHandling(logger, service) + public class ProfileControlsTFController(ILogger logger, IProfileControlsTFService service) : CRUDControllerBase(logger, service) { [NonAction] public override Task GetAll() => base.GetAll(); + [NonAction] + public override Task Update(ProfileControlsTFUpdateDto updateDto) => base.Update(updateDto); + [HttpGet] public async Task GetAsync( bool withProfile = true, bool withUser = false, @@ -52,5 +55,63 @@ namespace WorkFlow.API.Controllers return StatusCode(StatusCodes.Status500InternalServerError, "An internal server error occurred."); } } + + [HttpPost] + public override async Task Create([FromBody] ProfileControlsTFCreateDto createDto) + { + 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."); + } + + if (createDto.UserId != id) + return Unauthorized(); + + return await base.Create(createDto); + } + 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."); + } + } + + public override async Task Delete([FromRoute] int id) + { + try + { + if (!this.TryGetUserId(out int? 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(), + Fail: IActionResult (msg, ntc) => + { + _logger.LogNotice(ntc); + return ntc.HasFlag(Flag.NotFound) ? NotFound() : StatusCode(StatusCodes.Status500InternalServerError); + }); + } + 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."); + } + } } } \ No newline at end of file