dotnet-digital-ops/DigitalOps.CQRS/Queries/Organization/OrganizationQuery.cs

35 lines
1.1 KiB
C#

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