From 27e4b4b2ef60a361c86c61c6b3633709295375ca Mon Sep 17 00:00:00 2001 From: TekH Date: Tue, 29 Jul 2025 22:09:11 +0200 Subject: [PATCH] feat(ReadProfile): add logic to read buttons --- .../DTO/Profile/ProfileDto.cs | 12 ----- .../ProfileControlsTF/ProfileControlsTFDto.cs | 1 - .../DTO/ProfileObjState/ProfileObjStateDto.cs | 1 - src/WorkFlow.Application/MappingProfile.cs | 1 + .../Profiles/ReadProfile.cs | 49 ++++++++++++++----- .../DependencyInjection.cs | 1 + .../Repositories/ButtonRepository.cs | 3 ++ 7 files changed, 43 insertions(+), 25 deletions(-) delete mode 100644 src/WorkFlow.Application/DTO/Profile/ProfileDto.cs diff --git a/src/WorkFlow.Application/DTO/Profile/ProfileDto.cs b/src/WorkFlow.Application/DTO/Profile/ProfileDto.cs deleted file mode 100644 index 750b2ac..0000000 --- a/src/WorkFlow.Application/DTO/Profile/ProfileDto.cs +++ /dev/null @@ -1,12 +0,0 @@ -namespace WorkFlow.Application.DTO.Profile -{ - public record ProfileDto(int Id, - string IntlName, - int ExtId1, - bool Active, - byte TypeId, - string AddedWho, - DateTime AddedWhen, - string? ChangedWho = null, - DateTime? ChangedWhen = null); -} \ No newline at end of file diff --git a/src/WorkFlow.Application/DTO/ProfileControlsTF/ProfileControlsTFDto.cs b/src/WorkFlow.Application/DTO/ProfileControlsTF/ProfileControlsTFDto.cs index 2e5f68b..f526e64 100644 --- a/src/WorkFlow.Application/DTO/ProfileControlsTF/ProfileControlsTFDto.cs +++ b/src/WorkFlow.Application/DTO/ProfileControlsTF/ProfileControlsTFDto.cs @@ -16,6 +16,5 @@ namespace WorkFlow.Application.DTO.ProfileControlsTF string AddedWho, DateTime AddedWhen, string? ChoiceList = null, - ProfileDto? Profile = null, UserReadDto? User = null); } \ No newline at end of file diff --git a/src/WorkFlow.Application/DTO/ProfileObjState/ProfileObjStateDto.cs b/src/WorkFlow.Application/DTO/ProfileObjState/ProfileObjStateDto.cs index cc26d01..df4d354 100644 --- a/src/WorkFlow.Application/DTO/ProfileObjState/ProfileObjStateDto.cs +++ b/src/WorkFlow.Application/DTO/ProfileObjState/ProfileObjStateDto.cs @@ -14,7 +14,6 @@ namespace WorkFlow.Application.DTO.ProfileObjState string? State2 = null, string? State3 = null, string? State4 = null, - ProfileDto? Profile = null, UserReadDto? User = null, StateDto? State = null); } \ No newline at end of file diff --git a/src/WorkFlow.Application/MappingProfile.cs b/src/WorkFlow.Application/MappingProfile.cs index 4fad9d3..5468aa9 100644 --- a/src/WorkFlow.Application/MappingProfile.cs +++ b/src/WorkFlow.Application/MappingProfile.cs @@ -5,6 +5,7 @@ using WorkFlow.Application.DTO.Profile; using WorkFlow.Application.DTO.ProfileControlsTF; using WorkFlow.Application.DTO.ProfileObjState; using WorkFlow.Application.DTO.State; +using WorkFlow.Application.Profiles; using WorkFlow.Domain.Entities; namespace WorkFlow.Application diff --git a/src/WorkFlow.Application/Profiles/ReadProfile.cs b/src/WorkFlow.Application/Profiles/ReadProfile.cs index fb6fd1c..c248966 100644 --- a/src/WorkFlow.Application/Profiles/ReadProfile.cs +++ b/src/WorkFlow.Application/Profiles/ReadProfile.cs @@ -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? Buttons { get; set; } = Array.Empty(); +} + /// /// 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, bool IncludeObject = true) : IRequest; +public record ReadProfileRequest(int UserId, bool IncludeObject = true) : IRequest; /// -/// Handles the request by retrieving the user profile +/// Handles the request by retrieving the user profile /// from the data store using the . /// -public class ReadProfileHandler : IRequestHandler +public class ReadProfileHandler : IRequestHandler { private readonly IProfileRepository _profileRepository; @@ -21,42 +42,48 @@ public class ReadProfileHandler : IRequestHandler /// Initializes a new instance of the class. /// /// The profile repository used to access profile data. /// The profile object repository used to access object data. - 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; } /// - /// Handles the request by retrieving the profile + /// 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) + public async Task 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(profile); + + if (profile?.Id is int pId) { var bttns = await _bttnRepository.ReadAllAsync(pId); + profileDto.Buttons = _mapper.Map>(bttns); } - return profile; + return profileDto; } } public static class ReadProfileExtensions { - public static Task ReadProfileAsync(this IMediator mediator, int userId, bool includeObject = true) - => mediator.Send(new ReadProfile(UserId: userId, IncludeObject: includeObject)); + public static Task ReadProfileAsync(this IMediator mediator, int userId, bool includeObject = true) + => mediator.Send(new ReadProfileRequest(UserId: userId, IncludeObject: includeObject)); } \ No newline at end of file diff --git a/src/WorkFlow.Infrastructure/DependencyInjection.cs b/src/WorkFlow.Infrastructure/DependencyInjection.cs index 2ecdf2b..5f92956 100644 --- a/src/WorkFlow.Infrastructure/DependencyInjection.cs +++ b/src/WorkFlow.Infrastructure/DependencyInjection.cs @@ -15,6 +15,7 @@ public static class DependencyInjection services.TryAddScoped(); services.TryAddScoped(); services.TryAddScoped(); + services.TryAddScoped(); return services; } } \ No newline at end of file diff --git a/src/WorkFlow.Infrastructure/Repositories/ButtonRepository.cs b/src/WorkFlow.Infrastructure/Repositories/ButtonRepository.cs index ed7d993..5e9d0d1 100644 --- a/src/WorkFlow.Infrastructure/Repositories/ButtonRepository.cs +++ b/src/WorkFlow.Infrastructure/Repositories/ButtonRepository.cs @@ -1,5 +1,6 @@ using DigitalData.Core.Abstractions.Infrastructure; using DigitalData.Core.Infrastructure; +using Microsoft.EntityFrameworkCore; using WorkFlow.Application.Contracts.Repositories; using WorkFlow.Domain.Entities; @@ -11,4 +12,6 @@ public class ButtonRepository : CRUDRepository, IButto public ButtonRepository(WFDBContext dbContext) : base(dbContext, dbContext.Buttons) { } + + public async Task> ReadAllAsync(int profileId) => await _dbSet.Where(b => b.ProfileId == profileId).ToListAsync(); } \ No newline at end of file