using AutoMapper; using Bit.Core.Entities; using Bit.Core.Enums; using Bit.Core.Repositories; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; #nullable enable namespace Bit.Infrastructure.EntityFramework.Repositories; public class OrganizationConnectionRepository : Repository, IOrganizationConnectionRepository { public OrganizationConnectionRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper) : base(serviceScopeFactory, mapper, context => context.OrganizationConnections) { } public async Task GetByIdOrganizationIdAsync(Guid id, Guid organizationId) { using (var scope = ServiceScopeFactory.CreateScope()) { var dbContext = GetDatabaseContext(scope); var connection = await dbContext.OrganizationConnections .FirstOrDefaultAsync(oc => oc.Id == id && oc.OrganizationId == organizationId); return Mapper.Map(connection); } } public async Task> GetByOrganizationIdTypeAsync(Guid organizationId, OrganizationConnectionType type) { using (var scope = ServiceScopeFactory.CreateScope()) { var dbContext = GetDatabaseContext(scope); var connections = await dbContext.OrganizationConnections .Where(oc => oc.OrganizationId == organizationId && oc.Type == type) .ToListAsync(); return Mapper.Map>(connections); } } public async Task> GetEnabledByOrganizationIdTypeAsync(Guid organizationId, OrganizationConnectionType type) { using (var scope = ServiceScopeFactory.CreateScope()) { var dbContext = GetDatabaseContext(scope); var connections = await dbContext.OrganizationConnections .Where(oc => oc.OrganizationId == organizationId && oc.Type == type && oc.Enabled) .ToListAsync(); return Mapper.Map>(connections); } } }