Branch Seed Manager eklendi

This commit is contained in:
Sedat Öztürk 2025-10-10 00:13:38 +03:00
parent 94b0d17c26
commit ca358bc4f9
12 changed files with 364 additions and 330 deletions

View file

@ -0,0 +1,12 @@
using System.Collections.Generic;
using System.Linq;
namespace Kurs.Platform.Branchs;
public class BranchSeedResultDto
{
public bool Success { get; set; }
public string Message { get; set; }
public int TotalInsertedCount => Details.Sum(x => x.InsertedCount);
public List<SeedDetailDto> Details { get; set; } = new();
}

View file

@ -0,0 +1,12 @@
using System.Collections.Generic;
namespace Kurs.Platform.Branchs;
public class SeedDetailDto
{
public string EntityName { get; set; } = string.Empty; // Örn: "RegistrationType"
public int InsertedCount { get; set; }
public List<string> InsertedItems { get; set; } = new(); // Eklenen kayıtların isimleri
public List<string> Warnings { get; set; } = new(); // Varsa uyarılar
public List<string> Errors { get; set; } = new(); // Varsa hatalar
}

View file

@ -1,338 +1,35 @@
using System;
using System.IO;
using System.Text.Json;
using System.Threading.Tasks;
using Kurs.Platform.Entities;
using Kurs.Platform.Seeds;
using Microsoft.Extensions.Configuration;
using Volo.Abp.Data;
using Volo.Abp.Domain.Repositories;
using Microsoft.EntityFrameworkCore;
using Kurs.Languages.Entities;
using Kurs.Settings.Entities;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Authorization;
using System.Threading.Tasks;
using Kurs.Platform;
using Kurs.Platform.Branchs;
using Kurs.Platform.Entities;
using Volo.Abp.Domain.Repositories;
namespace Kurs.Platform.Branchs;
public class BranchSeedResultDto
{
public bool Success { get; set; }
public string Message { get; set; }
public int TotalInsertedCount => Details.Sum(x => x.InsertedCount);
public List<SeedDetailDto> Details { get; set; } = new();
}
public class SeedDetailDto
{
public string EntityName { get; set; } = string.Empty; // Örn: "RegistrationType"
public int InsertedCount { get; set; }
public List<string> InsertedItems { get; set; } = new(); // Eklenen kayıtların isimleri
public List<string> Warnings { get; set; } = new(); // Varsa uyarılar
public List<string> Errors { get; set; } = new(); // Varsa hatalar
}
[Authorize]
public class BranchAppService : PlatformAppService
{
private readonly IRepository<Branch, Guid> _branchRepository;
private readonly IRepository<Language, Guid> _languages;
private readonly IRepository<LanguageKey, Guid> _languageKey;
private readonly IRepository<LanguageText, Guid> _languagesText;
private readonly IRepository<DataSource, Guid> _dataSources;
private readonly IRepository<SettingDefinition, Guid> _settings;
private readonly IRepository<RegistrationType, Guid> _registrationTypeRepository;
private readonly IRepository<RegistrationMethod, Guid> _registrationMethodRepository;
private readonly IRepository<ClassType, Guid> _classTypeRepository;
private readonly IRepository<Class, Guid> _classRepository;
private readonly IRepository<Level, Guid> _levelRepository;
private readonly IRepository<LessonPeriod, Guid> _lessonPeriodRepository;
private readonly IRepository<Schedule, Guid> _scheduleRepository;
private readonly BranchSeedManager _branchSeedManager;
public BranchAppService(
IRepository<Branch, Guid> branchRepository,
IRepository<Language, Guid> languages,
IRepository<LanguageKey, Guid> languageKey,
IRepository<LanguageText, Guid> languagesText,
IRepository<DataSource, Guid> dataSource,
IRepository<SettingDefinition, Guid> settings,
IRepository<Schedule, Guid> scheduleRepository,
IRepository<LessonPeriod, Guid> lessonPeriodRepository,
IRepository<RegistrationType, Guid> registrationTypeRepository,
IRepository<RegistrationMethod, Guid> registrationMethodRepository,
IRepository<ClassType, Guid> classTypeRepository,
IRepository<Class, Guid> classRepository,
IRepository<Level, Guid> levelRepository
)
BranchSeedManager branchSeedManager)
{
_branchRepository = branchRepository;
_languages = languages;
_languageKey = languageKey;
_languagesText = languagesText;
_dataSources = dataSource;
_settings = settings;
_registrationTypeRepository = registrationTypeRepository;
_registrationMethodRepository = registrationMethodRepository;
_classTypeRepository = classTypeRepository;
_classRepository = classRepository;
_levelRepository = levelRepository;
_lessonPeriodRepository = lessonPeriodRepository;
_scheduleRepository = scheduleRepository;
_branchSeedManager = branchSeedManager;
}
public async Task<BranchSeedResultDto> SeedAsync(Guid branchId)
{
var settings = await _settings.GetListAsync();
var dataSources = await _dataSources.GetListAsync();
var languages = await _languages.GetListAsync();
var keys = await _languageKey.GetListAsync();
var texts = await _languagesText.GetListAsync();
var branch = await _branchRepository.FirstOrDefaultAsync(x => x.Id == branchId);
if (branch == null)
return new BranchSeedResultDto { Success = false, Message = "Branch not found." };
var result = new BranchSeedResultDto();
var context = new DataSeedContext(branchId);
try
using (CurrentTenant.Change(branch.TenantId))
{
var branch = await _branchRepository.FirstOrDefaultAsync(x => x.Id == branchId);
if (branch == null)
{
result.Success = false;
result.Message = "Branch not found.";
return result;
}
var result = await _branchSeedManager.SeedRecordsAsync(branch.TenantId, branchId);
var tenantId = branch.TenantId;
var assemblyPath = Path.GetDirectoryName(typeof(BranchAppService).Assembly.Location)!;
var basePath = Path.Combine(assemblyPath, "Branch", "Seeds");
var configuration = new ConfigurationBuilder()
.SetBasePath(basePath)
.AddJsonFile("BranchData.json")
.AddJsonFile($"BranchData.{Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? ""}.json", optional: true)
.Build();
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
var items = configuration.Get<BranchSeederDto>();
if (items == null)
{
result.Success = false;
result.Message = "BranchData.json okunamadı veya format hatalı.";
return result;
}
// Yardımcı fonksiyon: kolay log ekleme
SeedDetailDto CreateLog(string entity) =>
new()
{
EntityName = entity,
InsertedCount = 0,
InsertedItems = []
};
var registrationTypeLog = CreateLog(nameof(RegistrationType));
foreach (var item in items.RegistrationTypes)
{
var exists = await _registrationTypeRepository.AnyAsync(x => x.Name == item.Name && x.BranchId == branchId);
if (!exists)
{
await _registrationTypeRepository.InsertAsync(new RegistrationType
{
TenantId = tenantId,
BranchId = branchId,
Name = item.Name,
Status = item.Status
}, autoSave: true);
registrationTypeLog.InsertedCount++;
registrationTypeLog.InsertedItems.Add(item.Name);
}
}
result.Details.Add(registrationTypeLog);
var registrationMethodLog = CreateLog(nameof(RegistrationMethod));
foreach (var item in items.RegistrationMethods)
{
var exists = await _registrationMethodRepository.AnyAsync(x => x.Name == item.Name && x.BranchId == branchId);
if (!exists)
{
var registrationType = await _registrationTypeRepository.FirstOrDefaultAsync(x => x.Name == item.RegistrationTypeName);
if (registrationType != null)
{
await _registrationMethodRepository.InsertAsync(new RegistrationMethod
{
TenantId = tenantId,
BranchId = branchId,
RegistrationTypeId = registrationType.Id,
Name = item.Name,
Status = item.Status
});
registrationMethodLog.InsertedCount++;
registrationMethodLog.InsertedItems.Add(item.Name);
}
}
}
result.Details.Add(registrationMethodLog);
var classTypeLog = CreateLog(nameof(ClassType));
foreach (var item in items.ClassTypes)
{
var exists = await _classTypeRepository.AnyAsync(x => x.Name == item.Name);
if (!exists)
{
var registrationType = await _registrationTypeRepository.FirstOrDefaultAsync(x => x.Name == item.RegistrationTypeName && x.BranchId == branchId);
if (registrationType != null)
{
await _classTypeRepository.InsertAsync(new ClassType
{
TenantId = tenantId,
BranchId = branchId,
RegistrationTypeId = registrationType.Id,
Name = item.Name,
MinStudentCount = item.MinStudentCount,
MaxStudentCount = item.MaxStudentCount,
Status = item.Status
}, autoSave: true);
classTypeLog.InsertedCount++;
classTypeLog.InsertedItems.Add(item.Name);
}
}
}
result.Details.Add(classTypeLog);
var classLog = CreateLog(nameof(Class));
foreach (var item in items.Classes)
{
var exists = await _classRepository.AnyAsync(x => x.Name == item.Name && x.BranchId == branchId);
if (!exists)
{
var classType = await _classTypeRepository.FirstOrDefaultAsync(x => x.Name == item.ClassTypeName);
if (classType != null)
{
await _classRepository.InsertAsync(new()
{
TenantId = tenantId,
BranchId = branchId,
ClassTypeId = classType.Id,
Name = item.Name,
Status = item.Status,
});
classLog.InsertedCount++;
classLog.InsertedItems.Add(item.Name);
}
}
}
result.Details.Add(classLog);
var levelLog = CreateLog(nameof(Level));
foreach (var item in items.Levels)
{
var exists = await _levelRepository.AnyAsync(x => x.Name == item.Name && x.BranchId == branchId);
if (!exists)
{
var classType = await _classTypeRepository.FirstOrDefaultAsync(x => x.Name == item.ClassTypeName);
if (classType != null)
{
await _levelRepository.InsertAsync(new Level
{
TenantId = tenantId,
BranchId = branchId,
ClassTypeId = classType.Id,
Name = item.Name,
LevelType = item.LevelType,
LessonCount = item.LessonCount,
Status = item.Status,
LessonDuration = item.LessonDuration,
MonthlyPaymentRate = item.MonthlyPaymentRate
});
levelLog.InsertedCount++;
levelLog.InsertedItems.Add(item.Name);
}
}
}
result.Details.Add(levelLog);
var lessonPeriodLog = CreateLog(nameof(LessonPeriod));
foreach (var item in items.LessonPeriods)
{
var exists = await _lessonPeriodRepository.AnyAsync(x => x.Name == item.Name && x.BranchId == branchId);
if (!exists)
{
await _lessonPeriodRepository.InsertAsync(new()
{
TenantId = tenantId,
BranchId = branchId,
Name = item.Name,
Day = item.Day,
Lesson1 = item.Lesson1,
Lesson2 = item.Lesson2,
Lesson3 = item.Lesson3,
Lesson4 = item.Lesson4,
});
lessonPeriodLog.InsertedCount++;
lessonPeriodLog.InsertedItems.Add(item.Name);
}
}
result.Details.Add(lessonPeriodLog);
var scheduleLog = CreateLog(nameof(Schedule));
foreach (var item in items.Schedules)
{
var exists = await _scheduleRepository.AnyAsync(x => x.Name == item.Name && x.BranchId == branchId);
if (!exists)
{
await _scheduleRepository.InsertAsync(new()
{
TenantId = tenantId,
BranchId = branchId,
Name = item.Name,
Status = item.Status,
StartTime = item.StartTime,
EndTime = item.EndTime,
LessonMinute = item.LessonMinute,
LessonBreakMinute = item.LessonBreakMinute,
LessonCount = item.LessonCount,
LunchTime = item.LunchTime,
LunchMinute = item.LunchMinute,
IncludeLunch = item.IncludeLunch,
Monday = item.Monday,
Tuesday = item.Tuesday,
Wednesday = item.Wednesday,
Thursday = item.Thursday,
Friday = item.Friday,
Saturday = item.Saturday,
Sunday = item.Sunday,
});
scheduleLog.InsertedCount++;
scheduleLog.InsertedItems.Add(item.Name);
}
}
result.Details.Add(scheduleLog);
result.Success = true;
result.Message = $"Seed işlemi başarıyla tamamlandı. Toplam {result.TotalInsertedCount} kayıt eklendi.";
return result;
}
catch (Exception ex)
{
result.Success = false;
result.Message = $"Hata: {ex.Message}";
return result;
return ObjectMapper.Map<BranchSeedResult, BranchSeedResultDto>(result);
}
}
}

