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.
This commit is contained in:
parent
63df235943
commit
709ebea097
@ -32,6 +32,6 @@ public class ProfileController : ControllerBase
|
||||
}
|
||||
|
||||
var profile = await _mediator.ReadProfileAsync(userId);
|
||||
return profile is null ? NotFound() : Ok(profile);
|
||||
return Ok(profile);
|
||||
}
|
||||
}
|
||||
@ -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.
|
||||
/// </summary>
|
||||
/// <param name="UserId">The ID of the user whose profile is being requested.</param>
|
||||
public record ReadProfileQuery(int UserId, bool IncludeObject = true) : IRequest<ProfileDto?>;
|
||||
public record ReadProfileQuery(int UserId, bool IncludeObject = true) : IRequest<ProfileDto>;
|
||||
|
||||
/// <summary>
|
||||
/// Handles the <see cref="ReadProfileQuery"/> request by retrieving the user profile
|
||||
/// from the data store using the <see cref="IProfileRepository"/>.
|
||||
/// </summary>
|
||||
public class ReadProfileHandler : IRequestHandler<ReadProfileQuery, ProfileDto?>
|
||||
public class ReadProfileHandler : IRequestHandler<ReadProfileQuery, ProfileDto>
|
||||
{
|
||||
private readonly IProfileRepository _profileRepository;
|
||||
|
||||
@ -48,15 +49,17 @@ public class ReadProfileHandler : IRequestHandler<ReadProfileQuery, ProfileDto?>
|
||||
/// <param name="request">The request containing the user ID.</param>
|
||||
/// <param name="cancel">A cancellation token for the operation.</param>
|
||||
/// <returns>The user profile if found; otherwise, <c>null</c>.</returns>
|
||||
public async Task<ProfileDto?> Handle(ReadProfileQuery request, CancellationToken cancel = default)
|
||||
public async Task<ProfileDto> 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<ProfileDto>(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<IEnumerable<ButtonDto>>(bttns);
|
||||
@ -68,6 +71,6 @@ public class ReadProfileHandler : IRequestHandler<ReadProfileQuery, ProfileDto?>
|
||||
|
||||
public static class ReadProfileQueryExtensions
|
||||
{
|
||||
public static Task<ProfileDto?> ReadProfileAsync(this IMediator mediator, int userId, bool includeObject = true)
|
||||
public static Task<ProfileDto> ReadProfileAsync(this IMediator mediator, int userId, bool includeObject = true)
|
||||
=> mediator.Send(new ReadProfileQuery(UserId: userId, IncludeObject: includeObject));
|
||||
}
|
||||
@ -9,6 +9,7 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="DigitalData.Core.Abstraction.Application" Version="1.0.0" />
|
||||
<PackageReference Include="DigitalData.Core.Application" Version="3.3.4" />
|
||||
<PackageReference Include="DigitalData.Core.Exceptions" Version="1.0.1" />
|
||||
<PackageReference Include="MediatR" Version="13.0.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.20" />
|
||||
<PackageReference Include="UserManager" Version="1.1.2" />
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user