erp-platform/api/src/Kurs.Platform.Domain/Tenants/TenantConnectionStringEventHandler.cs
2025-08-19 22:26:50 +03:00

62 lines
2.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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);
}
}
}