View file

@ -0,0 +1,11 @@
using AutoMapper;
using Kurs.Platform.Branchs;
public class BranchAutoMapperProfile : Profile
{
public BranchAutoMapperProfile()
{
CreateMap<BranchSeedResult, BranchSeedResultDto>();
CreateMap<BranchSeedDetail, SeedDetailDto>();
}
}

View file

@ -12,13 +12,6 @@
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
<ItemGroup>
<Content Include="Branch\Seeds\BranchData*.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\modules\Kurs.Languages\Kurs.Languages.Application\Kurs.Languages.Application.csproj" />
<ProjectReference Include="..\..\modules\Kurs.Notifications\Kurs.Notifications.Application\Kurs.Notifications.Application.csproj" />

View file

@ -0,0 +1,281 @@
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using System.Text.Json;
using Volo.Abp.Domain.Services;
using Volo.Abp.Domain.Repositories;
using Kurs.Platform.Entities;
namespace Kurs.Platform.Branchs;
public class BranchSeedManager : DomainService
{
private readonly IRepository<Branch, Guid> _branchRepository;
private readonly IRepository<RegistrationType, Guid> _registrationTypeRepository;
private readonly IRepository<RegistrationMethod, Guid> _registrationMethodRepository;
private readonly IRepository<ClassType, Guid> _classTypeRepository;
private readonly IRepository<Class, Guid> _classRepository;
private readonly IRepository<Level, Guid> _levelRepository;
private readonly IRepository<LessonPeriod, Guid> _lessonPeriodRepository;
private readonly IRepository<Schedule, Guid> _scheduleRepository;
public BranchSeedManager(
IRepository<Branch, Guid> branchRepository,
IRepository<RegistrationType, Guid> registrationTypeRepository,
IRepository<RegistrationMethod, Guid> registrationMethodRepository,
IRepository<ClassType, Guid> classTypeRepository,
IRepository<Class, Guid> classRepository,
IRepository<Level, Guid> levelRepository,
IRepository<LessonPeriod, Guid> lessonPeriodRepository,
IRepository<Schedule, Guid> scheduleRepository)
{
_branchRepository = branchRepository;
_registrationTypeRepository = registrationTypeRepository;
_registrationMethodRepository = registrationMethodRepository;
_classTypeRepository = classTypeRepository;
_classRepository = classRepository;
_levelRepository = levelRepository;
_lessonPeriodRepository = lessonPeriodRepository;
_scheduleRepository = scheduleRepository;
}
public async Task<BranchSeedResult> SeedRecordsAsync(Guid? tenantId, Guid branchId)
{
var result = new BranchSeedResult();
try
{
var assemblyPath = Path.GetDirectoryName(typeof(BranchSeedManager).Assembly.Location)!;
var basePath = Path.Combine(assemblyPath, "Branch", "Seeds");
var configuration = new ConfigurationBuilder()
.SetBasePath(basePath)
.AddJsonFile("BranchData.json")
.AddJsonFile($"BranchData.{Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? ""}.json", optional: true)
.Build();
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
var items = configuration.Get<BranchSeederDto>();
if (items == null)
{
result.Success = false;
result.Message = "BranchData.json okunamadı veya format hatalı.";
return result;
}
// Yardımcı fonksiyon: kolay log ekleme
BranchSeedDetail CreateLog(string entity) =>
new()
{
EntityName = entity,
InsertedCount = 0,
InsertedItems = []
};
var registrationTypeLog = CreateLog(nameof(RegistrationType));
foreach (var item in items.RegistrationTypes)
{
var exists = await _registrationTypeRepository.AnyAsync(x => x.Name == item.Name && x.BranchId == branchId);
if (!exists)
{
await _registrationTypeRepository.InsertAsync(new RegistrationType
{
TenantId = tenantId,
BranchId = branchId,
Name = item.Name,
Status = item.Status
}, autoSave: true);
registrationTypeLog.InsertedCount++;
registrationTypeLog.InsertedItems.Add(item.Name);
}
}
result.Details.Add(registrationTypeLog);
var registrationMethodLog = CreateLog(nameof(RegistrationMethod));
foreach (var item in items.RegistrationMethods)
{
var exists = await _registrationMethodRepository.AnyAsync(x => x.Name == item.Name && x.BranchId == branchId);
if (!exists)
{
var registrationType = await _registrationTypeRepository.FirstOrDefaultAsync(x => x.Name == item.RegistrationTypeName);
if (registrationType != null)
{
await _registrationMethodRepository.InsertAsync(new RegistrationMethod
{
TenantId = tenantId,
BranchId = branchId,
RegistrationTypeId = registrationType.Id,
Name = item.Name,
Status = item.Status
});
registrationMethodLog.InsertedCount++;
registrationMethodLog.InsertedItems.Add(item.Name);
}
}
}
result.Details.Add(registrationMethodLog);
var classTypeLog = CreateLog(nameof(ClassType));
foreach (var item in items.ClassTypes)
{
var exists = await _classTypeRepository.AnyAsync(x => x.Name == item.Name);
if (!exists)
{
var registrationType = await _registrationTypeRepository.FirstOrDefaultAsync(x => x.Name == item.RegistrationTypeName && x.BranchId == branchId);
if (registrationType != null)
{
await _classTypeRepository.InsertAsync(new ClassType
{
TenantId = tenantId,
BranchId = branchId,
RegistrationTypeId = registrationType.Id,
Name = item.Name,
MinStudentCount = item.MinStudentCount,
MaxStudentCount = item.MaxStudentCount,
Status = item.Status
}, autoSave: true);
classTypeLog.InsertedCount++;
classTypeLog.InsertedItems.Add(item.Name);
}
}
}
result.Details.Add(classTypeLog);
var classLog = CreateLog(nameof(Class));
foreach (var item in items.Classes)
{
var exists = await _classRepository.AnyAsync(x => x.Name == item.Name && x.BranchId == branchId);
if (!exists)
{
var classType = await _classTypeRepository.FirstOrDefaultAsync(x => x.Name == item.ClassTypeName);
if (classType != null)
{
await _classRepository.InsertAsync(new()
{
TenantId = tenantId,
BranchId = branchId,
ClassTypeId = classType.Id,
Name = item.Name,
Status = item.Status,
});
classLog.InsertedCount++;
classLog.InsertedItems.Add(item.Name);
}
}
}
result.Details.Add(classLog);
var levelLog = CreateLog(nameof(Level));
foreach (var item in items.Levels)
{
var exists = await _levelRepository.AnyAsync(x => x.Name == item.Name && x.BranchId == branchId);
if (!exists)
{
var classType = await _classTypeRepository.FirstOrDefaultAsync(x => x.Name == item.ClassTypeName);
if (classType != null)
{
await _levelRepository.InsertAsync(new Level
{
TenantId = tenantId,
BranchId = branchId,
ClassTypeId = classType.Id,
Name = item.Name,
LevelType = item.LevelType,
LessonCount = item.LessonCount,
Status = item.Status,
LessonDuration = item.LessonDuration,
MonthlyPaymentRate = item.MonthlyPaymentRate
});
levelLog.InsertedCount++;
levelLog.InsertedItems.Add(item.Name);
}
}
}
result.Details.Add(levelLog);
var lessonPeriodLog = CreateLog(nameof(LessonPeriod));
foreach (var item in items.LessonPeriods)
{
var exists = await _lessonPeriodRepository.AnyAsync(x => x.Name == item.Name && x.BranchId == branchId);
if (!exists)
{
await _lessonPeriodRepository.InsertAsync(new()
{
TenantId = tenantId,
BranchId = branchId,
Name = item.Name,
Day = item.Day,
Lesson1 = item.Lesson1,
Lesson2 = item.Lesson2,
Lesson3 = item.Lesson3,
Lesson4 = item.Lesson4,
});
lessonPeriodLog.InsertedCount++;
lessonPeriodLog.InsertedItems.Add(item.Name);
}
}
result.Details.Add(lessonPeriodLog);
var scheduleLog = CreateLog(nameof(Schedule));
foreach (var item in items.Schedules)
{
var exists = await _scheduleRepository.AnyAsync(x => x.Name == item.Name && x.BranchId == branchId);
if (!exists)
{
await _scheduleRepository.InsertAsync(new()
{
TenantId = tenantId,
BranchId = branchId,
Name = item.Name,
Status = item.Status,
StartTime = item.StartTime,
EndTime = item.EndTime,
LessonMinute = item.LessonMinute,
LessonBreakMinute = item.LessonBreakMinute,
LessonCount = item.LessonCount,
LunchTime = item.LunchTime,
LunchMinute = item.LunchMinute,
IncludeLunch = item.IncludeLunch,
Monday = item.Monday,
Tuesday = item.Tuesday,
Wednesday = item.Wednesday,
Thursday = item.Thursday,
Friday = item.Friday,
Saturday = item.Saturday,
Sunday = item.Sunday,
});
scheduleLog.InsertedCount++;
scheduleLog.InsertedItems.Add(item.Name);
}
}
result.Details.Add(scheduleLog);
result.Success = true;
result.Message = $"Seed işlemi başarıyla tamamlandı. Toplam {result.TotalInsertedCount} kayıt eklendi.";
return result;
}
catch (Exception ex)
{
result.Success = false;
result.Message = $"Hata: {ex.Message}";
return result;
}
}
}

