diff --git a/backend/LexBoxApi/GraphQL/LexQueries.cs b/backend/LexBoxApi/GraphQL/LexQueries.cs index 72d947436..1810b0950 100644 --- a/backend/LexBoxApi/GraphQL/LexQueries.cs +++ b/backend/LexBoxApi/GraphQL/LexQueries.cs @@ -43,11 +43,11 @@ public IQueryable Projects(LexBoxDbContext context, bool withDeleted = { if (withDeleted) { - return context.Projects.IgnoreQueryFilters(); + return context.Projects.AsNoTracking().IgnoreQueryFilters(); } else { - return context.Projects; + return context.Projects.AsNoTracking(); } } @@ -56,7 +56,7 @@ public IQueryable Projects(LexBoxDbContext context, bool withDeleted = public IQueryable 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] @@ -65,7 +65,7 @@ public IQueryable MyDraftProjects(LoggedInContext loggedInContext, [AdminRequired] public IQueryable DraftProjects(LexBoxDbContext context) { - return context.DraftProjects; + return context.DraftProjects.AsNoTracking(); } public record ProjectsByLangCodeAndOrgInput(Guid OrgId, string LangCode); @@ -75,8 +75,8 @@ public IQueryable 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 && @@ -103,7 +103,7 @@ public record ProjectsInMyOrgInput(Guid OrgId); public IQueryable 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)) { @@ -117,7 +117,7 @@ public IQueryable ProjectsInMyOrg(LoggedInContext loggedInContext, LexB public async Task> 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] @@ -141,20 +141,12 @@ public async Task> ProjectById(LexBoxDbContext context, IPer return project; } - [UseSingleOrDefault] - [UseProjection] - [AdminRequired] - public IQueryable DraftProjectByCode(LexBoxDbContext context, string code) - { - return context.DraftProjects.Where(p => p.Code == code); - } - [UseProjection] [UseFiltering] [UseSorting] public IQueryable Orgs(LexBoxDbContext context) { - return context.Orgs; + return context.Orgs.AsNoTracking(); } [UseProjection] @@ -177,16 +169,6 @@ public async Task> MyOrgs( return myOrgs.AsQueryable(); } - [UseOffsetPaging] - [UseProjection] - [UseFiltering] - [UseSorting] - public IQueryable 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] @@ -278,7 +260,7 @@ public IQueryable 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); diff --git a/backend/LexBoxApi/Services/UserService.cs b/backend/LexBoxApi/Services/UserService.cs index dc3f3a29f..c7a476f64 100644 --- a/backend/LexBoxApi/Services/UserService.cs +++ b/backend/LexBoxApi/Services/UserService.cs @@ -90,7 +90,7 @@ public IQueryable 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 =>