feat(ReadProfile): add logic to read buttons
This commit is contained in:
@@ -1,19 +1,40 @@
|
||||
using MediatR;
|
||||
using AutoMapper;
|
||||
using MediatR;
|
||||
using WorkFlow.Application.Buttons;
|
||||
using WorkFlow.Application.Contracts.Repositories;
|
||||
|
||||
namespace WorkFlow.Application.Profiles;
|
||||
|
||||
public class ProfileDto
|
||||
{
|
||||
public int? Id { get; init; }
|
||||
|
||||
public byte? TypeId { get; init; }
|
||||
|
||||
public string? Caption { get; init; }
|
||||
|
||||
public string? Subtitle { get; init; }
|
||||
|
||||
public int? CountObj { get; init; }
|
||||
|
||||
public string? ForeColor { get; init; }
|
||||
|
||||
public string? BackColor { get; init; }
|
||||
|
||||
public IEnumerable<ButtonDto>? Buttons { get; set; } = Array.Empty<ButtonDto>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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 ReadProfile(int UserId, bool IncludeObject = true) : IRequest<Domain.Entities.Profile?>;
|
||||
public record ReadProfileRequest(int UserId, bool IncludeObject = true) : IRequest<ProfileDto?>;
|
||||
|
||||
/// <summary>
|
||||
/// Handles the <see cref="ReadProfile"/> request by retrieving the user profile
|
||||
/// Handles the <see cref="ReadProfileRequest"/> request by retrieving the user profile
|
||||
/// from the data store using the <see cref="IProfileRepository"/>.
|
||||
/// </summary>
|
||||
public class ReadProfileHandler : IRequestHandler<ReadProfile, Domain.Entities.Profile?>
|
||||
public class ReadProfileHandler : IRequestHandler<ReadProfileRequest, ProfileDto?>
|
||||
{
|
||||
private readonly IProfileRepository _profileRepository;
|
||||
|
||||
@@ -21,42 +42,48 @@ public class ReadProfileHandler : IRequestHandler<ReadProfile, Domain.Entities.P
|
||||
|
||||
private readonly IButtonRepository _bttnRepository;
|
||||
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ReadProfileHandler"/> class.
|
||||
/// </summary>
|
||||
/// <param name="profileRepository">The profile repository used to access profile data.</param>
|
||||
/// <param name="objRepository">The profile object repository used to access object data.</param>
|
||||
public ReadProfileHandler(IProfileRepository profileRepository, IProfileObjRepository objRepository, IButtonRepository buttonRepository)
|
||||
public ReadProfileHandler(IProfileRepository profileRepository, IProfileObjRepository objRepository, IButtonRepository buttonRepository, IMapper mapper)
|
||||
{
|
||||
_profileRepository = profileRepository;
|
||||
_objRepository = objRepository;
|
||||
_bttnRepository = buttonRepository;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles the <see cref="ReadProfile"/> request by retrieving the profile
|
||||
/// Handles the <see cref="ReadProfileRequest"/> request by retrieving the profile
|
||||
/// corresponding to the specified user ID.
|
||||
/// </summary>
|
||||
/// <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<Domain.Entities.Profile?> Handle(ReadProfile request, CancellationToken cancel = default)
|
||||
public async Task<ProfileDto?> Handle(ReadProfileRequest request, CancellationToken cancel = default)
|
||||
{
|
||||
var profile = await _profileRepository.ReadAsync(request.UserId, cancel);
|
||||
if (request.IncludeObject && profile?.Id is int profileId)
|
||||
profile.Objects = await _objRepository.ReadAsync(request.UserId, profileId, cancel);
|
||||
|
||||
if(profile?.Id is int pId)
|
||||
var profileDto = _mapper.Map<ProfileDto>(profile);
|
||||
|
||||
if (profile?.Id is int pId)
|
||||
{
|
||||
var bttns = await _bttnRepository.ReadAllAsync(pId);
|
||||
profileDto.Buttons = _mapper.Map<IEnumerable<ButtonDto>>(bttns);
|
||||
}
|
||||
|
||||
return profile;
|
||||
return profileDto;
|
||||
}
|
||||
}
|
||||
|
||||
public static class ReadProfileExtensions
|
||||
{
|
||||
public static Task<Domain.Entities.Profile?> ReadProfileAsync(this IMediator mediator, int userId, bool includeObject = true)
|
||||
=> mediator.Send(new ReadProfile(UserId: userId, IncludeObject: includeObject));
|
||||
public static Task<ProfileDto?> ReadProfileAsync(this IMediator mediator, int userId, bool includeObject = true)
|
||||
=> mediator.Send(new ReadProfileRequest(UserId: userId, IncludeObject: includeObject));
|
||||
}
|
||||
Reference in New Issue
Block a user