dotnet-digital-ops/DigitalOps.CQRS/Queries/Client/ClientQuery.cs

38 lines
1.3 KiB
C#

using DigitalOps.Authority.Services;
using DigitalOps.Dal;
using Microsoft.EntityFrameworkCore;
using PoweredSoft.DynamicLinq;
namespace DigitalOps.CQRS.Queries.Client;
public class ClientItem
{
public long Id { get; set; }
public IEnumerable<long> OrganizationIds { get; set; }
public required string Name { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime? UpdatedAt { get; set; }
}
public class ClientQueryableProvider(MainDbContext dbContext, UserIdentityService userIdentityService): IQueryableProviderOverride<ClientItem>
{
public async Task<IQueryable<ClientItem>> GetQueryableAsync(object query, CancellationToken cancellationToken = default)
{
var user = await userIdentityService.GetUserOrDefaultAsync(cancellationToken);
var queryable = dbContext.Clients
.AsNoTracking()
.Where(client =>
client.OrganizationClients.Any(organizationClient => organizationClient.Organization.OrganizationUsers.Any(organizationUser => organizationUser.UserId == user!.Id)))
.Select(client => new ClientItem
{
Id = client.Id,
OrganizationIds = client.OrganizationClients.Select(organizationClient => organizationClient.OrganizationId),
Name = client.Name,
CreatedAt = client.CreatedAt,
UpdatedAt = client.UpdatedAt
});
return queryable;
}
}