feat(ReadProfile): add logic to read buttons
This commit is contained in:
parent
82eb03b420
commit
27e4b4b2ef
@ -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);
|
||||
}
|
||||
@ -16,6 +16,5 @@ namespace WorkFlow.Application.DTO.ProfileControlsTF
|
||||
string AddedWho,
|
||||
DateTime AddedWhen,
|
||||
string? ChoiceList = null,
|
||||
ProfileDto? Profile = null,
|
||||
UserReadDto? User = null);
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
@ -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
|
||||
|
||||
@ -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<ButtonDto>? Buttons { get; set; } = Array.Empty<ButtonDto>();
|
||||
}
|
||||
|
||||
/// <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?>;
|
||||
public record ReadProfileRequest(int UserId, bool IncludeObject = true) : IRequest<ProfileDto?>;
|
||||
|
||||
/// <summary>
|
||||
/// Handles the <see cref="ReadProfile"/> request by retrieving the user profile
|
||||
/// Handles the <see cref="ReadProfileRequest"/> request by retrieving the user profile
|
||||
/// from the data store using the <see cref="IProfileRepository"/>.
|
||||
/// </summary>
|
||||
public class ReadProfileHandler : IRequestHandler<ReadProfile, Domain.Entities.Profile?>
|
||||
public class ReadProfileHandler : IRequestHandler<ReadProfileRequest, ProfileDto?>
|
||||
{
|
||||
private readonly IProfileRepository _profileRepository;
|
||||
|
||||
@ -21,42 +42,48 @@ public class ReadProfileHandler : IRequestHandler<ReadProfile, Domain.Entities.P
|
||||
|
||||
private readonly IButtonRepository _bttnRepository;
|
||||
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
/// <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, IButtonRepository buttonRepository)
|
||||
public ReadProfileHandler(IProfileRepository profileRepository, IProfileObjRepository objRepository, IButtonRepository buttonRepository, IMapper mapper)
|
||||
{
|
||||
_profileRepository = profileRepository;
|
||||
_objRepository = objRepository;
|
||||
_bttnRepository = buttonRepository;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles the <see cref="ReadProfile"/> request by retrieving the profile
|
||||
/// Handles the <see cref="ReadProfileRequest"/> 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)
|
||||
public async Task<ProfileDto?> 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);
|
||||
|
||||
var profileDto = _mapper.Map<ProfileDto>(profile);
|
||||
|
||||
if (profile?.Id is int pId)
|
||||
{
|
||||
var bttns = await _bttnRepository.ReadAllAsync(pId);
|
||||
profileDto.Buttons = _mapper.Map<IEnumerable<ButtonDto>>(bttns);
|
||||
}
|
||||
|
||||
return profile;
|
||||
return profileDto;
|
||||
}
|
||||
}
|
||||
|
||||
public static class ReadProfileExtensions
|
||||
{
|
||||
public static Task<Domain.Entities.Profile?> ReadProfileAsync(this IMediator mediator, int userId, bool includeObject = true)
|
||||
=> mediator.Send(new ReadProfile(UserId: userId, IncludeObject: includeObject));
|
||||
public static Task<ProfileDto?> ReadProfileAsync(this IMediator mediator, int userId, bool includeObject = true)
|
||||
=> mediator.Send(new ReadProfileRequest(UserId: userId, IncludeObject: includeObject));
|
||||
}
|
||||
@ -15,6 +15,7 @@ public static class DependencyInjection
|
||||
services.TryAddScoped<IProfileRepository, ProfileRepository>();
|
||||
services.TryAddScoped<IProfileObjRepository, ProfileObjRepository>();
|
||||
services.TryAddScoped<IStateRepository, StateRepository>();
|
||||
services.TryAddScoped<IButtonRepository, ButtonRepository>();
|
||||
return services;
|
||||
}
|
||||
}
|
||||
@ -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<Button, int, WFDBContext>, IButto
|
||||
public ButtonRepository(WFDBContext dbContext) : base(dbContext, dbContext.Buttons)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Button>> ReadAllAsync(int profileId) => await _dbSet.Where(b => b.ProfileId == profileId).ToListAsync();
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user