2026-08-21 19:27:59 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2026-03-04 13:47:25 +00:00
|
|
|
|
using System.IO;
|
2026-08-17 14:26:51 +00:00
|
|
|
|
using System.Linq;
|
2026-03-04 13:47:25 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
2026-08-21 19:27:59 +00:00
|
|
|
|
using Sozsoft.Platform.Entities;
|
2026-03-04 13:47:25 +00:00
|
|
|
|
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; }
|
2026-06-02 07:14:33 +00:00
|
|
|
|
public string ComponentType { get; set; }
|
2026-03-04 13:47:25 +00:00
|
|
|
|
public string ComponentPath { get; set; }
|
|
|
|
|
|
public string RouteType { get; set; }
|
|
|
|
|
|
public string[] Authority { get; set; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-31 14:17:50 +00:00
|
|
|
|
public class ModuleSeedDto
|
2026-03-17 19:56:34 +00:00
|
|
|
|
{
|
|
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-10 21:18:35 +00:00
|
|
|
|
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; }
|
2026-09-09 06:06:25 +00:00
|
|
|
|
public string CssClass { get; set; }
|
2026-03-10 21:18:35 +00:00
|
|
|
|
public string RequiredPermissionName { get; set; }
|
|
|
|
|
|
public bool IsDisabled { get; set; }
|
2026-08-31 14:17:50 +00:00
|
|
|
|
public string ModuleId { get; set; }
|
2026-03-10 21:18:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-04 13:47:25 +00:00
|
|
|
|
public class MenuSeederDto
|
|
|
|
|
|
{
|
|
|
|
|
|
public List<RouteSeedDto> Routes { get; set; }
|
2026-08-31 14:17:50 +00:00
|
|
|
|
public List<ModuleSeedDto> Modules { get; set; }
|
2026-03-04 13:47:25 +00:00
|
|
|
|
public List<MenuSeedDto> Menus { get; set; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public class MenuDataSeeder : IDataSeedContributor, ITransientDependency
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IRepository<Route, Guid> _routeRepository;
|
|
|
|
|
|
private readonly IRepository<Menu, Guid> _menuRepository;
|
2026-08-31 14:17:50 +00:00
|
|
|
|
private readonly IRepository<Module, string> _moduleRepository;
|
2026-03-04 13:47:25 +00:00
|
|
|
|
|
|
|
|
|
|
public MenuDataSeeder(
|
|
|
|
|
|
IRepository<Route, Guid> routeRepository,
|
2026-03-17 19:56:34 +00:00
|
|
|
|
IRepository<Menu, Guid> menuRepository,
|
2026-08-31 14:17:50 +00:00
|
|
|
|
IRepository<Module, string> moduleRepository
|
2026-03-04 13:47:25 +00:00
|
|
|
|
)
|
|
|
|
|
|
{
|
|
|
|
|
|
_routeRepository = routeRepository;
|
|
|
|
|
|
_menuRepository = menuRepository;
|
2026-08-31 14:17:50 +00:00
|
|
|
|
_moduleRepository = moduleRepository;
|
2026-03-04 13:47:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task SeedAsync(DataSeedContext context)
|
|
|
|
|
|
{
|
|
|
|
|
|
var configuration = new ConfigurationBuilder()
|
|
|
|
|
|
.SetBasePath(Directory.GetCurrentDirectory())
|
2026-08-12 12:20:00 +00:00
|
|
|
|
.AddJsonFile(Path.Combine("Migrations", "MenusData.json"))
|
2026-03-04 13:47:25 +00:00
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
|
|
var items = configuration.Get<MenuSeederDto>();
|
|
|
|
|
|
|
|
|
|
|
|
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,
|
2026-06-02 07:14:33 +00:00
|
|
|
|
item.ComponentType,
|
2026-03-04 13:47:25 +00:00
|
|
|
|
item.ComponentPath,
|
|
|
|
|
|
item.RouteType,
|
|
|
|
|
|
item.Authority ?? []
|
2026-05-02 18:43:32 +00:00
|
|
|
|
), autoSave: true);
|
2026-03-04 13:47:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-31 14:17:50 +00:00
|
|
|
|
foreach (var item in items.Modules)
|
2026-03-17 19:56:34 +00:00
|
|
|
|
{
|
2026-08-31 14:17:50 +00:00
|
|
|
|
var exists = await _moduleRepository.AnyAsync(x => x.Name == item.Name);
|
2026-03-17 19:56:34 +00:00
|
|
|
|
|
|
|
|
|
|
if (!exists)
|
|
|
|
|
|
{
|
2026-08-31 14:17:50 +00:00
|
|
|
|
await _moduleRepository.InsertAsync(new Module(item.Name)
|
2026-03-17 19:56:34 +00:00
|
|
|
|
{
|
|
|
|
|
|
Name = item.Name
|
2026-05-02 18:43:32 +00:00
|
|
|
|
}, autoSave: true);
|
2026-03-17 19:56:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-04 13:47:25 +00:00
|
|
|
|
foreach (var item in items.Menus)
|
|
|
|
|
|
{
|
|
|
|
|
|
var exists = await _menuRepository.AnyAsync(x => x.Code == item.Code);
|
|
|
|
|
|
|
|
|
|
|
|
if (!exists)
|
|
|
|
|
|
{
|
2026-08-31 14:17:50 +00:00
|
|
|
|
// Menunun bagli oldugu modul Modules bolumunde tanimli degilse burada olusturulur;
|
|
|
|
|
|
// aksi halde ModuleId yabanci anahtari seed sirasinda hata verir.
|
|
|
|
|
|
await EnsureModuleAsync(item.ModuleId);
|
|
|
|
|
|
|
2026-03-04 13:47:25 +00:00
|
|
|
|
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,
|
2026-09-09 06:06:25 +00:00
|
|
|
|
CssClass = item.CssClass,
|
2026-03-04 13:47:25 +00:00
|
|
|
|
RequiredPermissionName = item.RequiredPermissionName,
|
|
|
|
|
|
IsDisabled = item.IsDisabled,
|
2026-08-31 14:17:50 +00:00
|
|
|
|
ModuleId = item.ModuleId
|
2026-05-02 18:43:32 +00:00
|
|
|
|
}, autoSave: true);
|
2026-03-04 13:47:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-08-31 14:17:50 +00:00
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
2026-03-04 13:47:25 +00:00
|
|
|
|
}
|