DatabaseImplement Models + Database.cs

This commit is contained in:
sardq 2024-04-21 00:04:11 +04:00
parent a93d78d901
commit dd664a4ed5
11 changed files with 492 additions and 1 deletions

View File

@ -9,6 +9,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FactoryContracts", "Factory
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FactoryBuisinessLogic", "FactoryBuisinessLogic\FactoryBuisinessLogic.csproj", "{EA960B3E-6BFB-4B16-9B0A-E1D603967FEC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FactoryDatabaseImplement", "FactoryDatabaseImplement\FactoryDatabaseImplement.csproj", "{AC09F9B2-8744-4C66-AD47-33A35613BC40}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -27,6 +29,10 @@ Global
{EA960B3E-6BFB-4B16-9B0A-E1D603967FEC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EA960B3E-6BFB-4B16-9B0A-E1D603967FEC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EA960B3E-6BFB-4B16-9B0A-E1D603967FEC}.Release|Any CPU.Build.0 = Release|Any CPU
{AC09F9B2-8744-4C66-AD47-33A35613BC40}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AC09F9B2-8744-4C66-AD47-33A35613BC40}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AC09F9B2-8744-4C66-AD47-33A35613BC40}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AC09F9B2-8744-4C66-AD47-33A35613BC40}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -12,6 +12,6 @@ namespace FactoryContracts.BindingModels
public double Cost { get; set; }
//public Dictionary<int, (IComponentModel, int)> WorkComponents { get; set; } = new();
public Dictionary<int, (IComponentModel, int)> WorkpiecesProducts { get; set; } = new();
}
}

View File

@ -0,0 +1,29 @@
using FactoryDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace FactoryDatabaseImplement
{
public class FactoryDatabase : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseNpgsql(@"Host=localhost;Database=Factory;Username=postgres; Password = postgres");
}
base.OnConfiguring(optionsBuilder);
}
public virtual DbSet<Client> Clients { set; get; }
public virtual DbSet<ExecutionPhase> ExecutionPhases { set; get; }
public virtual DbSet<MachinePlanProduction> MachinePlanProductions { set; get; }
public virtual DbSet<PlanProduction> PlanProductions { set; get; }
public virtual DbSet<PlanProductionWorkpiece> PlanProductionWorkpieces { set; get; }
public virtual DbSet<Workpiece> Workpieces { set; get; }
public virtual DbSet<WorkpieceProduct> WorkpieceProducts { set; get; }
}
}

View File

@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.29" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.22" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\FactoryContracts\FactoryContracts.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,75 @@
using FactoryContracts.BindingModels;
using FactoryContracts.ViewModels;
using FactoryDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace FactoryDatabaseImplement.Models
{
public class Client : IClientModel
{
public int Id { get; private set; }
[Required]
public string Login { get; private set; } = string.Empty;
[Required]
public string Email { get; private set; } = string.Empty;
[Required]
public string Password { get; private set; } = string.Empty;
[ForeignKey("ClientId")]
public virtual List<ExecutionPhase> ExecutionPhases{ get; set; } = new();
[ForeignKey("ClientId")]
public virtual List<PlanProduction> PlanProductions { get; set; } = new();
[ForeignKey("ClientId")]
public virtual List<Workpiece> Workpieces { get; set; } = new();
public static Client? Create(ClientBindingModel model)
{
if (model == null)
{
return null;
}
return new Client()
{
Id = model.Id,
Login = model.Login,
Email= model.Email,
Password = model.Password,
};
}
public static Client Create(ClientViewModel model)
{
return new Client
{
Id = model.Id,
Login = model.Login,
Email = model.Email,
Password = model.Password,
};
}
public void Update(ClientBindingModel model)
{
if (model == null)
{
return;
}
Login = model.Login;
Email = model.Email;
Password = model.Password;
}
public ClientViewModel GetViewModel => new()
{
Id = Id,
Login = Login,
Email = Email,
Password = Password,
};
}
}

View File

