using System; using System.Threading.Tasks; using Kurs.Platform.Entities; using Kurs.Platform.Enums; using Volo.Abp.DependencyInjection; using Volo.Abp.Domain.Entities.Events; using Volo.Abp.Domain.Repositories; using Volo.Abp.EventBus; using Volo.Abp.TenantManagement; namespace Kurs.Platform.Tenants; //TODO: İleride DistributedEventBus kullanılırsa, IDistributedEventHandler implement edilmeli public class TenantConnectionStringEventHandler : ILocalEventHandler>, ILocalEventHandler>, ILocalEventHandler>, ITransientDependency { private readonly IRepository repoDataSource; private readonly ITenantRepository tenantRepository; public TenantConnectionStringEventHandler( IRepository repoDataSource, ITenantRepository tenantRepository ) { this.repoDataSource = repoDataSource; this.tenantRepository = tenantRepository; } public async Task HandleEventAsync(EntityCreatedEventData eventData) { var entity = eventData.Entity; var tenant = await tenantRepository.GetAsync(entity.TenantId); await repoDataSource.InsertAsync(new DataSource { Code = tenant.Name, ConnectionString = entity.Value, DataSourceType = entity.Value.IndexOf("Server") >= 0 ? DataSourceTypeEnum.Mssql : DataSourceTypeEnum.Postgresql, }); } public async Task HandleEventAsync(EntityDeletedEventData eventData) { var entity = eventData.Entity; var tenant = await tenantRepository.GetAsync(entity.TenantId); await repoDataSource.DeleteDirectAsync(a => a.Code == tenant.Name); } public async Task HandleEventAsync(EntityChangedEventData eventData) { var entity = eventData.Entity; var tenant = await tenantRepository.GetAsync(entity.TenantId); var item = await repoDataSource.FindAsync(a => a.Code == tenant.Name); if (item != null) { item.ConnectionString = entity.Value; await repoDataSource.UpdateAsync(item); } } }