92 lines
2.5 KiB
C#
92 lines
2.5 KiB
C#
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;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public record ReadThirdPartyModuleQuery : IRequest<IEnumerable<ThirdPartyModuleDto>>
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public string? Name { get; init; }
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public bool? Active { get; init; }
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public record ReadThirdPartyModuleQueryHandler : IRequestHandler<ReadThirdPartyModuleQuery, IEnumerable<ThirdPartyModuleDto>>
|
|
{
|
|
private readonly IMapper _mapper;
|
|
|
|
private readonly IRepository<ThirdPartyModule> _repo;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="mapper"></param>
|
|
/// <param name="repo"></param>
|
|
public ReadThirdPartyModuleQueryHandler(IMapper mapper, IRepository<ThirdPartyModule> repo)
|
|
{
|
|
_mapper = mapper;
|
|
_repo = repo;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <param name="cancel"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
public async Task<IEnumerable<ThirdPartyModuleDto>> 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<IEnumerable<ThirdPartyModuleDto>>(modules);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public static class ReadThirdPartyModuleQueryExtensions
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="mediator"></param>
|
|
/// <param name="name"></param>
|
|
/// <param name="active"></param>
|
|
/// <param name="cancel"></param>
|
|
/// <returns></returns>
|
|
public static async Task<string?> 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;
|
|
}
|
|
} |