From 6ddf9f88b6d3fa54794feb656b43589201c69c4d Mon Sep 17 00:00:00 2001 From: Alenka Date: Sat, 6 Apr 2024 23:26:02 +0400 Subject: [PATCH 1/2] Start --- .../JewelryStoreBusinessLogic/JewelryStoreBusinessLogic.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreBusinessLogic/JewelryStoreBusinessLogic.csproj b/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreBusinessLogic/JewelryStoreBusinessLogic.csproj index b375f2f..27509f7 100644 --- a/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreBusinessLogic/JewelryStoreBusinessLogic.csproj +++ b/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreBusinessLogic/JewelryStoreBusinessLogic.csproj @@ -11,6 +11,7 @@ + -- 2.25.1 From 974d11141dbea2134414b6c28005beceade39765 Mon Sep 17 00:00:00 2001 From: Alenka Date: Sat, 6 Apr 2024 23:40:12 +0400 Subject: [PATCH 2/2] Done --- .../JewelryStore.sln | 8 +- .../JewelryStore/FormComponent.Designer.cs | 1 + .../JewelryStore/FormJewels.cs | 1 - .../JewelryStore/FormMain.Designer.cs | 52 +++--- .../JewelryStore/FormMain.cs | 5 +- .../JewelryStore/JewelryStore.csproj | 5 + .../JewelryStore/Program.cs | 2 +- .../BusinessLogics/OrderLogic.cs | 35 +--- .../JewelryStoreBusinessLogic.csproj | 1 - .../Implements/ComponentStorage.cs | 80 ++++++++ .../Implements/JewelStorage.cs | 94 ++++++++++ .../Implements/OrderStorage.cs | 90 +++++++++ .../JewelryStoreDatabase.cs | 28 +++ .../JewelryStoreDatabaseImplement.csproj | 22 +++ .../20230319120118_InitialCreate.Designer.cs | 171 ++++++++++++++++++ .../20230319120118_InitialCreate.cs | 125 +++++++++++++ .../JewelryStoreDatabaseModelSnapshot.cs | 168 +++++++++++++++++ .../Models/Component.cs | 61 +++++++ .../Models/Jewel.cs | 97 ++++++++++ .../Models/JewelComponent.cs | 23 +++ .../Models/Order.cs | 69 +++++++ .../JewelryStoreFileImplement/Models/Order.cs | 7 +- 22 files changed, 1084 insertions(+), 61 deletions(-) create mode 100644 PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/Implements/ComponentStorage.cs create mode 100644 PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/Implements/JewelStorage.cs create mode 100644 PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/Implements/OrderStorage.cs create mode 100644 PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/JewelryStoreDatabase.cs create mode 100644 PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/JewelryStoreDatabaseImplement.csproj create mode 100644 PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/Migrations/20230319120118_InitialCreate.Designer.cs create mode 100644 PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/Migrations/20230319120118_InitialCreate.cs create mode 100644 PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/Migrations/JewelryStoreDatabaseModelSnapshot.cs create mode 100644 PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/Models/Component.cs create mode 100644 PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/Models/Jewel.cs create mode 100644 PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/Models/JewelComponent.cs create mode 100644 PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/Models/Order.cs diff --git a/PIbd-23_Panina.A.D_JewelryStore/JewelryStore.sln b/PIbd-23_Panina.A.D_JewelryStore/JewelryStore.sln index dbdf571..9030a26 100644 --- a/PIbd-23_Panina.A.D_JewelryStore/JewelryStore.sln +++ b/PIbd-23_Panina.A.D_JewelryStore/JewelryStore.sln @@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JewelryStoreBusinessLogic", EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JewelryStoreListImplement", "JewelryStoreListImplement\JewelryStoreListImplement.csproj", "{12C6B5CD-CE5C-4095-91B8-53A5CB08E780}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JewelryStoreFileImplement", "JewelryStoreFileImplement\JewelryStoreFileImplement.csproj", "{12AA98A9-D1DA-4DC3-B98C-105004B8498A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JewelryStoreFileImplement", "JewelryStoreFileImplement\JewelryStoreFileImplement.csproj", "{12AA98A9-D1DA-4DC3-B98C-105004B8498A}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JewelryStoreDatabaseImplement", "JewelryStoreDatabaseImplement\JewelryStoreDatabaseImplement.csproj", "{8AA20901-3D23-4622-A0E8-1FC830923335}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {12AA98A9-D1DA-4DC3-B98C-105004B8498A}.Debug|Any CPU.Build.0 = Debug|Any CPU {12AA98A9-D1DA-4DC3-B98C-105004B8498A}.Release|Any CPU.ActiveCfg = Release|Any CPU {12AA98A9-D1DA-4DC3-B98C-105004B8498A}.Release|Any CPU.Build.0 = Release|Any CPU + {8AA20901-3D23-4622-A0E8-1FC830923335}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8AA20901-3D23-4622-A0E8-1FC830923335}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8AA20901-3D23-4622-A0E8-1FC830923335}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8AA20901-3D23-4622-A0E8-1FC830923335}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/PIbd-23_Panina.A.D_JewelryStore/JewelryStore/FormComponent.Designer.cs b/PIbd-23_Panina.A.D_JewelryStore/JewelryStore/FormComponent.Designer.cs index 182fb3a..953f3df 100644 --- a/PIbd-23_Panina.A.D_JewelryStore/JewelryStore/FormComponent.Designer.cs +++ b/PIbd-23_Panina.A.D_JewelryStore/JewelryStore/FormComponent.Designer.cs @@ -101,6 +101,7 @@ this.Controls.Add(this.labelName); this.Name = "FormComponent"; this.Text = "Компонент"; + this.Load += new System.EventHandler(this.FormComponent_Load); this.ResumeLayout(false); this.PerformLayout(); diff --git a/PIbd-23_Panina.A.D_JewelryStore/JewelryStore/FormJewels.cs b/PIbd-23_Panina.A.D_JewelryStore/JewelryStore/FormJewels.cs index 748b1f5..c770810 100644 --- a/PIbd-23_Panina.A.D_JewelryStore/JewelryStore/FormJewels.cs +++ b/PIbd-23_Panina.A.D_JewelryStore/JewelryStore/FormJewels.cs @@ -25,7 +25,6 @@ namespace JewelryStore { dataGridView.DataSource = list; dataGridView.Columns["Id"].Visible = false; - dataGridView.Columns["JewelComponents"].Visible = false; dataGridView.Columns["JewelName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; } _logger.LogInformation("Загрузка изделий"); diff --git a/PIbd-23_Panina.A.D_JewelryStore/JewelryStore/FormMain.Designer.cs b/PIbd-23_Panina.A.D_JewelryStore/JewelryStore/FormMain.Designer.cs index 1ea6c48..5deb703 100644 --- a/PIbd-23_Panina.A.D_JewelryStore/JewelryStore/FormMain.Designer.cs +++ b/PIbd-23_Panina.A.D_JewelryStore/JewelryStore/FormMain.Designer.cs @@ -32,12 +32,12 @@ this.СправочникиToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.КомпонентыToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.ИзделияToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.dataGridView = new System.Windows.Forms.DataGridView(); this.buttonCreateOrder = new System.Windows.Forms.Button(); this.buttonTakeOrderInWork = new System.Windows.Forms.Button(); this.buttonOrderReady = new System.Windows.Forms.Button(); this.buttonIssuedOrder = new System.Windows.Forms.Button(); this.buttonRefresh = new System.Windows.Forms.Button(); - this.dataGridView = new System.Windows.Forms.DataGridView(); this.menuStrip.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); this.SuspendLayout(); @@ -49,7 +49,7 @@ this.СправочникиToolStripMenuItem}); this.menuStrip.Location = new System.Drawing.Point(0, 0); this.menuStrip.Name = "menuStrip"; - this.menuStrip.Size = new System.Drawing.Size(1260, 33); + this.menuStrip.Size = new System.Drawing.Size(1121, 33); this.menuStrip.TabIndex = 0; this.menuStrip.Text = "menuStrip1"; // @@ -76,10 +76,24 @@ this.ИзделияToolStripMenuItem.Text = "Изделия"; this.ИзделияToolStripMenuItem.Click += new System.EventHandler(this.ИзделияToolStripMenuItem_Click); // + // dataGridView + // + this.dataGridView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) + | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.dataGridView.BackgroundColor = System.Drawing.SystemColors.Window; + this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dataGridView.Location = new System.Drawing.Point(12, 36); + this.dataGridView.Name = "dataGridView"; + this.dataGridView.RowHeadersWidth = 62; + this.dataGridView.RowTemplate.Height = 33; + this.dataGridView.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect; + this.dataGridView.Size = new System.Drawing.Size(851, 402); + this.dataGridView.TabIndex = 1; + // // buttonCreateOrder // - this.buttonCreateOrder.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); - this.buttonCreateOrder.Location = new System.Drawing.Point(1038, 73); + this.buttonCreateOrder.Location = new System.Drawing.Point(884, 78); this.buttonCreateOrder.Name = "buttonCreateOrder"; this.buttonCreateOrder.Size = new System.Drawing.Size(210, 34); this.buttonCreateOrder.TabIndex = 2; @@ -89,8 +103,7 @@ // // buttonTakeOrderInWork // - this.buttonTakeOrderInWork.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); - this.buttonTakeOrderInWork.Location = new System.Drawing.Point(1038, 145); + this.buttonTakeOrderInWork.Location = new System.Drawing.Point(884, 150); this.buttonTakeOrderInWork.Name = "buttonTakeOrderInWork"; this.buttonTakeOrderInWork.Size = new System.Drawing.Size(210, 34); this.buttonTakeOrderInWork.TabIndex = 3; @@ -100,8 +113,7 @@ // // buttonOrderReady // - this.buttonOrderReady.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); - this.buttonOrderReady.Location = new System.Drawing.Point(1038, 216); + this.buttonOrderReady.Location = new System.Drawing.Point(884, 221); this.buttonOrderReady.Name = "buttonOrderReady"; this.buttonOrderReady.Size = new System.Drawing.Size(210, 34); this.buttonOrderReady.TabIndex = 4; @@ -111,8 +123,7 @@ // // buttonIssuedOrder // - this.buttonIssuedOrder.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); - this.buttonIssuedOrder.Location = new System.Drawing.Point(1038, 287); + this.buttonIssuedOrder.Location = new System.Drawing.Point(884, 292); this.buttonIssuedOrder.Name = "buttonIssuedOrder"; this.buttonIssuedOrder.Size = new System.Drawing.Size(210, 34); this.buttonIssuedOrder.TabIndex = 5; @@ -122,8 +133,7 @@ // // buttonRefresh // - this.buttonRefresh.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); - this.buttonRefresh.Location = new System.Drawing.Point(1038, 356); + this.buttonRefresh.Location = new System.Drawing.Point(884, 361); this.buttonRefresh.Name = "buttonRefresh"; this.buttonRefresh.Size = new System.Drawing.Size(210, 34); this.buttonRefresh.TabIndex = 6; @@ -131,29 +141,17 @@ this.buttonRefresh.UseVisualStyleBackColor = true; this.buttonRefresh.Click += new System.EventHandler(this.ButtonRefresh_Click); // - // dataGridView - // - this.dataGridView.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill; - this.dataGridView.BackgroundColor = System.Drawing.SystemColors.Window; - this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; - this.dataGridView.Location = new System.Drawing.Point(12, 36); - this.dataGridView.Name = "dataGridView"; - this.dataGridView.RowHeadersWidth = 62; - this.dataGridView.RowTemplate.Height = 33; - this.dataGridView.Size = new System.Drawing.Size(1003, 402); - this.dataGridView.TabIndex = 7; - // // FormMain // this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(1260, 450); - this.Controls.Add(this.dataGridView); + this.ClientSize = new System.Drawing.Size(1121, 450); this.Controls.Add(this.buttonRefresh); this.Controls.Add(this.buttonIssuedOrder); this.Controls.Add(this.buttonOrderReady); this.Controls.Add(this.buttonTakeOrderInWork); this.Controls.Add(this.buttonCreateOrder); + this.Controls.Add(this.dataGridView); this.Controls.Add(this.menuStrip); this.MainMenuStrip = this.menuStrip; this.Name = "FormMain"; @@ -173,11 +171,11 @@ private ToolStripMenuItem СправочникиToolStripMenuItem; private ToolStripMenuItem КомпонентыToolStripMenuItem; private ToolStripMenuItem ИзделияToolStripMenuItem; + private DataGridView dataGridView; private Button buttonCreateOrder; private Button buttonTakeOrderInWork; private Button buttonOrderReady; private Button buttonIssuedOrder; private Button buttonRefresh; - private DataGridView dataGridView; } } \ No newline at end of file diff --git a/PIbd-23_Panina.A.D_JewelryStore/JewelryStore/FormMain.cs b/PIbd-23_Panina.A.D_JewelryStore/JewelryStore/FormMain.cs index 168154a..d35464d 100644 --- a/PIbd-23_Panina.A.D_JewelryStore/JewelryStore/FormMain.cs +++ b/PIbd-23_Panina.A.D_JewelryStore/JewelryStore/FormMain.cs @@ -25,7 +25,6 @@ namespace JewelryStore private void LoadData() { - _logger.LogInformation("Загрузка заказов"); try { var list = _orderLogic.ReadList(null); @@ -33,12 +32,12 @@ namespace JewelryStore { dataGridView.DataSource = list; dataGridView.Columns["JewelId"].Visible = false; - dataGridView.Columns["JewelName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; } + _logger.LogInformation("Загрузка заказов"); } catch (Exception ex) { - _logger.LogError(ex, "Ошибка загрузки изделий"); + _logger.LogError(ex, "Ошибка загрузки заказов"); MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } diff --git a/PIbd-23_Panina.A.D_JewelryStore/JewelryStore/JewelryStore.csproj b/PIbd-23_Panina.A.D_JewelryStore/JewelryStore/JewelryStore.csproj index 69cae25..3f519ab 100644 --- a/PIbd-23_Panina.A.D_JewelryStore/JewelryStore/JewelryStore.csproj +++ b/PIbd-23_Panina.A.D_JewelryStore/JewelryStore/JewelryStore.csproj @@ -13,6 +13,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -22,6 +26,7 @@ + diff --git a/PIbd-23_Panina.A.D_JewelryStore/JewelryStore/Program.cs b/PIbd-23_Panina.A.D_JewelryStore/JewelryStore/Program.cs index 26ae8eb..8d5a0fc 100644 --- a/PIbd-23_Panina.A.D_JewelryStore/JewelryStore/Program.cs +++ b/PIbd-23_Panina.A.D_JewelryStore/JewelryStore/Program.cs @@ -1,6 +1,6 @@ using JewelryStoreContracts.BusinessLogicsContracts; using JewelryStoreContracts.StoragesContracts; -using JewelryStoreFileImplement.Implements; +using JewelryStoreDatabaseImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; diff --git a/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreBusinessLogic/BusinessLogics/OrderLogic.cs b/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreBusinessLogic/BusinessLogics/OrderLogic.cs index 59d3041..aeba2c9 100644 --- a/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreBusinessLogic/BusinessLogics/OrderLogic.cs @@ -33,41 +33,26 @@ namespace JewelryStoreBusinessLogic.BusinessLogics } public bool DeliveryOrder(OrderBindingModel model) - { - return ChangeStatus(model, OrderStatus.Выдан); - } - - private bool ChangeStatus(OrderBindingModel model, OrderStatus requiredStatus) { CheckModel(model, false); - var element = _orderStorage.GetElement(new OrderSearchModel() + var element = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id }); if (element == null) { - throw new ArgumentNullException(nameof(element)); + _logger.LogWarning("Read operation failed"); + return false; } - model.DateCreate = element.DateCreate; - model.JewelId = element.JewelId; - model.DateImplement = element.DateImplement; - model.Status = element.Status; - model.Count = element.Count; - model.Sum = element.Sum; - if (requiredStatus - model.Status == 1) + if (element.Status != OrderStatus.Готов) { - model.Status = requiredStatus; - if (model.Status == OrderStatus.Выдан) - model.DateImplement = DateTime.Now; - if (_orderStorage.Update(model) == null) - { - _logger.LogWarning("Update operation failed"); - return false; - } - return true; + _logger.LogWarning("Status change operation failed"); + throw new InvalidOperationException("Заказ должен быть переведен в статус готовности перед выдачей!"); } - _logger.LogWarning("Changing status operation faled: Current-{Status}:required-{requiredStatus}.", model.Status, requiredStatus); - throw new ArgumentException($"Невозможно присвоить статус {requiredStatus} заказу с текущим статусом {model.Status}"); + model.Status = OrderStatus.Выдан; + model.DateImplement = DateTime.Now; + _orderStorage.Update(model); + return true; } public bool FinishOrder(OrderBindingModel model) diff --git a/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreBusinessLogic/JewelryStoreBusinessLogic.csproj b/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreBusinessLogic/JewelryStoreBusinessLogic.csproj index 27509f7..b375f2f 100644 --- a/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreBusinessLogic/JewelryStoreBusinessLogic.csproj +++ b/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreBusinessLogic/JewelryStoreBusinessLogic.csproj @@ -11,7 +11,6 @@ - diff --git a/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/Implements/ComponentStorage.cs b/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..a77bfb3 --- /dev/null +++ b/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,80 @@ +using JewelryStoreContracts.BindingModels; +using JewelryStoreContracts.SearchModels; +using JewelryStoreContracts.StoragesContracts; +using JewelryStoreContracts.ViewModels; +using JewelryStoreDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JewelryStoreDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new JewelryStoreDatabase(); + return context.Components.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new JewelryStoreDatabase(); + return context.Components.Where(x => x.ComponentName.Contains(model.ComponentName)).Select(x => x.GetViewModel).ToList(); + } + + public ComponentViewModel? GetElement(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue) + { + return null; + } + using var context = new JewelryStoreDatabase(); + return context.Components.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName) || + (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public ComponentViewModel? Insert(ComponentBindingModel model) + { + var newComponent = Component.Create(model); + if (newComponent == null) + { + return null; + } + using var context = new JewelryStoreDatabase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new JewelryStoreDatabase(); + var component = context.Components.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } + public ComponentViewModel? Delete(ComponentBindingModel model) + { + using var context = new JewelryStoreDatabase(); + var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/Implements/JewelStorage.cs b/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/Implements/JewelStorage.cs new file mode 100644 index 0000000..edfbd8a --- /dev/null +++ b/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/Implements/JewelStorage.cs @@ -0,0 +1,94 @@ +using JewelryStoreContracts.BindingModels; +using JewelryStoreContracts.SearchModels; +using JewelryStoreContracts.StoragesContracts; +using JewelryStoreContracts.ViewModels; +using JewelryStoreDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JewelryStoreDatabaseImplement.Implements +{ + public class JewelStorage : IJewelStorage + { + public List GetFullList() + { + using var context = new JewelryStoreDatabase(); + return context.Jewels.Include(x => x.Components).ThenInclude(x => x.Component).ToList().Select(x => x.GetViewModel).ToList(); + } + public List GetFilteredList(JewelSearchModel model) + { + if (string.IsNullOrEmpty(model.JewelName)) + { + return new(); + } + using var context = new JewelryStoreDatabase(); + return context.Jewels.Include(x => x.Components).ThenInclude(x => x.Component) + .Where(x => x.JewelName.Contains(model.JewelName)).ToList().Select(x => x.GetViewModel).ToList(); + } + public JewelViewModel? GetElement(JewelSearchModel model) + { + if (string.IsNullOrEmpty(model.JewelName) && !model.Id.HasValue) + { + return null; + } + using var context = new JewelryStoreDatabase(); + return context.Jewels.Include(x => x.Components).ThenInclude(x => x.Component).FirstOrDefault(x => (!string.IsNullOrEmpty(model.JewelName) && + x.JewelName == model.JewelName) ||(model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + public JewelViewModel? Insert(JewelBindingModel model) + { + using var context = new JewelryStoreDatabase(); + var newProduct = Jewel.Create(context, model); + if (newProduct == null) + { + return null; + } + context.Jewels.Add(newProduct); + context.SaveChanges(); + return newProduct.GetViewModel; + } + public JewelViewModel? Update(JewelBindingModel model) + { + using var context = new JewelryStoreDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var product = context.Jewels.FirstOrDefault(rec => + rec.Id == model.Id); + if (product == null) + { + return null; + } + product.Update(model); + context.SaveChanges(); + product.UpdateComponents(context, model); + transaction.Commit(); + return product.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public JewelViewModel? Delete(JewelBindingModel model) + { + using var context = new JewelryStoreDatabase(); + var element = context.Jewels + .Include(x => x.Components) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Jewels.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + } +} diff --git a/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/Implements/OrderStorage.cs b/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..0a159f4 --- /dev/null +++ b/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,90 @@ +using JewelryStoreContracts.BindingModels; +using JewelryStoreContracts.SearchModels; +using JewelryStoreContracts.StoragesContracts; +using JewelryStoreContracts.ViewModels; +using JewelryStoreDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JewelryStoreDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public List GetFullList() + { + using var context = new JewelryStoreDatabase(); + return context.Orders + .Select(x => AccessJewelStorage(x.GetViewModel, context)) + .ToList(); + } + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new JewelryStoreDatabase(); + return context.Orders + .Where(x => x.Id == model.Id) + .Select(x => AccessJewelStorage(x.GetViewModel, context)) + .ToList(); + } + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new JewelryStoreDatabase(); + return AccessJewelStorage(context.Orders + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) + ?.GetViewModel, context); + } + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + using var context = new JewelryStoreDatabase(); + context.Orders.Add(newOrder); + context.SaveChanges(); + return AccessJewelStorage(newOrder.GetViewModel, context); + } + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new JewelryStoreDatabase(); + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + return AccessJewelStorage(order.GetViewModel, context); + } + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new JewelryStoreDatabase(); + var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + return AccessJewelStorage(element.GetViewModel, context); + } + return null; + } + static OrderViewModel AccessJewelStorage(OrderViewModel model, JewelryStoreDatabase context) + { + if (model == null) return model; + string? jewelName = context.Jewels.FirstOrDefault(x => x.Id == model.JewelId)?.JewelName; + if (jewelName != null) model.JewelName = jewelName; + return model; + } + } +} diff --git a/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/JewelryStoreDatabase.cs b/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/JewelryStoreDatabase.cs new file mode 100644 index 0000000..fb91643 --- /dev/null +++ b/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/JewelryStoreDatabase.cs @@ -0,0 +1,28 @@ +using JewelryStoreDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JewelryStoreDatabaseImplement +{ + public class JewelryStoreDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder + optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=JewelryStoreDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Components { set; get; } + public virtual DbSet Jewels { set; get; } + public virtual DbSet JewelComponents { set; get; } + public virtual DbSet Orders { set; get; } + } + +} diff --git a/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/JewelryStoreDatabaseImplement.csproj b/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/JewelryStoreDatabaseImplement.csproj new file mode 100644 index 0000000..a4777d7 --- /dev/null +++ b/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/JewelryStoreDatabaseImplement.csproj @@ -0,0 +1,22 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + diff --git a/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/Migrations/20230319120118_InitialCreate.Designer.cs b/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/Migrations/20230319120118_InitialCreate.Designer.cs new file mode 100644 index 0000000..05525da --- /dev/null +++ b/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/Migrations/20230319120118_InitialCreate.Designer.cs @@ -0,0 +1,171 @@ +// +using System; +using JewelryStoreDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace JewelryStoreDatabaseImplement.Migrations +{ + [DbContext(typeof(JewelryStoreDatabase))] + [Migration("20230319120118_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.4") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Jewel", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("JewelName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Jewels"); + }); + + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.JewelComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("JewelId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("JewelId"); + + b.ToTable("JewelComponents"); + }); + + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("JewelId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("JewelId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.JewelComponent", b => + { + b.HasOne("JewelryStoreDatabaseImplement.Models.Component", "Component") + .WithMany("JewelComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("JewelryStoreDatabaseImplement.Models.Jewel", "Jewel") + .WithMany("Components") + .HasForeignKey("JewelId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Jewel"); + }); + + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Order", b => + { + b.HasOne("JewelryStoreDatabaseImplement.Models.Jewel", "Jewel") + .WithMany("Orders") + .HasForeignKey("JewelId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Jewel"); + }); + + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Component", b => + { + b.Navigation("JewelComponents"); + }); + + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Jewel", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/Migrations/20230319120118_InitialCreate.cs b/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/Migrations/20230319120118_InitialCreate.cs new file mode 100644 index 0000000..60f3aa3 --- /dev/null +++ b/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/Migrations/20230319120118_InitialCreate.cs @@ -0,0 +1,125 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace JewelryStoreDatabaseImplement.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Components", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ComponentName = table.Column(type: "nvarchar(max)", nullable: false), + Cost = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Components", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Jewels", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + JewelName = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Jewels", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "JewelComponents", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + JewelId = table.Column(type: "int", nullable: false), + ComponentId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_JewelComponents", x => x.Id); + table.ForeignKey( + name: "FK_JewelComponents_Components_ComponentId", + column: x => x.ComponentId, + principalTable: "Components", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_JewelComponents_Jewels_JewelId", + column: x => x.JewelId, + principalTable: "Jewels", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + JewelId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false), + Sum = table.Column(type: "float", nullable: false), + Status = table.Column(type: "int", nullable: false), + DateCreate = table.Column(type: "datetime2", nullable: false), + DateImplement = table.Column(type: "datetime2", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Orders", x => x.Id); + table.ForeignKey( + name: "FK_Orders_Jewels_JewelId", + column: x => x.JewelId, + principalTable: "Jewels", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_JewelComponents_ComponentId", + table: "JewelComponents", + column: "ComponentId"); + + migrationBuilder.CreateIndex( + name: "IX_JewelComponents_JewelId", + table: "JewelComponents", + column: "JewelId"); + + migrationBuilder.CreateIndex( + name: "IX_Orders_JewelId", + table: "Orders", + column: "JewelId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "JewelComponents"); + + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "Components"); + + migrationBuilder.DropTable( + name: "Jewels"); + } + } +} diff --git a/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/Migrations/JewelryStoreDatabaseModelSnapshot.cs b/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/Migrations/JewelryStoreDatabaseModelSnapshot.cs new file mode 100644 index 0000000..860b574 --- /dev/null +++ b/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/Migrations/JewelryStoreDatabaseModelSnapshot.cs @@ -0,0 +1,168 @@ +// +using System; +using JewelryStoreDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace JewelryStoreDatabaseImplement.Migrations +{ + [DbContext(typeof(JewelryStoreDatabase))] + partial class JewelryStoreDatabaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.4") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Jewel", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("JewelName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Jewels"); + }); + + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.JewelComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("JewelId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("JewelId"); + + b.ToTable("JewelComponents"); + }); + + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("JewelId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("JewelId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.JewelComponent", b => + { + b.HasOne("JewelryStoreDatabaseImplement.Models.Component", "Component") + .WithMany("JewelComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("JewelryStoreDatabaseImplement.Models.Jewel", "Jewel") + .WithMany("Components") + .HasForeignKey("JewelId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Jewel"); + }); + + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Order", b => + { + b.HasOne("JewelryStoreDatabaseImplement.Models.Jewel", "Jewel") + .WithMany("Orders") + .HasForeignKey("JewelId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Jewel"); + }); + + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Component", b => + { + b.Navigation("JewelComponents"); + }); + + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Jewel", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/Models/Component.cs b/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..aef391a --- /dev/null +++ b/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/Models/Component.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using JewelryStoreContracts.BindingModels; +using JewelryStoreContracts.ViewModels; +using JewelryStoreDataModels.Models; + +namespace JewelryStoreDatabaseImplement.Models +{ + public class Component : IComponentModel + { + public int Id { get; private set; } + [Required] + public string ComponentName { get; private set; } = string.Empty; + [Required] + public double Cost { get; set; } + [ForeignKey("ComponentId")] + public virtual List JewelComponents { get; set; } = new(); + public static Component? Create(ComponentBindingModel model) + { + if (model == null) + { + return null; + } + return new Component() + { + Id = model.Id, + ComponentName = model.ComponentName, + Cost = model.Cost + }; + } + public static Component Create(ComponentViewModel model) + { + return new Component + { + Id = model.Id, + ComponentName = model.ComponentName, + Cost = model.Cost + }; + } + public void Update(ComponentBindingModel model) + { + if (model == null) + { + return; + } + ComponentName = model.ComponentName; + Cost = model.Cost; + } + public ComponentViewModel GetViewModel => new() + { + Id = Id, + ComponentName = ComponentName, + Cost = Cost + }; + } +} diff --git a/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/Models/Jewel.cs b/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/Models/Jewel.cs new file mode 100644 index 0000000..79dffdb --- /dev/null +++ b/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/Models/Jewel.cs @@ -0,0 +1,97 @@ +using JewelryStoreDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using JewelryStoreContracts.BindingModels; +using JewelryStoreContracts.ViewModels; + +namespace JewelryStoreDatabaseImplement.Models +{ + public class Jewel : IJewelModel + { + public int Id { get; set; } + [Required] + public string JewelName { get; set; } = string.Empty; + [Required] + public double Price { get; set; } + private Dictionary? _jewelComponents = null; + [NotMapped] + public Dictionary JewelComponents + { + get + { + if (_jewelComponents == null) + { + _jewelComponents = Components.ToDictionary(recJC => recJC.ComponentId, recJC =>(recJC.Component as IComponentModel, recJC.Count)); + } + return _jewelComponents; + } + } + [ForeignKey("JewelId")] + public virtual List Components { get; set; } = new(); + [ForeignKey("JewelId")] + public virtual List Orders { get; set; } = new(); + + public static Jewel Create(JewelryStoreDatabase context, JewelBindingModel model) + { + return new Jewel() + { + Id = model.Id, + JewelName = model.JewelName, + Price = model.Price, + Components = model.JewelComponents.Select(x => new JewelComponent + { + Component = context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + + public void Update(JewelBindingModel model) + { + JewelName = model.JewelName; + Price = model.Price; + } + + public JewelViewModel GetViewModel => new() + { + Id = Id, + JewelName = JewelName, + Price = Price, + JewelComponents = JewelComponents + }; + + public void UpdateComponents(JewelryStoreDatabase context, JewelBindingModel model) + { + var jewelComponents = context.JewelComponents.Where(rec => rec.JewelId == model.Id).ToList(); + if (jewelComponents != null && jewelComponents.Count > 0) + { // удалили те, которых нет в модели + context.JewelComponents.RemoveRange(jewelComponents.Where(rec => !model.JewelComponents.ContainsKey(rec.ComponentId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateComponent in jewelComponents) + { + updateComponent.Count = model.JewelComponents[updateComponent.ComponentId].Item2; + model.JewelComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + var jewel = context.Jewels.First(x => x.Id == Id); + foreach (var jc in model.JewelComponents) + { + context.JewelComponents.Add(new JewelComponent + { + Jewel = jewel, + Component = context.Components.First(x => x.Id == jc.Key), + Count = jc.Value.Item2 + }); + context.SaveChanges(); + } + _jewelComponents = null; + } + } +} diff --git a/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/Models/JewelComponent.cs b/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/Models/JewelComponent.cs new file mode 100644 index 0000000..27dc649 --- /dev/null +++ b/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/Models/JewelComponent.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JewelryStoreDatabaseImplement.Models +{ + public class JewelComponent + { + public int Id { get; set; } + [Required] + public int JewelId { get; set; } + [Required] + public int ComponentId { get; set; } + [Required] + public int Count { get; set; } + public virtual Component Component { get; set; } = new(); + public virtual Jewel Jewel { get; set; } = new(); + } + +} diff --git a/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/Models/Order.cs b/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..af1be1e --- /dev/null +++ b/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreDatabaseImplement/Models/Order.cs @@ -0,0 +1,69 @@ +using JewelryStoreContracts.BindingModels; +using JewelryStoreContracts.ViewModels; +using JewelryStoreDataModels.Enums; +using JewelryStoreDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JewelryStoreDatabaseImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + [Required] + public int JewelId { get; set; } + [Required] + public int Count { get; set; } + [Required] + public double Sum { get; set; } + [Required] + public OrderStatus Status { get; set; } + [Required] + public DateTime DateCreate { get; set; } + public DateTime? DateImplement { get; set; } + public Jewel Jewel { get; set; } + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + JewelId = model.JewelId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement + }; + } + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + Status = model.Status; + if (model.DateImplement.HasValue) DateImplement = model.DateImplement; + } + public OrderViewModel GetViewModel => new() + { + Id = Id, + JewelId = JewelId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement + }; + + } +} diff --git a/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreFileImplement/Models/Order.cs b/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreFileImplement/Models/Order.cs index da2fa9d..df13813 100644 --- a/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreFileImplement/Models/Order.cs +++ b/PIbd-23_Panina.A.D_JewelryStore/JewelryStoreFileImplement/Models/Order.cs @@ -64,9 +64,12 @@ namespace JewelryStoreFileImplement.Models { return; } - + JewelId = model.JewelId; + Count = model.Count; + Sum = model.Sum; Status = model.Status; - if (model.Status == OrderStatus.Выдан) DateImplement = model.DateImplement; + DateCreate = model.DateCreate; + DateImplement = model.DateImplement; } public OrderViewModel GetViewModel => new() -- 2.25.1