From 351921e1f785dcb4a91c8348b5b57b347cc80224 Mon Sep 17 00:00:00 2001 From: shadowik Date: Wed, 22 Mar 2023 21:07:34 +0300 Subject: [PATCH] TestFinish --- Pizzeria/Pizzeria/FormMain.Designer.cs | 18 ++- Pizzeria/Pizzeria/FormMain.cs | 9 ++ Pizzeria/Pizzeria/FormSellPizza.Designer.cs | 120 +++++++++++++++ Pizzeria/Pizzeria/FormSellPizza.cs | 83 ++++++++++ Pizzeria/Pizzeria/FormSellPizza.resx | 60 ++++++++ Pizzeria/Pizzeria/FormShop.Designer.cs | 33 +++- Pizzeria/Pizzeria/FormShop.cs | 4 +- Pizzeria/Pizzeria/FormShop.resx | 9 ++ Pizzeria/Pizzeria/Program.cs | 1 + .../BusinessLogics/OrderLogic.cs | 21 ++- .../BusinessLogics/ShopLogic.cs | 16 ++ .../BindingModels/ShopBindingModel.cs | 1 + .../BusinessLogicsContracts/IShopLogic.cs | 1 + .../SearchModels/SupplySearchModel.cs | 14 ++ .../StoragesContracts/IShopStorage.cs | 2 + .../ViewModels/ShopViewModel.cs | 2 + .../DataFileSingleton.cs | 6 + .../Implements/ShopStorage.cs | 144 ++++++++++++++++++ Pizzeria/PizzeriaFileImplement/Models/Shop.cs | 102 +++++++++++++ .../Implements/ShopStorage.cs | 10 ++ 20 files changed, 648 insertions(+), 8 deletions(-) create mode 100644 Pizzeria/Pizzeria/FormSellPizza.Designer.cs create mode 100644 Pizzeria/Pizzeria/FormSellPizza.cs create mode 100644 Pizzeria/Pizzeria/FormSellPizza.resx create mode 100644 Pizzeria/PizzeriaContracts/SearchModels/SupplySearchModel.cs create mode 100644 Pizzeria/PizzeriaFileImplement/Implements/ShopStorage.cs create mode 100644 Pizzeria/PizzeriaFileImplement/Models/Shop.cs diff --git a/Pizzeria/Pizzeria/FormMain.Designer.cs b/Pizzeria/Pizzeria/FormMain.Designer.cs index c54717f..be4bfc9 100644 --- a/Pizzeria/Pizzeria/FormMain.Designer.cs +++ b/Pizzeria/Pizzeria/FormMain.Designer.cs @@ -41,6 +41,7 @@ this.buttonOrderReady = new System.Windows.Forms.Button(); this.buttonIssuedOrder = new System.Windows.Forms.Button(); this.buttonRef = new System.Windows.Forms.Button(); + this.продажаToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.menuStrip1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); this.SuspendLayout(); @@ -71,28 +72,29 @@ // ingridientsToolStripMenuItem // this.ingridientsToolStripMenuItem.Name = "ingridientsToolStripMenuItem"; - this.ingridientsToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.ingridientsToolStripMenuItem.Size = new System.Drawing.Size(148, 22); this.ingridientsToolStripMenuItem.Text = "Ингредиенты"; this.ingridientsToolStripMenuItem.Click += new System.EventHandler(this.IngridentsToolStripMenuItem_Click); // // pizzasToolStripMenuItem // this.pizzasToolStripMenuItem.Name = "pizzasToolStripMenuItem"; - this.pizzasToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.pizzasToolStripMenuItem.Size = new System.Drawing.Size(148, 22); this.pizzasToolStripMenuItem.Text = "Пиццы"; this.pizzasToolStripMenuItem.Click += new System.EventHandler(this.PizzasToolStripMenuItem_Click); // // shopsToolStripMenuItem // this.shopsToolStripMenuItem.Name = "shopsToolStripMenuItem"; - this.shopsToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.shopsToolStripMenuItem.Size = new System.Drawing.Size(148, 22); this.shopsToolStripMenuItem.Text = "Магазины"; this.shopsToolStripMenuItem.Click += new System.EventHandler(this.shopsToolStripMenuItem_Click); // // operationToolStripMenuItem // this.operationToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { - this.transactionToolStripMenuItem}); + this.transactionToolStripMenuItem, + this.продажаToolStripMenuItem}); this.operationToolStripMenuItem.Name = "operationToolStripMenuItem"; this.operationToolStripMenuItem.Size = new System.Drawing.Size(75, 20); this.operationToolStripMenuItem.Text = "Операции"; @@ -173,6 +175,13 @@ this.buttonRef.UseVisualStyleBackColor = true; this.buttonRef.Click += new System.EventHandler(this.ButtonRef_Click); // + // продажаToolStripMenuItem + // + this.продажаToolStripMenuItem.Name = "продажаToolStripMenuItem"; + this.продажаToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.продажаToolStripMenuItem.Text = "Продажа"; + this.продажаToolStripMenuItem.Click += new System.EventHandler(this.SellToolStripMenuItem_Click); + // // FormMain // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); @@ -213,5 +222,6 @@ private ToolStripMenuItem shopsToolStripMenuItem; private ToolStripMenuItem operationToolStripMenuItem; private ToolStripMenuItem transactionToolStripMenuItem; + private ToolStripMenuItem продажаToolStripMenuItem; } } \ No newline at end of file diff --git a/Pizzeria/Pizzeria/FormMain.cs b/Pizzeria/Pizzeria/FormMain.cs index db4b865..b69f060 100644 --- a/Pizzeria/Pizzeria/FormMain.cs +++ b/Pizzeria/Pizzeria/FormMain.cs @@ -162,5 +162,14 @@ namespace PizzeriaView form.ShowDialog(); } } + + private void SellToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormSellPizza)); + if (service is FormSellPizza form) + { + form.ShowDialog(); + } + } } } diff --git a/Pizzeria/Pizzeria/FormSellPizza.Designer.cs b/Pizzeria/Pizzeria/FormSellPizza.Designer.cs new file mode 100644 index 0000000..2ef4ab2 --- /dev/null +++ b/Pizzeria/Pizzeria/FormSellPizza.Designer.cs @@ -0,0 +1,120 @@ +namespace PizzeriaView +{ + partial class FormSellPizza + { + /// + /// 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() + { + this.labelPizza = new System.Windows.Forms.Label(); + this.comboBoxPizza = new System.Windows.Forms.ComboBox(); + this.labelCount = new System.Windows.Forms.Label(); + this.textBoxCount = new System.Windows.Forms.TextBox(); + this.buttonSell = new System.Windows.Forms.Button(); + this.buttonCancel = new System.Windows.Forms.Button(); + this.SuspendLayout(); + // + // labelPizza + // + this.labelPizza.AutoSize = true; + this.labelPizza.Location = new System.Drawing.Point(12, 14); + this.labelPizza.Name = "labelPizza"; + this.labelPizza.Size = new System.Drawing.Size(75, 20); + this.labelPizza.TabIndex = 0; + this.labelPizza.Text = "Пицца: "; + // + // comboBoxPizza + // + this.comboBoxPizza.FormattingEnabled = true; + this.comboBoxPizza.Location = new System.Drawing.Point(115, 11); + this.comboBoxPizza.Name = "comboBoxPizza"; + this.comboBoxPizza.Size = new System.Drawing.Size(239, 28); + this.comboBoxPizza.TabIndex = 1; + // + // labelCount + // + this.labelCount.AutoSize = true; + this.labelCount.Location = new System.Drawing.Point(12, 55); + this.labelCount.Name = "labelCount"; + this.labelCount.Size = new System.Drawing.Size(97, 20); + this.labelCount.TabIndex = 2; + this.labelCount.Text = "Количество: "; + // + // textBoxCount + // + this.textBoxCount.Location = new System.Drawing.Point(115, 52); + this.textBoxCount.Name = "textBoxCount"; + this.textBoxCount.Size = new System.Drawing.Size(239, 27); + this.textBoxCount.TabIndex = 3; + // + // buttonSell + // + this.buttonSell.Location = new System.Drawing.Point(128, 99); + this.buttonSell.Name = "buttonSell"; + this.buttonSell.Size = new System.Drawing.Size(94, 29); + this.buttonSell.TabIndex = 4; + this.buttonSell.Text = "Продать"; + this.buttonSell.UseVisualStyleBackColor = true; + this.buttonSell.Click += new System.EventHandler(this.ButtonSell_Click); + // + // buttonCancel + // + this.buttonCancel.Location = new System.Drawing.Point(242, 99); + this.buttonCancel.Name = "buttonCancel"; + this.buttonCancel.Size = new System.Drawing.Size(94, 29); + this.buttonCancel.TabIndex = 5; + this.buttonCancel.Text = "Отмена"; + this.buttonCancel.UseVisualStyleBackColor = true; + this.buttonCancel.Click += new System.EventHandler(this.ButtonCancel_Click); + // + // FormSellPizza + // + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(366, 140); + this.Controls.Add(this.buttonCancel); + this.Controls.Add(this.buttonSell); + this.Controls.Add(this.textBoxCount); + this.Controls.Add(this.labelCount); + this.Controls.Add(this.comboBoxPizza); + this.Controls.Add(this.labelPizza); + this.Name = "FormSellPizza"; + this.Text = "Продажа пиццы"; + this.Load += new System.EventHandler(this.FormSellingPizza_Load); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private Label labelPizza; + private ComboBox comboBoxPizza; + private Label labelCount; + private TextBox textBoxCount; + private Button buttonSell; + private Button buttonCancel; + } +} \ No newline at end of file diff --git a/Pizzeria/Pizzeria/FormSellPizza.cs b/Pizzeria/Pizzeria/FormSellPizza.cs new file mode 100644 index 0000000..b7d7898 --- /dev/null +++ b/Pizzeria/Pizzeria/FormSellPizza.cs @@ -0,0 +1,83 @@ +using Microsoft.Extensions.Logging; +using PizzeriaContracts.BusinessLogicsContracts; +using PizzeriaContracts.SearchModels; +using PizzeriaContracts.StoragesContracts; +using PizzeriaContracts.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 PizzeriaView +{ + public partial class FormSellPizza : Form + { + private readonly ILogger _logger; + private readonly IPizzaLogic _logicP; + private readonly IShopLogic _logicS; + private List _pizzaList = new List(); + public FormSellPizza(ILogger logger, IPizzaLogic logicP, IShopLogic logicS) + { + InitializeComponent(); + _logger = logger; + _logicP = logicP; + _logicS = logicS; + } + private void FormSellingPizza_Load(object sender, EventArgs e) + { + _pizzaList = _logicP.ReadList(null); + if (_pizzaList != null) + { + comboBoxPizza.DisplayMember = "PizzaName"; + comboBoxPizza.ValueMember = "Id"; + comboBoxPizza.DataSource = _pizzaList; + comboBoxPizza.SelectedItem = null; + _logger.LogInformation("Загрузка пиццы для продажи"); + } + } + private void ButtonSell_Click(object sender, EventArgs e) + { + if (comboBoxPizza.SelectedValue == null) + { + MessageBox.Show("Выберите пиццу", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Создание покупки"); + try + { + bool resout = _logicS.Sale(new SupplySearchModel + { + PizzaId = Convert.ToInt32(comboBoxPizza.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/Pizzeria/Pizzeria/FormSellPizza.resx b/Pizzeria/Pizzeria/FormSellPizza.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/Pizzeria/Pizzeria/FormSellPizza.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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/Pizzeria/Pizzeria/FormShop.Designer.cs b/Pizzeria/Pizzeria/FormShop.Designer.cs index 010c144..1278e7e 100644 --- a/Pizzeria/Pizzeria/FormShop.Designer.cs +++ b/Pizzeria/Pizzeria/FormShop.Designer.cs @@ -40,7 +40,10 @@ this.Count = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.label1 = new System.Windows.Forms.Label(); this.dateTimeOpen = new System.Windows.Forms.DateTimePicker(); + this.label2 = new System.Windows.Forms.Label(); + this.numericUpPizzaMaxCount = new System.Windows.Forms.NumericUpDown(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); + ((System.ComponentModel.ISupportInitialize)(this.numericUpPizzaMaxCount)).BeginInit(); this.SuspendLayout(); // // labelName @@ -105,13 +108,13 @@ this.id, this.PizzaName, this.Count}); - this.dataGridView.Location = new System.Drawing.Point(12, 144); + this.dataGridView.Location = new System.Drawing.Point(12, 185); this.dataGridView.Name = "dataGridView"; this.dataGridView.ReadOnly = true; this.dataGridView.RowHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.None; this.dataGridView.RowHeadersWidthSizeMode = System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders; this.dataGridView.RowTemplate.Height = 29; - this.dataGridView.Size = new System.Drawing.Size(569, 307); + this.dataGridView.Size = new System.Drawing.Size(569, 266); this.dataGridView.TabIndex = 7; // // id @@ -152,11 +155,34 @@ this.dateTimeOpen.Size = new System.Drawing.Size(401, 27); this.dateTimeOpen.TabIndex = 9; // + // label2 + // + this.label2.AutoSize = true; + this.label2.Location = new System.Drawing.Point(12, 148); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(100, 20); + this.label2.TabIndex = 10; + this.label2.Text = "Вместимость"; + // + // numericUpPizzaMaxCount + // + this.numericUpPizzaMaxCount.Location = new System.Drawing.Point(128, 146); + this.numericUpPizzaMaxCount.Maximum = new decimal(new int[] { + 10000, + 0, + 0, + 0}); + this.numericUpPizzaMaxCount.Name = "numericUpPizzaMaxCount"; + this.numericUpPizzaMaxCount.Size = new System.Drawing.Size(401, 27); + this.numericUpPizzaMaxCount.TabIndex = 11; + // // FormShop // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(593, 513); + this.Controls.Add(this.numericUpPizzaMaxCount); + this.Controls.Add(this.label2); this.Controls.Add(this.dateTimeOpen); this.Controls.Add(this.label1); this.Controls.Add(this.dataGridView); @@ -170,6 +196,7 @@ this.Text = "Магазин"; this.Load += new System.EventHandler(this.FormShop_Load); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); + ((System.ComponentModel.ISupportInitialize)(this.numericUpPizzaMaxCount)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); @@ -189,5 +216,7 @@ private DataGridViewTextBoxColumn Count; private Label label1; private DateTimePicker dateTimeOpen; + private Label label2; + private NumericUpDown numericUpPizzaMaxCount; } } \ No newline at end of file diff --git a/Pizzeria/Pizzeria/FormShop.cs b/Pizzeria/Pizzeria/FormShop.cs index c110ef7..12783b8 100644 --- a/Pizzeria/Pizzeria/FormShop.cs +++ b/Pizzeria/Pizzeria/FormShop.cs @@ -46,6 +46,7 @@ namespace PizzeriaView textBoxName.Text = view.ShopName; textBoxAdress.Text = view.Adress; dateTimeOpen.Value = view.OpeningDate; + numericUpPizzaMaxCount.Value = view.PizzaMaxCount; _ShopPizzas = view.ShopPizzas ?? new Dictionary(); LoadData(); } @@ -99,7 +100,8 @@ namespace PizzeriaView Id = _id ?? 0, ShopName = textBoxName.Text, Adress = textBoxAdress.Text, - OpeningDate = dateTimeOpen.Value + OpeningDate = dateTimeOpen.Value, + PizzaMaxCount = (int) numericUpPizzaMaxCount.Value }; var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model); if (!operationResult) diff --git a/Pizzeria/Pizzeria/FormShop.resx b/Pizzeria/Pizzeria/FormShop.resx index 9368275..ef6993b 100644 --- a/Pizzeria/Pizzeria/FormShop.resx +++ b/Pizzeria/Pizzeria/FormShop.resx @@ -66,4 +66,13 @@ True + + True + + + True + + + True + \ No newline at end of file diff --git a/Pizzeria/Pizzeria/Program.cs b/Pizzeria/Pizzeria/Program.cs index 4a08120..bb3a7e0 100644 --- a/Pizzeria/Pizzeria/Program.cs +++ b/Pizzeria/Pizzeria/Program.cs @@ -56,6 +56,7 @@ namespace Pizzeria services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file diff --git a/Pizzeria/PizzeriaBusinessLogic/BusinessLogics/OrderLogic.cs b/Pizzeria/PizzeriaBusinessLogic/BusinessLogics/OrderLogic.cs index c9335e2..53b8f49 100644 --- a/Pizzeria/PizzeriaBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/Pizzeria/PizzeriaBusinessLogic/BusinessLogics/OrderLogic.cs @@ -12,10 +12,12 @@ namespace PizzeriatBusinessLogic.BusinessLogics { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; - public OrderLogic(ILogger logger, IOrderStorage orderStorage) + private readonly IShopStorage _shopStorage; + public OrderLogic(ILogger logger, IOrderStorage orderStorage, IShopStorage shopStorage) { _logger = logger; _orderStorage = orderStorage; + _shopStorage = shopStorage; } public List? ReadList(OrderSearchModel? model) { @@ -52,6 +54,23 @@ namespace PizzeriatBusinessLogic.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 + { + PizzaId = order.PizzaId, + Count = order.Count + })) + { + throw new ArgumentException("Недостаточно места"); + } + return ChangeStatus(model, OrderStatus.Выдан); } private void CheckModel(OrderBindingModel model, bool withParams = true) diff --git a/Pizzeria/PizzeriaBusinessLogic/BusinessLogics/ShopLogic.cs b/Pizzeria/PizzeriaBusinessLogic/BusinessLogics/ShopLogic.cs index 5b8b285..4f38cc3 100644 --- a/Pizzeria/PizzeriaBusinessLogic/BusinessLogics/ShopLogic.cs +++ b/Pizzeria/PizzeriaBusinessLogic/BusinessLogics/ShopLogic.cs @@ -148,5 +148,21 @@ namespace PizzeriaBusinessLogic.BusinessLogics throw new InvalidOperationException("Магазин с таким названием уже есть"); } } + + public bool Sale(SupplySearchModel model) + { + if (!model.PizzaId.HasValue || !model.Count.HasValue) + { + return false; + } + _logger.LogInformation("Check pizza count in all shops"); + if (_shopStorage.Sale(model)) + { + _logger.LogInformation("Selling sucsess"); + return true; + } + _logger.LogInformation("Selling failed"); + return false; + } } } diff --git a/Pizzeria/PizzeriaContracts/BindingModels/ShopBindingModel.cs b/Pizzeria/PizzeriaContracts/BindingModels/ShopBindingModel.cs index 71a41a3..a369879 100644 --- a/Pizzeria/PizzeriaContracts/BindingModels/ShopBindingModel.cs +++ b/Pizzeria/PizzeriaContracts/BindingModels/ShopBindingModel.cs @@ -14,5 +14,6 @@ namespace PizzeriaContracts.BindingModels public string Adress { get; set; } = string.Empty; public DateTime OpeningDate { get; set; } = DateTime.Now; public Dictionary ShopPizzas { get; set; } = new(); + public int PizzaMaxCount { get; set; } } } diff --git a/Pizzeria/PizzeriaContracts/BusinessLogicsContracts/IShopLogic.cs b/Pizzeria/PizzeriaContracts/BusinessLogicsContracts/IShopLogic.cs index 400091a..db59820 100644 --- a/Pizzeria/PizzeriaContracts/BusinessLogicsContracts/IShopLogic.cs +++ b/Pizzeria/PizzeriaContracts/BusinessLogicsContracts/IShopLogic.cs @@ -17,5 +17,6 @@ namespace PizzeriaContracts.BusinessLogicsContracts bool Update(ShopBindingModel model); bool Delete(ShopBindingModel model); bool MakeSupply(SupplyBindingModel model); + bool Sale(SupplySearchModel model); } } diff --git a/Pizzeria/PizzeriaContracts/SearchModels/SupplySearchModel.cs b/Pizzeria/PizzeriaContracts/SearchModels/SupplySearchModel.cs new file mode 100644 index 0000000..ea14616 --- /dev/null +++ b/Pizzeria/PizzeriaContracts/SearchModels/SupplySearchModel.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PizzeriaContracts.SearchModels +{ + public class SupplySearchModel + { + public int? PizzaId { get; set; } + public int? Count { get; set; } + } +} diff --git a/Pizzeria/PizzeriaContracts/StoragesContracts/IShopStorage.cs b/Pizzeria/PizzeriaContracts/StoragesContracts/IShopStorage.cs index 440c2fe..5194539 100644 --- a/Pizzeria/PizzeriaContracts/StoragesContracts/IShopStorage.cs +++ b/Pizzeria/PizzeriaContracts/StoragesContracts/IShopStorage.cs @@ -17,5 +17,7 @@ namespace PizzeriaContracts.StoragesContracts ShopViewModel? Insert(ShopBindingModel model); ShopViewModel? Update(ShopBindingModel model); ShopViewModel? Delete(ShopBindingModel model); + bool Sale(SupplySearchModel model); + bool RestockingShops(SupplyBindingModel model); } } diff --git a/Pizzeria/PizzeriaContracts/ViewModels/ShopViewModel.cs b/Pizzeria/PizzeriaContracts/ViewModels/ShopViewModel.cs index eef6457..d14e944 100644 --- a/Pizzeria/PizzeriaContracts/ViewModels/ShopViewModel.cs +++ b/Pizzeria/PizzeriaContracts/ViewModels/ShopViewModel.cs @@ -18,5 +18,7 @@ namespace PizzeriaContracts.ViewModels [DisplayName("Дата открытия")] public DateTime OpeningDate { get; set; } public Dictionary ShopPizzas { get; set; } = new(); + [DisplayName("Вместимость")] + public int PizzaMaxCount { get; set; } } } diff --git a/Pizzeria/PizzeriaFileImplement/DataFileSingleton.cs b/Pizzeria/PizzeriaFileImplement/DataFileSingleton.cs index 51fa506..28ee66b 100644 --- a/Pizzeria/PizzeriaFileImplement/DataFileSingleton.cs +++ b/Pizzeria/PizzeriaFileImplement/DataFileSingleton.cs @@ -9,9 +9,12 @@ namespace PizzeriaFileImplement private readonly string ComponentFileName = "Component.xml"; private readonly string OrderFileName = "Order.xml"; private readonly string PizzaFileName = "Pizza.xml"; + private readonly string ShopFileName = "Shop.xml"; public List Components { get; private set; } public List Orders { get; private set; } public List Pizzas { get; private set; } + public List Shops { get; private set; } + public static DataFileSingleton GetInstance() { if (instance == null) @@ -23,11 +26,14 @@ namespace PizzeriaFileImplement public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement); public void SavePizzas() => SaveData(Pizzas, PizzaFileName, "Pizzas", 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)!)!; Pizzas = LoadData(PizzaFileName, "Pizza", x => Pizza.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) { diff --git a/Pizzeria/PizzeriaFileImplement/Implements/ShopStorage.cs b/Pizzeria/PizzeriaFileImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..551db26 --- /dev/null +++ b/Pizzeria/PizzeriaFileImplement/Implements/ShopStorage.cs @@ -0,0 +1,144 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.SearchModels; +using PizzeriaContracts.StoragesContracts; +using PizzeriaContracts.ViewModels; +using PizzeriaFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PizzeriaFileImplement.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.PizzaId.HasValue || !model.Count.HasValue) + return false; + int remainingSpace = source.Shops.Select(x => x.Pizzas.ContainsKey(model.PizzaId.Value) ? x.Pizzas[model.PizzaId.Value] : 0).Sum(); + if(remainingSpace < model.Count) + { + return false; + } + var shops = source.Shops.Where(x => x.Pizzas.ContainsKey(model.PizzaId.Value)).OrderByDescending(x => x.Pizzas[model.PizzaId.Value]).ToList(); + foreach (var shop in shops) + { + int residue = model.Count.Value - shop.Pizzas[model.PizzaId.Value]; + if (residue > 0) + { + shop.Pizzas.Remove(model.PizzaId.Value); + shop.PizzasUpdate(); + model.Count = residue; + } + else + { + if (residue == 0) + { + shop.Pizzas.Remove(model.PizzaId.Value); + } + else + { + shop.Pizzas[model.PizzaId.Value] = -residue; + } + shop.PizzasUpdate(); + source.SaveShops(); + return true; + } + } + source.SaveShops(); + return false; + } + public bool RestockingShops(SupplyBindingModel model) + { + if (model==null || source.Shops.Select(x => x.PizzaMaxCount - x.ShopPizzas.Select(y => y.Value.Item2).Sum()).Sum() < model.Count) + { + return false; + } + foreach (Shop shop in source.Shops) + { + int free_places = shop.PizzaMaxCount - shop.ShopPizzas.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.Pizzas.ContainsKey(model.PizzaId)) + { + shop.Pizzas[model.PizzaId] += free_places; + } + else + { + shop.Pizzas.Add(model.PizzaId, free_places); + } + shop.PizzasUpdate(); + if (model.Count == 0) + { + source.SaveShops(); + return true; + } + } + return false; + } + } +} diff --git a/Pizzeria/PizzeriaFileImplement/Models/Shop.cs b/Pizzeria/PizzeriaFileImplement/Models/Shop.cs new file mode 100644 index 0000000..16310cf --- /dev/null +++ b/Pizzeria/PizzeriaFileImplement/Models/Shop.cs @@ -0,0 +1,102 @@ +using PizzeriaDataModels.Models; +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace PizzeriaFileImplement.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 Pizzas { get; private set; } = new(); + private Dictionary? _shopPizzas = null; + public Dictionary ShopPizzas { + get { + if (_shopPizzas == null) + { + var source = DataFileSingleton.GetInstance(); + _shopPizzas = Pizzas.ToDictionary(x => x.Key, y => ((source.Pizzas.FirstOrDefault(z => z.Id == y.Key) as IPizzaModel)!, y.Value)); + } + return _shopPizzas; + } + } + public int PizzaMaxCount { 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, + Pizzas = model.ShopPizzas.ToDictionary(x => x.Key, x => x.Value.Item2), + PizzaMaxCount = model.PizzaMaxCount + }; + } + 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), + Pizzas = element.Element("ShopPizzas")!.Elements("ShopPizza")!.ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), + x => Convert.ToInt32(x.Element("Value")?.Value)), + PizzaMaxCount = Convert.ToInt32(element.Element("PizzaMaxCount")!.Value) + }; + } + public void Update(ShopBindingModel? model) + { + if (model == null) + { + return; + } + ShopName = model.ShopName; + Adress = model.Adress; + OpeningDate = model.OpeningDate; + PizzaMaxCount = model.PizzaMaxCount; + Pizzas = model.ShopPizzas.ToDictionary(x => x.Key, x => x.Value.Item2); + _shopPizzas = null; + } + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Adress = Adress, + OpeningDate = OpeningDate, + ShopPizzas = ShopPizzas, + PizzaMaxCount = PizzaMaxCount + }; + + public XElement GetXElement => new("Shop", + new XAttribute("Id", Id), + new XElement("ShopName",ShopName), + new XElement("Adress", Adress), + new XElement("OpeningDate", OpeningDate.ToString()), + new XElement("ShopPizzas", Pizzas.Select( + x => new XElement("ShopPizza",new XElement("Key",x.Key),new XElement("Value",x.Value))).ToArray()), + new XElement("PizzaMaxCount", PizzaMaxCount.ToString()) + ); + + public void PizzasUpdate() { + _shopPizzas = null; + } + } +} diff --git a/Pizzeria/PizzeriaListImplement/Implements/ShopStorage.cs b/Pizzeria/PizzeriaListImplement/Implements/ShopStorage.cs index 857ee5a..c8faced 100644 --- a/Pizzeria/PizzeriaListImplement/Implements/ShopStorage.cs +++ b/Pizzeria/PizzeriaListImplement/Implements/ShopStorage.cs @@ -102,5 +102,15 @@ namespace PizzeriaListImplement.Implements } return null; } + + public bool Sale(SupplySearchModel model) + { + throw new NotImplementedException(); + } + + public bool RestockingShops(SupplyBindingModel model) + { + throw new NotImplementedException(); + } } }