From c08c5aacf350308a8e616cf20c46b2f7657902bf Mon Sep 17 00:00:00 2001 From: TekH Date: Thu, 24 Jul 2025 13:22:12 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20Lesevorgang=20f=C3=BCr=20Benutzerprofil?= =?UTF-8?q?=20mittels=20MediatR=20und=20Repository=20implementiert=20-=20R?= =?UTF-8?q?eadProfile-Request=20eingef=C3=BChrt,=20um=20Benutzerprofil=20a?= =?UTF-8?q?nhand=20der=20UserId=20abzurufen=20-=20ReadProfileHandler=20hin?= =?UTF-8?q?zugef=C3=BCgt,=20der=20das=20Profil=20aus=20dem=20IProfileRepos?= =?UTF-8?q?itory=20liest=20-=20Asynchrone=20Verarbeitung=20mit=20Unterst?= =?UTF-8?q?=C3=BCtzung=20f=C3=BCr=20CancellationToken=20integriert?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Repositories/IProfileRepository.cs | 3 +- .../Profile/ReadProfile.cs | 22 ---------- .../Profiles/ReadProfile.cs | 41 +++++++++++++++++++ .../Repositories/ProfileRepository.cs | 5 ++- 4 files changed, 46 insertions(+), 25 deletions(-) delete mode 100644 src/WorkFlow.Application/Profile/ReadProfile.cs create mode 100644 src/WorkFlow.Application/Profiles/ReadProfile.cs diff --git a/src/WorkFlow.Application/Contracts/Repositories/IProfileRepository.cs b/src/WorkFlow.Application/Contracts/Repositories/IProfileRepository.cs index bbd5ad8..ee365eb 100644 --- a/src/WorkFlow.Application/Contracts/Repositories/IProfileRepository.cs +++ b/src/WorkFlow.Application/Contracts/Repositories/IProfileRepository.cs @@ -11,11 +11,12 @@ public interface IProfileRepository /// Retrieves the associated with a given user ID by calling a database function. /// /// The unique identifier of the user whose profile is to be retrieved. + /// Propagates notification that operations should be canceled. /// /// A task that represents the asynchronous operation. The task result contains the object if found; otherwise, null. /// /// /// Logs an error if no profile is found, or if multiple profiles are returned, indicating potential data issues. /// - Task ReadAsync(int userId); + Task ReadAsync(int userId, CancellationToken cancel = default); } \ No newline at end of file diff --git a/src/WorkFlow.Application/Profile/ReadProfile.cs b/src/WorkFlow.Application/Profile/ReadProfile.cs deleted file mode 100644 index d142cdc..0000000 --- a/src/WorkFlow.Application/Profile/ReadProfile.cs +++ /dev/null @@ -1,22 +0,0 @@ -using MediatR; -using WorkFlow.Application.Contracts.Repositories; - -namespace WorkFlow.Application.Profile; - -public record ReadProfile(int UserId) : IRequest; - -public class ReadProfileHandler : IRequestHandler -{ - private readonly IProfileRepository _repository; - - public ReadProfileHandler(IProfileRepository repository) - { - _repository = repository; - } - - public async Task Handle(ReadProfile request, CancellationToken cancellationToken) - { - var profile = await _repository.ReadAsync(request.UserId); - return profile; - } -} diff --git a/src/WorkFlow.Application/Profiles/ReadProfile.cs b/src/WorkFlow.Application/Profiles/ReadProfile.cs new file mode 100644 index 0000000..c70551d --- /dev/null +++ b/src/WorkFlow.Application/Profiles/ReadProfile.cs @@ -0,0 +1,41 @@ +using MediatR; +using WorkFlow.Application.Contracts.Repositories; + +namespace WorkFlow.Application.Profiles; + +/// +/// Represents a request to read a user profile by their user ID. +/// +/// The ID of the user whose profile is being requested. +public record ReadProfile(int UserId) : IRequest; + +/// +/// Handles the request by retrieving the user profile +/// from the data store using the . +/// +public class ReadProfileHandler : IRequestHandler +{ + private readonly IProfileRepository _repository; + + /// + /// Initializes a new instance of the class. + /// + /// The profile repository used to access profile data. + public ReadProfileHandler(IProfileRepository repository) + { + _repository = repository; + } + + /// + /// Handles the request by retrieving the profile + /// corresponding to the specified user ID. + /// + /// The request containing the user ID. + /// A cancellation token for the operation. + /// The user profile if found; otherwise, null. + public async Task Handle(ReadProfile request, CancellationToken cancel = default) + { + var profile = await _repository.ReadAsync(request.UserId, cancel); + return profile; + } +} diff --git a/src/WorkFlow.Infrastructure/Repositories/ProfileRepository.cs b/src/WorkFlow.Infrastructure/Repositories/ProfileRepository.cs index 210f56a..5c272e2 100644 --- a/src/WorkFlow.Infrastructure/Repositories/ProfileRepository.cs +++ b/src/WorkFlow.Infrastructure/Repositories/ProfileRepository.cs @@ -28,17 +28,18 @@ public class ProfileRepository : IProfileRepository /// Retrieves the associated with a given user ID by calling a database function. /// /// The unique identifier of the user whose profile is to be retrieved. + /// Propagates notification that operations should be canceled. /// /// A task that represents the asynchronous operation. The task result contains the object if found; otherwise, null. /// /// /// Logs an error if no profile is found, or if multiple profiles are returned, indicating potential data issues. /// - public async Task ReadAsync(int userId) + public async Task 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) {