- Flag „IncludeObject” in ReadProfile-Anfrage eingeführt - IProfileObjRepository in ReadProfileHandler eingefügt - Handler aktualisiert, um Profilobjekte zu laden, wenn IncludeObject wahr ist
49 lines
2.1 KiB
C#
49 lines
2.1 KiB
C#
using MediatR;
|
|
using WorkFlow.Application.Contracts.Repositories;
|
|
|
|
namespace WorkFlow.Application.Profiles;
|
|
|
|
/// <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?>;
|
|
|
|
/// <summary>
|
|
/// Handles the <see cref="ReadProfile"/> request by retrieving the user profile
|
|
/// from the data store using the <see cref="IProfileRepository"/>.
|
|
/// </summary>
|
|
public class ReadProfileHandler : IRequestHandler<ReadProfile, Domain.Entities.Profile?>
|
|
{
|
|
private readonly IProfileRepository _profileRepository;
|
|
|
|
private readonly IProfileObjRepository _objRepository;
|
|
|
|
/// <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)
|
|
{
|
|
_profileRepository = profileRepository;
|
|
_objRepository = objRepository;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Handles the <see cref="ReadProfile"/> 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)
|
|
{
|
|
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);
|
|
|
|
return profile;
|
|
}
|
|
}
|