Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
akbaramd committed Dec 12, 2024
1 parent 48d5038 commit c3059fd
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 31 deletions.
1 change: 1 addition & 0 deletions Nezam.ESS.sln.DotSettings.user
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AIBonDbContextProvider_00601_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F73926448008f4654ba366e96c5f78f0a13200_003F3f_003Fc0aa34cd_003FIBonDbContextProvider_00601_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ALifetimeScope_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fd499e58f2ca0abe5f3273d5ea266296015c34f889364c67aae9449be9c9dd31_003FLifetimeScope_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ARuntimeType_002ECoreCLR_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F955ec549fe664629353c3b5424b6ad6c7dfcec4ab59bae709ab962c228cf45_003FRuntimeType_002ECoreCLR_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AServiceProvider_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fce37be1a06b16c6faa02038d2cc477dd3bca5b217ceeb41c5f2ad45c1bf9_003FServiceProvider_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AType_002ECoreCLR_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F394cf11a64687141fd58fb926ff8b3d5a299eba3bbadaf2de9b93928d523d4_003FType_002ECoreCLR_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AWebApplicationBuilder_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F9e6ecf3790a24c43487ae724bf9ea8185bdedaf6c28da376d6ea862e66453_003FWebApplicationBuilder_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AWebBonModularityApplication_00601_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fc544048954794c9f9154227c63b5c363ae00_003Fc5_003Fffd01909_003FWebBonModularityApplication_00601_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
Expand Down
1 change: 1 addition & 0 deletions Nezam.Modular.ESS.WebApi/Nezam.Modular.ESS.WebApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

<ItemGroup>
<ProjectReference Include="..\..\Payeh\Payeh.SharedKernel\Payeh.SharedKernel.csproj" />
<ProjectReference Include="..\Nezam.Modular.ESS.Identity.Application\Nezam.Modular.ESS.Identity.Application.csproj" />
<ProjectReference Include="..\Nezam.Modular.ESS.Infrastructure\Nezam.Modular.ESS.Infrastructure.csproj" />
</ItemGroup>

Expand Down
6 changes: 5 additions & 1 deletion Nezam.Modular.ESS.WebApi/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
using System.Globalization;
using Microsoft.AspNetCore.Localization;
using Nezam.Modular.ESS.Identity.Application;
using Nezam.Modular.ESS.Infrastructure;
using Nezam.Modular.ESS.Units.Application;
using Payeh.SharedKernel.EntityFrameworkCore.UnitOfWork;

var builder = WebApplication.CreateBuilder(args);

// Add localization services with default culture set to Persian
builder.Services.AddLocalization();

builder.Services.AddIdentityApplication();
builder.Services.AddUnitsApplication();
builder.Services.AddInfrastructure();

builder.Services.Configure<RequestLocalizationOptions>(options =>
Expand Down
20 changes: 15 additions & 5 deletions Nezam.Modular.ESS.infrastructure/Data/AppDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
using System.Reflection;
using Microsoft.EntityFrameworkCore;
using Nezam.Modular.ESS.Secretariat.Domain.Documents;
using Nezam.Modular.ESS.Units.Domain.Member;
using Nezam.Modular.ESS.Units.Domain.Units;
using Nezam.Modular.ESS.Identity.Domain.Engineer;
using Nezam.Modular.ESS.Identity.Domain.Employer;
using Nezam.Modular.ESS.Identity.Domain.Roles;
using Nezam.Modular.ESS.Identity.Domain.User;
using Payeh.SharedKernel.Domain;
using Payeh.SharedKernel.EntityFrameworkCore.Domain;

namespace Nezam.Modular.ESS.Infrastructure.Data;

public class AppDbContext : DbContext
{


public DbSet<UserEntity> Users { get; set; }
public DbSet<RoleEntity> Roles { get; set; }
public DbSet<UserVerificationTokenEntity> UserVerificationTokens { get; set; }
public DbSet<EngineerEntity> Engineers { get; set; }
public DbSet<EmployerEntity> Employers { get; set; }
public DbSet<DocumentAggregateRoot> Documents { get; set; }
// public DbSet<DocumentAggregateRoot> Documents { get; set; }
public DbSet<MemberEntity> Members { get; set; }
public DbSet<UnitEntity> Units { get; set; }
public DbSet<UnitMemberEntity> UnitMembers { get; set; }
Expand All @@ -31,11 +29,23 @@ public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ConfigureDomain();
var entityTypes = modelBuilder.Model.GetEntityTypes()
.Where(et => typeof(Entity).IsAssignableFrom(et.ClrType)) // Filter for types assignable from Entity
.ToList();

foreach (var entityType in entityTypes)
{
Console.WriteLine($"Configuring entity: {entityType.DisplayName()}");
modelBuilder.Entity(entityType.ClrType).DomainConfiguration(); // Apply custom domain configurations
}

// Apply configurations from the current assembly
modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());

base.OnModelCreating(modelBuilder);
}


protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.EnableSensitiveDataLogging();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Nezam.Modular.ESS.Identity.Domain.User;
using Payeh.SharedKernel.EntityFrameworkCore.Domain;

namespace Nezam.Modular.ESS.Infrastructure.Data.Configurations;

Expand All @@ -10,7 +11,9 @@ public void Configure(EntityTypeBuilder<UserEntity> builder)
{
// Configure UserId as the primary key
builder.HasKey(x => x.UserId); // Explicitly telling EF that UserId is the primary key




// Configuring value objects as owned types
builder.OwnsOne(x => x.UserName, userName =>
{
Expand Down
47 changes: 25 additions & 22 deletions Nezam.Modular.ESS.infrastructure/Data/Seeds/IdentitySeedService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Nezam.Modular.ESS.Identity.Domain.Shared.User;
using Nezam.Modular.ESS.Identity.Domain.User;
using Payeh.SharedKernel.UnitOfWork;
Expand All @@ -7,35 +8,37 @@ namespace Nezam.Modular.ESS.Infrastructure.Data.Seeds;

public class IdentitySeedService : BackgroundService
{
private readonly IUserDomainService _domainService;
private readonly IUnitOfWork _unitOfWorkManager;
private readonly IServiceProvider _serviceProvider;

public IdentitySeedService(IUserDomainService domainService, IUnitOfWork unitOfWorkManager)
public IdentitySeedService(IServiceProvider serviceProvider)
{
_domainService = domainService;
_unitOfWorkManager = unitOfWorkManager;
_serviceProvider = serviceProvider;
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{

var find = await _domainService.GetUserByUsernameAsync(new UserNameValue("admin"));

if (find.IsSuccess)
{
var user = find.Data;
user.UpdateProfile(new UserProfileValue("/default","admin","administrator"));
await _domainService.UpdateAsync(user);
}
else
using (var scope = _serviceProvider.CreateScope())
{
var profile = new UserProfileValue("/default","admin","administrator");
var user = new UserEntity(UserId.NewId(), new UserNameValue("admin"), new UserPasswordValue("Admin@123456"),
profile, new UserEmailValue("[email protected]"));
await _domainService.Create(user);
}
var domainService = scope.ServiceProvider.GetRequiredService<IUserDomainService>();
var unitOfWorkManager = scope.ServiceProvider.GetRequiredService<IUnitOfWork>();

var find = await domainService.GetUserByUsernameAsync(new UserNameValue("admin"));

await _unitOfWorkManager.CommitAsync();
if (find.IsSuccess)
{
var user = find.Data;
user.UpdateProfile(new UserProfileValue("/default", "admin", "administrator"));
await domainService.UpdateAsync(user);
}
else
{
var profile = new UserProfileValue("/default", "admin", "administrator");
var user = new UserEntity(UserId.NewId(), new UserNameValue("admin"),
new UserPasswordValue("Admin@123456"), profile, new UserEmailValue("[email protected]"));
await domainService.Create(user);
}

await unitOfWorkManager.CommitAsync();
}
}
}
7 changes: 6 additions & 1 deletion Nezam.Modular.ESS.infrastructure/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Nezam.Modular.ESS.Infrastructure.Data.Seeds;
using Nezam.Modular.ESS.Units.Domain.Member;
using Nezam.Modular.ESS.Units.Domain.Units;
using Payeh.SharedKernel.EntityFrameworkCore.UnitOfWork;

namespace Nezam.Modular.ESS.Infrastructure;

Expand All @@ -19,7 +20,11 @@ public static class Extensions
public static IServiceCollection AddInfrastructure(this IServiceCollection services)
{
services.AddHostedService<IdentitySeedService>();

services.AddUnitOfWork<AppDbContext>();
services.AddMediatR(c =>
{
c.RegisterServicesFromAssemblies(AppDomain.CurrentDomain.GetAssemblies());
});
services.AddDbContext<AppDbContext>(c =>
{
c.UseSqlite("Data Source=NezamEes.db;Mode=ReadWrite;");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

<ItemGroup>
<ProjectReference Include="..\..\Payeh\Payeh.SharedKernel\Payeh.SharedKernel.csproj" />
<ProjectReference Include="..\New\modules\Secretariat\Nezam.Modular.ESS.Secretariat.Application\Nezam.Modular.ESS.Secretariat.Application.csproj" />
<ProjectReference Include="..\New\modules\Units\Nezam.Modular.ESS.Units.Application\Nezam.Modular.ESS.Units.Application.csproj" />
<ProjectReference Include="..\Nezam.Modular.ESS.Identity.Domain\Nezam.Modular.ESS.Identity.Domain.csproj" />
</ItemGroup>
Expand Down

0 comments on commit c3059fd

Please sign in to comment.