View file

@ -0,0 +1,21 @@
using System.Collections.Generic;
using System.Linq;
namespace Kurs.Platform.Branchs;
public class BranchSeedResult
{
public bool Success { get; set; }
public string Message { get; set; } = string.Empty;
public List<BranchSeedDetail> Details { get; set; } = new();
public int TotalInsertedCount => Details.Sum(x => x.InsertedCount);
}
public class BranchSeedDetail
{
public string EntityName { get; set; } = string.Empty;
public int InsertedCount { get; set; }
public List<string> InsertedItems { get; set; } = new();
public List<string> Warnings { get; set; } = new();
public List<string> Errors { get; set; } = new();
}

View file

@ -1,6 +1,6 @@
using System.Collections.Generic;
namespace Kurs.Platform.Seeds;
namespace Kurs.Platform.Branchs;
public class BranchSeederDto
{

View file

@ -1,12 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\common.props" />
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<RootNamespace>Kurs.Platform</RootNamespace>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\modules\Kurs.Languages\Kurs.Languages.Domain\Kurs.Languages.Domain.csproj" />
<ProjectReference Include="..\..\modules\Kurs.Sender\Kurs.Sender.csproj" />
@ -15,7 +15,14 @@
<ProjectReference Include="..\..\modules\Kurs.Settings\Kurs.Settings.Domain\Kurs.Settings.Domain.csproj" />
<ProjectReference Include="..\Kurs.Platform.Domain.Shared\Kurs.Platform.Domain.Shared.csproj" />
</ItemGroup>
<ItemGroup>
<Content Include="Branch\Seeds\BranchData*.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Flurl.Http" Version="4.0.2" />
<PackageReference Include="Volo.Abp.Emailing" Version="9.0.2" />
@ -31,5 +38,5 @@
<PackageReference Include="Volo.Abp.BlobStoring" Version="9.0.2" />
<PackageReference Include="Volo.Abp.BlobStoring.FileSystem" Version="9.0.2" />
</ItemGroup>
</Project>