@ -0,0 +1,75 @@
using FactoryContracts.BindingModels;
using FactoryContracts.ViewModels;
using FactoryDataModels.Enums;
using FactoryDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace FactoryDatabaseImplement.Models
{
public class ExecutionPhase : IExecutionPhaseModel
{
public int Id { get; private set; }
[Required]
public int ClientId { get; private set; }
public virtual Client Client { get; private set; } = new();
[Required]
public string ExecutionPhaseName { get; private set; } = string.Empty;
[Required]
public string ImplementerFIO { get; private set; } = string.Empty;
[Required]
public ExecutionPhaseStatus Status { get; private set; } = ExecutionPhaseStatus.Неизвестен;
[ForeignKey("ExecutionPhaseId")]
public virtual List<PlanProduction> PlanProductions { get; set; } = new();
public static ExecutionPhase? Create(ExecutionPhaseBindingModel model)
{
if (model == null)
{
return null;
}
return new ExecutionPhase()
{
Id = model.Id,
ExecutionPhaseName = model.ExecutionPhaseName,
ImplementerFIO = model.ImplementerFIO,
ClientId = model.ClientId,
Status = model.Status,
};
}
public static ExecutionPhase Create(ExecutionPhaseViewModel model)
{
return new ExecutionPhase
{
Id = model.Id,
ExecutionPhaseName = model.ExecutionPhaseName,
ImplementerFIO = model.ImplementerFIO,
ClientId = model.ClientId,
Status = model.Status,
};
}
public void Update(ExecutionPhaseBindingModel model)
{
if (model == null)
{
return;
}
ExecutionPhaseName = model.ExecutionPhaseName;
ImplementerFIO = model.ImplementerFIO;
Status = model.Status;
}
public ExecutionPhaseViewModel GetViewModel => new()
{
Id = Id,
ExecutionPhaseName = ExecutionPhaseName,
ImplementerFIO = ImplementerFIO,
ClientId = ClientId,
Status = Status,
};
}
}

View File

@ -0,0 +1,18 @@
using System.ComponentModel.DataAnnotations;
namespace FactoryDatabaseImplement.Models
{
public class MachinePlanProduction
{
public int Id { get; set; }
//[Required]
//public int MachineId { get; set; }
[Required]
public int PlanProductionId { get; set; }
public virtual PlanProduction PlanProduction { get; set; } = new();
//public virtual Machine Machine{ get; set; } = new();
}
}

View File

