From 62a0ca881fb688d9fbefe7692447483daa2a5f7c Mon Sep 17 00:00:00 2001 From: Matt Gibson Date: Tue, 21 Sep 2021 14:18:11 -0400 Subject: [PATCH] Process collections client-side (#1591) CollectionDetails is not an entity and so cannot be processed server-side --- .../EntityFramework/CollectionRepository.cs | 20 +++++++---- .../Queries/CollectionReadByIdUserId.cs | 22 ------------ .../Queries/CollectionReadByUserId.cs | 34 ------------------- 3 files changed, 14 insertions(+), 62 deletions(-) delete mode 100644 src/Core/Repositories/EntityFramework/Queries/CollectionReadByIdUserId.cs delete mode 100644 src/Core/Repositories/EntityFramework/Queries/CollectionReadByUserId.cs diff --git a/src/Core/Repositories/EntityFramework/CollectionRepository.cs b/src/Core/Repositories/EntityFramework/CollectionRepository.cs index ea8fc9dd9f..ab9a075b62 100644 --- a/src/Core/Repositories/EntityFramework/CollectionRepository.cs +++ b/src/Core/Repositories/EntityFramework/CollectionRepository.cs @@ -70,9 +70,7 @@ namespace Bit.Core.Repositories.EntityFramework using (var scope = ServiceScopeFactory.CreateScope()) { var dbContext = GetDatabaseContext(scope); - var query = new CollectionReadByIdUserId(id, userId).Run(dbContext); - var collection = await query.FirstOrDefaultAsync(); - return collection; + return (await GetManyByUserIdAsync(userId)).FirstOrDefault(c => c.Id == id); } } @@ -138,9 +136,19 @@ namespace Bit.Core.Repositories.EntityFramework using (var scope = ServiceScopeFactory.CreateScope()) { var dbContext = GetDatabaseContext(scope); - var query = new CollectionReadByUserId(userId).Run(dbContext); - var collections = await query.ToListAsync(); - return collections; + 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(); } } diff --git a/src/Core/Repositories/EntityFramework/Queries/CollectionReadByIdUserId.cs b/src/Core/Repositories/EntityFramework/Queries/CollectionReadByIdUserId.cs deleted file mode 100644 index ced18e34ea..0000000000 --- a/src/Core/Repositories/EntityFramework/Queries/CollectionReadByIdUserId.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System.Linq; -using System; -using Bit.Core.Models.Data; - -namespace Bit.Core.Repositories.EntityFramework.Queries -{ - public class CollectionReadByIdUserId : CollectionReadByUserId - { - private readonly Guid _id; - - public CollectionReadByIdUserId(Guid id, Guid userId) : base(userId) - { - _id = id; - } - - public override IQueryable Run(DatabaseContext dbContext) - { - var query = base.Run(dbContext); - return query.Where(c => c.Id == _id); - } - } -} diff --git a/src/Core/Repositories/EntityFramework/Queries/CollectionReadByUserId.cs b/src/Core/Repositories/EntityFramework/Queries/CollectionReadByUserId.cs deleted file mode 100644 index c37db0ceb5..0000000000 --- a/src/Core/Repositories/EntityFramework/Queries/CollectionReadByUserId.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System.Linq; -using System; -using Bit.Core.Models.Data; - -namespace Bit.Core.Repositories.EntityFramework.Queries -{ - public class CollectionReadByUserId : UserCollectionDetailsQuery - { - private readonly Guid _userId; - - public CollectionReadByUserId(Guid userId) : base(userId) - { - _userId = userId; - } - - public override IQueryable Run(DatabaseContext dbContext) - { - var query = base.Run(dbContext); - return query - .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) - }); - } - } -}