2025-05-06 06:45:49 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Kurs.Platform.Entities;
|
2025-06-13 14:42:50 +00:00
|
|
|
|
using Kurs.Platform.Enums;
|
2025-05-06 06:45:49 +00:00
|
|
|
|
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<EntityCreatedEventData<TenantConnectionString>>,
|
|
|
|
|
|
ILocalEventHandler<EntityDeletedEventData<TenantConnectionString>>,
|
|
|
|
|
|
ILocalEventHandler<EntityChangedEventData<TenantConnectionString>>,
|
|
|
|
|
|
ITransientDependency
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IRepository<DataSource, Guid> repoDataSource;
|
|
|
|
|
|
private readonly ITenantRepository tenantRepository;
|
|
|
|
|
|
|
|
|
|
|
|
public TenantConnectionStringEventHandler(
|
|
|
|
|
|
IRepository<DataSource, Guid> repoDataSource,
|
|
|
|
|
|
ITenantRepository tenantRepository
|
|
|
|
|
|
)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.repoDataSource = repoDataSource;
|
|
|
|
|
|
this.tenantRepository = tenantRepository;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task HandleEventAsync(EntityCreatedEventData<TenantConnectionString> eventData)
|
|
|
|
|
|
{
|
|
|
|
|
|
var entity = eventData.Entity;
|
|
|
|
|
|
var tenant = await tenantRepository.GetAsync(entity.TenantId);
|
|
|
|
|
|
await repoDataSource.InsertAsync(new DataSource
|
|
|
|
|
|
{
|
|
|
|
|
|
Code = tenant.Name,
|
|
|
|
|
|
ConnectionString = entity.Value,
|
2025-06-13 14:42:50 +00:00
|
|
|
|
DataSourceType = entity.Value.IndexOf("Server") >= 0 ? DataSourceTypeEnum.Mssql : DataSourceTypeEnum.Postgresql,
|
2025-05-06 06:45:49 +00:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task HandleEventAsync(EntityDeletedEventData<TenantConnectionString> 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<TenantConnectionString> 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);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|