Refactored `InvokeRecActionCommand` and `ReadRecActionQuery` to leverage C# `record` types for immutability and improved data structure clarity. Introduced `ReadRecActionQueryBase` as a shared base record to separate common properties and logic. Updated `InvokeRecActionCommandHandler` to use the new `ToReadQuery` method for compatibility. These changes enhance code maintainability, reusability, and separation of concerns while preserving existing functionality.
31 lines
1.1 KiB
C#
31 lines
1.1 KiB
C#
using MediatR;
|
|
using DigitalData.Core.Abstraction.Application.Repository;
|
|
using ReC.Domain.Entities;
|
|
using AutoMapper;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using DigitalData.Core.Exceptions;
|
|
using ReC.Application.Common.Dto;
|
|
|
|
namespace ReC.Application.RecActions.Queries;
|
|
|
|
public record ReadRecActionQueryBase
|
|
{
|
|
public int ProfileId { get; init; }
|
|
|
|
public ReadRecActionQuery ToReadQuery() => new(this);
|
|
}
|
|
|
|
public record ReadRecActionQuery(ReadRecActionQueryBase Root) : ReadRecActionQueryBase(Root), IRequest<IEnumerable<RecActionDto>>;
|
|
|
|
public class ReadRecActionQueryHandler(IRepository<RecAction> repo, IMapper mapper) : IRequestHandler<ReadRecActionQuery, IEnumerable<RecActionDto>>
|
|
{
|
|
public async Task<IEnumerable<RecActionDto>> Handle(ReadRecActionQuery request, CancellationToken cancel)
|
|
{
|
|
var actions = await repo.Where(x => x.ProfileId == request.ProfileId).ToListAsync(cancel);
|
|
|
|
if(actions.Count != 0)
|
|
throw new NotFoundException($"No actions found for the profile {request.ProfileId}.");
|
|
|
|
return mapper.Map<IEnumerable<RecActionDto>>(actions);
|
|
}
|
|
} |