From a0c8eb42c8b7d22963c5f8e4d893910a42fa6d4f Mon Sep 17 00:00:00 2001 From: Ismailov_Rovshan Date: Sat, 8 Apr 2023 00:35:02 +0400 Subject: [PATCH] Db Model --- .../BindingModels/FurnitureBindingModel.cs | 8 ++ .../HeadsetModuleBindingModel.cs | 6 +- .../BindingModels/MasterBindingModel.cs | 2 +- .../BindingModels/MaterialBindingModel.cs | 2 +- .../ViewModel/FurnitureViewModel.cs | 6 + .../ViewModel/HeadsetModuleViewModel.cs | 6 +- .../ViewModel/MasterViewModel.cs | 2 +- .../ViewModel/MaterialViewModel.cs | 2 +- .../Models/Furniture.cs | 108 +++++++++++++++++- .../Models/FurnitureMaterial.cs | 22 ++++ .../Models/HeadsetModule.cs | 94 ++++++++++++++- .../Models/HeadsetModuleMaterial.cs | 20 ++++ .../Models/Master.cs | 57 +++++++++ .../Models/Material.cs | 67 ++++++++++- .../Models/IFurnitureModel.cs | 6 +- .../Models/IHeadsetModuleModel.cs | 3 +- .../Models/IMasterModel.cs | 2 +- .../Models/IMaterialModel.cs | 2 +- 18 files changed, 394 insertions(+), 21 deletions(-) create mode 100644 FurnitureFactory/FurnitureFactoryDataBaseImplement/Models/FurnitureMaterial.cs create mode 100644 FurnitureFactory/FurnitureFactoryDataBaseImplement/Models/HeadsetModuleMaterial.cs create mode 100644 FurnitureFactory/FurnitureFactoryDataBaseImplement/Models/Master.cs diff --git a/FurnitureFactory/FurnitureContracts/BindingModels/FurnitureBindingModel.cs b/FurnitureFactory/FurnitureContracts/BindingModels/FurnitureBindingModel.cs index 0a7a40c..685d4b8 100644 --- a/FurnitureFactory/FurnitureContracts/BindingModels/FurnitureBindingModel.cs +++ b/FurnitureFactory/FurnitureContracts/BindingModels/FurnitureBindingModel.cs @@ -18,5 +18,13 @@ namespace FurnitureContracts.BindingModels public Dictionary FurnitureMaterial { get; set; } public int Id { get; set; } + + public int MasterId { get; set; } + + public int OrdersId { get; set; } + + public string OrdersName { get; set; } = string.Empty; + + public string MasterName { get; set; } = string.Empty; } } diff --git a/FurnitureFactory/FurnitureContracts/BindingModels/HeadsetModuleBindingModel.cs b/FurnitureFactory/FurnitureContracts/BindingModels/HeadsetModuleBindingModel.cs index 55b9bf9..47b678e 100644 --- a/FurnitureFactory/FurnitureContracts/BindingModels/HeadsetModuleBindingModel.cs +++ b/FurnitureFactory/FurnitureContracts/BindingModels/HeadsetModuleBindingModel.cs @@ -13,8 +13,12 @@ namespace FurnitureContracts.BindingModels public int Cost { get; set; } - public int UserID { get; set; } + public int MasterId { get; set; } public int Id { get; set; } + + public string MasterName { get; set; } = string.Empty; + + public Dictionary HeadsetModuleMaterial { get; set; }; } } diff --git a/FurnitureFactory/FurnitureContracts/BindingModels/MasterBindingModel.cs b/FurnitureFactory/FurnitureContracts/BindingModels/MasterBindingModel.cs index 2eb5971..daae306 100644 --- a/FurnitureFactory/FurnitureContracts/BindingModels/MasterBindingModel.cs +++ b/FurnitureFactory/FurnitureContracts/BindingModels/MasterBindingModel.cs @@ -8,7 +8,7 @@ namespace FurnitureContracts.BindingModels { public class MasterBindingModel : IMasterModel { - public string UserName { get; set; } = string.Empty; + public string Name { get; set; } = string.Empty; public string Login { get; set; } = string.Empty; public string Password { get; set; } = string.Empty; public string Email { get; set; } = string.Empty; diff --git a/FurnitureFactory/FurnitureContracts/BindingModels/MaterialBindingModel.cs b/FurnitureFactory/FurnitureContracts/BindingModels/MaterialBindingModel.cs index c23a1bb..19cc640 100644 --- a/FurnitureFactory/FurnitureContracts/BindingModels/MaterialBindingModel.cs +++ b/FurnitureFactory/FurnitureContracts/BindingModels/MaterialBindingModel.cs @@ -13,7 +13,7 @@ namespace FurnitureContracts.BindingModels public int Cost { get; set; } - public int UserID { get; set; } + public int MasterId { get; set; } public int Id { get; set; } } diff --git a/FurnitureFactory/FurnitureContracts/ViewModel/FurnitureViewModel.cs b/FurnitureFactory/FurnitureContracts/ViewModel/FurnitureViewModel.cs index e2e9f88..d3829e9 100644 --- a/FurnitureFactory/FurnitureContracts/ViewModel/FurnitureViewModel.cs +++ b/FurnitureFactory/FurnitureContracts/ViewModel/FurnitureViewModel.cs @@ -22,5 +22,11 @@ namespace FurnitureContracts.ViewModel public Dictionary FurnitureMaterial { get; set; } public int Id { get; set; } + + public int MasterId { get; set; } + public string MasterName { get; set; } = string.Empty; + public int OrdersId { get; set; } + public string OrdersName { get; set; } = string.Empty; + } } diff --git a/FurnitureFactory/FurnitureContracts/ViewModel/HeadsetModuleViewModel.cs b/FurnitureFactory/FurnitureContracts/ViewModel/HeadsetModuleViewModel.cs index 4455263..dbe904a 100644 --- a/FurnitureFactory/FurnitureContracts/ViewModel/HeadsetModuleViewModel.cs +++ b/FurnitureFactory/FurnitureContracts/ViewModel/HeadsetModuleViewModel.cs @@ -15,8 +15,12 @@ namespace FurnitureContracts.ViewModel [DisplayName("Цена")] public int Cost { get; set; } - public int UserID { get; set; } + public int MasterId { get; set; } public int Id { get; set; } + + public Dictionary HeadsetModuleMaterial { get; set; } + + public string MasterName { get; set; } = string.Empty; } } diff --git a/FurnitureFactory/FurnitureContracts/ViewModel/MasterViewModel.cs b/FurnitureFactory/FurnitureContracts/ViewModel/MasterViewModel.cs index 7c9ca8b..2a11d14 100644 --- a/FurnitureFactory/FurnitureContracts/ViewModel/MasterViewModel.cs +++ b/FurnitureFactory/FurnitureContracts/ViewModel/MasterViewModel.cs @@ -16,7 +16,7 @@ namespace FurnitureContracts.ViewModel public string Password { get; set; } [DisplayName("Имя пользователя")] public string Email { get; set; } - public string UserName { get; set; } = string.Empty; + public string Name { get; set; } = string.Empty; public int Id { get; set; } } } diff --git a/FurnitureFactory/FurnitureContracts/ViewModel/MaterialViewModel.cs b/FurnitureFactory/FurnitureContracts/ViewModel/MaterialViewModel.cs index 360a16c..7e2bbff 100644 --- a/FurnitureFactory/FurnitureContracts/ViewModel/MaterialViewModel.cs +++ b/FurnitureFactory/FurnitureContracts/ViewModel/MaterialViewModel.cs @@ -16,7 +16,7 @@ namespace FurnitureContracts.ViewModel [DisplayName("Цена")] public int Cost { get; set; } - public int UserID { get; set; } + public int MasterId { get; set; } public int Id { get; set; } } diff --git a/FurnitureFactory/FurnitureFactoryDataBaseImplement/Models/Furniture.cs b/FurnitureFactory/FurnitureFactoryDataBaseImplement/Models/Furniture.cs index 143f0ed..e3cd7b1 100644 --- a/FurnitureFactory/FurnitureFactoryDataBaseImplement/Models/Furniture.cs +++ b/FurnitureFactory/FurnitureFactoryDataBaseImplement/Models/Furniture.cs @@ -1,12 +1,116 @@ -using System; +using FurnitureContracts.BindingModels; +using FurnitureContracts.ViewModel; +using FurnitureFactoryDataModels.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 FurnitureFactoryDataBaseImplement.Models { - public class Furniture + public class Furniture : IFurnitureModel { + [Required] + public string Name { get; set; } = string.Empty; + + [Required] + public int OrdersId { get; set; } + + public string OrdersName { get; set; } = string.Empty; + public string Color { get; set; } = string.Empty; + + public string Type { get; set; } = string.Empty; + public virtual Orders Orders { get; set; } + [Required] + public int MasterId { get; set; } + public string MasterName { get; set; } = string.Empty; + public virtual Master Master { get; set; } + private Dictionary? _FurnitureMaterials = null; + [NotMapped] + public Dictionary FurnitureMaterials + { + get + { + if (_FurnitureMaterials == null) + { + _FurnitureMaterials = Materials + .ToDictionary(recPC => recPC.MaterialId, recPC => (recPC.Material as IMaterialModel)); + } + return _FurnitureMaterials; + } + } + + public int Id { get; set; } + + [ForeignKey("FurnitureId")] + public virtual List Materials { get; set; } = new(); + public static Furniture Create(SchoolDataBase context, FurnitureBindingModel model) + { + return new Furniture() + { + Id = model.Id, + Name = model.Name, + Color = model.Color, + Type = model.Type, + OrdersId = model.OrdersId, + OrdersName = model.OrdersName, + MasterId = model.MasterId, + MasterName = model.MasterName, + Materials = model.FurnitureMaterial.Select(x => new FurnitureMaterial + { + Material = context.Materials.First(y => y.Id == x.Key), + }).ToList() + }; + } + + public void Update(FurnitureBindingModel model) + { + Name = model.Name; + } + + public FurnitureViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + OrdersId = OrdersId, + OrdersName = OrdersName, + MasterId = MasterId, + MasterName = MasterName, + FurnitureMaterial = FurnitureMaterials + }; + + + + public Dictionary FurnitureMaterial => throw new NotImplementedException(); + + public void UpdateMaterials(SchoolDataBase 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.FurnitureMaterial.ContainsKey(rec.MaterialId))); + context.SaveChanges(); + + foreach (var updateMaterial in FurnitureMaterials) + { + model.FurnitureMaterial.Remove(updateMaterial.MaterialId); + } + context.SaveChanges(); + } + var Furniture = context.Furnitures.First(x => x.Id == Id); + foreach (var pc in model.FurnitureMaterial) + { + context.FurnitureMaterials.Add(new FurnitureMaterial + { + Furniture = Furniture, + Material = context.Materials.First(x => x.Id == pc.Key), + }); + context.SaveChanges(); + } + _FurnitureMaterials = null; + } } } diff --git a/FurnitureFactory/FurnitureFactoryDataBaseImplement/Models/FurnitureMaterial.cs b/FurnitureFactory/FurnitureFactoryDataBaseImplement/Models/FurnitureMaterial.cs new file mode 100644 index 0000000..77fe9b7 --- /dev/null +++ b/FurnitureFactory/FurnitureFactoryDataBaseImplement/Models/FurnitureMaterial.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureFactoryDataBaseImplement.Models +{ + public class FurnitureMaterial + { + public int Id { get; set; } + [Required] + public int FurnitureId { get; set; } + [Required] + public int MaterialId { get; set; } + public virtual Furniture Furniture { get; set; } = new(); + public virtual Material Material { get; set; } = new(); + + + } +} diff --git a/FurnitureFactory/FurnitureFactoryDataBaseImplement/Models/HeadsetModule.cs b/FurnitureFactory/FurnitureFactoryDataBaseImplement/Models/HeadsetModule.cs index 500394d..aec2acb 100644 --- a/FurnitureFactory/FurnitureFactoryDataBaseImplement/Models/HeadsetModule.cs +++ b/FurnitureFactory/FurnitureFactoryDataBaseImplement/Models/HeadsetModule.cs @@ -1,12 +1,100 @@ -using System; +using FurnitureContracts.BindingModels; +using FurnitureContracts.ViewModel; +using FurnitureFactoryDataModels.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 FurnitureFactoryDataBaseImplement.Models { - public class HeadsetModule + public class HeadsetModule : IHeadsetModuleModel { + [Required] + public string Style { get; set; } = string.Empty; + [Required] + public int Cost { get; set; } + [Required] + public int MasterId { get; set; } + public Dictionary _HeadsetModuleMaterial = null; + + public Dictionary HeadsetModuleMaterial + { + get + { + if (_HeadsetModuleMaterial == null) + { + _HeadsetModuleMaterial = Materials.ToDictionary(recPC => recPC.MaterialId, recPC => (recPC.Material as IMaterialModel)); + } + return _HeadsetModuleMaterial; + + } + } + + [ForeignKey("HeadsetModuleId")] + public virtual List Materials { get; set; } = new(); + [ForeignKey("HeadsetModuleId")] + public virtual List Headsets { get; set; } = new(); + public int Id { get; set; } + public string MasterName { get; set; } = string.Empty; + public static HeadsetModule Create(DateBase context, HeadsetModuleBindingModel model) + { + return new HeadsetModule() + { + Id = model.Id, + Style = model.Style, + Cost = model.Cost, + MasterId = model.MasterId, + MasterName = model.MasterName, + Headsets = model.HeadsetModuleMaterial.Select(x => new HeadsetModuleMaterial + { + Material = context.Materials.First(y => y.Id == x.Key), + }).ToList() + }; + } + public void Update(HeadsetModuleBindingModel model) + { + Style = model.Style; + Cost = model.Cost; + } + public HeadsetModuleViewModel GetViewModel => new() + { + Id = Id, + Style = Style, + Cost = Cost, + MasterId = MasterId, + MasterName = MasterName, + HeadsetModuleMaterial = HeadsetModuleMaterial + }; + public void UpdateMaterials(FurnitureDataBase context, HeadsetModuleBindingModel model) + { + var headsetModule = context.HeadsetModulMaterial.Where(rec => rec.HeadsetModuleId == model.Id).ToList(); + if (headsetModule != null && headsetModule.Count > 0) + { + context.HeadsetModuleMaterials.RemoveRange(headsetModule.Where(rec => !model.HeadsetModuleMaterial.ContainsKey(rec.MaterialId))); + context.SaveChanges(); + + foreach (var updateMaterial in headsetModule) + { + model.HeadsetModuleMaterial.Remove(updateMaterial.MaterialId); + } + context.SaveChanges(); + } + var HeadsetModule = context.HeadsetModules.First(x => x.Id == Id); + foreach (var pc in model.HeadsetModuleMaterial) + { + context.HeadsetModuleMaterial.Add(new HeadsetModuleMaterial + { + HeadsetModule = HeadsetModule, + Material = context.Materials.First(x => x.Id == pc.Key), + }); + context.SaveChanges(); + } + _HeadsetModuleMaterial = null; + } } } diff --git a/FurnitureFactory/FurnitureFactoryDataBaseImplement/Models/HeadsetModuleMaterial.cs b/FurnitureFactory/FurnitureFactoryDataBaseImplement/Models/HeadsetModuleMaterial.cs new file mode 100644 index 0000000..9a25c78 --- /dev/null +++ b/FurnitureFactory/FurnitureFactoryDataBaseImplement/Models/HeadsetModuleMaterial.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureFactoryDataBaseImplement.Models +{ + public class HeadsetModuleMaterial + { + public int Id { get; set; } + [Required] + public int TaskId { get; set; } + [Required] + public int MaterialId { get; set; } + public virtual HeadsetModule HeadsetModule { get; set; } = new(); + public virtual Material Material { get; set; } = new(); + } +} diff --git a/FurnitureFactory/FurnitureFactoryDataBaseImplement/Models/Master.cs b/FurnitureFactory/FurnitureFactoryDataBaseImplement/Models/Master.cs new file mode 100644 index 0000000..1ba71aa --- /dev/null +++ b/FurnitureFactory/FurnitureFactoryDataBaseImplement/Models/Master.cs @@ -0,0 +1,57 @@ +using FurnitureContracts.BindingModels; +using FurnitureContracts.ViewModel; +using FurnitureFactoryDataModels.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 FurnitureFactoryDataBaseImplement.Models +{ + public class Master : IMasterModel + { + [Required] + public string Login { get; set; } = string.Empty; + [Required] + public string Password { get; set; } = string.Empty; + [Required] + public string Name { get; set; } = string.Empty; + [Required] + public string Email { get; set; } = string.Empty; + public int Id { get; set; } + + [ForeignKey("MasterId")] + public virtual List Materials { get; set; } = new(); + [ForeignKey("MasterId")] + public virtual List Furnitures { get; set; } = new(); + [ForeignKey("MasterId")] + public virtual List HeadsetModules { get; set; } = new(); + public static Master Create(DateBase context, MasterBindingModel model) + { + return new Master() + { + Id = model.Id, + Name = model.Name, + Email = model.Email, + Password = model.Password, + }; + } + public void Update(MasterBindingModel model) + { + Id = model.Id; + Name = model.Name; + Email = model.Email; + Password = model.Password; + } + public MasterViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Email = Email, + Password = Password, + }; + } +} diff --git a/FurnitureFactory/FurnitureFactoryDataBaseImplement/Models/Material.cs b/FurnitureFactory/FurnitureFactoryDataBaseImplement/Models/Material.cs index feb27cf..3f9e8c2 100644 --- a/FurnitureFactory/FurnitureFactoryDataBaseImplement/Models/Material.cs +++ b/FurnitureFactory/FurnitureFactoryDataBaseImplement/Models/Material.cs @@ -1,20 +1,75 @@ -using FurnitureFactoryDataModels.Models; +using FurnitureContracts.BindingModels; +using FurnitureContracts.ViewModel; +using FurnitureFactoryDataModels.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; +using System.Threading.HeadsetModules; namespace FurnitureFactoryDataBaseImplement.Models { public class Material : IMaterialModel { + [Required] public string Name { get; set; } = string.Empty; - + [Required] public int Cost { get; set; } - - public int UserID { get; set; } - + [Required] + public int MasterId { get; set; } + public virtual Master Master { get; set; } public int Id { get; set; } + + [ForeignKey("MaterialId")] + public virtual List FurnitureMaterials { get; set; } = new(); + [ForeignKey("MaterialId")] + public virtual List HeadsetModuleMaterial { 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, + MasterId = model.MasterId, + }; + } + + public static Material Create(MaterialViewModel model) + { + return new Material + { + Id = model.Id, + Name = model.Name, + Cost = model.Cost, + MasterId = model.MasterId + }; + } + + public void Update(MaterialBindingModel model) + { + if (model == null) + { + return; + } + Name = model.Name; + Cost = model.Cost; + } + + public MaterialViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Cost = Cost, + MasterId = MasterId, + }; } } diff --git a/FurnitureFactory/FurnitureFactoryDataModels/Models/IFurnitureModel.cs b/FurnitureFactory/FurnitureFactoryDataModels/Models/IFurnitureModel.cs index 04d8277..be1b231 100644 --- a/FurnitureFactory/FurnitureFactoryDataModels/Models/IFurnitureModel.cs +++ b/FurnitureFactory/FurnitureFactoryDataModels/Models/IFurnitureModel.cs @@ -11,7 +11,11 @@ namespace FurnitureFactoryDataModels.Models string Name { get;} string Color { get; } string Type { get; } - int UserID { get; } + int MasterId { get; } + int OrdersId { get; } + string OrdersName { get; } + string MasterName { get; } public Dictionary FurnitureMaterial { get; } + } } diff --git a/FurnitureFactory/FurnitureFactoryDataModels/Models/IHeadsetModuleModel.cs b/FurnitureFactory/FurnitureFactoryDataModels/Models/IHeadsetModuleModel.cs index 9f97f3d..550d86d 100644 --- a/FurnitureFactory/FurnitureFactoryDataModels/Models/IHeadsetModuleModel.cs +++ b/FurnitureFactory/FurnitureFactoryDataModels/Models/IHeadsetModuleModel.cs @@ -10,7 +10,8 @@ namespace FurnitureFactoryDataModels.Models { string Style { get;} int Cost { get;} - int UserID { get;} + int MasterId { get;} + string MasterName { get; } public Dictionary HeadsetModuleMaterial { get; } } } diff --git a/FurnitureFactory/FurnitureFactoryDataModels/Models/IMasterModel.cs b/FurnitureFactory/FurnitureFactoryDataModels/Models/IMasterModel.cs index 6f746fb..a5de272 100644 --- a/FurnitureFactory/FurnitureFactoryDataModels/Models/IMasterModel.cs +++ b/FurnitureFactory/FurnitureFactoryDataModels/Models/IMasterModel.cs @@ -10,7 +10,7 @@ namespace FurnitureFactoryDataModels.Models { string Login { get; } string Password { get; } - string UserName { get; } + string Name { get; } string Email { get; } } } diff --git a/FurnitureFactory/FurnitureFactoryDataModels/Models/IMaterialModel.cs b/FurnitureFactory/FurnitureFactoryDataModels/Models/IMaterialModel.cs index e236f35..4cded6f 100644 --- a/FurnitureFactory/FurnitureFactoryDataModels/Models/IMaterialModel.cs +++ b/FurnitureFactory/FurnitureFactoryDataModels/Models/IMaterialModel.cs @@ -10,6 +10,6 @@ namespace FurnitureFactoryDataModels.Models { string Name { get; } int Cost { get; } - int UserID { get; } + int MasterId { get; } } }