62 lines
2.3 KiB
C#
62 lines
2.3 KiB
C#
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<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,
|
||
DataSourceType = entity.Value.IndexOf("Server") >= 0 ? DataSourceTypeEnum.Mssql : DataSourceTypeEnum.Postgresql,
|
||
});
|
||
}
|
||
|
||
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);
|
||
}
|
||
}
|
||
}
|