From 4daebe4f47cba37f8edf5acaf6a80578a99cbddf Mon Sep 17 00:00:00 2001 From: Rui Tome Date: Thu, 6 Apr 2023 15:24:05 +0100 Subject: [PATCH] [AC-432] Changed ProviderOrganization_ReadByOrganizationIds to only get count --- .../Services/ProviderService.cs | 4 ++-- .../IProviderOrganizationRepository.cs | 2 +- .../ProviderOrganizationRepository.cs | 8 +++---- .../ProviderOrganizationRepository.cs | 12 +++-------- ...OrganizationCountByOrganizationIdsQuery.cs | 21 +++++++++++++++++++ src/Sql/Sql.sqlproj | 2 +- ...ganization_ReadCountByOrganizationIds.sql} | 4 ++-- ...22_00_ProviderAddExistingOrganizations.sql | 4 ++-- 8 files changed, 36 insertions(+), 21 deletions(-) create mode 100644 src/Infrastructure.EntityFramework/Repositories/Queries/ProviderOrganizationCountByOrganizationIdsQuery.cs rename src/Sql/dbo/Stored Procedures/{ProviderOrganization_ReadByOrganizationIds.sql => ProviderOrganization_ReadCountByOrganizationIds.sql} (75%) diff --git a/bitwarden_license/src/Commercial.Core/Services/ProviderService.cs b/bitwarden_license/src/Commercial.Core/Services/ProviderService.cs index ad5315f0d..6eef1deeb 100644 --- a/bitwarden_license/src/Commercial.Core/Services/ProviderService.cs +++ b/bitwarden_license/src/Commercial.Core/Services/ProviderService.cs @@ -373,8 +373,8 @@ public class ProviderService : IProviderService throw new BadRequestException("Provider must be of type Reseller in order to assign Organizations to it."); } - var existingProviderOrganizations = await _providerOrganizationRepository.GetManyByOrganizationIdsAsync(organizationIds); - if (existingProviderOrganizations.Any()) + var existingProviderOrganizationsCount = await _providerOrganizationRepository.GetCountByOrganizationIdsAsync(organizationIds); + if (existingProviderOrganizationsCount > 0) { throw new BadRequestException("Organizations must not be assigned to any Provider."); } diff --git a/src/Core/Repositories/IProviderOrganizationRepository.cs b/src/Core/Repositories/IProviderOrganizationRepository.cs index d69198598..8550a3d7b 100644 --- a/src/Core/Repositories/IProviderOrganizationRepository.cs +++ b/src/Core/Repositories/IProviderOrganizationRepository.cs @@ -9,5 +9,5 @@ public interface IProviderOrganizationRepository : IRepository> GetManyDetailsByProviderAsync(Guid providerId); Task GetByOrganizationId(Guid organizationId); Task> GetManyByUserAsync(Guid userId); - Task> GetManyByOrganizationIdsAsync(IEnumerable organizationIds); + Task GetCountByOrganizationIdsAsync(IEnumerable organizationIds); } diff --git a/src/Infrastructure.Dapper/Repositories/ProviderOrganizationRepository.cs b/src/Infrastructure.Dapper/Repositories/ProviderOrganizationRepository.cs index 3c4b5087e..604d81a6e 100644 --- a/src/Infrastructure.Dapper/Repositories/ProviderOrganizationRepository.cs +++ b/src/Infrastructure.Dapper/Repositories/ProviderOrganizationRepository.cs @@ -99,17 +99,17 @@ public class ProviderOrganizationRepository : Repository> GetManyByOrganizationIdsAsync( + public async Task GetCountByOrganizationIdsAsync( IEnumerable organizationIds) { using (var connection = new SqlConnection(ConnectionString)) { - var results = await connection.QueryAsync( - $"[{Schema}].[ProviderOrganization_ReadByOrganizationIds]", + var results = await connection.ExecuteScalarAsync( + $"[{Schema}].[ProviderOrganization_ReadCountByOrganizationIds]", new { Ids = organizationIds.ToGuidIdArrayTVP() }, commandType: CommandType.StoredProcedure); - return results.ToList(); + return results; } } diff --git a/src/Infrastructure.EntityFramework/Repositories/ProviderOrganizationRepository.cs b/src/Infrastructure.EntityFramework/Repositories/ProviderOrganizationRepository.cs index 44ee1237a..c5a6d34e2 100644 --- a/src/Infrastructure.EntityFramework/Repositories/ProviderOrganizationRepository.cs +++ b/src/Infrastructure.EntityFramework/Repositories/ProviderOrganizationRepository.cs @@ -68,15 +68,9 @@ public class ProviderOrganizationRepository : } } - public async Task> GetManyByOrganizationIdsAsync(IEnumerable organizationIds) + public async Task GetCountByOrganizationIdsAsync(IEnumerable organizationIds) { - using (var scope = ServiceScopeFactory.CreateScope()) - { - var dbContext = GetDatabaseContext(scope); - var query = from po in dbContext.ProviderOrganizations - where organizationIds.Contains(po.OrganizationId) - select po; - return await query.ToListAsync(); - } + var query = new ProviderOrganizationCountByOrganizationIdsQuery(organizationIds); + return await GetCountFromQuery(query); } } diff --git a/src/Infrastructure.EntityFramework/Repositories/Queries/ProviderOrganizationCountByOrganizationIdsQuery.cs b/src/Infrastructure.EntityFramework/Repositories/Queries/ProviderOrganizationCountByOrganizationIdsQuery.cs new file mode 100644 index 000000000..4d9357f2f --- /dev/null +++ b/src/Infrastructure.EntityFramework/Repositories/Queries/ProviderOrganizationCountByOrganizationIdsQuery.cs @@ -0,0 +1,21 @@ +using Bit.Core.Entities.Provider; + +namespace Bit.Infrastructure.EntityFramework.Repositories.Queries; + +public class ProviderOrganizationCountByOrganizationIdsQuery : IQuery +{ + private readonly IEnumerable _organizationIds; + + public ProviderOrganizationCountByOrganizationIdsQuery(IEnumerable organizationIds) + { + _organizationIds = organizationIds; + } + + public IQueryable Run(DatabaseContext dbContext) + { + var query = from po in dbContext.ProviderOrganizations + where _organizationIds.Contains(po.OrganizationId) + select po; + return query; + } +} diff --git a/src/Sql/Sql.sqlproj b/src/Sql/Sql.sqlproj index 31e699f83..e90d236a8 100644 --- a/src/Sql/Sql.sqlproj +++ b/src/Sql/Sql.sqlproj @@ -264,7 +264,7 @@ - + diff --git a/src/Sql/dbo/Stored Procedures/ProviderOrganization_ReadByOrganizationIds.sql b/src/Sql/dbo/Stored Procedures/ProviderOrganization_ReadCountByOrganizationIds.sql similarity index 75% rename from src/Sql/dbo/Stored Procedures/ProviderOrganization_ReadByOrganizationIds.sql rename to src/Sql/dbo/Stored Procedures/ProviderOrganization_ReadCountByOrganizationIds.sql index 08cee543d..43ee0dcce 100644 --- a/src/Sql/dbo/Stored Procedures/ProviderOrganization_ReadByOrganizationIds.sql +++ b/src/Sql/dbo/Stored Procedures/ProviderOrganization_ReadCountByOrganizationIds.sql @@ -1,4 +1,4 @@ -CREATE PROCEDURE [dbo].[ProviderOrganization_ReadByOrganizationIds] +CREATE PROCEDURE [dbo].[ProviderOrganization_ReadCountByOrganizationIds] @Ids AS [dbo].[GuidIdArray] READONLY AS BEGIN @@ -10,7 +10,7 @@ BEGIN END SELECT - * + COUNT(1) FROM [dbo].[ProviderOrganizationView] WHERE diff --git a/util/Migrator/DbScripts/2023-03-22_00_ProviderAddExistingOrganizations.sql b/util/Migrator/DbScripts/2023-03-22_00_ProviderAddExistingOrganizations.sql index 96533fc5b..76035d101 100644 --- a/util/Migrator/DbScripts/2023-03-22_00_ProviderAddExistingOrganizations.sql +++ b/util/Migrator/DbScripts/2023-03-22_00_ProviderAddExistingOrganizations.sql @@ -53,7 +53,7 @@ BEGIN END GO -CREATE OR ALTER PROCEDURE [dbo].[ProviderOrganization_ReadByOrganizationIds] +CREATE OR ALTER PROCEDURE [dbo].[ProviderOrganization_ReadCountByOrganizationIds] @Ids AS [dbo].[GuidIdArray] READONLY AS BEGIN @@ -65,7 +65,7 @@ BEGIN END SELECT - * + COUNT(1) FROM [dbo].[ProviderOrganizationView] WHERE