Commit 3523f34a authored by hasan khaddour's avatar hasan khaddour

Update Persistence an bakjobs

parent 624fadfb
......@@ -5,6 +5,8 @@ using PSManagement.Domain.Identity.Entities;
using PSManagement.Domain.Projects.Entities;
using PSManagement.Domain.Tracking;
using PSManagement.Domain.Tracking.Entities;
using PSManagement.Infrastructure.Persistence.SeedDataContext;
using PSManagement.SharedKernel.Entities;
namespace PSManagement.Infrastructure.Persistence
{
......@@ -37,6 +39,9 @@ namespace PSManagement.Infrastructure.Persistence
{
modelBuilder.ApplyConfigurationsFromAssembly(typeof(AppDbContext).Assembly);
// modelBuilder.Entity<BaseEntity>().Ignore(e => e.Events);
SeedData.SeedAsync(modelBuilder).Wait();
base.OnModelCreating(modelBuilder);
......
......@@ -7,6 +7,8 @@ using PSManagement.Domain.Projects.Repositories;
using PSManagement.Infrastructure.Persistence.Repositories.CustomerRepository;
using PSManagement.Infrastructure.Persistence.Repositories.ProjectRepository;
using PSManagement.Infrastructure.Persistence.Repositories.UserRepository;
using PSManagement.Infrastructure.Persistence.UoW;
using PSManagement.SharedKernel.Interfaces;
namespace PSManagement.Infrastructure.Persistence.DI
{
......@@ -23,6 +25,8 @@ namespace PSManagement.Infrastructure.Persistence.DI
services.AddScoped<IProjectsRepository, ProjectsRepository>();
services.AddScoped<IUnitOfWork, UnitOfWork>();
return services;
}
......
......@@ -23,9 +23,6 @@ namespace PSManagement.Infrastructure.Persistence.EntitiesConfiguration
}
);
builder.HasMany(e => e.Projects).WithOne(e => e.Proposer);
builder.OwnsMany(c => c.ContactInfo);
}
......
......@@ -5,10 +5,10 @@ using PSManagement.Domain.Projects.Entities;
namespace PSManagement.Infrastructure.Persistence.EntitiesConfiguration
{
public class FinicialSpendingEntityConfiguration :
IEntityTypeConfiguration<FinincialSpending>
IEntityTypeConfiguration<FinancialSpending>
{
public void Configure(EntityTypeBuilder<FinincialSpending> builder)
public void Configure(EntityTypeBuilder<FinancialSpending> builder)
{
builder.OwnsOne(e => e.ExternalPurchase, p =>
{
......@@ -17,13 +17,6 @@ namespace PSManagement.Infrastructure.Persistence.EntitiesConfiguration
});
builder.OwnsOne(e => e.LocalPurchase, p =>
{
p.Property(e => e.Ammount).HasColumnName("LocalPurchaseAmmount");
p.Property(e => e.Currency).HasColumnName("LocalPurchaseCurrency").HasDefaultValue("SP");
});
}
}
}
......@@ -31,6 +31,8 @@ namespace PSManagement.Infrastructure.Persistence.EntitiesConfiguration
p.Property(e => e.Description).HasColumnName("Description");
p.Property(e => e.Code).HasColumnName("Code");
p.Property(e => e.Name).HasColumnName("Name");
p.Property(e => e.StartDate).HasColumnName("StartDate");
p.Property(e => e.ExpectedEndDate).HasColumnName("ExpectedEndDate");
}
......@@ -54,15 +56,15 @@ namespace PSManagement.Infrastructure.Persistence.EntitiesConfiguration
p.Property(e => e.ProposingBookNumber).HasColumnName("ProposingBookNumber");
}
);
builder.OwnsOne(c => c.FinincialFund,
builder.OwnsOne(c => c.FinancialFund,
p => {
p.Property(e => e.FinicialStatus).HasColumnName("FinicialStatus");
p.Property(e => e.FinancialStatus).HasColumnName("FinicialStatus");
p.Property(e => e.Source).HasColumnName("FinicialSource");
}
);
builder.HasOne(e => e.ProjectStatus).WithMany();
builder.HasMany(e => e.FinincialSpending);
builder.HasMany(e => e.FinancialSpending);
builder.HasMany(e => e.Tracks).WithOne(e => e.Project).HasForeignKey(e => e.ProjectId);
}
......
using Microsoft.EntityFrameworkCore.Migrations;
namespace PSManagement.Infrastructure.Persistence.Migrations
{
public partial class AddDomainsUpdate4 : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_FinincialSpending_Projects_ProjectId",
table: "FinincialSpending");
migrationBuilder.DropPrimaryKey(
name: "PK_FinincialSpending",
table: "FinincialSpending");
migrationBuilder.DropColumn(
name: "LocalPurchaseAmmount",
table: "FinincialSpending");
migrationBuilder.DropColumn(
name: "LocalPurchaseCurrency",
table: "FinincialSpending");
migrationBuilder.RenameTable(
name: "FinincialSpending",
newName: "FinancialSpending");
migrationBuilder.RenameIndex(
name: "IX_FinincialSpending_ProjectId",
table: "FinancialSpending",
newName: "IX_FinancialSpending_ProjectId");
migrationBuilder.AddColumn<int>(
name: "LocalPurchase",
table: "FinancialSpending",
type: "int",
nullable: false,
defaultValue: 0);
migrationBuilder.AddPrimaryKey(
name: "PK_FinancialSpending",
table: "FinancialSpending",
column: "Id");
migrationBuilder.InsertData(
table: "Departments",
columns: new[] { "Id", "Name" },
values: new object[] { 1, "قسم المعلوميات" });
migrationBuilder.InsertData(
table: "Departments",
columns: new[] { "Id", "Name" },
values: new object[] { 2, "قسم النظم الإلكترونيى" });
migrationBuilder.InsertData(
table: "Departments",
columns: new[] { "Id", "Name" },
values: new object[] { 3, "شؤون الطلاب" });
migrationBuilder.AddForeignKey(
name: "FK_FinancialSpending_Projects_ProjectId",
table: "FinancialSpending",
column: "ProjectId",
principalTable: "Projects",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_FinancialSpending_Projects_ProjectId",
table: "FinancialSpending");
migrationBuilder.DropPrimaryKey(
name: "PK_FinancialSpending",
table: "FinancialSpending");
migrationBuilder.DeleteData(
table: "Departments",
keyColumn: "Id",
keyValue: 1);
migrationBuilder.DeleteData(
table: "Departments",
keyColumn: "Id",
keyValue: 2);
migrationBuilder.DeleteData(
table: "Departments",
keyColumn: "Id",
keyValue: 3);
migrationBuilder.DropColumn(
name: "LocalPurchase",
table: "FinancialSpending");
migrationBuilder.RenameTable(
name: "FinancialSpending",
newName: "FinincialSpending");
migrationBuilder.RenameIndex(
name: "IX_FinancialSpending_ProjectId",
table: "FinincialSpending",
newName: "IX_FinincialSpending_ProjectId");
migrationBuilder.AddColumn<int>(
name: "LocalPurchaseAmmount",
table: "FinincialSpending",
type: "int",
nullable: true);
migrationBuilder.AddColumn<string>(
name: "LocalPurchaseCurrency",
table: "FinincialSpending",
type: "nvarchar(max)",
nullable: true,
defaultValue: "SP");
migrationBuilder.AddPrimaryKey(
name: "PK_FinincialSpending",
table: "FinincialSpending",
column: "Id");
migrationBuilder.AddForeignKey(
name: "FK_FinincialSpending_Projects_ProjectId",
table: "FinincialSpending",
column: "ProjectId",
principalTable: "Projects",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
}
}
}
using System;
using Microsoft.EntityFrameworkCore.Migrations;
namespace PSManagement.Infrastructure.Persistence.Migrations
{
public partial class AddDomainsUpdate5 : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<DateTime>(
name: "ExpectedSpendingDate",
table: "FinancialSpending",
type: "datetime2",
nullable: false,
defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified));
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "ExpectedSpendingDate",
table: "FinancialSpending");
}
}
}
using System;
using Microsoft.EntityFrameworkCore.Migrations;
namespace PSManagement.Infrastructure.Persistence.Migrations
{
public partial class AddDomainsUpdate6 : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<DateTime>(
name: "ExpectedEndDate",
table: "Projects",
type: "datetime2",
nullable: true);
migrationBuilder.AddColumn<DateTime>(
name: "StartDate",
table: "Projects",
type: "datetime2",
nullable: true);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "ExpectedEndDate",
table: "Projects");
migrationBuilder.DropColumn(
name: "StartDate",
table: "Projects");
}
}
}
......@@ -51,6 +51,23 @@ namespace PSManagement.Infrastructure.Persistence.Migrations
b.HasKey("Id");
b.ToTable("Departments");
b.HasData(
new
{
Id = 1,
Name = "قسم المعلوميات"
},
new
{
Id = 2,
Name = "قسم النظم الإلكترونيى"
},
new
{
Id = 3,
Name = "شؤون الطلاب"
});
});
modelBuilder.Entity("PSManagement.Domain.Employees.Entities.Employee", b =>
......@@ -179,7 +196,7 @@ namespace PSManagement.Infrastructure.Persistence.Migrations
b.ToTable("EmployeeParticipate");
});
modelBuilder.Entity("PSManagement.Domain.Projects.Entities.FinincialSpending", b =>
modelBuilder.Entity("PSManagement.Domain.Projects.Entities.FinancialSpending", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
......@@ -192,6 +209,12 @@ namespace PSManagement.Infrastructure.Persistence.Migrations
b.Property<string>("Description")
.HasColumnType("nvarchar(max)");
b.Property<DateTime>("ExpectedSpendingDate")
.HasColumnType("datetime2");
b.Property<int>("LocalPurchase")
.HasColumnType("int");
b.Property<int?>("ProjectId")
.HasColumnType("int");
......@@ -199,7 +222,7 @@ namespace PSManagement.Infrastructure.Persistence.Migrations
b.HasIndex("ProjectId");
b.ToTable("FinincialSpending");
b.ToTable("FinancialSpending");
});
modelBuilder.Entity("PSManagement.Domain.Projects.Entities.Project", b =>
......@@ -583,15 +606,15 @@ namespace PSManagement.Infrastructure.Persistence.Migrations
b.Navigation("Project");
});
modelBuilder.Entity("PSManagement.Domain.Projects.Entities.FinincialSpending", b =>
modelBuilder.Entity("PSManagement.Domain.Projects.Entities.FinancialSpending", b =>
{
b.HasOne("PSManagement.Domain.Projects.Entities.Project", null)
.WithMany("FinincialSpending")
.WithMany("FinancialSpending")
.HasForeignKey("ProjectId");
b.OwnsOne("PSManagement.SharedKernel.ValueObjects.Money", "ExternalPurchase", b1 =>
{
b1.Property<int>("FinincialSpendingId")
b1.Property<int>("FinancialSpendingId")
.ValueGeneratedOnAdd()
.HasColumnType("int")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
......@@ -601,46 +624,20 @@ namespace PSManagement.Infrastructure.Persistence.Migrations
.HasColumnName("ExternalPurchaseAmmount");
b1.Property<string>("Currency")
.ValueGeneratedOnAdd()
.HasColumnType("nvarchar(max)")
.HasDefaultValue("USD")
.HasColumnName("ExternalPurchaseCurrency");
b1.HasKey("FinincialSpendingId");
b1.ToTable("FinincialSpending");
b1.WithOwner()
.HasForeignKey("FinincialSpendingId");
});
b.OwnsOne("PSManagement.SharedKernel.ValueObjects.Money", "LocalPurchase", b1 =>
{
b1.Property<int>("FinincialSpendingId")
.ValueGeneratedOnAdd()
.HasColumnType("int")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
b1.Property<int>("Ammount")
.HasColumnType("int")
.HasColumnName("LocalPurchaseAmmount");
b1.Property<string>("Currency")
.ValueGeneratedOnAdd()
.HasColumnType("nvarchar(max)")
.HasDefaultValue("SP")
.HasColumnName("LocalPurchaseCurrency");
b1.HasKey("FinincialSpendingId");
b1.HasKey("FinancialSpendingId");
b1.ToTable("FinincialSpending");
b1.ToTable("FinancialSpending");
b1.WithOwner()
.HasForeignKey("FinincialSpendingId");
.HasForeignKey("FinancialSpendingId");
});
b.Navigation("ExternalPurchase");
b.Navigation("LocalPurchase");
});
modelBuilder.Entity("PSManagement.Domain.Projects.Entities.Project", b =>
......@@ -698,14 +695,14 @@ namespace PSManagement.Infrastructure.Persistence.Migrations
.HasForeignKey("ProjectId");
});
b.OwnsOne("PSManagement.Domain.Projects.ValueObjects.FinincialFund", "FinincialFund", b1 =>
b.OwnsOne("PSManagement.Domain.Projects.ValueObjects.FinancialFund", "FinancialFund", b1 =>
{
b1.Property<int>("ProjectId")
.ValueGeneratedOnAdd()
.HasColumnType("int")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
b1.Property<string>("FinicialStatus")
b1.Property<string>("FinancialStatus")
.HasColumnType("nvarchar(max)")
.HasColumnName("FinicialStatus");
......@@ -736,10 +733,18 @@ namespace PSManagement.Infrastructure.Persistence.Migrations
.HasColumnType("nvarchar(max)")
.HasColumnName("Description");
b1.Property<DateTime>("ExpectedEndDate")
.HasColumnType("datetime2")
.HasColumnName("ExpectedEndDate");
b1.Property<string>("Name")
.HasColumnType("nvarchar(max)")
.HasColumnName("Name");
b1.Property<DateTime>("StartDate")
.HasColumnType("datetime2")
.HasColumnName("StartDate");
b1.HasKey("ProjectId");
b1.ToTable("Projects");
......@@ -773,7 +778,7 @@ namespace PSManagement.Infrastructure.Persistence.Migrations
b.Navigation("Executer");
b.Navigation("FinincialFund");
b.Navigation("FinancialFund");
b.Navigation("ProjectAggreement");
......@@ -903,7 +908,7 @@ namespace PSManagement.Infrastructure.Persistence.Migrations
b.Navigation("EmployeeParticipates");
b.Navigation("FinincialSpending");
b.Navigation("FinancialSpending");
b.Navigation("Steps");
......
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using PSManagement.Domain.Employees.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PSManagement.Infrastructure.Persistence.SeedDataContext
{
public class SeedData
{
public static Task SeedAsync(ModelBuilder builder)
{
SeedDepartments(builder);
return Task.CompletedTask;
}
public static void SeedDepartments(ModelBuilder builder) {
builder.Entity<Department>().HasData(
new Department { Id = 1, Name = "قسم المعلوميات" },
new Department { Id = 2, Name = "قسم النظم الإلكترونيى" },
new Department { Id = 3, Name = "شؤون الطلاب" }
) ;
}
}
}
using MediatR;
using PSManagement.SharedKernel.Entities;
using PSManagement.SharedKernel.Events;
using PSManagement.SharedKernel.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace PSManagement.Infrastructure.Persistence.UoW
{
public class UnitOfWork :IUnitOfWork
{
private readonly AppDbContext _dbContext;
private readonly IMediator _mediator;
public UnitOfWork(AppDbContext dbContext, IMediator mediator)
{
_dbContext = dbContext;
_mediator = mediator;
}
public async Task SaveChangesAsync(CancellationToken cancellationToken = default)
{
await DispatchEventsAsync();
await _dbContext.SaveChangesAsync(cancellationToken);
}
private async Task DispatchEventsAsync()
{
var processedDomainEvents = new List<IDomainEvent>();
var unprocessedDomainEvents = GetDomainEvents().AsEnumerable();
// this is needed incase another DomainEvent is published from a DomainEventHandler
while (unprocessedDomainEvents.Any())
{
await DispatchDomainEventsAsync(unprocessedDomainEvents);
processedDomainEvents.AddRange(unprocessedDomainEvents);
unprocessedDomainEvents = GetDomainEvents()
.Where(e => !processedDomainEvents.Contains(e))
.ToList();
}
ClearDomainEvents();
}
private List<IDomainEvent> GetDomainEvents()
{
var aggregateRoots = GetTrackedEntites();
return aggregateRoots
.SelectMany(x => x.Events)
.ToList();
}
private List<BaseEntity> GetTrackedEntites()
{
return _dbContext.ChangeTracker
.Entries<BaseEntity>()
.Where(x => x.Entity.Events != null && x.Entity.Events.Any())
.Select(e => e.Entity)
.ToList();
}
private async Task DispatchDomainEventsAsync(IEnumerable<IDomainEvent> domainEvents)
{
foreach (var domainEvent in domainEvents)
{
await _mediator.Publish(domainEvent);
}
}
private void ClearDomainEvents()
{
var aggregateRoots = GetTrackedEntites();
aggregateRoots.ForEach(aggregate => aggregate.ClearDomainEvents());
}
public void Dispose()
{
_dbContext.Dispose();
}
}
}
......@@ -26,4 +26,8 @@
<ProjectReference Include="..\PSManagement.Domain\PSManagement.Domain.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="BackgroundServcies\" />
</ItemGroup>
</Project>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment