Skip to content

Commit

Permalink
Remove unused queries and use AsNoTracking
Browse files Browse the repository at this point in the history
  • Loading branch information
myieye committed Nov 28, 2024
1 parent 7d002cf commit bedfdb4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 29 deletions.
38 changes: 10 additions & 28 deletions backend/LexBoxApi/GraphQL/LexQueries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ public IQueryable<Project> Projects(LexBoxDbContext context, bool withDeleted =
{
if (withDeleted)
{
return context.Projects.IgnoreQueryFilters();
return context.Projects.AsNoTracking().IgnoreQueryFilters();
}
else
{
return context.Projects;
return context.Projects.AsNoTracking();
}
}

Expand All @@ -56,7 +56,7 @@ public IQueryable<Project> Projects(LexBoxDbContext context, bool withDeleted =
public IQueryable<DraftProject> MyDraftProjects(LoggedInContext loggedInContext, LexBoxDbContext context)
{
var userId = loggedInContext.User.Id;
return context.DraftProjects.Where(p => p.ProjectManagerId == userId);
return context.DraftProjects.AsNoTracking().Where(p => p.ProjectManagerId == userId);
}

[UseProjection]
Expand All @@ -65,7 +65,7 @@ public IQueryable<DraftProject> MyDraftProjects(LoggedInContext loggedInContext,
[AdminRequired]
public IQueryable<DraftProject> DraftProjects(LexBoxDbContext context)
{
return context.DraftProjects;
return context.DraftProjects.AsNoTracking();
}

public record ProjectsByLangCodeAndOrgInput(Guid OrgId, string LangCode);
Expand All @@ -75,8 +75,8 @@ public IQueryable<Project> ProjectsByLangCodeAndOrg(LoggedInContext loggedInCont
{
if (!loggedInContext.User.IsAdmin && !permissionService.IsOrgMember(input.OrgId)) throw new UnauthorizedAccessException();
// Convert 3-letter code to 2-letter code if relevant, otherwise leave as-is
var langCode = Services.LangTagConstants.ThreeToTwo.GetValueOrDefault(input.LangCode, input.LangCode);
var query = context.Projects.Where(p =>
var langCode = LangTagConstants.ThreeToTwo.GetValueOrDefault(input.LangCode, input.LangCode);
var query = context.Projects.AsNoTracking().Where(p =>
p.Organizations.Any(o => o.Id == input.OrgId) &&
p.FlexProjectMetadata != null &&
p.FlexProjectMetadata.WritingSystems != null &&
Expand All @@ -103,7 +103,7 @@ public record ProjectsInMyOrgInput(Guid OrgId);
public IQueryable<Project> ProjectsInMyOrg(LoggedInContext loggedInContext, LexBoxDbContext context, IPermissionService permissionService, ProjectsInMyOrgInput input)
{
if (!loggedInContext.User.IsAdmin && !permissionService.IsOrgMember(input.OrgId)) throw new UnauthorizedAccessException();
var query = context.Projects.Where(p => p.Organizations.Any(o => o.Id == input.OrgId));
var query = context.Projects.AsNoTracking().Where(p => p.Organizations.Any(o => o.Id == input.OrgId));
// Org admins can see all projects, everyone else can only see non-confidential
if (!permissionService.CanEditOrg(input.OrgId))
{
Expand All @@ -117,7 +117,7 @@ public IQueryable<Project> ProjectsInMyOrg(LoggedInContext loggedInContext, LexB
public async Task<IQueryable<Project>> ProjectById(LexBoxDbContext context, IPermissionService permissionService, Guid projectId)
{
await permissionService.AssertCanViewProject(projectId);
return context.Projects.Where(p => p.Id == projectId);
return context.Projects.AsNoTracking().Where(p => p.Id == projectId);
}

[UseProjection]
Expand All @@ -141,20 +141,12 @@ public async Task<IQueryable<Project>> ProjectById(LexBoxDbContext context, IPer
return project;
}

[UseSingleOrDefault]
[UseProjection]
[AdminRequired]
public IQueryable<DraftProject> DraftProjectByCode(LexBoxDbContext context, string code)
{
return context.DraftProjects.Where(p => p.Code == code);
}

[UseProjection]
[UseFiltering]
[UseSorting]
public IQueryable<Organization> Orgs(LexBoxDbContext context)
{
return context.Orgs;
return context.Orgs.AsNoTracking();
}

[UseProjection]
Expand All @@ -177,16 +169,6 @@ public async Task<IQueryable<Organization>> MyOrgs(
return myOrgs.AsQueryable();
}

[UseOffsetPaging]
[UseProjection]
[UseFiltering]
[UseSorting]
public IQueryable<User> UsersInMyOrg(LexBoxDbContext context, LoggedInContext loggedInContext)
{
var myOrgIds = loggedInContext.User.Orgs.Select(o => o.OrgId).ToList();
return context.Users.Where(u => u.Organizations.Any(orgMember => myOrgIds.Contains(orgMember.OrgId)));
}

[UseOffsetPaging]
[UseProjection]
[UseFiltering]
Expand Down Expand Up @@ -278,7 +260,7 @@ public IQueryable<User> Users(LexBoxDbContext context)
// Only site admins and org admins are allowed to run this query
if (!permissionService.CanEditOrg(orgId)) return null;

var user = await context.Users.Include(u => u.Organizations).Include(u => u.CreatedBy).Where(u => u.Id == userId).FirstOrDefaultAsync();
var user = await context.Users.AsNoTracking().Include(u => u.Organizations).Include(u => u.CreatedBy).Where(u => u.Id == userId).FirstOrDefaultAsync();
if (user is null) return null;

var userInOrg = user.Organizations.Any(om => om.OrgId == orgId);
Expand Down
2 changes: 1 addition & 1 deletion backend/LexBoxApi/Services/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public IQueryable<User> UserQueryForTypeahead(LexAuthUser user)
var myOrgIds = user.Orgs.Select(o => o.OrgId).ToList();
var myProjectIds = user.Projects.Select(p => p.ProjectId).ToList();
var myManagedProjectIds = user.Projects.Where(p => p.Role == ProjectRole.Manager).Select(p => p.ProjectId).ToList();
return dbContext.Users.Where(u =>
return dbContext.Users.AsNoTracking().Where(u =>
u.Id == user.Id ||
u.Organizations.Any(orgMember => myOrgIds.Contains(orgMember.OrgId)) ||
u.Projects.Any(projMember =>
Expand Down

0 comments on commit bedfdb4

Please sign in to comment.