From 923255d8ecad50adc9d75d73a94f426e1006f9cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B5=D0=BE=D0=BD=D0=B8=D0=B4=20=D0=9C=D0=B0=D0=BB?= =?UTF-8?q?=D0=B0=D1=84=D0=B5=D0=B5=D0=B2?= Date: Sun, 28 Apr 2024 16:58:32 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9C=D0=BD=D0=BE=D0=B3=D0=B8=D0=B5-=D0=BA?= =?UTF-8?q?=D0=BE-=D0=BC=D0=BD=D0=BE=D0=B3=D0=B8=D0=BC=20=D0=9F=D1=80?= =?UTF-8?q?=D0=B5=D0=B4=D0=BF=D1=80=D0=BE=D0=B4=D0=B0=D0=B6=D0=BD=D0=B0?= =?UTF-8?q?=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=20=D0=9A=D0=BE?= =?UTF-8?q?=D0=BC=D0=BF=D0=BB=D0=B5=D0=BA=D1=82=D0=B0=D1=86=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BindingModels/PresaleBindingModel.cs | 2 +- .../ViewModels/PresaleViewModel.cs | 1 + .../Models/IPresaleModel.cs | 1 + .../CarCenterDatabase.cs | 1 + .../Models/Bundling.cs | 2 + .../Models/Presale.cs | 37 ++++++++++++++++++- .../Models/PresaleBundling.cs | 22 +++++++++++ 7 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 CarCenter/CarCenterDatabaseImplement/Models/PresaleBundling.cs diff --git a/CarCenter/CarCenterContracts/BindingModels/PresaleBindingModel.cs b/CarCenter/CarCenterContracts/BindingModels/PresaleBindingModel.cs index 8797748..7a26689 100644 --- a/CarCenter/CarCenterContracts/BindingModels/PresaleBindingModel.cs +++ b/CarCenter/CarCenterContracts/BindingModels/PresaleBindingModel.cs @@ -18,6 +18,6 @@ namespace CarCenterContracts.BindingModels public DateTime DueTill { get; set; } public double Price { get; set; } - + public Dictionary PresaleBundlings { get; set; } = new(); } } diff --git a/CarCenter/CarCenterContracts/ViewModels/PresaleViewModel.cs b/CarCenter/CarCenterContracts/ViewModels/PresaleViewModel.cs index e9c7c56..29843d0 100644 --- a/CarCenter/CarCenterContracts/ViewModels/PresaleViewModel.cs +++ b/CarCenter/CarCenterContracts/ViewModels/PresaleViewModel.cs @@ -20,5 +20,6 @@ namespace CarCenterContracts.ViewModels public DateTime DueTill { get; set; } [DisplayName("Цена")] public double Price { get; set; } + public Dictionary PresaleBundlings { get; set; } = new(); } } diff --git a/CarCenter/CarCenterDataModels/Models/IPresaleModel.cs b/CarCenter/CarCenterDataModels/Models/IPresaleModel.cs index 49d8b3a..6d59bf9 100644 --- a/CarCenter/CarCenterDataModels/Models/IPresaleModel.cs +++ b/CarCenter/CarCenterDataModels/Models/IPresaleModel.cs @@ -17,5 +17,6 @@ namespace CarCenterDataModels.Models double Price { get; } + Dictionary PresaleBundlings { get; } } } diff --git a/CarCenter/CarCenterDatabaseImplement/CarCenterDatabase.cs b/CarCenter/CarCenterDatabaseImplement/CarCenterDatabase.cs index e05023a..a5ba287 100644 --- a/CarCenter/CarCenterDatabaseImplement/CarCenterDatabase.cs +++ b/CarCenter/CarCenterDatabaseImplement/CarCenterDatabase.cs @@ -30,5 +30,6 @@ namespace CarCenterDatabaseImplement public virtual DbSet Features { set; get; } public virtual DbSet Bundlings { set; get; } public virtual DbSet CarBundlings { set; get; } + public virtual DbSet PresaleBundlings { set; get; } } } diff --git a/CarCenter/CarCenterDatabaseImplement/Models/Bundling.cs b/CarCenter/CarCenterDatabaseImplement/Models/Bundling.cs index 3ce6f7c..a44931e 100644 --- a/CarCenter/CarCenterDatabaseImplement/Models/Bundling.cs +++ b/CarCenter/CarCenterDatabaseImplement/Models/Bundling.cs @@ -26,6 +26,8 @@ namespace CarCenterDatabaseImplement.Models public double Price { get; set; } [ForeignKey("BundlingId")] public virtual List CarBundling { get; set; } = new(); + [ForeignKey("BundlingId")] + public virtual List PresaleBundling { get; set; } = new(); public static Bundling? Create(BundlingBindingModel model) { if (model == null) diff --git a/CarCenter/CarCenterDatabaseImplement/Models/Presale.cs b/CarCenter/CarCenterDatabaseImplement/Models/Presale.cs index 51b7574..7f663e2 100644 --- a/CarCenter/CarCenterDatabaseImplement/Models/Presale.cs +++ b/CarCenter/CarCenterDatabaseImplement/Models/Presale.cs @@ -27,7 +27,22 @@ namespace CarCenterDatabaseImplement.Models public virtual List Requests { get; set; } = new(); [ForeignKey("PresaleId")] public virtual List OrderPresales { get; set; } = new(); - public static Presale? Create(PresaleBindingModel? model) + + private Dictionary? _presaleBundlings = null; + [ForeignKey("PresaleId")] + public virtual List Bundlings { get; set; } = new(); + public Dictionary PresaleBundlings + { + get + { + if (_presaleBundlings == null) + { + _presaleBundlings = Bundlings.ToDictionary(recPc => recPc.BundlingId, recPc => recPc.Bundling as IBundlingModel); + } + return _presaleBundlings; + } + } + public static Presale? Create(CarCenterDatabase context, PresaleBindingModel model) { if (model == null) { @@ -40,9 +55,28 @@ namespace CarCenterDatabaseImplement.Models Description = model.Description, Price = model.Price, DueTill = model.DueTill, + Bundlings = model.PresaleBundlings.Select(x => new PresaleBundling + { + Bundling = context.Bundlings.First(y => y.Id == x.Key) + }).ToList() }; } + public void UpdateBundlings(CarCenterDatabase context, PresaleBindingModel model) + { + var presale = context.Presales.First(x => x.Id == Id); + foreach (var pc in model.PresaleBundlings) + { + context.PresaleBundlings.Add(new PresaleBundling + { + Presale = presale, + Bundling = context.Bundlings.First(x => x.Id == pc.Key), + }); + context.SaveChanges(); + } + _presaleBundlings = null; + } + public void Update(PresaleBindingModel? model) { if (model == null) @@ -62,6 +96,7 @@ namespace CarCenterDatabaseImplement.Models Description = Description, DueTill = DueTill, Price = Price, + PresaleBundlings = PresaleBundlings, }; } } diff --git a/CarCenter/CarCenterDatabaseImplement/Models/PresaleBundling.cs b/CarCenter/CarCenterDatabaseImplement/Models/PresaleBundling.cs new file mode 100644 index 0000000..1c05446 --- /dev/null +++ b/CarCenter/CarCenterDatabaseImplement/Models/PresaleBundling.cs @@ -0,0 +1,22 @@ +using CarCenterContracts.SearchModels; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CarCenterDatabaseImplement.Models +{ + public class PresaleBundling + { + public int Id { get; set; } + [Required] + public int PresaleId { get; set; } + [Required] + public int BundlingId { get; set; } + [Required] + public virtual Presale Presale { get; set; } = new(); + public virtual Bundling Bundling { get; set; } = new(); + } +}