From 1b9e26415d96e5e57773ce9ebc980b1ee8596847 Mon Sep 17 00:00:00 2001 From: Zyzf Date: Thu, 6 Apr 2023 01:20:48 +0400 Subject: [PATCH] added database implement + many fixes in models and contracts --- .../FurnitureAssembly_Worker.csproj | 11 + .../BindingModels/FurnitureBindingModel.cs | 4 +- .../FurnitureModuleBindingModel.cs | 4 +- .../BindingModels/MaterialBindingModel.cs | 5 +- .../BindingModels/OrderBindingModel.cs | 4 +- .../BindingModels/SetBindingModel.cs | 2 +- .../BindingModels/UserBindingModel.cs | 2 +- .../FurnitureModuleSearchModel.cs | 1 + .../SearchModels/FurnitureSearchModel.cs | 1 + .../SearchModels/MaterialSearchModel.cs | 1 + .../SearchModels/OrderSearchModel.cs | 1 + .../SearchModels/SetSearchModel.cs | 1 + .../SearchModels/UserSearchModel.cs | 1 + .../ViewModels/FurnitureModuleViewModel.cs | 2 +- .../Models/IFurnitureModuleModel.cs | 2 +- .../FurnitureAssemblyDatabase.cs | 16 +- .../Implements/FurnitureModuleStorage.cs | 119 +++++ .../Implements/FurnitureStorage.cs | 117 ++++ .../Implements/MaterialStorage.cs | 106 ++++ .../Implements/OrderStorage.cs | 124 +++++ .../Implements/ScopeStorage.cs | 99 ++++ .../Implements/SetStorage.cs | 120 +++++ .../Implements/UserStorage.cs | 137 +++++ .../20230405211314_InitMigration.Designer.cs | 505 ++++++++++++++++++ .../20230405211314_InitMigration.cs | 375 +++++++++++++ .../FurnitureAssemblyDatabaseModelSnapshot.cs | 502 +++++++++++++++++ .../Models/Furniture.cs | 110 ++++ .../Models/FurnitureMaterial.cs | 23 + .../Models/FurnitureModule.cs | 118 ++++ .../Models/FurnitureModuleFurniture.cs | 23 + .../Models/Material.cs | 66 +++ .../Models/Order.cs | 68 +++ .../Models/Role.cs | 3 + .../Models/Scope.cs | 47 ++ .../Models/Set.cs | 111 ++++ .../Models/SetFurnitureModule.cs | 23 + .../Models/User.cs | 50 +- 37 files changed, 2889 insertions(+), 15 deletions(-) create mode 100644 FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Implements/FurnitureModuleStorage.cs create mode 100644 FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Implements/FurnitureStorage.cs create mode 100644 FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Implements/MaterialStorage.cs create mode 100644 FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Implements/OrderStorage.cs create mode 100644 FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Implements/ScopeStorage.cs create mode 100644 FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Implements/SetStorage.cs create mode 100644 FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Implements/UserStorage.cs create mode 100644 FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Migrations/20230405211314_InitMigration.Designer.cs create mode 100644 FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Migrations/20230405211314_InitMigration.cs create mode 100644 FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Migrations/FurnitureAssemblyDatabaseModelSnapshot.cs create mode 100644 FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Models/Furniture.cs create mode 100644 FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Models/FurnitureMaterial.cs create mode 100644 FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Models/FurnitureModule.cs create mode 100644 FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Models/FurnitureModuleFurniture.cs create mode 100644 FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Models/Material.cs create mode 100644 FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Models/Order.cs create mode 100644 FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Models/Scope.cs create mode 100644 FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Models/Set.cs create mode 100644 FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Models/SetFurnitureModule.cs diff --git a/FurnitureAssembly_Worker/FurnitureAssembly_Worker/FurnitureAssembly_Worker.csproj b/FurnitureAssembly_Worker/FurnitureAssembly_Worker/FurnitureAssembly_Worker.csproj index 132c02c..11a20e9 100644 --- a/FurnitureAssembly_Worker/FurnitureAssembly_Worker/FurnitureAssembly_Worker.csproj +++ b/FurnitureAssembly_Worker/FurnitureAssembly_Worker/FurnitureAssembly_Worker.csproj @@ -6,4 +6,15 @@ enable + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + diff --git a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/BindingModels/FurnitureBindingModel.cs b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/BindingModels/FurnitureBindingModel.cs index 7c4bb1a..92cb0b7 100644 --- a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/BindingModels/FurnitureBindingModel.cs +++ b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/BindingModels/FurnitureBindingModel.cs @@ -9,11 +9,11 @@ namespace FurnitureAssembly_WorkerContracts.BindingModels { public class FurnitureBindingModel : IFurnitureModel { + public int Id { get; set; } public string Name { get; set; } = string.Empty; public double Cost { get; set; } + public int UserId { get; set; } public DateTime DateCreate { get; set; } = DateTime.Now; public Dictionary FurnitureMaterials { get; set; } = new(); - public IUserModel User { get; set; } = new UserBindingModel(); - public int Id { get; set; } } } diff --git a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/BindingModels/FurnitureModuleBindingModel.cs b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/BindingModels/FurnitureModuleBindingModel.cs index 9e99ca5..60a18df 100644 --- a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/BindingModels/FurnitureModuleBindingModel.cs +++ b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/BindingModels/FurnitureModuleBindingModel.cs @@ -15,7 +15,7 @@ namespace FurnitureAssembly_WorkerContracts.BindingModels public string Name { get; set; } = string.Empty; public double Cost { get; set; } public DateTime DateCreate { get; set; } - public IUserModel User { get; set; } = new UserViewModel(); - public Dictionary FurnitureFurnitureModules { get; set; } = new(); + public int UserId { get; set; } + public Dictionary FurnitureModuleFurnitures { get; set; } = new(); } } diff --git a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/BindingModels/MaterialBindingModel.cs b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/BindingModels/MaterialBindingModel.cs index ae1d37c..87cf34c 100644 --- a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/BindingModels/MaterialBindingModel.cs +++ b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/BindingModels/MaterialBindingModel.cs @@ -10,9 +10,10 @@ namespace FurnitureAssembly_WorkerContracts.BindingModels { public class MaterialBindingModel : IMaterialModel { + public int Id { get; set; } public string Name { get; set; } = string.Empty; public double Cost { get; set; } - public IScopeModel scope { get; set; } = new ScopeBindingModel(); - public int Id { get; set; } + public int ScopeId { get; set; } + public int UserId { get; set; } } } diff --git a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/BindingModels/OrderBindingModel.cs b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/BindingModels/OrderBindingModel.cs index 50f5fea..703e7c8 100644 --- a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/BindingModels/OrderBindingModel.cs +++ b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/BindingModels/OrderBindingModel.cs @@ -15,8 +15,8 @@ namespace FurnitureAssembly_WorkerContracts.BindingModels public int Id { get; set; } public string CustomerName { get; set; } = string.Empty; public PaymentType PaymentType { get; set; } = PaymentType.Неизвестен; - public Dictionary OrderSets { get; set; } = new(); + public int SetId { get; set; } public DateTime DateCreate { get; set; } - public IUserModel User { get; set; } = new UserViewModel(); + public int UserId { get; set; } } } diff --git a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/BindingModels/SetBindingModel.cs b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/BindingModels/SetBindingModel.cs index 1af316c..84fb7e3 100644 --- a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/BindingModels/SetBindingModel.cs +++ b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/BindingModels/SetBindingModel.cs @@ -15,7 +15,7 @@ namespace FurnitureAssembly_WorkerContracts.BindingModels public string Name { get; set; } = string.Empty; public double Cost { get; set; } public DateTime DateCreate { get; set; } - public IUserModel User { get; set; } = new UserViewModel(); + public int UserId { get; set; } public Dictionary SetFurnitureModules { get; set; } = new(); } } diff --git a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/BindingModels/UserBindingModel.cs b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/BindingModels/UserBindingModel.cs index 3c55843..78fb62f 100644 --- a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/BindingModels/UserBindingModel.cs +++ b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/BindingModels/UserBindingModel.cs @@ -13,6 +13,6 @@ namespace FurnitureAssembly_WorkerContracts.BindingModels public string Login { get; set; } = string.Empty; public string Password { get; set; } = string.Empty; public string Name { get; set; } = string.Empty; - public IRoleModel Role { get; set; } = new RoleBindingModel(); + public int RoleId { get; set; } } } diff --git a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/SearchModels/FurnitureModuleSearchModel.cs b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/SearchModels/FurnitureModuleSearchModel.cs index e647abf..a789038 100644 --- a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/SearchModels/FurnitureModuleSearchModel.cs +++ b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/SearchModels/FurnitureModuleSearchModel.cs @@ -10,5 +10,6 @@ namespace FurnitureAssembly_WorkerContracts.SearchModels { public int? Id { get; set; } public string? Name { get; set; } + public int? UserId { get; set; } } } diff --git a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/SearchModels/FurnitureSearchModel.cs b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/SearchModels/FurnitureSearchModel.cs index 7eba1d9..e8fa5c4 100644 --- a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/SearchModels/FurnitureSearchModel.cs +++ b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/SearchModels/FurnitureSearchModel.cs @@ -10,5 +10,6 @@ namespace FurnitureAssembly_WorkerContracts.SearchModels { public int? Id { get; set; } public string? Name { get; set; } + public int? UserId { get; set; } } } diff --git a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/SearchModels/MaterialSearchModel.cs b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/SearchModels/MaterialSearchModel.cs index c1431f0..f454d48 100644 --- a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/SearchModels/MaterialSearchModel.cs +++ b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/SearchModels/MaterialSearchModel.cs @@ -10,5 +10,6 @@ namespace FurnitureAssembly_WorkerContracts.SearchModels { public int? Id { get; set; } public string? Name { get; set; } + public int? UserId { get; set; } } } diff --git a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/SearchModels/OrderSearchModel.cs b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/SearchModels/OrderSearchModel.cs index 7b1bf55..1542ce7 100644 --- a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/SearchModels/OrderSearchModel.cs +++ b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/SearchModels/OrderSearchModel.cs @@ -9,6 +9,7 @@ namespace FurnitureAssembly_WorkerContracts.SearchModels public class OrderSearchModel { public int? Id { get; set; } + public int? UserId { get; set; } public DateTime? DateFrom { get; set; } public DateTime? DateTo { get; set; } } diff --git a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/SearchModels/SetSearchModel.cs b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/SearchModels/SetSearchModel.cs index 311870b..af64606 100644 --- a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/SearchModels/SetSearchModel.cs +++ b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/SearchModels/SetSearchModel.cs @@ -10,5 +10,6 @@ namespace FurnitureAssembly_WorkerContracts.SearchModels { public int? Id { get; set; } public string? Name { get; set; } + public int? UserId { get; set; } } } diff --git a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/SearchModels/UserSearchModel.cs b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/SearchModels/UserSearchModel.cs index 4221edf..5ac5184 100644 --- a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/SearchModels/UserSearchModel.cs +++ b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/SearchModels/UserSearchModel.cs @@ -9,6 +9,7 @@ namespace FurnitureAssembly_WorkerContracts.SearchModels public class UserSearchModel { public int? Id { get; set; } + public int? RoleId { get; set; } public string? Login { get; set; } public string? Password { get; set; } } diff --git a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/ViewModels/FurnitureModuleViewModel.cs b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/ViewModels/FurnitureModuleViewModel.cs index 0663b35..0a3fd92 100644 --- a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/ViewModels/FurnitureModuleViewModel.cs +++ b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerContracts/ViewModels/FurnitureModuleViewModel.cs @@ -20,6 +20,6 @@ namespace FurnitureAssembly_WorkerContracts.ViewModels public int UserId { get; set; } [DisplayName("Изготовитель")] public string UserName { get; set; } = string.Empty; - public Dictionary FurnitureFurnitureModules { get; set; } = new(); + public Dictionary FurnitureModuleFurnitures { get; set; } = new(); } } diff --git a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDataModels/Models/IFurnitureModuleModel.cs b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDataModels/Models/IFurnitureModuleModel.cs index 504fc3b..a155673 100644 --- a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDataModels/Models/IFurnitureModuleModel.cs +++ b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDataModels/Models/IFurnitureModuleModel.cs @@ -12,6 +12,6 @@ namespace FurnitureAssembly_WorkerDataModels.Models double Cost { get; } DateTime DateCreate { get; } int UserId { get; } - Dictionary FurnitureFurnitureModules { get; } + Dictionary FurnitureModuleFurnitures { get; } } } diff --git a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/FurnitureAssemblyDatabase.cs b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/FurnitureAssemblyDatabase.cs index 20d70e7..fa37348 100644 --- a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/FurnitureAssemblyDatabase.cs +++ b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/FurnitureAssemblyDatabase.cs @@ -1,4 +1,5 @@ -using Microsoft.EntityFrameworkCore; +using FurnitureAssembly_WorkerDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; @@ -7,7 +8,7 @@ using System.Threading.Tasks; namespace FurnitureAssembly_WorkerDatabaseImplement { - internal class FurnitureAssemblyDatabase : DbContext + public class FurnitureAssemblyDatabase : DbContext { protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { @@ -17,5 +18,16 @@ namespace FurnitureAssembly_WorkerDatabaseImplement } base.OnConfiguring(optionsBuilder); } + public virtual DbSet Sets { get; set; } + public virtual DbSet SetFurnitureModules { get; set; } + public virtual DbSet FurnitureModules { get; set; } + public virtual DbSet FurnitureModuleFurnitures { get; set; } + public virtual DbSet Furnitures { get; set; } + public virtual DbSet FurnitureMaterials { get; set; } + public virtual DbSet Materials { get; set; } + public virtual DbSet Roles { get; set; } + public virtual DbSet Scopes { get; set; } + public virtual DbSet Users { get; set; } + public virtual DbSet Orders { get; set; } } } diff --git a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Implements/FurnitureModuleStorage.cs b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Implements/FurnitureModuleStorage.cs new file mode 100644 index 0000000..581b1e6 --- /dev/null +++ b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Implements/FurnitureModuleStorage.cs @@ -0,0 +1,119 @@ +using FurnitureAssembly_WorkerContracts.BindingModels; +using FurnitureAssembly_WorkerContracts.SearchModels; +using FurnitureAssembly_WorkerContracts.StorageContracts; +using FurnitureAssembly_WorkerContracts.ViewModels; +using FurnitureAssembly_WorkerDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssembly_WorkerDatabaseImplement.Implements +{ + public class FurnitureModuleStorage : IFurnitureModuleStorage + { + public List GetFullList() + { + using var context = new FurnitureAssemblyDatabase(); + return context.FurnitureModules + .Include(x => x.User) + .Include(x => x.Furnitures) + .ThenInclude(x => x.Furniture).ToList() + .Select(x => x.GetViewModel).ToList(); + } + public List GetFilteredList(FurnitureModuleSearchModel model) + { + using var context = new FurnitureAssemblyDatabase(); + if (!string.IsNullOrEmpty(model.Name)) + { + return context.FurnitureModules + .Include(x => x.User) + .Include(x => x.Furnitures) + .ThenInclude(x => x.Furniture) + .Where(x => x.Name.Contains(model.Name)).ToList() + .Select(x => x.GetViewModel).ToList(); + } + else if (model.UserId.HasValue) + { + return context.FurnitureModules + .Include(x => x.User) + .Include(x => x.Furnitures) + .ThenInclude(x => x.Furniture) + .Where(x => x.UserId == model.UserId) + .Select(x => x.GetViewModel).ToList(); + } + else + { + return new(); + } + } + public FurnitureModuleViewModel? GetElement(FurnitureModuleSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && + !model.Id.HasValue) + { + return null; + } + using var context = new FurnitureAssemblyDatabase(); + return context.FurnitureModules + .Include(x => x.User) + .Include(x => x.Furnitures) + .ThenInclude(x => x.Furniture) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) || (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + public FurnitureModuleViewModel? Insert(FurnitureModuleBindingModel model) + { + using var context = new FurnitureAssemblyDatabase(); + var newFurnitureModule = FurnitureModule.Create(context, model); + if (newFurnitureModule == null) + { + return null; + } + context.FurnitureModules.Add(newFurnitureModule); + context.SaveChanges(); + return newFurnitureModule.GetViewModel; + } + public FurnitureModuleViewModel? Update(FurnitureModuleBindingModel model) + { + using var context = new FurnitureAssemblyDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var furnitureModule = context.FurnitureModules.FirstOrDefault(rec => + rec.Id == model.Id); + if (furnitureModule == null) + { + return null; + } + furnitureModule.Update(model); + context.SaveChanges(); + furnitureModule.UpdateFurnitures(context, model); + transaction.Commit(); + return furnitureModule.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public FurnitureModuleViewModel? Delete(FurnitureModuleBindingModel model) + { + using var context = new FurnitureAssemblyDatabase(); + var element = context.FurnitureModules + .Include(x => x.User) + .Include(x => x.Furnitures) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.FurnitureModules.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Implements/FurnitureStorage.cs b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Implements/FurnitureStorage.cs new file mode 100644 index 0000000..35499f0 --- /dev/null +++ b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Implements/FurnitureStorage.cs @@ -0,0 +1,117 @@ +using FurnitureAssembly_WorkerContracts.BindingModels; +using FurnitureAssembly_WorkerContracts.SearchModels; +using FurnitureAssembly_WorkerContracts.StorageContracts; +using FurnitureAssembly_WorkerContracts.ViewModels; +using FurnitureAssembly_WorkerDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssembly_WorkerDatabaseImplement.Implements +{ + public class FurnitureStorage : IFurnitureStorage + { + public List GetFullList() + { + using var context = new FurnitureAssemblyDatabase(); + return context.Furnitures + .Include(x => x.User) + .Include(x => x.Materials) + .ThenInclude(x => x.Material).ToList() + .Select(x => x.GetViewModel).ToList(); + } + public List GetFilteredList(FurnitureSearchModel model) + { + using var context = new FurnitureAssemblyDatabase(); + if (!string.IsNullOrEmpty(model.Name)) + { + return context.Furnitures + .Include(x => x.User) + .Include(x => x.Materials) + .ThenInclude(x => x.Material) + .Where(x => x.Name.Contains(model.Name)).ToList() + .Select(x => x.GetViewModel).ToList(); + } + else if (model.UserId.HasValue) + { + return context.Furnitures + .Include(x => x.User) + .Include(x => x.Materials) + .ThenInclude(x => x.Material) + .Where(x => x.UserId == model.UserId) + .Select(x => x.GetViewModel).ToList(); + } + else + { + return new(); + } + } + public FurnitureViewModel? GetElement(FurnitureSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && + !model.Id.HasValue) + { + return null; + } + using var context = new FurnitureAssemblyDatabase(); + return context.Furnitures.Include(x => x.User) + .Include(x => x.Materials).ThenInclude(x => x.Material) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + public FurnitureViewModel? Insert(FurnitureBindingModel model) + { + using var context = new FurnitureAssemblyDatabase(); + var newFurniture = Furniture.Create(context, model); + if (newFurniture == null) + { + return null; + } + context.Furnitures.Add(newFurniture); + context.SaveChanges(); + return newFurniture.GetViewModel; + } + public FurnitureViewModel? Update(FurnitureBindingModel model) + { + using var context = new FurnitureAssemblyDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var furniture = context.Furnitures + .Include(x => x.User) + .FirstOrDefault(rec => rec.Id == model.Id); + if (furniture == null) + { + return null; + } + furniture.Update(model); + context.SaveChanges(); + furniture.UpdateMaterials(context, model); + transaction.Commit(); + return furniture.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public FurnitureViewModel? Delete(FurnitureBindingModel model) + { + using var context = new FurnitureAssemblyDatabase(); + var element = context.Furnitures + .Include(x => x.User) + .Include(x => x.Materials) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Furnitures.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Implements/MaterialStorage.cs b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Implements/MaterialStorage.cs new file mode 100644 index 0000000..4b18e08 --- /dev/null +++ b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Implements/MaterialStorage.cs @@ -0,0 +1,106 @@ +using FurnitureAssembly_WorkerContracts.BindingModels; +using FurnitureAssembly_WorkerContracts.SearchModels; +using FurnitureAssembly_WorkerContracts.StorageContracts; +using FurnitureAssembly_WorkerContracts.ViewModels; +using FurnitureAssembly_WorkerDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssembly_WorkerDatabaseImplement.Implements +{ + public class MaterialStorage : IMaterialStorage + { + public List GetFullList() + { + using var context = new FurnitureAssemblyDatabase(); + return context.Materials + .Include(x => x.User) + .Include(x => x.Scope) + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(MaterialSearchModel model) + { + using var context = new FurnitureAssemblyDatabase(); + if (!string.IsNullOrEmpty(model.Name)) + { + return context.Materials + .Include(x => x.User) + .Include(x => x.Scope) + .Where(x => x.Name.Contains(model.Name)) + .Select(x => x.GetViewModel).ToList(); + } + else if (model.UserId.HasValue) + { + return context.Materials + .Include(x => x.User) + .Include(x => x.Scope) + .Where(x => x.UserId == model.UserId) + .Select(x => x.GetViewModel).ToList(); + } + else + { + return new(); + } + } + public MaterialViewModel? GetElement(MaterialSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + { + return null; + } + using var context = new FurnitureAssemblyDatabase(); + return context.Materials + .Include(x => x.User) + .Include(x => x.Scope) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + public MaterialViewModel? Insert(MaterialBindingModel model) + { + var newMaterial = Material.Create(model); + if (newMaterial == null) + { + return null; + } + using var context = new FurnitureAssemblyDatabase(); + context.Materials.Add(newMaterial); + context.SaveChanges(); + return newMaterial.GetViewModel; + } + public MaterialViewModel? Update(MaterialBindingModel model) + { + using var context = new FurnitureAssemblyDatabase(); + var material = context.Materials + .Include(x => x.User) + .FirstOrDefault(x => x.Id == model.Id); + if (material == null) + { + return null; + } + material.Update(model); + context.SaveChanges(); + return material.GetViewModel; + } + public MaterialViewModel? Delete(MaterialBindingModel model) + { + using var context = new FurnitureAssemblyDatabase(); + var element = context.Materials + .Include(x => x.User) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Materials.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Implements/OrderStorage.cs b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..e6a9625 --- /dev/null +++ b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,124 @@ +using FurnitureAssembly_WorkerContracts.BindingModels; +using FurnitureAssembly_WorkerContracts.SearchModels; +using FurnitureAssembly_WorkerContracts.StorageContracts; +using FurnitureAssembly_WorkerContracts.ViewModels; +using Microsoft.EntityFrameworkCore; +using FurnitureAssembly_WorkerDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssembly_WorkerDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new FurnitureAssemblyDatabase(); + var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + var deletedElement = context.Orders + .Include(x => x.Set) + .Include(x => x.User) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + context.Orders.Remove(element); + context.SaveChanges(); + return deletedElement; + } + return null; + } + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new FurnitureAssemblyDatabase(); + return context.Orders + .Include(x => x.Set) + .Include(x => x.User) + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) + ?.GetViewModel; + } + public List GetFilteredList(OrderSearchModel model) + { + using var context = new FurnitureAssemblyDatabase(); + if (model.Id.HasValue) + { + return context.Orders + .Include(x => x.Set) + .Include(x => x.User) + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + else if (model.DateFrom != null && model.DateTo != null) + { + return context.Orders + .Include(x => x.Set) + .Include(x => x.User) + .Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo) + .Select(x => x.GetViewModel) + .ToList(); + } + else if (model.UserId.HasValue) + { + return context.Orders + .Include(x => x.Set) + .Include(x => x.User) + .Where(x => x.UserId == model.UserId) + .Select(x => x.GetViewModel) + .ToList(); + } + else + { + return new(); + } + } + public List GetFullList() + { + using var context = new FurnitureAssemblyDatabase(); + return context.Orders + .Include(x => x.Set) + .Include(x => x.User) + .Select(x => x.GetViewModel) + .ToList(); + } + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + using var context = new FurnitureAssemblyDatabase(); + context.Orders.Add(newOrder); + context.SaveChanges(); + return context.Orders + .Include(x => x.Set) + .Include(x => x.User) + .FirstOrDefault(x => x.Id == newOrder.Id) + ?.GetViewModel; + } + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new FurnitureAssemblyDatabase(); + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + return context.Orders + .Include(x => x.Set) + .Include(x => x.User) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + } + } +} diff --git a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Implements/ScopeStorage.cs b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Implements/ScopeStorage.cs new file mode 100644 index 0000000..b6d110b --- /dev/null +++ b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Implements/ScopeStorage.cs @@ -0,0 +1,99 @@ +using FurnitureAssembly_WorkerContracts.BindingModels; +using FurnitureAssembly_WorkerContracts.SearchModels; +using FurnitureAssembly_WorkerContracts.StorageContracts; +using FurnitureAssembly_WorkerContracts.ViewModels; +using FurnitureAssembly_WorkerDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssembly_WorkerDatabaseImplement.Implements +{ + public class ScopeStorage : IScopeStorage + { + public List GetFullList() + { + using var context = new FurnitureAssemblyDatabase(); + return context.Scopes + .Include(x => x.Materials) + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ScopeSearchModel model) + { + if (model == null) + { + return new(); + } + if (!string.IsNullOrEmpty(model.Name)) + { + using var context = new FurnitureAssemblyDatabase(); + return context.Scopes + .Include(x => x.Materials) + .Where(x => x.Name.Contains(model.Name)) + .Select(x => x.GetViewModel) + .ToList(); + } + return new(); + } + public ScopeViewModel? GetElement(ScopeSearchModel model) + { + using var context = new FurnitureAssemblyDatabase(); + if (model.Id.HasValue) + { + return context.Scopes + .FirstOrDefault(x => (x.Id == model.Id)) + ?.GetViewModel; + } + else if (!string.IsNullOrEmpty(model.Name)) + { + return context.Scopes + .FirstOrDefault(x => (x.Name == model.Name)) + ?.GetViewModel; + } + return new(); + } + public ScopeViewModel? Insert(ScopeBindingModel model) + { + var newScope = Scope.Create(model); + if (newScope == null) + { + return null; + } + using var context = new FurnitureAssemblyDatabase(); + context.Scopes.Add(newScope); + context.SaveChanges(); + return newScope.GetViewModel; + } + public ScopeViewModel? Update(ScopeBindingModel model) + { + using var context = new FurnitureAssemblyDatabase(); + var scope = context.Scopes + .FirstOrDefault(x => x.Id == model.Id); + if (scope == null) + { + return null; + } + scope.Update(model); + context.SaveChanges(); + return scope.GetViewModel; + } + public ScopeViewModel? Delete(ScopeBindingModel model) + { + using var context = new FurnitureAssemblyDatabase(); + var element = context.Scopes + .Include(x => x.Materials) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Scopes.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Implements/SetStorage.cs b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Implements/SetStorage.cs new file mode 100644 index 0000000..ab2af24 --- /dev/null +++ b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Implements/SetStorage.cs @@ -0,0 +1,120 @@ +using FurnitureAssembly_WorkerContracts.BindingModels; +using FurnitureAssembly_WorkerContracts.SearchModels; +using FurnitureAssembly_WorkerContracts.StorageContracts; +using FurnitureAssembly_WorkerContracts.ViewModels; +using FurnitureAssembly_WorkerDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssembly_WorkerDatabaseImplement.Implements +{ + public class SetStorage : ISetStorage + { + public List GetFullList() + { + using var context = new FurnitureAssemblyDatabase(); + return context.Sets + .Include(x => x.User) + .Include(x => x.FurnitureModules) + .ThenInclude(x => x.FurnitureModule).ToList() + .Select(x => x.GetViewModel).ToList(); + } + public List GetFilteredList(SetSearchModel model) + { + using var context = new FurnitureAssemblyDatabase(); + if (!string.IsNullOrEmpty(model.Name)) + { + return context.Sets + .Include(x => x.User) + .Include(x => x.FurnitureModules) + .ThenInclude(x => x.FurnitureModule) + .Where(x => x.Name.Contains(model.Name)).ToList() + .Select(x => x.GetViewModel).ToList(); + } + else if (model.UserId.HasValue) + { + return context.Sets + .Include(x => x.User) + .Include(x => x.FurnitureModules) + .ThenInclude(x => x.FurnitureModule) + .Where(x => x.UserId == model.UserId) + .Select(x => x.GetViewModel).ToList(); + } + else + { + return new(); + } + } + public SetViewModel? GetElement(SetSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && + !model.Id.HasValue) + { + return null; + } + using var context = new FurnitureAssemblyDatabase(); + return context.Sets + .Include(x => x.User) + .Include(x => x.FurnitureModules) + .ThenInclude(x => x.FurnitureModule) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) || (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + public SetViewModel? Insert(SetBindingModel model) + { + using var context = new FurnitureAssemblyDatabase(); + var newSet = Set.Create(context, model); + if (newSet == null) + { + return null; + } + context.Sets.Add(newSet); + context.SaveChanges(); + return newSet.GetViewModel; + } + public SetViewModel? Update(SetBindingModel model) + { + using var context = new FurnitureAssemblyDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var set = context.Sets + .Include(x => x.User) + .FirstOrDefault(rec => rec.Id == model.Id); + if (set == null) + { + return null; + } + set.Update(model); + context.SaveChanges(); + set.UpdateFurnitureModules(context, model); + transaction.Commit(); + return set.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public SetViewModel? Delete(SetBindingModel model) + { + using var context = new FurnitureAssemblyDatabase(); + var element = context.Sets + .Include(x => x.User) + .Include(x => x.FurnitureModules) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Sets.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Implements/UserStorage.cs b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Implements/UserStorage.cs new file mode 100644 index 0000000..b1cc06b --- /dev/null +++ b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Implements/UserStorage.cs @@ -0,0 +1,137 @@ +using FurnitureAssembly_WorkerContracts.BindingModels; +using FurnitureAssembly_WorkerContracts.SearchModels; +using FurnitureAssembly_WorkerContracts.StorageContracts; +using FurnitureAssembly_WorkerContracts.ViewModels; +using FurnitureAssembly_WorkerDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssembly_WorkerDatabaseImplement.Implements +{ + public class UserStorage : IUserStorage + { + public UserViewModel? Delete(UserBindingModel model) + { + using var context = new FurnitureAssemblyDatabase(); + var element = context + .Users.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + var deletedElement = context.Users + .Include(x => x.Sets) + .Include(x => x.Orders) + .Include(x => x.FurnitureModules) + .Include(x => x.Furnitures) + .Include(x => x.Materials) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + context.Users.Remove(element); + context.SaveChanges(); + return deletedElement; + } + return null; + } + public UserViewModel? GetElement(UserSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new FurnitureAssemblyDatabase(); + return context.Users + .Include(x => x.Sets) + .Include(x => x.Orders) + .Include(x => x.FurnitureModules) + .Include(x => x.Furnitures) + .Include(x => x.Materials) + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) + ?.GetViewModel; + } + public List GetFilteredList(UserSearchModel model) + { + using var context = new FurnitureAssemblyDatabase(); + if (model.Id.HasValue) + { + return context.Users + .Include(x => x.Sets) + .Include(x => x.Orders) + .Include(x => x.FurnitureModules) + .Include(x => x.Furnitures) + .Include(x => x.Materials) + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + else if (model.RoleId.HasValue) + { + return context.Users + .Include(x => x.Sets) + .Include(x => x.Orders) + .Include(x => x.FurnitureModules) + .Include(x => x.Furnitures) + .Include(x => x.Materials) + .Where(x => x.RoleId == model.RoleId) + .Select(x => x.GetViewModel) + .ToList(); + } + else + { + return new(); + } + } + public List GetFullList() + { + using var context = new FurnitureAssemblyDatabase(); + return context.Users + .Include(x => x.Sets) + .Include(x => x.Orders) + .Include(x => x.FurnitureModules) + .Include(x => x.Furnitures) + .Include(x => x.Materials) + .Select(x => x.GetViewModel) + .ToList(); + } + public UserViewModel? Insert(UserBindingModel model) + { + var newUser = User.Create(model); + if (newUser == null) + { + return null; + } + using var context = new FurnitureAssemblyDatabase(); + context.Users.Add(newUser); + context.SaveChanges(); + return context.Users + .Include(x => x.Sets) + .Include(x => x.Orders) + .Include(x => x.FurnitureModules) + .Include(x => x.Furnitures) + .Include(x => x.Materials) + .FirstOrDefault(x => x.Id == newUser.Id) + ?.GetViewModel; + } + public UserViewModel? Update(UserBindingModel model) + { + using var context = new FurnitureAssemblyDatabase(); + var user = context.Users.FirstOrDefault(x => x.Id == model.Id); + if (user == null) + { + return null; + } + user.Update(model); + context.SaveChanges(); + return context.Users + .Include(x => x.Sets) + .Include(x => x.Orders) + .Include(x => x.FurnitureModules) + .Include(x => x.Furnitures) + .Include(x => x.Materials) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + } + } +} diff --git a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Migrations/20230405211314_InitMigration.Designer.cs b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Migrations/20230405211314_InitMigration.Designer.cs new file mode 100644 index 0000000..7bb755d --- /dev/null +++ b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Migrations/20230405211314_InitMigration.Designer.cs @@ -0,0 +1,505 @@ +// +using System; +using FurnitureAssembly_WorkerDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace FurnitureAssembly_WorkerDatabaseImplement.Migrations +{ + [DbContext(typeof(FurnitureAssemblyDatabase))] + [Migration("20230405211314_InitMigration")] + partial class InitMigration + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.4") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.Furniture", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Cost") + .HasColumnType("double precision"); + + b.Property("DateCreate") + .HasColumnType("timestamp with time zone"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("Furnitures"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.FurnitureMaterial", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("FurnitureId") + .HasColumnType("integer"); + + b.Property("MaterialId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("FurnitureId"); + + b.HasIndex("MaterialId"); + + b.ToTable("FurnitureMaterials"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.FurnitureModule", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Cost") + .HasColumnType("double precision"); + + b.Property("DateCreate") + .HasColumnType("timestamp with time zone"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("FurnitureModules"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.FurnitureModuleFurniture", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("FurnitureId") + .HasColumnType("integer"); + + b.Property("FurnitureModuleId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("FurnitureId"); + + b.HasIndex("FurnitureModuleId"); + + b.ToTable("FurnitureModuleFurnitures"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.Material", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Cost") + .HasColumnType("double precision"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("ScopeId") + .HasColumnType("integer"); + + b.Property("UserId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("ScopeId"); + + b.HasIndex("UserId"); + + b.ToTable("Materials"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("CustomerName") + .IsRequired() + .HasColumnType("text"); + + b.Property("DateCreate") + .HasColumnType("timestamp with time zone"); + + b.Property("PaymentType") + .HasColumnType("integer"); + + b.Property("SetId") + .HasColumnType("integer"); + + b.Property("UserId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("SetId"); + + b.HasIndex("UserId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.Role", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Roles"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.Scope", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Scopes"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.Set", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Cost") + .HasColumnType("double precision"); + + b.Property("DateCreate") + .HasColumnType("timestamp with time zone"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("Sets"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.SetFurnitureModule", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("FurnitureModuleId") + .HasColumnType("integer"); + + b.Property("SetId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("FurnitureModuleId"); + + b.HasIndex("SetId"); + + b.ToTable("SetFurnitureModules"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Login") + .IsRequired() + .HasColumnType("text"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("Password") + .IsRequired() + .HasColumnType("text"); + + b.Property("RoleId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("RoleId"); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.Furniture", b => + { + b.HasOne("FurnitureAssembly_WorkerDatabaseImplement.Models.User", "User") + .WithMany("Furnitures") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("User"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.FurnitureMaterial", b => + { + b.HasOne("FurnitureAssembly_WorkerDatabaseImplement.Models.Furniture", "Furniture") + .WithMany("Materials") + .HasForeignKey("FurnitureId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FurnitureAssembly_WorkerDatabaseImplement.Models.Material", "Material") + .WithMany("FurnitureMaterials") + .HasForeignKey("MaterialId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Furniture"); + + b.Navigation("Material"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.FurnitureModule", b => + { + b.HasOne("FurnitureAssembly_WorkerDatabaseImplement.Models.User", "User") + .WithMany("FurnitureModules") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("User"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.FurnitureModuleFurniture", b => + { + b.HasOne("FurnitureAssembly_WorkerDatabaseImplement.Models.Furniture", "Furniture") + .WithMany() + .HasForeignKey("FurnitureId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FurnitureAssembly_WorkerDatabaseImplement.Models.FurnitureModule", "FurnitureModule") + .WithMany("Furnitures") + .HasForeignKey("FurnitureModuleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Furniture"); + + b.Navigation("FurnitureModule"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.Material", b => + { + b.HasOne("FurnitureAssembly_WorkerDatabaseImplement.Models.Scope", "Scope") + .WithMany("Materials") + .HasForeignKey("ScopeId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FurnitureAssembly_WorkerDatabaseImplement.Models.User", "User") + .WithMany("Materials") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Scope"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.Order", b => + { + b.HasOne("FurnitureAssembly_WorkerDatabaseImplement.Models.Set", "Set") + .WithMany("Orders") + .HasForeignKey("SetId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FurnitureAssembly_WorkerDatabaseImplement.Models.User", "User") + .WithMany("Orders") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Set"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.Set", b => + { + b.HasOne("FurnitureAssembly_WorkerDatabaseImplement.Models.User", "User") + .WithMany("Sets") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("User"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.SetFurnitureModule", b => + { + b.HasOne("FurnitureAssembly_WorkerDatabaseImplement.Models.FurnitureModule", "FurnitureModule") + .WithMany("Sets") + .HasForeignKey("FurnitureModuleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FurnitureAssembly_WorkerDatabaseImplement.Models.Set", "Set") + .WithMany("FurnitureModules") + .HasForeignKey("SetId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("FurnitureModule"); + + b.Navigation("Set"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.User", b => + { + b.HasOne("FurnitureAssembly_WorkerDatabaseImplement.Models.Role", "Role") + .WithMany("Users") + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Role"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.Furniture", b => + { + b.Navigation("Materials"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.FurnitureModule", b => + { + b.Navigation("Furnitures"); + + b.Navigation("Sets"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.Material", b => + { + b.Navigation("FurnitureMaterials"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.Role", b => + { + b.Navigation("Users"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.Scope", b => + { + b.Navigation("Materials"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.Set", b => + { + b.Navigation("FurnitureModules"); + + b.Navigation("Orders"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.User", b => + { + b.Navigation("FurnitureModules"); + + b.Navigation("Furnitures"); + + b.Navigation("Materials"); + + b.Navigation("Orders"); + + b.Navigation("Sets"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Migrations/20230405211314_InitMigration.cs b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Migrations/20230405211314_InitMigration.cs new file mode 100644 index 0000000..a8de5c9 --- /dev/null +++ b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Migrations/20230405211314_InitMigration.cs @@ -0,0 +1,375 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace FurnitureAssembly_WorkerDatabaseImplement.Migrations +{ + /// + public partial class InitMigration : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Roles", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + Name = table.Column(type: "text", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Roles", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Scopes", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + Name = table.Column(type: "text", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Scopes", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Users", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + Login = table.Column(type: "text", nullable: false), + Password = table.Column(type: "text", nullable: false), + Name = table.Column(type: "text", nullable: false), + RoleId = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Users", x => x.Id); + table.ForeignKey( + name: "FK_Users_Roles_RoleId", + column: x => x.RoleId, + principalTable: "Roles", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "FurnitureModules", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + Name = table.Column(type: "text", nullable: false), + Cost = table.Column(type: "double precision", nullable: false), + DateCreate = table.Column(type: "timestamp with time zone", nullable: false), + UserId = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_FurnitureModules", x => x.Id); + table.ForeignKey( + name: "FK_FurnitureModules_Users_UserId", + column: x => x.UserId, + principalTable: "Users", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "Furnitures", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + Name = table.Column(type: "text", nullable: false), + Cost = table.Column(type: "double precision", nullable: false), + DateCreate = table.Column(type: "timestamp with time zone", nullable: false), + UserId = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Furnitures", x => x.Id); + table.ForeignKey( + name: "FK_Furnitures_Users_UserId", + column: x => x.UserId, + principalTable: "Users", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "Materials", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + Name = table.Column(type: "text", nullable: false), + Cost = table.Column(type: "double precision", nullable: false), + ScopeId = table.Column(type: "integer", nullable: false), + UserId = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Materials", x => x.Id); + table.ForeignKey( + name: "FK_Materials_Scopes_ScopeId", + column: x => x.ScopeId, + principalTable: "Scopes", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_Materials_Users_UserId", + column: x => x.UserId, + principalTable: "Users", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "Sets", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + Name = table.Column(type: "text", nullable: false), + Cost = table.Column(type: "double precision", nullable: false), + DateCreate = table.Column(type: "timestamp with time zone", nullable: false), + UserId = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Sets", x => x.Id); + table.ForeignKey( + name: "FK_Sets_Users_UserId", + column: x => x.UserId, + principalTable: "Users", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "FurnitureModuleFurnitures", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + FurnitureModuleId = table.Column(type: "integer", nullable: false), + FurnitureId = table.Column(type: "integer", nullable: false), + Count = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_FurnitureModuleFurnitures", x => x.Id); + table.ForeignKey( + name: "FK_FurnitureModuleFurnitures_FurnitureModules_FurnitureModuleId", + column: x => x.FurnitureModuleId, + principalTable: "FurnitureModules", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_FurnitureModuleFurnitures_Furnitures_FurnitureId", + column: x => x.FurnitureId, + principalTable: "Furnitures", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "FurnitureMaterials", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + FurnitureId = table.Column(type: "integer", nullable: false), + MaterialId = table.Column(type: "integer", nullable: false), + Count = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_FurnitureMaterials", x => x.Id); + table.ForeignKey( + name: "FK_FurnitureMaterials_Furnitures_FurnitureId", + column: x => x.FurnitureId, + principalTable: "Furnitures", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_FurnitureMaterials_Materials_MaterialId", + column: x => x.MaterialId, + principalTable: "Materials", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + CustomerName = table.Column(type: "text", nullable: false), + PaymentType = table.Column(type: "integer", nullable: false), + SetId = table.Column(type: "integer", nullable: false), + DateCreate = table.Column(type: "timestamp with time zone", nullable: false), + UserId = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Orders", x => x.Id); + table.ForeignKey( + name: "FK_Orders_Sets_SetId", + column: x => x.SetId, + principalTable: "Sets", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_Orders_Users_UserId", + column: x => x.UserId, + principalTable: "Users", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "SetFurnitureModules", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + SetId = table.Column(type: "integer", nullable: false), + FurnitureModuleId = table.Column(type: "integer", nullable: false), + Count = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_SetFurnitureModules", x => x.Id); + table.ForeignKey( + name: "FK_SetFurnitureModules_FurnitureModules_FurnitureModuleId", + column: x => x.FurnitureModuleId, + principalTable: "FurnitureModules", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_SetFurnitureModules_Sets_SetId", + column: x => x.SetId, + principalTable: "Sets", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_FurnitureMaterials_FurnitureId", + table: "FurnitureMaterials", + column: "FurnitureId"); + + migrationBuilder.CreateIndex( + name: "IX_FurnitureMaterials_MaterialId", + table: "FurnitureMaterials", + column: "MaterialId"); + + migrationBuilder.CreateIndex( + name: "IX_FurnitureModuleFurnitures_FurnitureId", + table: "FurnitureModuleFurnitures", + column: "FurnitureId"); + + migrationBuilder.CreateIndex( + name: "IX_FurnitureModuleFurnitures_FurnitureModuleId", + table: "FurnitureModuleFurnitures", + column: "FurnitureModuleId"); + + migrationBuilder.CreateIndex( + name: "IX_FurnitureModules_UserId", + table: "FurnitureModules", + column: "UserId"); + + migrationBuilder.CreateIndex( + name: "IX_Furnitures_UserId", + table: "Furnitures", + column: "UserId"); + + migrationBuilder.CreateIndex( + name: "IX_Materials_ScopeId", + table: "Materials", + column: "ScopeId"); + + migrationBuilder.CreateIndex( + name: "IX_Materials_UserId", + table: "Materials", + column: "UserId"); + + migrationBuilder.CreateIndex( + name: "IX_Orders_SetId", + table: "Orders", + column: "SetId"); + + migrationBuilder.CreateIndex( + name: "IX_Orders_UserId", + table: "Orders", + column: "UserId"); + + migrationBuilder.CreateIndex( + name: "IX_SetFurnitureModules_FurnitureModuleId", + table: "SetFurnitureModules", + column: "FurnitureModuleId"); + + migrationBuilder.CreateIndex( + name: "IX_SetFurnitureModules_SetId", + table: "SetFurnitureModules", + column: "SetId"); + + migrationBuilder.CreateIndex( + name: "IX_Sets_UserId", + table: "Sets", + column: "UserId"); + + migrationBuilder.CreateIndex( + name: "IX_Users_RoleId", + table: "Users", + column: "RoleId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "FurnitureMaterials"); + + migrationBuilder.DropTable( + name: "FurnitureModuleFurnitures"); + + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "SetFurnitureModules"); + + migrationBuilder.DropTable( + name: "Materials"); + + migrationBuilder.DropTable( + name: "Furnitures"); + + migrationBuilder.DropTable( + name: "FurnitureModules"); + + migrationBuilder.DropTable( + name: "Sets"); + + migrationBuilder.DropTable( + name: "Scopes"); + + migrationBuilder.DropTable( + name: "Users"); + + migrationBuilder.DropTable( + name: "Roles"); + } + } +} diff --git a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Migrations/FurnitureAssemblyDatabaseModelSnapshot.cs b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Migrations/FurnitureAssemblyDatabaseModelSnapshot.cs new file mode 100644 index 0000000..55e06d5 --- /dev/null +++ b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Migrations/FurnitureAssemblyDatabaseModelSnapshot.cs @@ -0,0 +1,502 @@ +// +using System; +using FurnitureAssembly_WorkerDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace FurnitureAssembly_WorkerDatabaseImplement.Migrations +{ + [DbContext(typeof(FurnitureAssemblyDatabase))] + partial class FurnitureAssemblyDatabaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.4") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.Furniture", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Cost") + .HasColumnType("double precision"); + + b.Property("DateCreate") + .HasColumnType("timestamp with time zone"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("Furnitures"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.FurnitureMaterial", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("FurnitureId") + .HasColumnType("integer"); + + b.Property("MaterialId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("FurnitureId"); + + b.HasIndex("MaterialId"); + + b.ToTable("FurnitureMaterials"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.FurnitureModule", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Cost") + .HasColumnType("double precision"); + + b.Property("DateCreate") + .HasColumnType("timestamp with time zone"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("FurnitureModules"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.FurnitureModuleFurniture", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("FurnitureId") + .HasColumnType("integer"); + + b.Property("FurnitureModuleId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("FurnitureId"); + + b.HasIndex("FurnitureModuleId"); + + b.ToTable("FurnitureModuleFurnitures"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.Material", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Cost") + .HasColumnType("double precision"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("ScopeId") + .HasColumnType("integer"); + + b.Property("UserId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("ScopeId"); + + b.HasIndex("UserId"); + + b.ToTable("Materials"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("CustomerName") + .IsRequired() + .HasColumnType("text"); + + b.Property("DateCreate") + .HasColumnType("timestamp with time zone"); + + b.Property("PaymentType") + .HasColumnType("integer"); + + b.Property("SetId") + .HasColumnType("integer"); + + b.Property("UserId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("SetId"); + + b.HasIndex("UserId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.Role", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Roles"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.Scope", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Scopes"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.Set", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Cost") + .HasColumnType("double precision"); + + b.Property("DateCreate") + .HasColumnType("timestamp with time zone"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("Sets"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.SetFurnitureModule", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("FurnitureModuleId") + .HasColumnType("integer"); + + b.Property("SetId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("FurnitureModuleId"); + + b.HasIndex("SetId"); + + b.ToTable("SetFurnitureModules"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Login") + .IsRequired() + .HasColumnType("text"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("Password") + .IsRequired() + .HasColumnType("text"); + + b.Property("RoleId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("RoleId"); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.Furniture", b => + { + b.HasOne("FurnitureAssembly_WorkerDatabaseImplement.Models.User", "User") + .WithMany("Furnitures") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("User"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.FurnitureMaterial", b => + { + b.HasOne("FurnitureAssembly_WorkerDatabaseImplement.Models.Furniture", "Furniture") + .WithMany("Materials") + .HasForeignKey("FurnitureId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FurnitureAssembly_WorkerDatabaseImplement.Models.Material", "Material") + .WithMany("FurnitureMaterials") + .HasForeignKey("MaterialId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Furniture"); + + b.Navigation("Material"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.FurnitureModule", b => + { + b.HasOne("FurnitureAssembly_WorkerDatabaseImplement.Models.User", "User") + .WithMany("FurnitureModules") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("User"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.FurnitureModuleFurniture", b => + { + b.HasOne("FurnitureAssembly_WorkerDatabaseImplement.Models.Furniture", "Furniture") + .WithMany() + .HasForeignKey("FurnitureId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FurnitureAssembly_WorkerDatabaseImplement.Models.FurnitureModule", "FurnitureModule") + .WithMany("Furnitures") + .HasForeignKey("FurnitureModuleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Furniture"); + + b.Navigation("FurnitureModule"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.Material", b => + { + b.HasOne("FurnitureAssembly_WorkerDatabaseImplement.Models.Scope", "Scope") + .WithMany("Materials") + .HasForeignKey("ScopeId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FurnitureAssembly_WorkerDatabaseImplement.Models.User", "User") + .WithMany("Materials") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Scope"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.Order", b => + { + b.HasOne("FurnitureAssembly_WorkerDatabaseImplement.Models.Set", "Set") + .WithMany("Orders") + .HasForeignKey("SetId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FurnitureAssembly_WorkerDatabaseImplement.Models.User", "User") + .WithMany("Orders") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Set"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.Set", b => + { + b.HasOne("FurnitureAssembly_WorkerDatabaseImplement.Models.User", "User") + .WithMany("Sets") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("User"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.SetFurnitureModule", b => + { + b.HasOne("FurnitureAssembly_WorkerDatabaseImplement.Models.FurnitureModule", "FurnitureModule") + .WithMany("Sets") + .HasForeignKey("FurnitureModuleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FurnitureAssembly_WorkerDatabaseImplement.Models.Set", "Set") + .WithMany("FurnitureModules") + .HasForeignKey("SetId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("FurnitureModule"); + + b.Navigation("Set"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.User", b => + { + b.HasOne("FurnitureAssembly_WorkerDatabaseImplement.Models.Role", "Role") + .WithMany("Users") + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Role"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.Furniture", b => + { + b.Navigation("Materials"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.FurnitureModule", b => + { + b.Navigation("Furnitures"); + + b.Navigation("Sets"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.Material", b => + { + b.Navigation("FurnitureMaterials"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.Role", b => + { + b.Navigation("Users"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.Scope", b => + { + b.Navigation("Materials"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.Set", b => + { + b.Navigation("FurnitureModules"); + + b.Navigation("Orders"); + }); + + modelBuilder.Entity("FurnitureAssembly_WorkerDatabaseImplement.Models.User", b => + { + b.Navigation("FurnitureModules"); + + b.Navigation("Furnitures"); + + b.Navigation("Materials"); + + b.Navigation("Orders"); + + b.Navigation("Sets"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Models/Furniture.cs b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Models/Furniture.cs new file mode 100644 index 0000000..f718bb6 --- /dev/null +++ b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Models/Furniture.cs @@ -0,0 +1,110 @@ +using FurnitureAssembly_WorkerContracts.BindingModels; +using FurnitureAssembly_WorkerContracts.ViewModels; +using FurnitureAssembly_WorkerDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssembly_WorkerDatabaseImplement.Models +{ + public class Furniture : IFurnitureModel + { + public int Id { get; set; } + [Required] + public string Name { get; set; } = string.Empty; + [Required] + public double Cost { get; set; } + [Required] + public DateTime DateCreate { get; set; } = DateTime.Now; + [Required] + public int UserId { get; set; } + public virtual User User { get; set; } + public Dictionary? _furnitureMaterials = null; + [NotMapped] + public Dictionary FurnitureMaterials + { + get + { + if (_furnitureMaterials == null) + { + _furnitureMaterials = Materials + .ToDictionary(recPC => recPC.MaterialId, recPC => + (recPC.Material as IMaterialModel, recPC.Count)); + } + return _furnitureMaterials; + } + } + [ForeignKey("FurnitureId")] + public virtual List Materials { get; set; } = new(); + public static Furniture Create(FurnitureAssemblyDatabase context, FurnitureBindingModel model) + { + return new Furniture() + { + Id = model.Id, + Name = model.Name, + Cost = model.Cost, + UserId = model.UserId, + DateCreate = model.DateCreate, + Materials = model.FurnitureMaterials.Select(x => new FurnitureMaterial + { + Material = context.Materials.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(FurnitureBindingModel model) + { + if (model == null) + { + return; + } + Name = model.Name; + Cost = model.Cost; + } + public FurnitureViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Cost = Cost, + UserId = UserId, + UserName = User.Name, + DateCreate = DateCreate, + FurnitureMaterials = FurnitureMaterials + }; + public void UpdateMaterials(FurnitureAssemblyDatabase context, FurnitureBindingModel model) + { + var furnitureMaterials = context.FurnitureMaterials.Where(rec => + rec.FurnitureId == model.Id).ToList(); + if (furnitureMaterials != null && furnitureMaterials.Count > 0) + { // удалили те, которых нет в модели + context.FurnitureMaterials.RemoveRange(furnitureMaterials.Where(rec + => !model.FurnitureMaterials.ContainsKey(rec.MaterialId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateMaterial in furnitureMaterials) + { + updateMaterial.Count = model.FurnitureMaterials[updateMaterial.MaterialId].Item2; + model.FurnitureMaterials.Remove(updateMaterial.MaterialId); + } + context.SaveChanges(); + } + var furniture = context.Furnitures.First(x => x.Id == Id); + foreach (var pc in model.FurnitureMaterials) + { + context.FurnitureMaterials.Add(new FurnitureMaterial + { + Furniture = furniture, + Material = context.Materials.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _furnitureMaterials = null; + } + } +} diff --git a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Models/FurnitureMaterial.cs b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Models/FurnitureMaterial.cs new file mode 100644 index 0000000..239b94d --- /dev/null +++ b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Models/FurnitureMaterial.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssembly_WorkerDatabaseImplement.Models +{ + public class FurnitureMaterial + { + public int Id { get; set; } + [Required] + public int FurnitureId { get; set; } + [Required] + public int MaterialId { get; set; } + [Required] + public int Count { get; set; } + public virtual Furniture Furniture { get; set; } = new(); + public virtual Material Material { get; set; } = new(); + } +} diff --git a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Models/FurnitureModule.cs b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Models/FurnitureModule.cs new file mode 100644 index 0000000..12a808e --- /dev/null +++ b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Models/FurnitureModule.cs @@ -0,0 +1,118 @@ +using FurnitureAssembly_WorkerContracts.BindingModels; +using FurnitureAssembly_WorkerContracts.ViewModels; +using FurnitureAssembly_WorkerDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssembly_WorkerDatabaseImplement.Models +{ + public class FurnitureModule : IFurnitureModuleModel + { + public int Id { get; set; } + [Required] + public string Name { get; set; } = string.Empty; + [Required] + public double Cost { get; set; } + [Required] + public DateTime DateCreate { get; set; } + [Required] + public int UserId { get; set; } + public virtual User User { get; set; } + public Dictionary? _furnitureModuleFurnitures = null; + [NotMapped] + public Dictionary FurnitureModuleFurnitures + { + get + { + if (_furnitureModuleFurnitures == null) + { + _furnitureModuleFurnitures = Furnitures + .ToDictionary(recPC => recPC.FurnitureId, recPC => + (recPC.Furniture as IFurnitureModel, recPC.Count)); + } + return _furnitureModuleFurnitures; + } + } + [ForeignKey("FurnitureModuleId")] + public virtual List Furnitures { get; set; } = new(); + [ForeignKey("FurnitureModuleId")] + public virtual List Sets { get; set; } = new(); + public static FurnitureModule Create(FurnitureAssemblyDatabase context, FurnitureModuleBindingModel model) + { + if (model == null) + { + return null; + } + return new FurnitureModule() + { + Id = model.Id, + Name = model.Name, + Cost = model.Cost, + DateCreate = model.DateCreate, + UserId = model.UserId, + Furnitures = model.FurnitureModuleFurnitures.Select(x => new FurnitureModuleFurniture + { + Furniture = context.Furnitures.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(FurnitureModuleBindingModel model) + { + if (model == null) + { + return; + } + Name = model.Name; + Cost = model.Cost; + } + public FurnitureModuleViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Cost = Cost, + DateCreate = DateCreate, + UserId = UserId, + UserName = User.Name, + FurnitureModuleFurnitures = FurnitureModuleFurnitures + }; + public void UpdateFurnitures(FurnitureAssemblyDatabase context, FurnitureModuleBindingModel model) + { + var furnitureModuleFurnitures = context.FurnitureModuleFurnitures.Where(rec => + rec.FurnitureModuleId == model.Id).ToList(); + if (furnitureModuleFurnitures != null && furnitureModuleFurnitures.Count > 0) + { // удалили те, которых нет в модели + context.FurnitureModuleFurnitures.RemoveRange(furnitureModuleFurnitures.Where(rec + => !model.FurnitureModuleFurnitures.ContainsKey(rec.FurnitureId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateFurniture in furnitureModuleFurnitures) + { + updateFurniture.Count = model.FurnitureModuleFurnitures[updateFurniture.FurnitureId].Item2; + model.FurnitureModuleFurnitures.Remove(updateFurniture.FurnitureId); + } + context.SaveChanges(); + } + var furnitureModule = context.FurnitureModules.First(x => x.Id == Id); + foreach (var pc in model.FurnitureModuleFurnitures) + { + context.FurnitureModuleFurnitures.Add(new FurnitureModuleFurniture + { + FurnitureModule = furnitureModule, + Furniture = context.Furnitures.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _furnitureModuleFurnitures = null; + } + + } +} diff --git a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Models/FurnitureModuleFurniture.cs b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Models/FurnitureModuleFurniture.cs new file mode 100644 index 0000000..c9d73ad --- /dev/null +++ b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Models/FurnitureModuleFurniture.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssembly_WorkerDatabaseImplement.Models +{ + public class FurnitureModuleFurniture + { + public int Id { get; set; } + [Required] + public int FurnitureModuleId { get; set; } + [Required] + public int FurnitureId { get; set; } + [Required] + public int Count { get; set; } + public virtual FurnitureModule FurnitureModule { get; set; } = new(); + public virtual Furniture Furniture { get; set; } = new(); + } +} diff --git a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Models/Material.cs b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Models/Material.cs new file mode 100644 index 0000000..bb2b1d7 --- /dev/null +++ b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Models/Material.cs @@ -0,0 +1,66 @@ +using FurnitureAssembly_WorkerContracts.BindingModels; +using FurnitureAssembly_WorkerContracts.ViewModels; +using FurnitureAssembly_WorkerDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssembly_WorkerDatabaseImplement.Models +{ + public class Material : IMaterialModel + { + public int Id { get; set; } + [Required] + public string Name { get; set; } = string.Empty; + [Required] + public double Cost { get; set; } + [Required] + public int ScopeId { get; set; } + public virtual Scope Scope { get; set; } + [Required] + public int UserId { get; set; } + public virtual User User { get; set; } + [ForeignKey("MaterialId")] + public virtual List FurnitureMaterials { get; set; } = new(); + public static Material? Create(MaterialBindingModel model) + { + if (model == null) + { + return null; + } + return new Material() + { + Id = model.Id, + Name = model.Name, + Cost = model.Cost, + ScopeId = model.ScopeId, + UserId = model.UserId + }; + } + public void Update(MaterialBindingModel model) + { + if (model == null) + { + return; + } + Name = model.Name; + Cost = model.Cost; + ScopeId = model.ScopeId; + } + public MaterialViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Cost = Cost, + ScopeId = ScopeId, + ScopeName = Scope.Name, + UserId = UserId, + UserName = User.Name + }; + } +} diff --git a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Models/Order.cs b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..f832285 --- /dev/null +++ b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Models/Order.cs @@ -0,0 +1,68 @@ +using FurnitureAssembly_WorkerContracts.BindingModels; +using FurnitureAssembly_WorkerContracts.ViewModels; +using FurnitureAssembly_WorkerDataModels.Enums; +using FurnitureAssembly_WorkerDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssembly_WorkerDatabaseImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; set; } + [Required] + public string CustomerName { get; set; } = string.Empty; + [Required] + public PaymentType PaymentType { get; set; } = PaymentType.Неизвестен; + [Required] + public int SetId { get; set; } + public virtual Set Set { get; set; } + [Required] + public DateTime DateCreate { get; set; } + [Required] + public int UserId { get; set; } + public virtual User User { get; set; } + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + CustomerName = model.CustomerName, + PaymentType = model.PaymentType, + SetId = model.SetId, + DateCreate = model.DateCreate, + UserId = model.UserId + }; + } + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + CustomerName = model.CustomerName; + PaymentType = model.PaymentType; + SetId = model.SetId; + UserId = model.UserId; + } + public OrderViewModel GetViewModel => new() + { + Id = Id, + CustomerName = CustomerName, + PaymentType = PaymentType, + SetId = SetId, + SetName = Set.Name, + DateCreate = DateCreate, + UserId = UserId, + UserName = User.Name + }; + } +} diff --git a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Models/Role.cs b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Models/Role.cs index 47e7ee7..133f7ae 100644 --- a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Models/Role.cs +++ b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Models/Role.cs @@ -4,6 +4,7 @@ using FurnitureAssembly_WorkerDataModels.Models; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -15,6 +16,8 @@ namespace FurnitureAssembly_WorkerDatabaseImplement.Models public int Id { get; set; } [Required] public string Name { get; set; } = string.Empty; + [ForeignKey("RoleId")] + public virtual List Users { get; set; } = new(); public static Role? Create(RoleBindingModel? model) { if (model == null) diff --git a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Models/Scope.cs b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Models/Scope.cs new file mode 100644 index 0000000..8a91f22 --- /dev/null +++ b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Models/Scope.cs @@ -0,0 +1,47 @@ +using FurnitureAssembly_WorkerContracts.BindingModels; +using FurnitureAssembly_WorkerContracts.ViewModels; +using FurnitureAssembly_WorkerDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssembly_WorkerDatabaseImplement.Models +{ + public class Scope : IScopeModel + { + public int Id { get; set; } + [Required] + public string Name { get; set; } = string.Empty; + [ForeignKey("ScopeId")] + public virtual List Materials { get; set; } = new(); + public static Scope? Create(ScopeBindingModel? model) + { + if (model == null) + { + return null; + } + return new Scope() + { + Id = model.Id, + Name = model.Name + }; + } + public void Update(ScopeBindingModel? model) + { + if (model == null) + { + return; + } + Name = model.Name; + } + public ScopeViewModel GetViewModel => new() + { + Id = Id, + Name = Name + }; + } +} diff --git a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Models/Set.cs b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Models/Set.cs new file mode 100644 index 0000000..2f081e4 --- /dev/null +++ b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Models/Set.cs @@ -0,0 +1,111 @@ +using FurnitureAssembly_WorkerContracts.BindingModels; +using FurnitureAssembly_WorkerContracts.ViewModels; +using FurnitureAssembly_WorkerDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssembly_WorkerDatabaseImplement.Models +{ + public class Set : ISetModel + { + public int Id { get; set; } + [Required] + public string Name { get; set; } = string.Empty; + [Required] + public double Cost { get; set; } + [Required] + public DateTime DateCreate { get; set; } + [Required] + public int UserId { get; set; } + public virtual User User { get; set; } + public Dictionary? _setFurnitureModules = null; + [NotMapped] + public Dictionary SetFurnitureModules + { + get + { + if (_setFurnitureModules == null) + { + _setFurnitureModules = FurnitureModules + .ToDictionary(recPC => recPC.FurnitureModuleId, recPC => + (recPC.FurnitureModule as IFurnitureModuleModel, recPC.Count)); + } + return _setFurnitureModules; + } + } + [ForeignKey("SetId")] + public virtual List FurnitureModules { get; set; } = new(); + [ForeignKey("SetId")] + public virtual List Orders { get; set; } = new(); + public static Set Create(FurnitureAssemblyDatabase context, SetBindingModel model) + { + return new Set() + { + Id = model.Id, + Name = model.Name, + Cost = model.Cost, + DateCreate = model.DateCreate, + FurnitureModules = model.SetFurnitureModules.Select(x => new SetFurnitureModule + { + FurnitureModule = context.FurnitureModules.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(SetBindingModel model) + { + if (model == null) + { + return; + } + Name = model.Name; + Cost = model.Cost; + } + public SetViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Cost = Cost, + DateCreate = DateCreate, + UserId = UserId, + UserName = User.Name, + SetFurnitureModules = SetFurnitureModules + }; + public void UpdateFurnitureModules(FurnitureAssemblyDatabase context, SetBindingModel model) + { + var setFurnitureModules = context.SetFurnitureModules.Where(rec => + rec.SetId == model.Id).ToList(); + if (setFurnitureModules != null && setFurnitureModules.Count > 0) + { // удалили те, которых нет в модели + context.SetFurnitureModules.RemoveRange(setFurnitureModules.Where(rec + => !model.SetFurnitureModules.ContainsKey(rec.FurnitureModuleId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateFurnitureModule in setFurnitureModules) + { + updateFurnitureModule.Count = model.SetFurnitureModules[updateFurnitureModule.FurnitureModuleId].Item2; + model.SetFurnitureModules.Remove(updateFurnitureModule.FurnitureModuleId); + } + context.SaveChanges(); + } + var set = context.Sets.First(x => x.Id == Id); + foreach (var pc in model.SetFurnitureModules) + { + context.SetFurnitureModules.Add(new SetFurnitureModule + { + Set = set, + FurnitureModule = context.FurnitureModules.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _setFurnitureModules = null; + } + } +} diff --git a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Models/SetFurnitureModule.cs b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Models/SetFurnitureModule.cs new file mode 100644 index 0000000..59058b6 --- /dev/null +++ b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Models/SetFurnitureModule.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssembly_WorkerDatabaseImplement.Models +{ + public class SetFurnitureModule + { + public int Id { get; set; } + [Required] + public int SetId { get; set; } + [Required] + public int FurnitureModuleId { get; set; } + [Required] + public int Count { get; set; } + public virtual Set Set { get; set; } = new(); + public virtual FurnitureModule FurnitureModule { get; set; } = new(); + } +} diff --git a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Models/User.cs b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Models/User.cs index 5768d57..0c1e3a7 100644 --- a/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Models/User.cs +++ b/FurnitureAssembly_Worker/FurnitureAssembly_WorkerDatabaseImplement/Models/User.cs @@ -1,7 +1,10 @@ -using FurnitureAssembly_WorkerDataModels.Models; +using FurnitureAssembly_WorkerContracts.BindingModels; +using FurnitureAssembly_WorkerContracts.ViewModels; +using FurnitureAssembly_WorkerDataModels.Models; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -20,5 +23,50 @@ namespace FurnitureAssembly_WorkerDatabaseImplement.Models [Required] public int RoleId { get; set; } public virtual Role Role { get; set; } + [ForeignKey("UserId")] + public virtual List Orders { get; set; } = new(); + [ForeignKey("UserId")] + public virtual List Sets { get; set; } = new(); + [ForeignKey("UserId")] + public virtual List FurnitureModules { get; set; } = new(); + [ForeignKey("UserId")] + public virtual List Furnitures { get; set; } = new(); + [ForeignKey("UserId")] + public virtual List Materials { get; set; } = new(); + public static User? Create(UserBindingModel? model) + { + if (model == null) + { + return null; + } + return new User() + { + Id = model.Id, + Login = model.Login, + Password = model.Password, + Name = model.Name, + RoleId = model.RoleId + }; + } + public void Update(UserBindingModel? model) + { + if (model == null) + { + return; + } + Login = model.Login; + Password = model.Password; + Name = model.Name; + RoleId = model.RoleId; + } + public UserViewModel GetViewModel => new() + { + Id = Id, + Password = Password, + Login = Login, + Name = Name, + RoleId = RoleId, + RoleName = Role.Name + }; } }