diff --git a/AbstractShopBusinessLogic/BusinessLogics/OrderLogic.cs b/AbstractShopBusinessLogic/BusinessLogics/OrderLogic.cs index 005107e..22ffbf7 100644 --- a/AbstractShopBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/AbstractShopBusinessLogic/BusinessLogics/OrderLogic.cs @@ -3,7 +3,7 @@ using DinerContracts.BusinessLogicsContracts; using DinerContracts.SearchModels; using DinerContracts.StoragesContracts; using DinerContracts.ViewModels; -using DinerShopDataModels.Models; +using DinerDataModels.Models; using DinerDataModels.Enum; using Microsoft.Extensions.Logging; @@ -16,9 +16,6 @@ namespace DinerBusinessLogic.BusinessLogics private readonly IShopStorage _shopStorage; private readonly IShopLogic _shopLogic; - private readonly IShopStorage _shopStorage; - - public OrderLogic(ILogger logger, IOrderStorage orderStorage, IShopStorage shopStorage) { _orderStorage = orderStorage; @@ -51,64 +48,7 @@ namespace DinerBusinessLogic.BusinessLogics } return true; } - public bool CheckSupply(IDinerModel snack, int count) - { - if (count <= 0) - { - _logger.LogWarning("Check then supply operation error. IceCream count < 0."); - return false; - } - int sumCapacity = 0; - int sumCount = 0; - sumCapacity = _shopStorage.GetFullList().Select(x => x.MaxCapacity).Sum(); - sumCount = _shopStorage.GetFullList().Select(x => x.ShopIceCreams.Select(y => y.Value.Item2).Sum()).Sum(); - int freeSpace = sumCapacity - sumCount; - - if (freeSpace - count < 0) - { - _logger.LogWarning("Check then supply operation error. There's no place for new IceCream in shops."); - return false; - } - - foreach (var shop in _shopStorage.GetFullList()) - { - freeSpace = shop.MaxCapacity; - foreach (var doc in shop.ShopIceCreams) - { - freeSpace -= doc.Value.Item2; - } - if (freeSpace == 0) - { - continue; - } - if (freeSpace - count >= 0) - { - if (_shopLogic.MakeSupply(new() { Id = shop.Id }, iceCream, count)) - count = 0; - else - { - _logger.LogWarning("Supply error"); - return false; - } - } - if (freeSpace - count < 0) - { - if (_shopLogic.MakeSupply(new() { Id = shop.Id }, iceCream, freeSpace)) - count -= freeSpace; - else - { - _logger.LogWarning("Supply error"); - return false; - } - } - if (count <= 0) - { - return true; - } - } - return false; - } public bool TakeOrderInWork(OrderBindingModel model) { return ChangeStatus(model, OrderStatus.Выполняется); @@ -118,10 +58,6 @@ namespace DinerBusinessLogic.BusinessLogics return ChangeStatus(model, OrderStatus.Готов); } public bool DeliveryOrder(OrderBindingModel model) - { - return ChangeStatus(model, OrderStatus.Выдан); - } - public bool DeliveryOrder(OrderBindingModel model) { var order = _orderStorage.GetElement(new OrderSearchModel { @@ -139,9 +75,9 @@ namespace DinerBusinessLogic.BusinessLogics { 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 ed9d705..75076cd 100644 --- a/AbstractShopBusinessLogic/BusinessLogics/ShopLogic.cs +++ b/AbstractShopBusinessLogic/BusinessLogics/ShopLogic.cs @@ -159,11 +159,11 @@ namespace DinerBusinessLogic.BusinessLogics } public bool Sale(SupplySearchModel model) { - if (!model.PizzaId.HasValue || !model.Count.HasValue) + if (!model.SnackId.HasValue || !model.Count.HasValue) { return false; } - _logger.LogInformation("Check pizza count in all shops"); + _logger.LogInformation("Check snack count in all shops"); if (_shopStorage.Sale(model)) { _logger.LogInformation("Selling sucsess"); 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 8b18c18..cd25210 100644 --- a/Diner/Diner/FormMain.Designer.cs +++ b/Diner/Diner/FormMain.Designer.cs @@ -41,7 +41,7 @@ магазиныToolStripMenuItem = new ToolStripMenuItem(); toolStripDropDownButton1 = new ToolStripDropDownButton(); поставкаToolStripMenuItem = new ToolStripMenuItem(); - продажаToolStripMenuItem = new ToolStripMenuItem(); + SellToolStripMenuItem = new ToolStripMenuItem(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); toolStrip1.SuspendLayout(); SuspendLayout(); @@ -148,7 +148,7 @@ // toolStripDropDownButton1 // toolStripDropDownButton1.DisplayStyle = ToolStripItemDisplayStyle.Text; - toolStripDropDownButton1.DropDownItems.AddRange(new ToolStripItem[] { поставкаToolStripMenuItem, продажаToolStripMenuItem }); + toolStripDropDownButton1.DropDownItems.AddRange(new ToolStripItem[] { поставкаToolStripMenuItem, SellToolStripMenuItem }); toolStripDropDownButton1.ImageTransparentColor = Color.Magenta; toolStripDropDownButton1.Name = "toolStripDropDownButton1"; toolStripDropDownButton1.Size = new Size(95, 24); @@ -161,12 +161,12 @@ поставкаToolStripMenuItem.Text = "Поставка"; поставкаToolStripMenuItem.Click += transactionToolStripMenuItem_Click; // - // продажаToolStripMenuItem + // SellToolStripMenuItem // - продажаToolStripMenuItem.Name = "продажаToolStripMenuItem"; - продажаToolStripMenuItem.Size = new Size(224, 26); - продажаToolStripMenuItem.Text = "Продажа"; - продажаToolStripMenuItem.Click += SellToolStripMenuItem_Click; + SellToolStripMenuItem.Name = "SellToolStripMenuItem"; + SellToolStripMenuItem.Size = new Size(224, 26); + SellToolStripMenuItem.Text = "Продажа"; + SellToolStripMenuItem.Click += SellToolStripMenuItem_Click; // // FormMain // @@ -205,6 +205,6 @@ private ToolStripMenuItem магазиныToolStripMenuItem; private ToolStripDropDownButton toolStripDropDownButton1; private ToolStripMenuItem поставкаToolStripMenuItem; - 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 19c9fc2..ade35fa 100644 --- a/Diner/Diner/FormMain.cs +++ b/Diner/Diner/FormMain.cs @@ -182,7 +182,7 @@ namespace Diner } private void SellToolStripMenuItem_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormSellPizza)); + var service = Program.ServiceProvider?.GetService(typeof(FormSellSnack)); if (service is FormSellSnack form) { form.ShowDialog(); diff --git a/Diner/Diner/FormSell.Designer.cs b/Diner/Diner/FormSell.Designer.cs deleted file mode 100644 index 95509c4..0000000 --- a/Diner/Diner/FormSell.Designer.cs +++ /dev/null @@ -1,117 +0,0 @@ -namespace IceCreamShop -{ - partial class SellForm - { - /// - /// 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() - { - IceCreamComboBox = new ComboBox(); - IceCreamLabel = new Label(); - CountLabel = new Label(); - CountTextBox = new TextBox(); - SaveButton = new Button(); - CancelButton = new Button(); - SuspendLayout(); - // - // IceCreamComboBox - // - IceCreamComboBox.FormattingEnabled = true; - IceCreamComboBox.Location = new Point(99, 12); - IceCreamComboBox.Name = "IceCreamComboBox"; - IceCreamComboBox.Size = new Size(121, 23); - IceCreamComboBox.TabIndex = 0; - // - // IceCreamLabel - // - IceCreamLabel.AutoSize = true; - IceCreamLabel.Location = new Point(12, 20); - IceCreamLabel.Name = "IceCreamLabel"; - IceCreamLabel.Size = new Size(81, 15); - IceCreamLabel.TabIndex = 1; - IceCreamLabel.Text = "Мороженное"; - // - // CountLabel - // - CountLabel.AutoSize = true; - CountLabel.Location = new Point(21, 49); - CountLabel.Name = "CountLabel"; - CountLabel.Size = new Size(72, 15); - CountLabel.TabIndex = 2; - CountLabel.Text = "Количество"; - // - // CountTextBox - // - CountTextBox.Location = new Point(99, 41); - CountTextBox.Name = "CountTextBox"; - CountTextBox.Size = new Size(121, 23); - CountTextBox.TabIndex = 3; - // - // SaveButton - // - SaveButton.Location = new Point(64, 73); - SaveButton.Name = "SaveButton"; - SaveButton.Size = new Size(75, 23); - SaveButton.TabIndex = 4; - SaveButton.Text = "Сохранить"; - SaveButton.UseVisualStyleBackColor = true; - SaveButton.Click += SaveButton_Click; - // - // CancelButton - // - CancelButton.Location = new Point(145, 73); - CancelButton.Name = "CancelButton"; - CancelButton.Size = new Size(75, 23); - CancelButton.TabIndex = 5; - CancelButton.Text = "Отмена"; - CancelButton.UseVisualStyleBackColor = true; - // - // SellForm - // - AutoScaleDimensions = new SizeF(7F, 15F); - AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(224, 108); - Controls.Add(CancelButton); - Controls.Add(SaveButton); - Controls.Add(CountTextBox); - Controls.Add(CountLabel); - Controls.Add(IceCreamLabel); - Controls.Add(IceCreamComboBox); - Name = "SellForm"; - Text = "Форма продажи"; - ResumeLayout(false); - PerformLayout(); - } - - #endregion - - private ComboBox IceCreamComboBox; - private Label IceCreamLabel; - private Label CountLabel; - private TextBox CountTextBox; - private Button SaveButton; - private Button CancelButton; - } -} \ No newline at end of file diff --git a/Diner/Diner/FormSell.cs b/Diner/Diner/FormSell.cs deleted file mode 100644 index 96143c6..0000000 --- a/Diner/Diner/FormSell.cs +++ /dev/null @@ -1,117 +0,0 @@ -using IceCreamShopContracts.BusinessLogicsContracts; -using IceCreamShopContracts.SearchModels; -using IceCreamShopContracts.ViewModels; -using IceCreamShopDataModels.Models; -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 IceCreamShop -{ - public partial class SellForm : Form - { - private readonly List? _iceCreamList; - IShopLogic _shopLogic; - IIceCreamLogic _iceCreamLogic; - public SellForm(IIceCreamLogic iceCreamLogic, IShopLogic shopLogic) - { - InitializeComponent(); - _shopLogic = shopLogic; - _iceCreamLogic = iceCreamLogic; - _iceCreamList = iceCreamLogic.ReadList(null); - if (_iceCreamList != null) - { - IceCreamComboBox.DisplayMember = "IceCreamName"; - IceCreamComboBox.ValueMember = "Id"; - IceCreamComboBox.DataSource = _iceCreamList; - IceCreamComboBox.SelectedItem = null; - } - } - public int IceCreamId - { - get - { - return - Convert.ToInt32(IceCreamComboBox.SelectedValue); - } - set - { - IceCreamComboBox.SelectedValue = value; - } - } - - public IIceCreamModel? IceCreamModel - { - get - { - if (_iceCreamList == null) - { - return null; - } - foreach (var elem in _iceCreamList) - { - if (elem.Id == IceCreamId) - { - return elem; - } - } - return null; - } - } - - - public int Count - { - get { return Convert.ToInt32(CountTextBox.Text); } - set - { CountTextBox.Text = value.ToString(); } - } - - private void SaveButton_Click(object sender, EventArgs e) - { - if (string.IsNullOrEmpty(CountTextBox.Text)) - { - MessageBox.Show("Заполните поле Количество", "Ошибка", - MessageBoxButtons.OK, MessageBoxIcon.Error); - return; - } - if (IceCreamComboBox.SelectedValue == null) - { - MessageBox.Show("Выберите мороженное", "Ошибка", - MessageBoxButtons.OK, MessageBoxIcon.Error); - return; - } - - try - { - int count = Convert.ToInt32(CountTextBox.Text); - - bool res = _shopLogic.MakeSell( - _iceCreamLogic.ReadElement(new() { Id = Convert.ToInt32(IceCreamComboBox.SelectedValue) }), - count - ); - - if (!res) - { - throw new Exception("Ошибка при продаже."); - } - - MessageBox.Show("Продажа прошла успешно"); - DialogResult = DialogResult.OK; - Close(); - - } - catch (Exception err) - { - MessageBox.Show("Ошибка продажи"); - return; - } - } - } -} diff --git a/Diner/Diner/FormSell.resx b/Diner/Diner/FormSell.resx deleted file mode 100644 index af32865..0000000 --- a/Diner/Diner/FormSell.resx +++ /dev/null @@ -1,120 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 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/FormSellSnack.Designer.cs b/Diner/Diner/FormSellSnack.Designer.cs index 36fbfeb..8145f17 100644 --- a/Diner/Diner/FormSellSnack.Designer.cs +++ b/Diner/Diner/FormSellSnack.Designer.cs @@ -1,4 +1,4 @@ -namespace PizzeriaView +namespace DinerView { partial class FormSellSnack { @@ -101,8 +101,8 @@ Controls.Add(comboBoxSnack); Controls.Add(labelSnack); Name = "FormSellSnack"; - Text = "Продажа пиццы"; - Load += FormSellingSnack_Load; + Text = "Продажа закуски"; + Load += FormSellingPizza_Load; ResumeLayout(false); PerformLayout(); } diff --git a/Diner/Diner/FormSellSnack.cs b/Diner/Diner/FormSellSnack.cs index b6070ae..d6f96a4 100644 --- a/Diner/Diner/FormSellSnack.cs +++ b/Diner/Diner/FormSellSnack.cs @@ -13,7 +13,7 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; -namespace PizzeriaView +namespace DinerView { public partial class FormSellSnack : Form { @@ -28,12 +28,12 @@ namespace PizzeriaView _logicP = logicP; _logicS = logicS; } - private void FormSellingSnack_Load(object sender, EventArgs e) + private void FormSellingPizza_Load(object sender, EventArgs e) { _snackList = _logicP.ReadList(null); if (_snackList != null) { - comboBoxSnack.DisplayMember = "PizzaName"; + comboBoxSnack.DisplayMember = "SnackName"; comboBoxSnack.ValueMember = "Id"; comboBoxSnack.DataSource = _snackList; comboBoxSnack.SelectedItem = null; @@ -52,7 +52,7 @@ namespace PizzeriaView { bool resout = _logicS.Sale(new SupplySearchModel { - PizzaId = Convert.ToInt32(comboBoxSnack.SelectedValue), + SnackId = Convert.ToInt32(comboBoxSnack.SelectedValue), Count = Convert.ToInt32(textBoxCount.Text) }); if (resout) 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 cbde433..aeca90d 100644 --- a/Diner/Diner/Program.cs +++ b/Diner/Diner/Program.cs @@ -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())); + } +}