diff --git a/AbstractShopBusinessLogic/BusinessLogics/OrderLogic.cs b/AbstractShopBusinessLogic/BusinessLogics/OrderLogic.cs index 1466f90..22ffbf7 100644 --- a/AbstractShopBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/AbstractShopBusinessLogic/BusinessLogics/OrderLogic.cs @@ -3,6 +3,7 @@ using DinerContracts.BusinessLogicsContracts; using DinerContracts.SearchModels; using DinerContracts.StoragesContracts; using DinerContracts.ViewModels; +using DinerDataModels.Models; using DinerDataModels.Enum; using Microsoft.Extensions.Logging; @@ -12,10 +13,15 @@ namespace DinerBusinessLogic.BusinessLogics { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; - public OrderLogic(ILogger logger, IOrderStorage orderStorage) + private readonly IShopStorage _shopStorage; + private readonly IShopLogic _shopLogic; + + public OrderLogic(ILogger logger, IOrderStorage orderStorage, IShopStorage shopStorage) { - _logger = logger; _orderStorage = orderStorage; + _logger = logger; + _shopStorage = shopStorage; + } public List? ReadList(OrderSearchModel? model) { @@ -29,6 +35,7 @@ namespace DinerBusinessLogic.BusinessLogics _logger.LogInformation("ReadList. Count:{Count}", list.Count); return list; } + public bool CreateOrder(OrderBindingModel model) { CheckModel(model); @@ -41,6 +48,7 @@ namespace DinerBusinessLogic.BusinessLogics } return true; } + public bool TakeOrderInWork(OrderBindingModel model) { return ChangeStatus(model, OrderStatus.Выполняется); @@ -51,8 +59,25 @@ namespace DinerBusinessLogic.BusinessLogics } public bool DeliveryOrder(OrderBindingModel model) { + var order = _orderStorage.GetElement(new OrderSearchModel + { + Id = model.Id, + }); + if (order == null) + { + throw new ArgumentNullException(nameof(order)); + } + if (!_shopStorage.RestockingShops(new SupplyBindingModel + { + SnackId = order.SnackId, + Count = order.Count + })) + { + throw new ArgumentException("Недостаточно места"); + } return ChangeStatus(model, OrderStatus.Выдан); } + private void CheckModel(OrderBindingModel model, bool withParams = true) { if (model == null) diff --git a/AbstractShopBusinessLogic/BusinessLogics/ShopLogic.cs b/AbstractShopBusinessLogic/BusinessLogics/ShopLogic.cs index 6f35a4c..75076cd 100644 --- a/AbstractShopBusinessLogic/BusinessLogics/ShopLogic.cs +++ b/AbstractShopBusinessLogic/BusinessLogics/ShopLogic.cs @@ -157,5 +157,20 @@ namespace DinerBusinessLogic.BusinessLogics throw new InvalidOperationException("Магазин с таким названием уже есть"); } } + public bool Sale(SupplySearchModel model) + { + if (!model.SnackId.HasValue || !model.Count.HasValue) + { + return false; + } + _logger.LogInformation("Check snack count in all shops"); + if (_shopStorage.Sale(model)) + { + _logger.LogInformation("Selling sucsess"); + return true; + } + _logger.LogInformation("Selling failed"); + return false; + } } } diff --git a/AbstractShopContracts/BindingModels/ShopBindingModel.cs b/AbstractShopContracts/BindingModels/ShopBindingModel.cs index d4d5088..c347698 100644 --- a/AbstractShopContracts/BindingModels/ShopBindingModel.cs +++ b/AbstractShopContracts/BindingModels/ShopBindingModel.cs @@ -14,5 +14,6 @@ namespace DinerContracts.BindingModels public string Adress { get; set; } = string.Empty; public DateTime OpeningDate { get; set; } = DateTime.Now; public Dictionary ShopSnacks { get; set; } = new(); + public int SnackMaxCount { get; set; } } } \ No newline at end of file diff --git a/AbstractShopContracts/BusinessLogicsContracts/IShopLogic.cs b/AbstractShopContracts/BusinessLogicsContracts/IShopLogic.cs index 1d540ad..2adad26 100644 --- a/AbstractShopContracts/BusinessLogicsContracts/IShopLogic.cs +++ b/AbstractShopContracts/BusinessLogicsContracts/IShopLogic.cs @@ -17,5 +17,6 @@ namespace DinerContracts.BusinessLogicsContracts bool Update(ShopBindingModel model); bool Delete(ShopBindingModel model); bool MakeSupply(SupplyBindingModel model); + bool Sale(SupplySearchModel model); } } \ No newline at end of file diff --git a/AbstractShopContracts/SearchModels/SupplySearchModel.cs b/AbstractShopContracts/SearchModels/SupplySearchModel.cs new file mode 100644 index 0000000..67d40b6 --- /dev/null +++ b/AbstractShopContracts/SearchModels/SupplySearchModel.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DinerContracts.SearchModels +{ + public class SupplySearchModel + { + public int? SnackId { get; set; } + public int? Count { get; set; } + } +} diff --git a/AbstractShopContracts/StoragesContracts/IShopStorage.cs b/AbstractShopContracts/StoragesContracts/IShopStorage.cs index 9c62afa..c5d4ae2 100644 --- a/AbstractShopContracts/StoragesContracts/IShopStorage.cs +++ b/AbstractShopContracts/StoragesContracts/IShopStorage.cs @@ -17,5 +17,7 @@ namespace DinerContracts.StoragesContracts ShopViewModel? Insert(ShopBindingModel model); ShopViewModel? Update(ShopBindingModel model); ShopViewModel? Delete(ShopBindingModel model); + bool Sale(SupplySearchModel model); + bool RestockingShops(SupplyBindingModel model); } } \ No newline at end of file diff --git a/AbstractShopContracts/ViewModels/ShopViewModel.cs b/AbstractShopContracts/ViewModels/ShopViewModel.cs index 85331fc..7684fe8 100644 --- a/AbstractShopContracts/ViewModels/ShopViewModel.cs +++ b/AbstractShopContracts/ViewModels/ShopViewModel.cs @@ -18,5 +18,7 @@ namespace DinerContracts.ViewModels [DisplayName("Дата открытия")] public DateTime OpeningDate { get; set; } public Dictionary ShopSnacks { get; set; } = new(); + [DisplayName("Вместимость")] + public int SnackMaxCount { get; set; } } } \ No newline at end of file diff --git a/AbstractShopDataModels/Models/IShopModel.cs b/AbstractShopDataModels/Models/IShopModel.cs index e236e00..3e48cc7 100644 --- a/AbstractShopDataModels/Models/IShopModel.cs +++ b/AbstractShopDataModels/Models/IShopModel.cs @@ -14,5 +14,6 @@ namespace DinerDataModels.Models string Adress { get; } DateTime OpeningDate { get; } Dictionary ShopSnacks { get; } + public int SnackMaxCount { get; } } } \ No newline at end of file diff --git a/Diner/AbstractShopListImplement/DinerListImplement.csproj b/Diner/AbstractShopListImplement/DinerListImplement.csproj index 51f4155..f83c53f 100644 --- a/Diner/AbstractShopListImplement/DinerListImplement.csproj +++ b/Diner/AbstractShopListImplement/DinerListImplement.csproj @@ -1,4 +1,4 @@ - + net6.0 diff --git a/Diner/AbstractShopListImplement/Implements/ShopStorage.cs b/Diner/AbstractShopListImplement/Implements/ShopStorage.cs index 7095fc8..d9bee3f 100644 --- a/Diner/AbstractShopListImplement/Implements/ShopStorage.cs +++ b/Diner/AbstractShopListImplement/Implements/ShopStorage.cs @@ -109,5 +109,14 @@ namespace DinerListImplement.Implements } return null; } + public bool Sale(SupplySearchModel model) + { + throw new NotImplementedException(); + } + + public bool RestockingShops(SupplyBindingModel model) + { + throw new NotImplementedException(); + } } } \ No newline at end of file diff --git a/Diner/AbstractShopListImplement/Models/Order.cs b/Diner/AbstractShopListImplement/Models/Order.cs index 2423aaf..409e3c0 100644 --- a/Diner/AbstractShopListImplement/Models/Order.cs +++ b/Diner/AbstractShopListImplement/Models/Order.cs @@ -39,14 +39,9 @@ namespace DinerListImplement.Models { return; } - Id = model.Id; - SnackID = model.SnackId; - SnackName = model.SnackName; - Count = model.Count; - Sum = model.Sum; Status = model.Status; - DateCreate = model.DateCreate; - DateImplement = model.DateImplement; + if (model.Status == OrderStatus.Выдан) DateImplement = model.DateImplement; + if (model.Status == OrderStatus.Выдан) DateImplement = model.DateImplement; } public OrderViewModel GetViewModel => new() { diff --git a/Diner/AbstractShopListImplement/Models/Shop.cs b/Diner/AbstractShopListImplement/Models/Shop.cs index 8a666ee..4fcebf8 100644 --- a/Diner/AbstractShopListImplement/Models/Shop.cs +++ b/Diner/AbstractShopListImplement/Models/Shop.cs @@ -16,6 +16,7 @@ namespace DinerListImplement.Models public string Adress { get; private set; } = string.Empty; public DateTime OpeningDate { get; private set; } public Dictionary ShopSnacks { get; private set; } = new(); + public int SnackMaxCount { get; private set; } public static Shop? Create(ShopBindingModel? model) { @@ -28,7 +29,8 @@ namespace DinerListImplement.Models Id = model.Id, ShopName = model.ShopName, Adress = model.Adress, - OpeningDate = model.OpeningDate + OpeningDate = model.OpeningDate, + SnackMaxCount = model.SnackMaxCount, }; } @@ -41,6 +43,7 @@ namespace DinerListImplement.Models ShopName = model.ShopName; Adress = model.Adress; OpeningDate = model.OpeningDate; + SnackMaxCount = model.SnackMaxCount; } public ShopViewModel GetViewModel => new() @@ -49,7 +52,8 @@ namespace DinerListImplement.Models ShopName = ShopName, Adress = Adress, OpeningDate = OpeningDate, - ShopSnacks = ShopSnacks + ShopSnacks = ShopSnacks, + SnackMaxCount = SnackMaxCount, }; } } \ No newline at end of file diff --git a/Diner/Diner.sln b/Diner/Diner.sln index be21916..6cb40de 100644 --- a/Diner/Diner.sln +++ b/Diner/Diner.sln @@ -13,6 +13,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DinerBusinessLogic", "..\Ab EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DinerListImplement", "AbstractShopListImplement\DinerListImplement.csproj", "{A24E7474-4B43-4E81-A6BF-2B7323D5C26A}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DinerFileImplement", "DinerFileImplement\DinerFileImplement.csproj", "{13294C1E-C8DF-4E4B-B645-A61900A797EB}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -39,6 +41,10 @@ Global {A24E7474-4B43-4E81-A6BF-2B7323D5C26A}.Debug|Any CPU.Build.0 = Debug|Any CPU {A24E7474-4B43-4E81-A6BF-2B7323D5C26A}.Release|Any CPU.ActiveCfg = Release|Any CPU {A24E7474-4B43-4E81-A6BF-2B7323D5C26A}.Release|Any CPU.Build.0 = Release|Any CPU + {13294C1E-C8DF-4E4B-B645-A61900A797EB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {13294C1E-C8DF-4E4B-B645-A61900A797EB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {13294C1E-C8DF-4E4B-B645-A61900A797EB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {13294C1E-C8DF-4E4B-B645-A61900A797EB}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Diner/Diner/DinerView.csproj b/Diner/Diner/DinerView.csproj index 9c3757b..a7b8bd0 100644 --- a/Diner/Diner/DinerView.csproj +++ b/Diner/Diner/DinerView.csproj @@ -9,12 +9,18 @@ + + + + + + diff --git a/Diner/Diner/FormMain.Designer.cs b/Diner/Diner/FormMain.Designer.cs index 2f7fe59..cd25210 100644 --- a/Diner/Diner/FormMain.Designer.cs +++ b/Diner/Diner/FormMain.Designer.cs @@ -38,9 +38,10 @@ toolStripLabel1 = new ToolStripDropDownButton(); componentsToolStripMenuItem = new ToolStripMenuItem(); snacksToolStripMenuItem = new ToolStripMenuItem(); + магазиныToolStripMenuItem = new ToolStripMenuItem(); toolStripDropDownButton1 = new ToolStripDropDownButton(); поставкаToolStripMenuItem = new ToolStripMenuItem(); - магазиныToolStripMenuItem = new ToolStripMenuItem(); + SellToolStripMenuItem = new ToolStripMenuItem(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); toolStrip1.SuspendLayout(); SuspendLayout(); @@ -126,21 +127,28 @@ // componentsToolStripMenuItem // componentsToolStripMenuItem.Name = "componentsToolStripMenuItem"; - componentsToolStripMenuItem.Size = new Size(224, 26); + componentsToolStripMenuItem.Size = new Size(182, 26); componentsToolStripMenuItem.Text = "Компоненты"; componentsToolStripMenuItem.Click += ComponentToolStripMenuItem_Click; // // snacksToolStripMenuItem // snacksToolStripMenuItem.Name = "snacksToolStripMenuItem"; - snacksToolStripMenuItem.Size = new Size(224, 26); + snacksToolStripMenuItem.Size = new Size(182, 26); snacksToolStripMenuItem.Text = "Закуски"; snacksToolStripMenuItem.Click += ProductToolStripMenuItem_Click; // + // магазиныToolStripMenuItem + // + магазиныToolStripMenuItem.Name = "магазиныToolStripMenuItem"; + магазиныToolStripMenuItem.Size = new Size(182, 26); + магазиныToolStripMenuItem.Text = "Магазины"; + магазиныToolStripMenuItem.Click += shopsToolStripMenuItem_Click; + // // toolStripDropDownButton1 // toolStripDropDownButton1.DisplayStyle = ToolStripItemDisplayStyle.Text; - toolStripDropDownButton1.DropDownItems.AddRange(new ToolStripItem[] { поставкаToolStripMenuItem }); + toolStripDropDownButton1.DropDownItems.AddRange(new ToolStripItem[] { поставкаToolStripMenuItem, SellToolStripMenuItem }); toolStripDropDownButton1.ImageTransparentColor = Color.Magenta; toolStripDropDownButton1.Name = "toolStripDropDownButton1"; toolStripDropDownButton1.Size = new Size(95, 24); @@ -153,12 +161,12 @@ поставкаToolStripMenuItem.Text = "Поставка"; поставкаToolStripMenuItem.Click += transactionToolStripMenuItem_Click; // - // магазиныToolStripMenuItem + // SellToolStripMenuItem // - магазиныToolStripMenuItem.Name = "магазиныToolStripMenuItem"; - магазиныToolStripMenuItem.Size = new Size(224, 26); - магазиныToolStripMenuItem.Text = "Магазины"; - магазиныToolStripMenuItem.Click += shopsToolStripMenuItem_Click; + SellToolStripMenuItem.Name = "SellToolStripMenuItem"; + SellToolStripMenuItem.Size = new Size(224, 26); + SellToolStripMenuItem.Text = "Продажа"; + SellToolStripMenuItem.Click += SellToolStripMenuItem_Click; // // FormMain // @@ -197,5 +205,6 @@ private ToolStripMenuItem магазиныToolStripMenuItem; private ToolStripDropDownButton toolStripDropDownButton1; private ToolStripMenuItem поставкаToolStripMenuItem; + private ToolStripMenuItem SellToolStripMenuItem; } } \ No newline at end of file diff --git a/Diner/Diner/FormMain.cs b/Diner/Diner/FormMain.cs index b7fe7b9..ade35fa 100644 --- a/Diner/Diner/FormMain.cs +++ b/Diner/Diner/FormMain.cs @@ -180,5 +180,13 @@ namespace Diner form.ShowDialog(); } } + private void SellToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormSellSnack)); + if (service is FormSellSnack form) + { + form.ShowDialog(); + } + } } } diff --git a/Diner/Diner/FormSellSnack.Designer.cs b/Diner/Diner/FormSellSnack.Designer.cs new file mode 100644 index 0000000..8145f17 --- /dev/null +++ b/Diner/Diner/FormSellSnack.Designer.cs @@ -0,0 +1,119 @@ +namespace DinerView +{ + partial class FormSellSnack + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + labelSnack = new Label(); + comboBoxSnack = new ComboBox(); + labelCount = new Label(); + textBoxCount = new TextBox(); + buttonSell = new Button(); + buttonCancel = new Button(); + SuspendLayout(); + // + // labelSnack + // + labelSnack.AutoSize = true; + labelSnack.Location = new Point(12, 14); + labelSnack.Name = "labelSnack"; + labelSnack.Size = new Size(68, 20); + labelSnack.TabIndex = 0; + labelSnack.Text = "Закуска: "; + // + // comboBoxSnack + // + comboBoxSnack.FormattingEnabled = true; + comboBoxSnack.Location = new Point(115, 11); + comboBoxSnack.Name = "comboBoxSnack"; + comboBoxSnack.Size = new Size(239, 28); + comboBoxSnack.TabIndex = 1; + // + // labelCount + // + labelCount.AutoSize = true; + labelCount.Location = new Point(12, 55); + labelCount.Name = "labelCount"; + labelCount.Size = new Size(97, 20); + labelCount.TabIndex = 2; + labelCount.Text = "Количество: "; + // + // textBoxCount + // + textBoxCount.Location = new Point(115, 52); + textBoxCount.Name = "textBoxCount"; + textBoxCount.Size = new Size(239, 27); + textBoxCount.TabIndex = 3; + // + // buttonSell + // + buttonSell.Location = new Point(128, 99); + buttonSell.Name = "buttonSell"; + buttonSell.Size = new Size(94, 29); + buttonSell.TabIndex = 4; + buttonSell.Text = "Продать"; + buttonSell.UseVisualStyleBackColor = true; + buttonSell.Click += ButtonSell_Click; + // + // buttonCancel + // + buttonCancel.Location = new Point(242, 99); + buttonCancel.Name = "buttonCancel"; + buttonCancel.Size = new Size(94, 29); + buttonCancel.TabIndex = 5; + buttonCancel.Text = "Отмена"; + buttonCancel.UseVisualStyleBackColor = true; + buttonCancel.Click += ButtonCancel_Click; + // + // FormSellSnack + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(366, 140); + Controls.Add(buttonCancel); + Controls.Add(buttonSell); + Controls.Add(textBoxCount); + Controls.Add(labelCount); + Controls.Add(comboBoxSnack); + Controls.Add(labelSnack); + Name = "FormSellSnack"; + Text = "Продажа закуски"; + Load += FormSellingPizza_Load; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private Label labelSnack; + private ComboBox comboBoxSnack; + private Label labelCount; + private TextBox textBoxCount; + private Button buttonSell; + private Button buttonCancel; + } +} \ No newline at end of file diff --git a/Diner/Diner/FormSellSnack.cs b/Diner/Diner/FormSellSnack.cs new file mode 100644 index 0000000..d6f96a4 --- /dev/null +++ b/Diner/Diner/FormSellSnack.cs @@ -0,0 +1,83 @@ +using Microsoft.Extensions.Logging; +using DinerContracts.BusinessLogicsContracts; +using DinerContracts.SearchModels; +using DinerContracts.StoragesContracts; +using DinerContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace DinerView +{ + public partial class FormSellSnack : Form + { + private readonly ILogger _logger; + private readonly ISnackLogic _logicP; + private readonly IShopLogic _logicS; + private List _snackList = new List(); + public FormSellSnack(ILogger logger, ISnackLogic logicP, IShopLogic logicS) + { + InitializeComponent(); + _logger = logger; + _logicP = logicP; + _logicS = logicS; + } + private void FormSellingPizza_Load(object sender, EventArgs e) + { + _snackList = _logicP.ReadList(null); + if (_snackList != null) + { + comboBoxSnack.DisplayMember = "SnackName"; + comboBoxSnack.ValueMember = "Id"; + comboBoxSnack.DataSource = _snackList; + comboBoxSnack.SelectedItem = null; + _logger.LogInformation("Загрузка закуски для продажи"); + } + } + private void ButtonSell_Click(object sender, EventArgs e) + { + if (comboBoxSnack.SelectedValue == null) + { + MessageBox.Show("Выберите закуску", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Создание покупки"); + try + { + bool resout = _logicS.Sale(new SupplySearchModel + { + SnackId = Convert.ToInt32(comboBoxSnack.SelectedValue), + Count = Convert.ToInt32(textBoxCount.Text) + }); + if (resout) + { + _logger.LogInformation("Проверка пройдена, продажа проведена"); + MessageBox.Show("Продажа проведена", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + Close(); + } + else { + _logger.LogInformation("Проверка не пройдена"); + MessageBox.Show("Продажа не может быть создана.", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Warning); + } + } + catch(Exception ex) + { + _logger.LogError(ex, "Ошибка создания покупки"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + + } + private void ButtonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + } +} diff --git a/Diner/Diner/FormSellSnack.resx b/Diner/Diner/FormSellSnack.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/Diner/Diner/FormSellSnack.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Diner/Diner/FormShop.Designer.cs b/Diner/Diner/FormShop.Designer.cs index ee461b3..f7a0154 100644 --- a/Diner/Diner/FormShop.Designer.cs +++ b/Diner/Diner/FormShop.Designer.cs @@ -35,12 +35,15 @@ buttonCancel = new Button(); buttonSave = new Button(); dataGridView = new DataGridView(); - label1 = new Label(); - dateTimeOpen = new DateTimePicker(); id = new DataGridViewTextBoxColumn(); DinerName = new DataGridViewTextBoxColumn(); Count = new DataGridViewTextBoxColumn(); + label1 = new Label(); + dateTimeOpen = new DateTimePicker(); + label2 = new Label(); + numericUpSnackMaxCount = new NumericUpDown(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); + ((System.ComponentModel.ISupportInitialize)numericUpSnackMaxCount).BeginInit(); SuspendLayout(); // // labelName @@ -102,31 +105,15 @@ dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; dataGridView.Columns.AddRange(new DataGridViewColumn[] { id, DinerName, Count }); - dataGridView.Location = new Point(12, 144); + dataGridView.Location = new Point(12, 197); dataGridView.Name = "dataGridView"; dataGridView.ReadOnly = true; dataGridView.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.None; dataGridView.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders; dataGridView.RowTemplate.Height = 29; - dataGridView.Size = new Size(569, 307); + dataGridView.Size = new Size(569, 254); dataGridView.TabIndex = 7; // - // label1 - // - label1.AutoSize = true; - label1.Location = new Point(12, 103); - label1.Name = "label1"; - label1.Size = new Size(110, 20); - label1.TabIndex = 8; - label1.Text = "Дата открытия"; - // - // dateTimeOpen - // - dateTimeOpen.Location = new Point(128, 103); - dateTimeOpen.Name = "dateTimeOpen"; - dateTimeOpen.Size = new Size(401, 27); - dateTimeOpen.TabIndex = 9; - // // id // id.HeaderText = "id"; @@ -149,11 +136,46 @@ Count.Name = "Count"; Count.ReadOnly = true; // + // label1 + // + label1.AutoSize = true; + label1.Location = new Point(12, 103); + label1.Name = "label1"; + label1.Size = new Size(110, 20); + label1.TabIndex = 8; + label1.Text = "Дата открытия"; + // + // dateTimeOpen + // + dateTimeOpen.Location = new Point(128, 103); + dateTimeOpen.Name = "dateTimeOpen"; + dateTimeOpen.Size = new Size(401, 27); + dateTimeOpen.TabIndex = 9; + // + // label2 + // + label2.AutoSize = true; + label2.Location = new Point(12, 156); + label2.Name = "label2"; + label2.Size = new Size(107, 20); + label2.TabIndex = 10; + label2.Text = "Вместимость: "; + // + // numericUpSnackMaxCount + // + numericUpSnackMaxCount.Location = new Point(128, 154); + numericUpSnackMaxCount.Maximum = new decimal(new int[] { 10000, 0, 0, 0 }); + numericUpSnackMaxCount.Name = "numericUpSnackMaxCount"; + numericUpSnackMaxCount.Size = new Size(401, 27); + numericUpSnackMaxCount.TabIndex = 12; + // // FormShop // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(593, 513); + Controls.Add(numericUpSnackMaxCount); + Controls.Add(label2); Controls.Add(dateTimeOpen); Controls.Add(label1); Controls.Add(dataGridView); @@ -167,6 +189,7 @@ Text = "Магазин"; Load += FormShop_Load; ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); + ((System.ComponentModel.ISupportInitialize)numericUpSnackMaxCount).EndInit(); ResumeLayout(false); PerformLayout(); } @@ -185,5 +208,7 @@ private DataGridViewTextBoxColumn id; private DataGridViewTextBoxColumn DinerName; private DataGridViewTextBoxColumn Count; + private Label label2; + private NumericUpDown numericUpSnackMaxCount; } } \ No newline at end of file diff --git a/Diner/Diner/FormShop.cs b/Diner/Diner/FormShop.cs index c34f982..2eacec2 100644 --- a/Diner/Diner/FormShop.cs +++ b/Diner/Diner/FormShop.cs @@ -48,6 +48,7 @@ namespace DinerView textBoxName.Text = view.ShopName; textBoxAdress.Text = view.Adress; dateTimeOpen.Value = view.OpeningDate; + numericUpSnackMaxCount.Value = view.SnackMaxCount; _ShopSnacks = view.ShopSnacks ?? new Dictionary(); LoadData(); } @@ -101,7 +102,8 @@ namespace DinerView Id = _id ?? 0, ShopName = textBoxName.Text, Adress = textBoxAdress.Text, - OpeningDate = dateTimeOpen.Value + OpeningDate = dateTimeOpen.Value, + SnackMaxCount = (int)numericUpSnackMaxCount.Value }; var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model); if (!operationResult) diff --git a/Diner/Diner/FormShops.cs b/Diner/Diner/FormShops.cs index 11735c1..cacb4a6 100644 --- a/Diner/Diner/FormShops.cs +++ b/Diner/Diner/FormShops.cs @@ -40,7 +40,7 @@ namespace DinerView { dataGridView.DataSource = list; dataGridView.Columns["Id"].Visible = false; - dataGridView.Columns["ShopDiners"].Visible = false; + dataGridView.Columns["ShopName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; } diff --git a/Diner/Diner/Program.cs b/Diner/Diner/Program.cs index 8a61d8f..aeca90d 100644 --- a/Diner/Diner/Program.cs +++ b/Diner/Diner/Program.cs @@ -1,6 +1,6 @@ using DinerContracts.BusinessLogicsContracts; using DinerContracts.StoragesContracts; -using DinerListImplement.Implements; +using DinerFileImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; @@ -52,6 +52,7 @@ namespace Diner services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file diff --git a/Diner/DinerFileImplement/DataFileSingleton.cs b/Diner/DinerFileImplement/DataFileSingleton.cs new file mode 100644 index 0000000..93d9658 --- /dev/null +++ b/Diner/DinerFileImplement/DataFileSingleton.cs @@ -0,0 +1,55 @@ +using DinerFileImplement.Models; +using System.Xml.Linq; + +namespace DinerFileImplement +{ + public class DataFileSingleton + { + private static DataFileSingleton? instance; + private readonly string ComponentFileName = "Component.xml"; + private readonly string OrderFileName = "Order.xml"; + private readonly string SnackFileName = "Snack.xml"; + private readonly string ShopFileName = "Shop.xml"; + public List Components { get; private set; } + public List Orders { get; private set; } + public List Snacks { get; private set; } + public List Shops { get; private set; } + + public static DataFileSingleton GetInstance() + { + if (instance == null) + { + instance = new DataFileSingleton(); + } + return instance; + } + public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement); + public void SaveSnacks() => SaveData(Snacks, SnackFileName, "Snacks", x => x.GetXElement); + public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + + public void SaveShops() => SaveData(Shops, ShopFileName, "Shops", x => x.GetXElement); + private DataFileSingleton() + { + Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; + Snacks = LoadData(SnackFileName, "Snack", x => Snack.Create(x)!)!; + Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; + Shops = LoadData(ShopFileName, "Shop", x => Shop.Create(x)!)!; + } + private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) + { + if (File.Exists(filename)) + { + return XDocument.Load(filename)?.Root?.Elements(xmlNodeName)?.Select(selectFunction)?.ToList(); + } + return new List(); + } + private static void SaveData(List data, string filename, string xmlNodeName, Func selectFunction) + { + if (data != null) + { + new XDocument(new XElement(xmlNodeName, data.Select(selectFunction).ToArray())).Save(filename); + } + } + + } +} \ No newline at end of file diff --git a/Diner/DinerFileImplement/DinerFileImplement.csproj b/Diner/DinerFileImplement/DinerFileImplement.csproj new file mode 100644 index 0000000..f83c53f --- /dev/null +++ b/Diner/DinerFileImplement/DinerFileImplement.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + enable + enable + + + + + + + + diff --git a/Diner/DinerFileImplement/Implements/ComponentStorage.cs b/Diner/DinerFileImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..6f610b7 --- /dev/null +++ b/Diner/DinerFileImplement/Implements/ComponentStorage.cs @@ -0,0 +1,74 @@ +using DinerContracts.BindingModels; +using DinerContracts.SearchModels; +using DinerContracts.StoragesContracts; +using DinerContracts.ViewModels; +using DinerFileImplement.Models; + +namespace DinerFileImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + private readonly DataFileSingleton source; + public ComponentStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Components.Select(x => x.GetViewModel).ToList(); + } + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + return source.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; + } + return source.Components.FirstOrDefault(x => + (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + public ComponentViewModel? Insert(ComponentBindingModel model) + { + model.Id = source.Components.Count > 0 ? source.Components.Max(x => x.Id) + 1 : 1; + var newComponent = Component.Create(model); + if (newComponent == null) + { + return null; + } + source.Components.Add(newComponent); + source.SaveComponents(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + var component = source.Components.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + source.SaveComponents(); + return component.GetViewModel; + } + public ComponentViewModel? Delete(ComponentBindingModel model) + { + var element = source.Components.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + source.Components.Remove(element); + source.SaveComponents(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/Diner/DinerFileImplement/Implements/OrderStorage.cs b/Diner/DinerFileImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..a2c60b9 --- /dev/null +++ b/Diner/DinerFileImplement/Implements/OrderStorage.cs @@ -0,0 +1,84 @@ +using DinerContracts.BindingModels; +using DinerContracts.SearchModels; +using DinerContracts.StoragesContracts; +using DinerContracts.ViewModels; +using DinerFileImplement.Models; + +namespace DinerFileImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + private readonly DataFileSingleton source; + public OrderStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() => source.Orders.Select(x => AttachPizzaName(x.GetViewModel)).ToList(); + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + return source.Orders.Where(x => x.Id == model.Id) + .Select(x => AttachPizzaName(x.GetViewModel)) + .ToList(); + } + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + return AttachPizzaName(source.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel); + } + public OrderViewModel? Insert(OrderBindingModel model) + { + model.Id = source.Orders.Count > 0 ? source.Orders.Max(x => x.Id) + 1 : 1; + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + source.Orders.Add(newOrder); + source.SaveOrders(); + return AttachPizzaName(newOrder.GetViewModel); + } + public OrderViewModel? Update(OrderBindingModel model) + { + var order = source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + source.SaveOrders(); + return AttachPizzaName(order.GetViewModel); + } + public OrderViewModel? Delete(OrderBindingModel model) + { + var order = source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order != null) + { + source.Orders.Remove(order); + source.SaveOrders(); + return AttachPizzaName(order.GetViewModel); + } + return null; + } + private OrderViewModel? AttachPizzaName(OrderViewModel? model) + { + if (model == null) + { + return null; + } + var snack = source.Snacks.FirstOrDefault(x => x.Id == model.SnackId); + if (snack != null) + { + model.SnackName = snack.SnackName; + } + return model; + } + } + +} diff --git a/Diner/DinerFileImplement/Implements/ShopStorage.cs b/Diner/DinerFileImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..3f7f4ba --- /dev/null +++ b/Diner/DinerFileImplement/Implements/ShopStorage.cs @@ -0,0 +1,144 @@ +using DinerContracts.BindingModels; +using DinerContracts.SearchModels; +using DinerContracts.StoragesContracts; +using DinerContracts.ViewModels; +using DinerFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DinerFileImplement.Implements +{ + public class ShopStorage : IShopStorage + { + private readonly DataFileSingleton source; + public ShopStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Shops.Select(x=>x.GetViewModel).ToList(); + } + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName)) + { + return new(); + } + return source.Shops.Where(x=>x.ShopName.Contains(model.ShopName)).Select(x=>x.GetViewModel).ToList(); + } + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue) + { + return null; + } + return source.Shops.FirstOrDefault(x => + (!string.IsNullOrEmpty(model.ShopName) && x.ShopName == model.ShopName) || + (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + public ShopViewModel? Insert(ShopBindingModel model) + { + model.Id = source.Shops.Count > 0 ? source.Shops.Max(x => x.Id) + 1 : 1; + var newShop = Shop.Create(model); + if (newShop == null) + { + return null; + } + source.Shops.Add(newShop); + source.SaveShops(); + return newShop.GetViewModel; + } + public ShopViewModel? Update(ShopBindingModel model) + { + var shop = source.Shops.FirstOrDefault(x=>x.Id == model.Id); + if(shop == null) + { + return null; + } + shop.Update(model); + source.SaveShops(); + return shop.GetViewModel; + } + public ShopViewModel? Delete(ShopBindingModel model) + { + var shop = source.Shops.FirstOrDefault(x => x.Id == model.Id); + if(shop != null) + { + source.Shops.Remove(shop); + source.SaveShops(); + return shop.GetViewModel; + } + return null; + } + public bool Sale(SupplySearchModel model) { + if (model == null || !model.SnackId.HasValue || !model.Count.HasValue) + return false; + int remainingSpace = source.Shops.Select(x => x.Snacks.ContainsKey(model.SnackId.Value) ? x.Snacks[model.SnackId.Value] : 0).Sum(); + if(remainingSpace < model.Count) + { + return false; + } + var shops = source.Shops.Where(x => x.Snacks.ContainsKey(model.SnackId.Value)).OrderByDescending(x => x.Snacks[model.SnackId.Value]).ToList(); + foreach (var shop in shops) + { + int residue = model.Count.Value - shop.Snacks[model.SnackId.Value]; + if (residue > 0) + { + shop.Snacks.Remove(model.SnackId.Value); + shop.SnackUpdate(); + model.Count = residue; + } + else + { + if (residue == 0) + { + shop.Snacks.Remove(model.SnackId.Value); + } + else + { + shop.Snacks[model.SnackId.Value] = -residue; + } + shop.SnackUpdate(); + source.SaveShops(); + return true; + } + } + source.SaveShops(); + return false; + } + public bool RestockingShops(SupplyBindingModel model) + { + if (model==null || source.Shops.Select(x => x.SnackMaxCount - x.ShopSnacks.Select(y => y.Value.Item2).Sum()).Sum() < model.Count) + { + return false; + } + foreach (Shop shop in source.Shops) + { + int free_places = shop.SnackMaxCount - shop.ShopSnacks.Select(x => x.Value.Item2).Sum(); + if (free_places <= 0) + continue; + free_places = Math.Min(free_places, model.Count); + model.Count -= free_places; + if (shop.Snacks.ContainsKey(model.SnackId)) + { + shop.Snacks[model.SnackId] += free_places; + } + else + { + shop.Snacks.Add(model.SnackId, free_places); + } + shop.SnackUpdate(); + if (model.Count == 0) + { + source.SaveShops(); + return true; + } + } + return false; + } + } +} diff --git a/Diner/DinerFileImplement/Implements/SnackStorage.cs b/Diner/DinerFileImplement/Implements/SnackStorage.cs new file mode 100644 index 0000000..4720cb5 --- /dev/null +++ b/Diner/DinerFileImplement/Implements/SnackStorage.cs @@ -0,0 +1,74 @@ +using DinerContracts.BindingModels; +using DinerContracts.SearchModels; +using DinerContracts.StoragesContracts; +using DinerContracts.ViewModels; +using DinerFileImplement.Models; + +namespace DinerFileImplement.Implements +{ + public class SnackStorage : ISnackStorage + { + private readonly DataFileSingleton source; + public SnackStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Snacks.Select(x => x.GetViewModel).ToList(); + } + public List GetFilteredList(SnackSearchModel model) + { + if (string.IsNullOrEmpty(model.SnackName)) + { + return new(); + } + return source.Snacks.Where(x => x.SnackName.Contains(model.SnackName)).Select(x => x.GetViewModel).ToList(); + } + public SnackViewModel? GetElement(SnackSearchModel model) + { + if (string.IsNullOrEmpty(model.SnackName) && !model.Id.HasValue) + { + return null; + } + return source.Snacks.FirstOrDefault(x => + (!string.IsNullOrEmpty(model.SnackName) && x.SnackName == model.SnackName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + public SnackViewModel? Insert(SnackBindingModel model) + { + model.Id = source.Snacks.Count > 0 ? source.Snacks.Max(x => x.Id) + 1 : 1; + var newSnack = Snack.Create(model); + if (newSnack == null) + { + return null; + } + source.Snacks.Add(newSnack); + source.SaveSnacks(); + return newSnack.GetViewModel; + } + public SnackViewModel? Update(SnackBindingModel model) + { + var Snack = source.Snacks.FirstOrDefault(x => x.Id == model.Id); + if (Snack == null) + { + return null; + } + Snack.Update(model); + source.SaveSnacks(); + return Snack.GetViewModel; + } + public SnackViewModel? Delete(SnackBindingModel model) + { + var Snack = source.Snacks.FirstOrDefault(x => x.Id == model.Id); + if (Snack != null) + { + source.Snacks.Remove(Snack); + source.SaveSnacks(); + return Snack.GetViewModel; + } + return null; + } + } +} diff --git a/Diner/DinerFileImplement/Models/Component.cs b/Diner/DinerFileImplement/Models/Component.cs new file mode 100644 index 0000000..6b59123 --- /dev/null +++ b/Diner/DinerFileImplement/Models/Component.cs @@ -0,0 +1,59 @@ +using DinerContracts.BindingModels; +using DinerContracts.ViewModels; +using DinerDataModels.Models; +using System.Xml.Linq; + +namespace DinerFileImplement.Models +{ + public class Component : IComponentModel + { + public int Id { get; private set; } + public string ComponentName { get; private set; } = string.Empty; + public double Cost { get; set; } + 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(XElement element) + { + if (element == null) + { + return null; + } + return new Component() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ComponentName = element.Element("ComponentName")!.Value, + Cost = Convert.ToDouble(element.Element("Cost")!.Value) + }; + } + 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 + }; + public XElement GetXElement => new("Component", + new XAttribute("Id", Id), + new XElement("ComponentName", ComponentName), + new XElement("Cost", Cost.ToString())); + } +} diff --git a/Diner/DinerFileImplement/Models/Order.cs b/Diner/DinerFileImplement/Models/Order.cs new file mode 100644 index 0000000..223ce81 --- /dev/null +++ b/Diner/DinerFileImplement/Models/Order.cs @@ -0,0 +1,83 @@ +using DinerDataModels.Enum; +using DinerDataModels.Models; +using DinerContracts.BindingModels; +using DinerContracts.ViewModels; +using System.Xml.Linq; + +namespace DinerFileImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + public int SnackId { get; private set; } + public int Count { get; private set; } + public double Sum { get; private set; } + public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; + public DateTime DateCreate { get; private set; } = DateTime.Now; + public DateTime? DateImplement { get; private set; } + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + SnackId = model.SnackId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement, + }; + } + public static Order? Create(XElement element) + { + if (element == null) + { + return null; + } + string dateImplement = element.Element("DateImplement")!.Value; + return new Order() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + SnackId = Convert.ToInt32(element.Element("SnackId")!.Value), + Count = Convert.ToInt32(element.Element("Count")!.Value), + Sum = Convert.ToDouble(element.Element("Sum")!.Value), + Status = (OrderStatus)(Enum.Parse(typeof(OrderStatus), element.Element("Status")!.Value)), + DateCreate = Convert.ToDateTime(element.Element("DateCreate")!.Value), + DateImplement = (dateImplement == "" || dateImplement is null) ? Convert.ToDateTime(null) : Convert.ToDateTime(dateImplement) + }; + + } + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + Status = model.Status; + if (model.Status == OrderStatus.Выдан) DateImplement = model.DateImplement; + } + public OrderViewModel GetViewModel => new() + { + Id = Id, + SnackId = SnackId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + }; + + public XElement GetXElement => new("Order", + new XAttribute("Id", Id), + new XElement("SnackId", SnackId.ToString()), + new XElement("Count", Count.ToString()), + new XElement("Sum", Sum.ToString()), + new XElement("Status", Status.ToString()), + new XElement("DateCreate", DateCreate.ToString()), + new XElement("DateImplement", DateImplement.ToString())); + } +} diff --git a/Diner/DinerFileImplement/Models/Shop.cs b/Diner/DinerFileImplement/Models/Shop.cs new file mode 100644 index 0000000..a248b24 --- /dev/null +++ b/Diner/DinerFileImplement/Models/Shop.cs @@ -0,0 +1,102 @@ +using DinerDataModels.Models; +using DinerContracts.BindingModels; +using DinerContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace DinerFileImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; private set; } + public string ShopName { get; private set; } = string.Empty; + public string Adress { get; private set; } = string.Empty; + public DateTime OpeningDate { get; private set; } + public Dictionary Snacks { get; private set; } = new(); + private Dictionary? _shopSnacks = null; + public Dictionary ShopSnacks { + get { + if (_shopSnacks == null) + { + var source = DataFileSingleton.GetInstance(); + _shopSnacks = Snacks.ToDictionary(x => x.Key, y => ((source.Snacks.FirstOrDefault(z => z.Id == y.Key) as ISnackModel)!, y.Value)); + } + return _shopSnacks; + } + } + public int SnackMaxCount { get; private set; } + public static Shop? Create(ShopBindingModel? model) + { + if (model == null) + { + return null; + } + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Adress = model.Adress, + OpeningDate = model.OpeningDate, + Snacks = model.ShopSnacks.ToDictionary(x => x.Key, x => x.Value.Item2), + SnackMaxCount = model.SnackMaxCount + }; + } + public static Shop? Create(XElement element) + { + if (element == null) + { + return null; + } + return new() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ShopName = element.Element("ShopName")!.Value, + Adress = element.Element("Adress")!.Value, + OpeningDate = Convert.ToDateTime(element.Element("OpeningDate")!.Value), + Snacks = element.Element("ShopSnacks")!.Elements("ShopSnack")!.ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), + x => Convert.ToInt32(x.Element("Value")?.Value)), + SnackMaxCount = Convert.ToInt32(element.Element("SnackMaxCount")!.Value) + }; + } + public void Update(ShopBindingModel? model) + { + if (model == null) + { + return; + } + ShopName = model.ShopName; + Adress = model.Adress; + OpeningDate = model.OpeningDate; + SnackMaxCount = model.SnackMaxCount; + Snacks = model.ShopSnacks.ToDictionary(x => x.Key, x => x.Value.Item2); + _shopSnacks = null; + } + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Adress = Adress, + OpeningDate = OpeningDate, + ShopSnacks = ShopSnacks, + SnackMaxCount = SnackMaxCount + }; + + public XElement GetXElement => new("Shop", + new XAttribute("Id", Id), + new XElement("ShopName",ShopName), + new XElement("Adress", Adress), + new XElement("OpeningDate", OpeningDate.ToString()), + new XElement("ShopSnacks", Snacks.Select( + x => new XElement("ShopSnack", new XElement("Key",x.Key),new XElement("Value",x.Value))).ToArray()), + new XElement("SnackMaxCount", SnackMaxCount.ToString()) + ); + + public void SnackUpdate() { + _shopSnacks = null; + } + } +} diff --git a/Diner/DinerFileImplement/Models/Snack.cs b/Diner/DinerFileImplement/Models/Snack.cs new file mode 100644 index 0000000..5fc55e4 --- /dev/null +++ b/Diner/DinerFileImplement/Models/Snack.cs @@ -0,0 +1,81 @@ +using DinerContracts.BindingModels; +using DinerContracts.ViewModels; +using DinerDataModels.Models; +using System.Xml.Linq; + +namespace DinerFileImplement.Models +{ + public class Snack : ISnackModel + { + public int Id { get; private set; } + public string SnackName { get; private set; } = string.Empty; + public double Price { get; private set; } + public Dictionary Components { get; private set; } = new(); + private Dictionary? _snackComponents = null; + public Dictionary SnackComponents + { + get + { + if (_snackComponents == null) + { + var source = DataFileSingleton.GetInstance(); + _snackComponents = Components.ToDictionary(x => x.Key, y => ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, y.Value)); + } + return _snackComponents; + } + } + public static Snack? Create(SnackBindingModel model) + { + if (model == null) + { + return null; + } + return new Snack() + { + Id = model.Id, + SnackName = model.SnackName, + Price = model.Price, + Components = model.SnackComponents.ToDictionary(x => x.Key, x => x.Value.Item2) + }; + } + public static Snack? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Snack() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + SnackName = element.Element("SnackName")!.Value, + Price = Convert.ToDouble(element.Element("Price")!.Value), + Components = element.Element("SnackComponents")!.Elements("SnackComponent").ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), + x => Convert.ToInt32(x.Element("Value")?.Value)) + }; + } + public void Update(SnackBindingModel model) + { + if (model == null) + { + return; + } + SnackName = model.SnackName; + Price = model.Price; + Components = model.SnackComponents.ToDictionary(x => x.Key, x => x.Value.Item2); + _snackComponents = null; + } + public SnackViewModel GetViewModel => new() + { + Id = Id, + SnackName = SnackName, + Price = Price, + SnackComponents = SnackComponents + }; + public XElement GetXElement => new("Snack", + new XAttribute("Id", Id), + new XElement("SnackName", SnackName), + new XElement("Price", Price.ToString()), + new XElement("SnackComponents", Components.Select( + x => new XElement("SnackComponent", new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray())); + } +}