@ -0,0 +1,120 @@
using FactoryContracts.BindingModels;
using FactoryContracts.ViewModels;
using FactoryDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace FactoryDatabaseImplement.Models
{
public class PlanProduction : IPlanProductionModel
{
public int Id { get; private set; }
[Required]
public int ExecutionPhaseId { get; private set; }
public virtual ExecutionPhase ExecutionPhase { get; private set; } = new();
[Required]
public int ClientId { get; private set; }
public virtual Client Client { get; private set; } = new();
[Required]
public string ProductionName { get; private set; } = string.Empty;
[Required]
public int Count { get; private set; }
[Required]
public DateTime Term { get; private set; }
private Dictionary<int, (IWorkpieceModel, int)>? _planProductionWorkpieces = null;
[NotMapped]
public Dictionary<int, (IWorkpieceModel, int)> PlanProductionWorkpieces
{
get
{
if (_planProductionWorkpieces == null)
{
_planProductionWorkpieces = Workpieces
.ToDictionary(recPC => recPC.WorkpieceId, recPC => (recPC.Workpiece as IWorkpieceModel, recPC.Count));
}
return _planProductionWorkpieces;
}
}
[ForeignKey("PlanProductionId")]
public virtual List<PlanProductionWorkpiece> Workpieces{ get; set; } = new();
[ForeignKey("PlanProductionId")]
public virtual List<MachinePlanProduction> Machines { get; set; } = new();
public static PlanProduction Create(FactoryDatabase context, PlanProductionBindingModel model)
{
if (model == null)
{
return null;
}
return new PlanProduction()
{
Id = model.Id,
ClientId = model.ClientId,
Count = model.Count,
ExecutionPhaseId = model.ExecutionPhaseId,
ProductionName = model.ProductionName,
Term = model.Term,
Workpieces = model.PlanProductionWorkpieces.Select(x => new PlanProductionWorkpiece
{
Workpiece = context.Workpieces.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(PlanProductionBindingModel model)
{
if (model == null)
{
return;
}
Count = model.Count;
ExecutionPhaseId = model.ExecutionPhaseId;
ProductionName = model.ProductionName;
Term = model.Term;
}
public PlanProductionBindingModel GetViewModel => new()
{
Id = Id,
ClientId = ClientId,
ExecutionPhaseId = ExecutionPhaseId,
ProductionName = ProductionName,
Term = Term,
Count = Count,
PlanProductionWorkpieces = PlanProductionWorkpieces
};
public void UpdateWorkpieces(FactoryDatabase context, PlanProductionBindingModel model)
{
var planProductionWorkpieces = context.PlanProductionWorkpieces.Where(rec => rec.PlanProductionId == model.Id).ToList();
if (planProductionWorkpieces != null && planProductionWorkpieces.Count > 0)
{
context.PlanProductionWorkpieces.RemoveRange(planProductionWorkpieces.Where(rec => !model.PlanProductionWorkpieces.ContainsKey(rec.WorkpieceId)));
context.SaveChanges();
foreach (var updateWorkpiece in planProductionWorkpieces)
{
updateWorkpiece.Count = model.PlanProductionWorkpieces[updateWorkpiece.WorkpieceId].Item2;
model.PlanProductionWorkpieces.Remove(updateWorkpiece.WorkpieceId);
}
context.SaveChanges();
}
var plan = context.PlanProductions.First(x => x.Id == Id);
foreach (var pc in model.PlanProductionWorkpieces)
{
context.PlanProductionWorkpieces.Add(new PlanProductionWorkpiece
{
PlanProduction = plan,
Workpiece = context.Workpieces.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_planProductionWorkpieces = null;
}
}
}

View File

@ -0,0 +1,22 @@
using System.ComponentModel.DataAnnotations;
namespace FactoryDatabaseImplement.Models
{
public class PlanProductionWorkpiece
{
public int Id { get; set; }
[Required]
public int PlanProductionId { get; set; }
[Required]
public int WorkpieceId { get; set; }
[Required]
public int Count { get; set; }
public virtual PlanProduction PlanProduction { get; set; } = new();
public virtual Workpiece Workpiece { get; set; } = new();
}
}

View File

@ -0,0 +1,107 @@
using FactoryContracts.BindingModels;
using FactoryContracts.ViewModels;
using FactoryDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace FactoryDatabaseImplement.Models
{
public class Workpiece : IWorkpieceModel
{
public int Id { get; set; }
[Required]
public string WorkpieceName { get; set; } = string.Empty;
[Required]
public int ClientId { get; set; }
public virtual Client Client { get; set; } = new Client();
[Required]
public string Material { get; set; } = string.Empty;
[Required]
public double Cost { get; set; }
private Dictionary<int, (IProductModel, int)>? _workpieceProducts = null;
[NotMapped]
public Dictionary<int, (IWorkpieceModel, int)> WorkpieceProducts
{
get
{
if (_workpieceProducts == null)
{
_workpieceProducts = Products
.ToDictionary(recPC => recPC.ProductId, recPC => (recPC.Product as IProductModel, recPC.Count));
}
return _workpieceProducts;
}
}
[ForeignKey("WorkpieceId")]
public virtual List<WorkpieceProduct> Products { get; set; } = new();
[ForeignKey("WorkpieceId")]
public virtual List<PlanProductionWorkpiece> PlanProductions { get; set; } = new();
public static Workpiece Create(FactoryDatabase context, WorkpieceBindingModel model)
{
return new Workpiece()
{
Id = model.Id,
WorkpieceName = model.WorkpieceName,
Cost = model.Cost,
Material = model.Material,
ClientId = model.ClientId,
Products = model.WorkpiecesProducts.Select(x => new WorkpieceProduct
{
Product = context.Products.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(WorkpieceBindingModel model)
{
WorkpieceName = model.WorkpieceName;
Cost = model.Cost;
Material = model.Material;
}
public WorkpieceBindingModel GetViewModel => new()
{
Id = Id,
WorkpieceName = WorkpieceName,
Cost = Cost,
Material = Material,
ClientId = ClientId,
WorkpiecesProducts = WorkpieceProducts
};
public void UpdateProducts(FactoryDatabase context, WorkpieceBindingModel model)
{
var workpieceProducts = context.WorkpieceProducts.Where(rec => rec.WorkpieceId == model.Id).ToList();
if (workpieceProducts != null && workpieceProducts.Count > 0)
{
context.WorkpieceProducts.RemoveRange(workpieceProducts.Where(rec => !model.WorkpiecesProducts.ContainsKey(rec.ProductId)));
context.SaveChanges();
foreach (var updateProduct in workpieceProducts)
{
updateProduct.Count = model.WorkComponents[updateProduct.ProductId].Item2;
model.WorkpiecesProducts.Remove(updateProduct.ProductId);
}
context.SaveChanges();
}
var workpiece = context.Workpieces.First(x => x.Id == Id);
foreach (var pc in model.WorkpiecesProducts)
{
context.WorkpieceProducts.Add(new WorkpieceProduct
{
Workpiece = workpiece,
Product = context.Products.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_workpieceProducts = null;
}
}
}

View File

@ -0,0 +1,21 @@
using System.ComponentModel.DataAnnotations;
namespace FactoryDatabaseImplement.Models
{
public class WorkpieceProduct
{
public int Id { get; set; }
[Required]
public int WorkpieceId { get; set; }
[Required]
public int ProductId { get; set; }
[Required]
public int Count { get; set; }
public virtual Product Product { get; set; } = new();
public virtual Workpiece Workpiece { get; set; } = new();
}
}