From 709ebea09720c72a7c20b75205e6f89eab707bbe Mon Sep 17 00:00:00 2001 From: TekH Date: Fri, 1 Aug 2025 01:41:21 +0200 Subject: [PATCH] Refactor profile handling and error management - Updated `ProfileController` to always return `Ok(profile)` instead of `NotFound()`. - Changed `ReadProfileQuery` to return a non-nullable `ProfileDto`, throwing `NotFoundException` if not found. - Modified `ReadProfileHandler` to handle the new return type and throw exceptions appropriately. - Adjusted `ReadProfileAsync` to align with the new non-nullable return type. - Added dependency on `DigitalData.Core.Exceptions` for improved error handling. --- .../Controllers/ProfileController.cs | 2 +- .../Profiles/ReadProfileQuery.cs | 17 ++++++++++------- .../WorkFlow.Application.csproj | 1 + 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/WorkFlow.API/Controllers/ProfileController.cs b/src/WorkFlow.API/Controllers/ProfileController.cs index 7891e9f..0926e6a 100644 --- a/src/WorkFlow.API/Controllers/ProfileController.cs +++ b/src/WorkFlow.API/Controllers/ProfileController.cs @@ -32,6 +32,6 @@ public class ProfileController : ControllerBase } var profile = await _mediator.ReadProfileAsync(userId); - return profile is null ? NotFound() : Ok(profile); + return Ok(profile); } } \ No newline at end of file diff --git a/src/WorkFlow.Application/Profiles/ReadProfileQuery.cs b/src/WorkFlow.Application/Profiles/ReadProfileQuery.cs index 6717d61..b3c1fed 100644 --- a/src/WorkFlow.Application/Profiles/ReadProfileQuery.cs +++ b/src/WorkFlow.Application/Profiles/ReadProfileQuery.cs @@ -1,5 +1,6 @@ using AutoMapper; using DigitalData.Core.Abstraction.Application.Repository; +using DigitalData.Core.Exceptions; using MediatR; using WorkFlow.Application.Buttons; using WorkFlow.Application.Contracts.Repositories; @@ -12,13 +13,13 @@ namespace WorkFlow.Application.Profiles; /// Represents a request to read a user profile by their user ID. /// /// The ID of the user whose profile is being requested. -public record ReadProfileQuery(int UserId, bool IncludeObject = true) : IRequest; +public record ReadProfileQuery(int UserId, bool IncludeObject = true) : IRequest; /// /// Handles the request by retrieving the user profile /// from the data store using the . /// -public class ReadProfileHandler : IRequestHandler +public class ReadProfileHandler : IRequestHandler { private readonly IProfileRepository _profileRepository; @@ -48,15 +49,17 @@ public class ReadProfileHandler : IRequestHandler /// The request containing the user ID. /// A cancellation token for the operation. /// The user profile if found; otherwise, null. - public async Task Handle(ReadProfileQuery request, CancellationToken cancel = default) + public async Task Handle(ReadProfileQuery request, CancellationToken cancel = default) { - var profile = await _profileRepository.ReadAsync(request.UserId, cancel); - if (request.IncludeObject && profile?.Id is int profileId) + var profile = await _profileRepository.ReadAsync(request.UserId, cancel) + ?? throw new NotFoundException(); + + if (request.IncludeObject && profile.Id is int profileId) profile.Objects = await _objRepository.ReadAsync(request.UserId, profileId, cancel); var profileDto = _mapper.Map(profile); - if (profile?.Id is int pId) + if (profile.Id is int pId) { var bttns = await _bttnRepository.Read(b => b.ProfileId == pId).ToListAsync(cancel); profileDto.Buttons = _mapper.Map>(bttns); @@ -68,6 +71,6 @@ public class ReadProfileHandler : IRequestHandler public static class ReadProfileQueryExtensions { - public static Task ReadProfileAsync(this IMediator mediator, int userId, bool includeObject = true) + public static Task ReadProfileAsync(this IMediator mediator, int userId, bool includeObject = true) => mediator.Send(new ReadProfileQuery(UserId: userId, IncludeObject: includeObject)); } \ No newline at end of file diff --git a/src/WorkFlow.Application/WorkFlow.Application.csproj b/src/WorkFlow.Application/WorkFlow.Application.csproj index 32f9ac0..366b099 100644 --- a/src/WorkFlow.Application/WorkFlow.Application.csproj +++ b/src/WorkFlow.Application/WorkFlow.Application.csproj @@ -9,6 +9,7 @@ +