using AutoMapper; using Bit.Core.AdminConsole.Entities.Provider; using Bit.Core.AdminConsole.Models.Data.Provider; using Bit.Core.AdminConsole.Repositories; using Bit.Infrastructure.EntityFramework.AdminConsole.Repositories.Queries; using Bit.Infrastructure.EntityFramework.Repositories; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; namespace Bit.Infrastructure.EntityFramework.AdminConsole.Repositories; public class ProviderOrganizationRepository : Repository, IProviderOrganizationRepository { public ProviderOrganizationRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper) : base(serviceScopeFactory, mapper, context => context.ProviderOrganizations) { } public async Task> CreateManyAsync(IEnumerable providerOrganizations) { var entities = providerOrganizations.ToList(); if (!entities.Any()) { return default; } foreach (var providerOrganization in entities) { providerOrganization.SetNewId(); } using (var scope = ServiceScopeFactory.CreateScope()) { var dbContext = GetDatabaseContext(scope); await dbContext.AddRangeAsync(entities); await dbContext.SaveChangesAsync(); } return entities; } public async Task> GetManyDetailsByProviderAsync(Guid providerId) { using (var scope = ServiceScopeFactory.CreateScope()) { var dbContext = GetDatabaseContext(scope); var query = new ProviderOrganizationOrganizationDetailsReadByProviderIdQuery(providerId); var data = await query.Run(dbContext).ToListAsync(); return data; } } public async Task GetByOrganizationId(Guid organizationId) { using var scope = ServiceScopeFactory.CreateScope(); var dbContext = GetDatabaseContext(scope); return await GetDbSet(dbContext).Where(po => po.OrganizationId == organizationId).FirstOrDefaultAsync(); } public async Task> GetManyByUserAsync(Guid userId) { using (var scope = ServiceScopeFactory.CreateScope()) { var dbContext = GetDatabaseContext(scope); var query = new ProviderOrganizationReadByUserIdQuery(userId); var data = await query.Run(dbContext).ToListAsync(); return data; } } public async Task GetCountByOrganizationIdsAsync(IEnumerable organizationIds) { var query = new ProviderOrganizationCountByOrganizationIdsQuery(organizationIds); return await GetCountFromQuery(query); } }