From c25ef0be5cb82130b6476b067b4a5eaee3892920 Mon Sep 17 00:00:00 2001 From: Justin Baur <19896123+justindbaur@users.noreply.github.com> Date: Mon, 5 Dec 2022 13:59:11 -0500 Subject: [PATCH] [PS-1930] Fix `UpdateCollectionsForAdminAsync` (#2473) * Fix UpdateCollectionsForAdminAsync * Formatting --- .../CollectionCipherRepository.cs | 58 ++++++++----------- 1 file changed, 23 insertions(+), 35 deletions(-) diff --git a/src/Infrastructure.EntityFramework/Repositories/CollectionCipherRepository.cs b/src/Infrastructure.EntityFramework/Repositories/CollectionCipherRepository.cs index 745487047..7b41e81b5 100644 --- a/src/Infrastructure.EntityFramework/Repositories/CollectionCipherRepository.cs +++ b/src/Infrastructure.EntityFramework/Repositories/CollectionCipherRepository.cs @@ -147,42 +147,30 @@ public class CollectionCipherRepository : BaseEntityFrameworkRepository, ICollec using (var scope = ServiceScopeFactory.CreateScope()) { var dbContext = GetDatabaseContext(scope); - var availableCollectionsCte = from c in dbContext.Collections - where c.OrganizationId == organizationId - select c; - var target = from cc in dbContext.CollectionCiphers - where cc.CipherId == cipherId - select new { cc.CollectionId, cc.CipherId }; - var source = collectionIds.Select(x => new { CollectionId = x, CipherId = cipherId }); - var merge1 = from t in target - join s in source - on t.CollectionId equals s.CollectionId into s_g - from s in s_g.DefaultIfEmpty() - where t.CipherId == s.CipherId - select new { t, s }; - var merge2 = from s in source - join t in target - on s.CollectionId equals t.CollectionId into t_g - from t in t_g.DefaultIfEmpty() - where t.CipherId == s.CipherId - select new { t, s }; - var union = merge1.Union(merge2).Distinct(); - var insert = union - .Where(x => x.t == null && collectionIds.Contains(x.s.CollectionId)) - .Select(x => new Models.CollectionCipher + var availableCollections = await (from c in dbContext.Collections + where c.OrganizationId == organizationId + select c).ToListAsync(); + + var currentCollectionCiphers = await (from cc in dbContext.CollectionCiphers + where cc.CipherId == cipherId + select cc).ToListAsync(); + + foreach (var requestedCollectionId in collectionIds) + { + var requestedCollectionCipher = currentCollectionCiphers + .FirstOrDefault(cc => cc.CollectionId == requestedCollectionId); + + if (requestedCollectionCipher == null) { - CollectionId = x.s.CollectionId, - CipherId = x.s.CipherId, - }); - var delete = union - .Where(x => x.s == null && x.t.CipherId == cipherId) - .Select(x => new Models.CollectionCipher - { - CollectionId = x.t.CollectionId, - CipherId = x.t.CipherId, - }); - await dbContext.AddRangeAsync(insert); - dbContext.RemoveRange(delete); + dbContext.CollectionCiphers.Add(new Models.CollectionCipher + { + CipherId = cipherId, + CollectionId = requestedCollectionId, + }); + } + } + + dbContext.RemoveRange(currentCollectionCiphers.Where(cc => !collectionIds.Contains(cc.CollectionId))); await dbContext.UserBumpAccountRevisionDateByOrganizationIdAsync(organizationId); await dbContext.SaveChangesAsync(); }