using AutoMapper; using DigitalData.Core.Abstraction.Application.Repository; using EnvelopeGenerator.Application.Common.Dto; using EnvelopeGenerator.Domain.Entities; using MediatR; using Microsoft.EntityFrameworkCore; namespace EnvelopeGenerator.Application.ThirdPartyModules.Queries; /// /// /// public record ReadThirdPartyModuleQuery : IRequest> { /// /// /// public string? Name { get; init; } /// /// /// public bool? Active { get; init; } } /// /// /// public record ReadThirdPartyModuleQueryHandler : IRequestHandler> { private readonly IMapper _mapper; private readonly IRepository _repo; /// /// /// /// /// public ReadThirdPartyModuleQueryHandler(IMapper mapper, IRepository repo) { _mapper = mapper; _repo = repo; } /// /// /// /// /// /// /// public async Task> Handle(ReadThirdPartyModuleQuery request, CancellationToken cancel) { var query = _repo.Query; if(request.Name is string name) query = query.Where(m => m.Name == name); if (request.Active is bool active) query = query.Where(m => m.Active == active); var modules = await query.ToListAsync(cancel); return _mapper.Map>(modules); } } /// /// /// public static class ReadThirdPartyModuleQueryExtensions { /// /// /// /// /// /// /// /// public static async Task ReadThirdPartyModuleLicenseAsync(this IMediator mediator, string name, bool active = true, CancellationToken cancel = default) { var modules = await mediator.Send(new ReadThirdPartyModuleQuery() { Name = name, Active = active, }, cancel); return modules.FirstOrDefault()?.License; } }