fix client query
This commit is contained in:
		
							parent
							
								
									9032527af0
								
							
						
					
					
						commit
						fbb4de57f6
					
				| @ -111,7 +111,7 @@ app.UseCors(options => | ||||
|   var origins = new List<string> {"https://hoppscotch.io"}; | ||||
|   if (builder.Environment.IsDevelopment()) | ||||
|   { | ||||
|     origins.Add("http://localhost:8100"); | ||||
|     origins.Add("http://localhost:4200"); | ||||
|   } | ||||
|    | ||||
|   options.WithOrigins(origins.ToArray()); | ||||
|  | ||||
| @ -1,10 +1,15 @@ | ||||
| using DigitalOps.Authority.Services; | ||||
| using DigitalOps.Dal; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using PoweredSoft.DynamicLinq; | ||||
| using OpenHarbor.CQRS.DynamicQuery.Abstractions; | ||||
| 
 | ||||
| namespace DigitalOps.CQRS.Queries.Client; | ||||
| 
 | ||||
| public class ClientParams | ||||
| { | ||||
|   public long OrganizationId { get; set; } | ||||
| } | ||||
| 
 | ||||
| public class ClientItem | ||||
| { | ||||
|   public long Id { get; set; } | ||||
| @ -18,10 +23,28 @@ public class ClientQueryableProvider(MainDbContext dbContext, UserIdentityServic | ||||
| { | ||||
|   public async Task<IQueryable<ClientItem>> GetQueryableAsync(object query, CancellationToken cancellationToken = default) | ||||
|   { | ||||
|     long? organizationId = null; | ||||
|        | ||||
|     if (query is IDynamicQueryParams<ClientParams> dynamicQuery) | ||||
|     { | ||||
|       var queryParams = dynamicQuery.GetParams(); | ||||
|       organizationId = queryParams?.OrganizationId; | ||||
|     } | ||||
|      | ||||
|     if (organizationId is null) | ||||
|     { | ||||
|       // don't bother to call the database if organizationId is not set | ||||
|       return Enumerable.Empty<ClientItem>().AsQueryable(); | ||||
|     } | ||||
| 
 | ||||
|     var user = await userIdentityService.GetUserOrDefaultAsync(cancellationToken); | ||||
| 
 | ||||
|     var queryable = dbContext.Clients | ||||
|       .AsNoTracking(); | ||||
| 
 | ||||
|     var result = dbContext.Clients | ||||
|       .AsNoTracking() | ||||
|       .Where(client => client.OrganizationClients.Any(organizationClient => organizationClient.OrganizationId == organizationId)) | ||||
|       .Where(client => | ||||
|         client.OrganizationClients.Any(organizationClient => organizationClient.Organization.OrganizationUsers.Any(organizationUser => organizationUser.UserId == user!.Id))) | ||||
|       .Select(client => new ClientItem | ||||
| @ -33,6 +56,6 @@ public class ClientQueryableProvider(MainDbContext dbContext, UserIdentityServic | ||||
|         UpdatedAt = client.UpdatedAt | ||||
|       }); | ||||
| 
 | ||||
|     return queryable; | ||||
|     return result; | ||||
|   } | ||||
| } | ||||
| @ -9,7 +9,7 @@ public class RegisterClientQueriesModule : IModule | ||||
| { | ||||
|   public IServiceCollection ConfigureServices(IServiceCollection services) | ||||
|   { | ||||
|     services.AddDynamicQuery<ClientItem>() | ||||
|     services.AddDynamicQueryWithParams<ClientItem, ClientParams>() | ||||
|       .AddQueryableProviderOverride<ClientItem, ClientQueryableProvider>(); | ||||
|      | ||||
|     return services; | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user