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:
parent
14f5c73d43
commit
c08c5aacf3
@ -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);
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
41
src/WorkFlow.Application/Profiles/ReadProfile.cs
Normal file
41
src/WorkFlow.Application/Profiles/ReadProfile.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
@ -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)
|
||||
{
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user