feat: Lesevorgang für Benutzerprofil mittels MediatR und Repository implementiert

- ReadProfile-Request eingeführt, um Benutzerprofil anhand der UserId abzurufen
- ReadProfileHandler hinzugefügt, der das Profil aus dem IProfileRepository liest
- Asynchrone Verarbeitung mit Unterstützung für CancellationToken integriert
This commit is contained in:
tekh 2025-07-24 13:22:12 +02:00
parent 14f5c73d43
commit c08c5aacf3
4 changed files with 46 additions and 25 deletions

View File

@ -11,11 +11,12 @@ public interface IProfileRepository
/// Retrieves the <see cref="Profile"/> associated with a given user ID by calling a database function.
/// </summary>
/// <param name="userId">The unique identifier of the user whose profile is to be retrieved.</param>
/// <param name="cancel">Propagates notification that operations should be canceled.</param>
/// <returns>
/// A task that represents the asynchronous operation. The task result contains the <see cref="Profile"/> object if found; otherwise, <c>null</c>.
/// </returns>
/// <remarks>
/// Logs an error if no profile is found, or if multiple profiles are returned, indicating potential data issues.
/// </remarks>
Task<Profile?> ReadAsync(int userId);
Task<Profile?> ReadAsync(int userId, CancellationToken cancel = default);
}

View File

@ -1,22 +0,0 @@
using MediatR;
using WorkFlow.Application.Contracts.Repositories;
namespace WorkFlow.Application.Profile;
public record ReadProfile(int UserId) : IRequest<Domain.Entities.Profile?>;
public class ReadProfileHandler : IRequestHandler<ReadProfile, Domain.Entities.Profile?>
{
private readonly IProfileRepository _repository;
public ReadProfileHandler(IProfileRepository repository)
{
_repository = repository;
}
public async Task<Domain.Entities.Profile?> Handle(ReadProfile request, CancellationToken cancellationToken)
{
var profile = await _repository.ReadAsync(request.UserId);
return profile;
}
}

View File

@ -0,0 +1,41 @@
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) : 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 _repository;
/// <summary>
/// Initializes a new instance of the <see cref="ReadProfileHandler"/> class.
/// </summary>
/// <param name="repository">The profile repository used to access profile data.</param>
public ReadProfileHandler(IProfileRepository repository)
{
_repository = repository;
}
/// <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 _repository.ReadAsync(request.UserId, cancel);
return profile;
}
}

View File

@ -28,17 +28,18 @@ public class ProfileRepository : IProfileRepository
/// Retrieves the <see cref="Profile"/> associated with a given user ID by calling a database function.
/// </summary>
/// <param name="userId">The unique identifier of the user whose profile is to be retrieved.</param>
/// <param name="cancel">Propagates notification that operations should be canceled.</param>
/// <returns>
/// A task that represents the asynchronous operation. The task result contains the <see cref="Profile"/> object if found; otherwise, <c>null</c>.
/// </returns>
/// <remarks>
/// Logs an error if no profile is found, or if multiple profiles are returned, indicating potential data issues.
/// </remarks>
public async Task<Profile?> ReadAsync(int userId)
public async Task<Profile?> ReadAsync(int userId, CancellationToken cancel = default)
{
var profiles = await _context.Profiles
.FromSqlRaw("SELECT * FROM FNMWF_GET_PROFILES ({0})", userId)
.ToListAsync();
.ToListAsync(cancel);
if (profiles == null || profiles.Count == 0)
{