1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-01 08:02:49 -05:00

[PS-1806] Fix EF CollectionRepository GetManyByUserId (#2409)

* Rewrite ReadOnly and HidePasswords

* Rewrote them to generate a CASE statement similar to T-SQL

* Rewrite Grouping Expression

* Use multiple groups just like T-SQL
* Run it all on the database instead of in memory

* Fix linter
This commit is contained in:
Justin Baur
2022-11-14 10:18:09 -05:00
committed by GitHub
parent 9ecf69d9ca
commit b24ce17193
2 changed files with 21 additions and 15 deletions

View File

@ -131,19 +131,19 @@ public class CollectionRepository : Repository<Core.Entities.Collection, Collect
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
return (await new UserCollectionDetailsQuery(userId).Run(dbContext).ToListAsync())
.GroupBy(c => c.Id)
.Select(g => new CollectionDetails
{
Id = g.Key,
OrganizationId = g.FirstOrDefault().OrganizationId,
Name = g.FirstOrDefault().Name,
ExternalId = g.FirstOrDefault().ExternalId,
CreationDate = g.FirstOrDefault().CreationDate,
RevisionDate = g.FirstOrDefault().RevisionDate,
ReadOnly = g.Min(c => c.ReadOnly),
HidePasswords = g.Min(c => c.HidePasswords)
}).ToList();
return await (from c in new UserCollectionDetailsQuery(userId).Run(dbContext)
group c by new { c.Id, c.OrganizationId, c.Name, c.CreationDate, c.RevisionDate, c.ExternalId } into collectionGroup
select new CollectionDetails
{
Id = collectionGroup.Key.Id,
OrganizationId = collectionGroup.Key.OrganizationId,
Name = collectionGroup.Key.Name,
CreationDate = collectionGroup.Key.CreationDate,
RevisionDate = collectionGroup.Key.RevisionDate,
ExternalId = collectionGroup.Key.ExternalId,
ReadOnly = collectionGroup.Min(c => c.ReadOnly),
HidePasswords = collectionGroup.Min(c => c.HidePasswords),
}).ToListAsync();
}
}