Branch Seed Manager eklendi
This commit is contained in:
parent
94b0d17c26
commit
ca358bc4f9
12 changed files with 364 additions and 330 deletions
|
|
@ -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();
|
||||||
|
}
|
||||||
|
|
@ -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
|
||||||
|
}
|
||||||
|
|
@ -1,338 +1,35 @@
|
||||||
using System;
|
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 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
|
public class BranchAppService : PlatformAppService
|
||||||
{
|
{
|
||||||
private readonly IRepository<Branch, Guid> _branchRepository;
|
private readonly IRepository<Branch, Guid> _branchRepository;
|
||||||
private readonly IRepository<Language, Guid> _languages;
|
private readonly BranchSeedManager _branchSeedManager;
|
||||||
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;
|
|
||||||
|
|
||||||
public BranchAppService(
|
public BranchAppService(
|
||||||
IRepository<Branch, Guid> branchRepository,
|
IRepository<Branch, Guid> branchRepository,
|
||||||
IRepository<Language, Guid> languages,
|
BranchSeedManager branchSeedManager)
|
||||||
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
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
_branchRepository = branchRepository;
|
_branchRepository = branchRepository;
|
||||||
_languages = languages;
|
_branchSeedManager = branchSeedManager;
|
||||||
_languageKey = languageKey;
|
|
||||||
_languagesText = languagesText;
|
|
||||||
_dataSources = dataSource;
|
|
||||||
_settings = settings;
|
|
||||||
_registrationTypeRepository = registrationTypeRepository;
|
|
||||||
_registrationMethodRepository = registrationMethodRepository;
|
|
||||||
_classTypeRepository = classTypeRepository;
|
|
||||||
_classRepository = classRepository;
|
|
||||||
_levelRepository = levelRepository;
|
|
||||||
_lessonPeriodRepository = lessonPeriodRepository;
|
|
||||||
_scheduleRepository = scheduleRepository;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<BranchSeedResultDto> SeedAsync(Guid branchId)
|
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 result = new BranchSeedResultDto();
|
|
||||||
var context = new DataSeedContext(branchId);
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
var branch = await _branchRepository.FirstOrDefaultAsync(x => x.Id == branchId);
|
var branch = await _branchRepository.FirstOrDefaultAsync(x => x.Id == branchId);
|
||||||
if (branch == null)
|
if (branch == null)
|
||||||
{
|
return new BranchSeedResultDto { Success = false, Message = "Branch not found." };
|
||||||
result.Success = false;
|
|
||||||
result.Message = "Branch not found.";
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
var tenantId = branch.TenantId;
|
using (CurrentTenant.Change(branch.TenantId))
|
||||||
var assemblyPath = Path.GetDirectoryName(typeof(BranchAppService).Assembly.Location)!;
|
{
|
||||||
var basePath = Path.Combine(assemblyPath, "Branch", "Seeds");
|
var result = await _branchSeedManager.SeedRecordsAsync(branch.TenantId, branchId);
|
||||||
|
|
||||||
var configuration = new ConfigurationBuilder()
|
return ObjectMapper.Map<BranchSeedResult, BranchSeedResultDto>(result);
|
||||||
.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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
using AutoMapper;
|
||||||
|
using Kurs.Platform.Branchs;
|
||||||
|
|
||||||
|
public class BranchAutoMapperProfile : Profile
|
||||||
|
{
|
||||||
|
public BranchAutoMapperProfile()
|
||||||
|
{
|
||||||
|
CreateMap<BranchSeedResult, BranchSeedResultDto>();
|
||||||
|
CreateMap<BranchSeedDetail, SeedDetailDto>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -12,13 +12,6 @@
|
||||||
<FrameworkReference Include="Microsoft.AspNetCore.App" />
|
<FrameworkReference Include="Microsoft.AspNetCore.App" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<Content Include="Branch\Seeds\BranchData*.json">
|
|
||||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
|
||||||
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
|
|
||||||
</Content>
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\..\modules\Kurs.Languages\Kurs.Languages.Application\Kurs.Languages.Application.csproj" />
|
<ProjectReference Include="..\..\modules\Kurs.Languages\Kurs.Languages.Application\Kurs.Languages.Application.csproj" />
|
||||||
<ProjectReference Include="..\..\modules\Kurs.Notifications\Kurs.Notifications.Application\Kurs.Notifications.Application.csproj" />
|
<ProjectReference Include="..\..\modules\Kurs.Notifications\Kurs.Notifications.Application\Kurs.Notifications.Application.csproj" />
|
||||||
|
|
|
||||||
281
api/src/Kurs.Platform.Domain/Branch/BranchSeedManager.cs
Normal file
281
api/src/Kurs.Platform.Domain/Branch/BranchSeedManager.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
21
api/src/Kurs.Platform.Domain/Branch/BranchSeedResult.cs
Normal file
21
api/src/Kurs.Platform.Domain/Branch/BranchSeedResult.cs
Normal 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();
|
||||||
|
}
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace Kurs.Platform.Seeds;
|
namespace Kurs.Platform.Branchs;
|
||||||
|
|
||||||
public class BranchSeederDto
|
public class BranchSeederDto
|
||||||
{
|
{
|
||||||
|
|
@ -16,6 +16,13 @@
|
||||||
<ProjectReference Include="..\Kurs.Platform.Domain.Shared\Kurs.Platform.Domain.Shared.csproj" />
|
<ProjectReference Include="..\Kurs.Platform.Domain.Shared\Kurs.Platform.Domain.Shared.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Content Include="Branch\Seeds\BranchData*.json">
|
||||||
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
|
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
|
||||||
|
</Content>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Flurl.Http" Version="4.0.2" />
|
<PackageReference Include="Flurl.Http" Version="4.0.2" />
|
||||||
<PackageReference Include="Volo.Abp.Emailing" Version="9.0.2" />
|
<PackageReference Include="Volo.Abp.Emailing" Version="9.0.2" />
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue