Implement ReadResultViewQuery handler with filtering
Added ReadResultViewQueryHandler to support querying ResultView entities with optional filters (Id, ActionId, ProfileId). Integrated repository and AutoMapper, included error handling for no results, and improved using directives.
This commit is contained in:
@@ -1,5 +1,11 @@
|
||||
using MediatR;
|
||||
using AutoMapper;
|
||||
using DigitalData.Core.Abstraction.Application.Repository;
|
||||
using DigitalData.Core.Exceptions;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ReC.Application.Common.Dto;
|
||||
using ReC.Domain.Entities;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace ReC.Application.ResultViews.Queries;
|
||||
|
||||
@@ -10,4 +16,34 @@ public record ReadResultViewQuery : IRequest<IEnumerable<ResultViewDto>>
|
||||
public long? ActionId { get; init; } = null;
|
||||
|
||||
public long? ProfileId { get; init; } = null;
|
||||
}
|
||||
|
||||
public class ReadResultViewQueryHandler(IRepository<ResultView> repo, IMapper mapper) : IRequestHandler<ReadResultViewQuery, IEnumerable<ResultViewDto>>
|
||||
{
|
||||
public async Task<IEnumerable<ResultViewDto>> Handle(ReadResultViewQuery request, CancellationToken cancel)
|
||||
{
|
||||
var q = repo.Query;
|
||||
|
||||
if(request.Id is long id)
|
||||
q = q.Where(rv => rv.Id == id);
|
||||
|
||||
if(request.ActionId is long actionId)
|
||||
q = q.Where(rv => rv.ActionId == actionId);
|
||||
|
||||
if(request.ProfileId is long profileId)
|
||||
q = q.Where(rv => rv.ProfileId == profileId);
|
||||
|
||||
var entities = await q.ToListAsync(cancel);
|
||||
|
||||
if (entities.Count == 0)
|
||||
throw new NotFoundException($"No result views found for the given criteria. Criteria: {
|
||||
JsonSerializer.Serialize(request, options: new()
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
WriteIndented = true
|
||||
})}"
|
||||
);
|
||||
|
||||
return mapper.Map<IEnumerable<ResultViewDto>>(entities);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user