using System.Data; using Bit.Core.Entities; using Bit.Core.Models.Data.Organizations; using Bit.Core.Repositories; using Bit.Core.Settings; using Dapper; using Microsoft.Data.SqlClient; namespace Bit.Infrastructure.Dapper.Repositories; public class OrganizationRepository : Repository, IOrganizationRepository { public OrganizationRepository(GlobalSettings globalSettings) : this(globalSettings.SqlServer.ConnectionString, globalSettings.SqlServer.ReadOnlyConnectionString) { } public OrganizationRepository(string connectionString, string readOnlyConnectionString) : base(connectionString, readOnlyConnectionString) { } public async Task GetByIdentifierAsync(string identifier) { using (var connection = new SqlConnection(ConnectionString)) { var results = await connection.QueryAsync( "[dbo].[Organization_ReadByIdentifier]", new { Identifier = identifier }, commandType: CommandType.StoredProcedure); return results.SingleOrDefault(); } } public async Task> GetManyByEnabledAsync() { using (var connection = new SqlConnection(ConnectionString)) { var results = await connection.QueryAsync( "[dbo].[Organization_ReadByEnabled]", commandType: CommandType.StoredProcedure); return results.ToList(); } } public async Task> GetManyByUserIdAsync(Guid userId) { using (var connection = new SqlConnection(ConnectionString)) { var results = await connection.QueryAsync( "[dbo].[Organization_ReadByUserId]", new { UserId = userId }, commandType: CommandType.StoredProcedure); return results.ToList(); } } public async Task> SearchAsync(string name, string userEmail, bool? paid, int skip, int take) { using (var connection = new SqlConnection(ReadOnlyConnectionString)) { var results = await connection.QueryAsync( "[dbo].[Organization_Search]", new { Name = name, UserEmail = userEmail, Paid = paid, Skip = skip, Take = take }, commandType: CommandType.StoredProcedure, commandTimeout: 120); return results.ToList(); } } public async Task UpdateStorageAsync(Guid id) { using (var connection = new SqlConnection(ConnectionString)) { await connection.ExecuteAsync( "[dbo].[Organization_UpdateStorage]", new { Id = id }, commandType: CommandType.StoredProcedure, commandTimeout: 180); } } public async Task> GetManyAbilitiesAsync() { using (var connection = new SqlConnection(ConnectionString)) { var results = await connection.QueryAsync( "[dbo].[Organization_ReadAbilities]", commandType: CommandType.StoredProcedure); return results.ToList(); } } }