Files
ReC/src/ReC.Application/RecActions/Queries/ReadRecActionQuery.cs
TekH cb851a4ec6 Refactor RecAction commands and queries to use records
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.
2025-11-27 10:56:21 +01:00

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);
}
}