mirror of
https://github.com/bitwarden/server.git
synced 2024-11-21 12:05:42 +01:00
[AC-432] Changed ProviderOrganization_ReadByOrganizationIds to only get count
This commit is contained in:
parent
735775e002
commit
4daebe4f47
@ -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.");
|
||||
}
|
||||
|
@ -9,5 +9,5 @@ public interface IProviderOrganizationRepository : IRepository<ProviderOrganizat
|
||||
Task<ICollection<ProviderOrganizationOrganizationDetails>> GetManyDetailsByProviderAsync(Guid providerId);
|
||||
Task<ProviderOrganization> GetByOrganizationId(Guid organizationId);
|
||||
Task<IEnumerable<ProviderOrganizationProviderDetails>> GetManyByUserAsync(Guid userId);
|
||||
Task<IEnumerable<ProviderOrganization>> GetManyByOrganizationIdsAsync(IEnumerable<Guid> organizationIds);
|
||||
Task<int> GetCountByOrganizationIdsAsync(IEnumerable<Guid> organizationIds);
|
||||
}
|
||||
|
@ -99,17 +99,17 @@ public class ProviderOrganizationRepository : Repository<ProviderOrganization, G
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<ProviderOrganization>> GetManyByOrganizationIdsAsync(
|
||||
public async Task<int> GetCountByOrganizationIdsAsync(
|
||||
IEnumerable<Guid> organizationIds)
|
||||
{
|
||||
using (var connection = new SqlConnection(ConnectionString))
|
||||
{
|
||||
var results = await connection.QueryAsync<ProviderOrganization>(
|
||||
$"[{Schema}].[ProviderOrganization_ReadByOrganizationIds]",
|
||||
var results = await connection.ExecuteScalarAsync<int>(
|
||||
$"[{Schema}].[ProviderOrganization_ReadCountByOrganizationIds]",
|
||||
new { Ids = organizationIds.ToGuidIdArrayTVP() },
|
||||
commandType: CommandType.StoredProcedure);
|
||||
|
||||
return results.ToList();
|
||||
return results;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -68,15 +68,9 @@ public class ProviderOrganizationRepository :
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<ProviderOrganization>> GetManyByOrganizationIdsAsync(IEnumerable<Guid> organizationIds)
|
||||
public async Task<int> GetCountByOrganizationIdsAsync(IEnumerable<Guid> 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);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,21 @@
|
||||
using Bit.Core.Entities.Provider;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
||||
|
||||
public class ProviderOrganizationCountByOrganizationIdsQuery : IQuery<ProviderOrganization>
|
||||
{
|
||||
private readonly IEnumerable<Guid> _organizationIds;
|
||||
|
||||
public ProviderOrganizationCountByOrganizationIdsQuery(IEnumerable<Guid> organizationIds)
|
||||
{
|
||||
_organizationIds = organizationIds;
|
||||
}
|
||||
|
||||
public IQueryable<ProviderOrganization> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var query = from po in dbContext.ProviderOrganizations
|
||||
where _organizationIds.Contains(po.OrganizationId)
|
||||
select po;
|
||||
return query;
|
||||
}
|
||||
}
|
@ -264,7 +264,7 @@
|
||||
<Build Include="dbo\Stored Procedures\Policy_Update.sql" />
|
||||
<Build Include="dbo\Stored Procedures\ProviderOrganizationOrganizationDetails_ReadByProviderId.sql" />
|
||||
<Build Include="dbo\Stored Procedures\ProviderOrganizationProviderDetails_ReadByUserId.sql" />
|
||||
<Build Include="dbo\Stored Procedures\ProviderOrganization_ReadByOrganizationIds.sql" />
|
||||
<Build Include="dbo\Stored Procedures\ProviderOrganization_ReadCountByOrganizationIds.sql" />
|
||||
<Build Include="dbo\Stored Procedures\Provider_ReadByOrganizationId.sql" />
|
||||
<Build Include="dbo\Stored Procedures\Organization_UnassignedToProviderSearch.sql" />
|
||||
<Build Include="dbo\Stored Procedures\ProviderOrganization_Create.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
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user