using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using Microsoft.Extensions.Configuration; using Sozsoft.Platform.Entities; using Volo.Abp.Data; using Volo.Abp.DependencyInjection; using Volo.Abp.Domain.Repositories; namespace Sozsoft.Platform.Data.Seeds; public class RouteSeedDto { public string Key { get; set; } public string Path { get; set; } public string ComponentType { get; set; } public string ComponentPath { get; set; } public string RouteType { get; set; } public string[] Authority { get; set; } } public class ModuleSeedDto { public string Name { get; set; } } public class MenuSeedDto { public string ParentCode { get; set; } public string Code { get; set; } public string DisplayName { get; set; } public int Order { get; set; } public string Url { get; set; } public string Icon { get; set; } public string CssClass { get; set; } public string RequiredPermissionName { get; set; } public bool IsDisabled { get; set; } public string ModuleId { get; set; } } public class MenuSeederDto { public List Routes { get; set; } public List Modules { get; set; } public List Menus { get; set; } } public class MenuDataSeeder : IDataSeedContributor, ITransientDependency { private readonly IRepository _routeRepository; private readonly IRepository _menuRepository; private readonly IRepository _moduleRepository; public MenuDataSeeder( IRepository routeRepository, IRepository menuRepository, IRepository moduleRepository ) { _routeRepository = routeRepository; _menuRepository = menuRepository; _moduleRepository = moduleRepository; } public async Task SeedAsync(DataSeedContext context) { var configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile(Path.Combine("Migrations", "MenusData.json")) .Build(); var items = configuration.Get(); foreach (var item in items.Routes) { var exists = await _routeRepository.AnyAsync(x => x.Key == item.Key); if (!exists) { await _routeRepository.InsertAsync(new Route( item.Key, item.Path, item.ComponentType, item.ComponentPath, item.RouteType, item.Authority ?? [] ), autoSave: true); } } foreach (var item in items.Modules) { var exists = await _moduleRepository.AnyAsync(x => x.Name == item.Name); if (!exists) { await _moduleRepository.InsertAsync(new Module(item.Name) { Name = item.Name }, autoSave: true); } } foreach (var item in items.Menus) { var exists = await _menuRepository.AnyAsync(x => x.Code == item.Code); if (!exists) { // Menunun bagli oldugu modul Modules bolumunde tanimli degilse burada olusturulur; // aksi halde ModuleId yabanci anahtari seed sirasinda hata verir. await EnsureModuleAsync(item.ModuleId); await _menuRepository.InsertAsync(new Menu { ParentCode = string.IsNullOrWhiteSpace(item.ParentCode) ? null : item.ParentCode, Code = item.Code, DisplayName = item.DisplayName, Order = item.Order, Url = item.Url, Icon = item.Icon, CssClass = item.CssClass, RequiredPermissionName = item.RequiredPermissionName, IsDisabled = item.IsDisabled, ModuleId = item.ModuleId }, autoSave: true); } } } private async Task EnsureModuleAsync(string moduleId) { var cleaned = moduleId?.Trim(); if (string.IsNullOrWhiteSpace(cleaned)) { return; } if (await _moduleRepository.AnyAsync(x => x.Id == cleaned)) { return; } await _moduleRepository.InsertAsync(new Module(cleaned) { Name = cleaned }, autoSave: true); } }