From 40a93a182ae6bd04bc610cfa9a1b9e55d68fa268 Mon Sep 17 00:00:00 2001 From: Viltskaa Date: Sat, 11 Mar 2023 21:23:26 +0400 Subject: [PATCH] 2 hard lab --- .../.idea.SushiBar/.idea/indexLayout.xml | 8 + .../.idea/projectSettingsUpdater.xml | 6 + SushiBar/.idea/.idea.SushiBar/.idea/vcs.xml | 6 + SushiBar/SushiBar/FormMain.Designer.cs | 19 ++- SushiBar/SushiBar/FormMain.cs | 10 ++ SushiBar/SushiBar/FormStore.Designer.cs | 54 ++++-- SushiBar/SushiBar/FormStore.cs | 22 +-- SushiBar/SushiBar/FormStore.resx | 9 + SushiBar/SushiBar/FormStoreReplenishment.cs | 2 +- SushiBar/SushiBar/FormStoreSell.Designer.cs | 110 ++++++++++++ SushiBar/SushiBar/FormStoreSell.cs | 84 +++++++++ SushiBar/SushiBar/FormStoreSell.resx | 60 +++++++ .../BusinessLogics/OrderLogic.cs | 25 ++- .../BusinessLogics/StoreLogic.cs | 119 ++++++++++++- .../BindingModels/StoreBindingModel.cs | 5 +- .../BusinessLogicsContracts/IStoreLogic.cs | 4 +- .../StoragesContracts/IStoreStorage.cs | 2 + .../ViewModels/StoreViewModel.cs | 13 +- .../DataFileSingleton.cs | 19 ++- .../Implements/StoreStorage.cs | 161 ++++++++++++++++++ .../SushiBarFileImplement/Models/Suhsi.cs | 3 +- SushiBar/SushiBarModels/Models/IStoreModel.cs | 3 +- .../Implements/StoreStorage.cs | 6 + .../SushibarListImplement/Models/Store.cs | 11 +- 24 files changed, 700 insertions(+), 61 deletions(-) create mode 100644 SushiBar/.idea/.idea.SushiBar/.idea/indexLayout.xml create mode 100644 SushiBar/.idea/.idea.SushiBar/.idea/projectSettingsUpdater.xml create mode 100644 SushiBar/.idea/.idea.SushiBar/.idea/vcs.xml create mode 100644 SushiBar/SushiBar/FormStoreSell.Designer.cs create mode 100644 SushiBar/SushiBar/FormStoreSell.cs create mode 100644 SushiBar/SushiBar/FormStoreSell.resx create mode 100644 SushiBar/SushiBarFileImplement/Implements/StoreStorage.cs diff --git a/SushiBar/.idea/.idea.SushiBar/.idea/indexLayout.xml b/SushiBar/.idea/.idea.SushiBar/.idea/indexLayout.xml new file mode 100644 index 0000000..7b08163 --- /dev/null +++ b/SushiBar/.idea/.idea.SushiBar/.idea/indexLayout.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/SushiBar/.idea/.idea.SushiBar/.idea/projectSettingsUpdater.xml b/SushiBar/.idea/.idea.SushiBar/.idea/projectSettingsUpdater.xml new file mode 100644 index 0000000..4bb9f4d --- /dev/null +++ b/SushiBar/.idea/.idea.SushiBar/.idea/projectSettingsUpdater.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/SushiBar/.idea/.idea.SushiBar/.idea/vcs.xml b/SushiBar/.idea/.idea.SushiBar/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/SushiBar/.idea/.idea.SushiBar/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/SushiBar/SushiBar/FormMain.Designer.cs b/SushiBar/SushiBar/FormMain.Designer.cs index 4a260c2..aa69afe 100644 --- a/SushiBar/SushiBar/FormMain.Designer.cs +++ b/SushiBar/SushiBar/FormMain.Designer.cs @@ -40,6 +40,7 @@ this.buttonIssue = new System.Windows.Forms.Button(); this.buttonReload = new System.Windows.Forms.Button(); this.buttonReplenishment = new System.Windows.Forms.Button(); + this.ButtonSell = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); this.menuStrip1.SuspendLayout(); this.SuspendLayout(); @@ -76,21 +77,21 @@ // componentsToolStripMenuItem // this.componentsToolStripMenuItem.Name = "componentsToolStripMenuItem"; - this.componentsToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.componentsToolStripMenuItem.Size = new System.Drawing.Size(143, 22); this.componentsToolStripMenuItem.Text = "Components"; this.componentsToolStripMenuItem.Click += new System.EventHandler(this.ComponentsToolStripMenuItem_Click); // // sushiToolStripMenuItem // this.sushiToolStripMenuItem.Name = "sushiToolStripMenuItem"; - this.sushiToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.sushiToolStripMenuItem.Size = new System.Drawing.Size(143, 22); this.sushiToolStripMenuItem.Text = "Sushi"; this.sushiToolStripMenuItem.Click += new System.EventHandler(this.SushiToolStripMenuItem_Click); // // storeToolStripMenuItem // this.storeToolStripMenuItem.Name = "storeToolStripMenuItem"; - this.storeToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.storeToolStripMenuItem.Size = new System.Drawing.Size(143, 22); this.storeToolStripMenuItem.Text = "Store"; this.storeToolStripMenuItem.Click += new System.EventHandler(this.StoreToolStripMenuItem_Click); // @@ -154,11 +155,22 @@ this.buttonReplenishment.UseVisualStyleBackColor = true; this.buttonReplenishment.Click += new System.EventHandler(this.ButtonReplenishment_Click); // + // ButtonSell + // + this.ButtonSell.Location = new System.Drawing.Point(674, 386); + this.ButtonSell.Name = "ButtonSell"; + this.ButtonSell.Size = new System.Drawing.Size(114, 23); + this.ButtonSell.TabIndex = 8; + this.ButtonSell.Text = "Sell"; + this.ButtonSell.UseVisualStyleBackColor = true; + this.ButtonSell.Click += new System.EventHandler(this.ButtonSell_Click); + // // FormMain // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(800, 450); + this.Controls.Add(this.ButtonSell); this.Controls.Add(this.buttonReplenishment); this.Controls.Add(this.buttonReload); this.Controls.Add(this.buttonIssue); @@ -193,5 +205,6 @@ private ToolStripMenuItem sushiToolStripMenuItem; private ToolStripMenuItem storeToolStripMenuItem; private Button buttonReplenishment; + private Button ButtonSell; } } \ No newline at end of file diff --git a/SushiBar/SushiBar/FormMain.cs b/SushiBar/SushiBar/FormMain.cs index 2ca647b..89dd0e9 100644 --- a/SushiBar/SushiBar/FormMain.cs +++ b/SushiBar/SushiBar/FormMain.cs @@ -190,5 +190,15 @@ namespace SushiBar form.ShowDialog(); } } + + private void ButtonSell_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormStoreSell)); + + if (service is FormStoreSell form) + { + form.ShowDialog(); + } + } } } diff --git a/SushiBar/SushiBar/FormStore.Designer.cs b/SushiBar/SushiBar/FormStore.Designer.cs index 9d9db1d..1817a92 100644 --- a/SushiBar/SushiBar/FormStore.Designer.cs +++ b/SushiBar/SushiBar/FormStore.Designer.cs @@ -29,6 +29,9 @@ private void InitializeComponent() { this.dataGridView = new System.Windows.Forms.DataGridView(); + this.SushiName = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.Cost = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.Count = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.comboBoxName = new System.Windows.Forms.ComboBox(); this.textBoxAddress = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); @@ -36,10 +39,10 @@ this.buttonSave = new System.Windows.Forms.Button(); this.buttonCancel = new System.Windows.Forms.Button(); this.dateTimePicker = new System.Windows.Forms.DateTimePicker(); - this.SushiName = new System.Windows.Forms.DataGridViewTextBoxColumn(); - this.Cost = new System.Windows.Forms.DataGridViewTextBoxColumn(); - this.Count = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.labelMaxSushi = new System.Windows.Forms.Label(); + this.maxSushi = new System.Windows.Forms.NumericUpDown(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); + ((System.ComponentModel.ISupportInitialize)(this.maxSushi)).BeginInit(); this.SuspendLayout(); // // dataGridView @@ -55,6 +58,22 @@ this.dataGridView.Size = new System.Drawing.Size(598, 426); this.dataGridView.TabIndex = 0; // + // SushiName + // + this.SushiName.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill; + this.SushiName.HeaderText = "Sushi"; + this.SushiName.Name = "SushiName"; + // + // Cost + // + this.Cost.HeaderText = "Cost"; + this.Cost.Name = "Cost"; + // + // Count + // + this.Count.HeaderText = "Count"; + this.Count.Name = "Count"; + // // comboBoxName // this.comboBoxName.FormattingEnabled = true; @@ -116,27 +135,29 @@ this.dateTimePicker.Size = new System.Drawing.Size(172, 23); this.dateTimePicker.TabIndex = 8; // - // SushiName + // labelMaxSushi // - this.SushiName.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill; - this.SushiName.HeaderText = "Sushi"; - this.SushiName.Name = "SushiName"; + this.labelMaxSushi.AutoSize = true; + this.labelMaxSushi.Location = new System.Drawing.Point(616, 126); + this.labelMaxSushi.Name = "labelMaxSushi"; + this.labelMaxSushi.Size = new System.Drawing.Size(61, 15); + this.labelMaxSushi.TabIndex = 9; + this.labelMaxSushi.Text = "Max Suhsi"; // - // Cost + // maxSushi // - this.Cost.HeaderText = "Cost"; - this.Cost.Name = "Cost"; - // - // Count - // - this.Count.HeaderText = "Count"; - this.Count.Name = "Count"; + this.maxSushi.Location = new System.Drawing.Point(616, 144); + this.maxSushi.Name = "maxSushi"; + this.maxSushi.Size = new System.Drawing.Size(172, 23); + this.maxSushi.TabIndex = 10; // // FormStore // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(800, 450); + this.Controls.Add(this.maxSushi); + this.Controls.Add(this.labelMaxSushi); this.Controls.Add(this.dateTimePicker); this.Controls.Add(this.buttonCancel); this.Controls.Add(this.buttonSave); @@ -149,6 +170,7 @@ this.Text = "FormStore"; this.Load += new System.EventHandler(this.FormStore_Load); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); + ((System.ComponentModel.ISupportInitialize)(this.maxSushi)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); @@ -167,5 +189,7 @@ private DataGridViewTextBoxColumn SushiName; private DataGridViewTextBoxColumn Cost; private DataGridViewTextBoxColumn Count; + private Label labelMaxSushi; + private NumericUpDown maxSushi; } } \ No newline at end of file diff --git a/SushiBar/SushiBar/FormStore.cs b/SushiBar/SushiBar/FormStore.cs index 035995c..4aae052 100644 --- a/SushiBar/SushiBar/FormStore.cs +++ b/SushiBar/SushiBar/FormStore.cs @@ -35,12 +35,13 @@ namespace SushiBar if (model != null) { comboBoxName.Text = model.StoreName; - textBoxAddress.Text = model.StoreAdress; + textBoxAddress.Text = model.StoreAddress; dateTimePicker.Text = Convert.ToString(model.OpeningDate); + maxSushi.Value = model.maxSushi; dataGridView.Rows.Clear(); foreach (var el in model.Sushis.Values) { - dataGridView.Rows.Add(new object[] { el.Item1.SushiName, el.Item1.Price, el.Item2 }); + dataGridView.Rows.Add(el.Item1.SushiName, el.Item1.Price, el.Item2); } } _logger.LogInformation("Load stores"); @@ -52,7 +53,7 @@ namespace SushiBar } } - private IStoreModel? GetStore(int id) + private IStoreModel? GetStore(int Id) { if (_listStores == null) { @@ -60,7 +61,7 @@ namespace SushiBar } foreach (var elem in _listStores) { - if (elem.Id == id) + if (elem.Id == Id) { return elem; } @@ -90,15 +91,16 @@ namespace SushiBar StoreBindingModel model = new() { StoreName = comboBoxName.Text, - StoreAdress = textBoxAddress.Text, - OpeningDate = dateTime + StoreAddress = textBoxAddress.Text, + OpeningDate = dateTime, + maxSushi = (int)maxSushi.Value }; - var search_model = GetStore(id); - bool operationResult = false; + var searchModel = GetStore(id); + bool operationResult; - if (search_model != null) + if (searchModel != null) { - model.Id = search_model.Id; + model.Id = searchModel.Id; operationResult = _logic.Update(model); } else diff --git a/SushiBar/SushiBar/FormStore.resx b/SushiBar/SushiBar/FormStore.resx index 769b272..538d7b3 100644 --- a/SushiBar/SushiBar/FormStore.resx +++ b/SushiBar/SushiBar/FormStore.resx @@ -66,4 +66,13 @@ True + + True + + + True + + + True + \ No newline at end of file diff --git a/SushiBar/SushiBar/FormStoreReplenishment.cs b/SushiBar/SushiBar/FormStoreReplenishment.cs index 36c2b40..0200c4b 100644 --- a/SushiBar/SushiBar/FormStoreReplenishment.cs +++ b/SushiBar/SushiBar/FormStoreReplenishment.cs @@ -64,7 +64,7 @@ namespace SushiBar throw new Exception("Sushi is not found. Additional info below."); } - var resultOperation = _storeLogic.AddPackage( + var resultOperation = _storeLogic.SupplySushi( model: new() { Id = (int)comboBoxStore.SelectedValue }, sushi: sushi, quantity: Convert.ToInt32(textBoxCount.Text) diff --git a/SushiBar/SushiBar/FormStoreSell.Designer.cs b/SushiBar/SushiBar/FormStoreSell.Designer.cs new file mode 100644 index 0000000..5230d39 --- /dev/null +++ b/SushiBar/SushiBar/FormStoreSell.Designer.cs @@ -0,0 +1,110 @@ +namespace SushiBar +{ + partial class FormStoreSell + { + /// + /// 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.SushiNames = new System.Windows.Forms.ComboBox(); + this.labelSushi = new System.Windows.Forms.Label(); + this.Count = new System.Windows.Forms.NumericUpDown(); + this.buttonSell = new System.Windows.Forms.Button(); + this.buttonCancel = new System.Windows.Forms.Button(); + ((System.ComponentModel.ISupportInitialize)(this.Count)).BeginInit(); + this.SuspendLayout(); + // + // SushiNames + // + this.SushiNames.FormattingEnabled = true; + this.SushiNames.Location = new System.Drawing.Point(12, 27); + this.SushiNames.Name = "SushiNames"; + this.SushiNames.Size = new System.Drawing.Size(172, 23); + this.SushiNames.TabIndex = 0; + // + // labelSushi + // + this.labelSushi.AutoSize = true; + this.labelSushi.Location = new System.Drawing.Point(12, 9); + this.labelSushi.Name = "labelSushi"; + this.labelSushi.Size = new System.Drawing.Size(35, 15); + this.labelSushi.TabIndex = 1; + this.labelSushi.Text = "Sushi"; + // + // Count + // + this.Count.Location = new System.Drawing.Point(12, 56); + this.Count.Name = "Count"; + this.Count.Size = new System.Drawing.Size(172, 23); + this.Count.TabIndex = 2; + // + // buttonSell + // + this.buttonSell.Location = new System.Drawing.Point(12, 85); + this.buttonSell.Name = "buttonSell"; + this.buttonSell.Size = new System.Drawing.Size(82, 23); + this.buttonSell.TabIndex = 3; + this.buttonSell.Text = "Sell"; + this.buttonSell.UseVisualStyleBackColor = true; + this.buttonSell.Click += new System.EventHandler(this.ButtonSell_Click); + // + // buttonCancel + // + this.buttonCancel.Location = new System.Drawing.Point(100, 85); + this.buttonCancel.Name = "buttonCancel"; + this.buttonCancel.Size = new System.Drawing.Size(84, 23); + this.buttonCancel.TabIndex = 4; + this.buttonCancel.Text = "Cancel"; + this.buttonCancel.UseVisualStyleBackColor = true; + this.buttonCancel.Click += new System.EventHandler(this.ButtonCancel_Click); + // + // FormStoreSell + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(198, 118); + this.Controls.Add(this.buttonCancel); + this.Controls.Add(this.buttonSell); + this.Controls.Add(this.Count); + this.Controls.Add(this.labelSushi); + this.Controls.Add(this.SushiNames); + this.Name = "FormStoreSell"; + this.Text = "FormStoreSell"; + this.Load += new System.EventHandler(this.FormStoreSell_Load); + ((System.ComponentModel.ISupportInitialize)(this.Count)).EndInit(); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private ComboBox SushiNames; + private Label labelSushi; + private NumericUpDown Count; + private Button buttonSell; + private Button buttonCancel; + } +} \ No newline at end of file diff --git a/SushiBar/SushiBar/FormStoreSell.cs b/SushiBar/SushiBar/FormStoreSell.cs new file mode 100644 index 0000000..7b7b53d --- /dev/null +++ b/SushiBar/SushiBar/FormStoreSell.cs @@ -0,0 +1,84 @@ +using Microsoft.Extensions.Logging; +using SushiBarContracts.BindingModels; +using SushiBarContracts.BusinessLogicsContracts; + +namespace SushiBar +{ + public partial class FormStoreSell : Form + { + private readonly ILogger _logger; + private readonly ISushiLogic _logicSushi; + private readonly IStoreLogic _logicStore; + + public FormStoreSell(ILogger logger, ISushiLogic sushiLogic, IStoreLogic storeLogic) + { + InitializeComponent(); + _logger = logger; + _logicSushi = sushiLogic; + _logicStore = storeLogic; + } + + private void ButtonSell_Click(object sender, EventArgs e) + { + if (Count.Value == 0) + { + MessageBox.Show("Count must be more then zero!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (SushiNames.SelectedValue == null) + { + MessageBox.Show("Select sushi", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Create sell"); + try + { + var operationResult = _logicStore.SellSushi( + new SushiBindingModel() + { + Id = Convert.ToInt32(SushiNames.SelectedValue) + }, + (int)Count.Value + ); + if (!operationResult) + { + throw new Exception("Error on create sell. Additional info below"); + } + MessageBox.Show("Complete create", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + Close(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error on create"); + MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void ButtonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + + private void FormStoreSell_Load(object sender, EventArgs e) + { + _logger.LogInformation("Load sushi to sell"); + try + { + var list = _logicSushi.ReadList(null); + if (list == null) return; + SushiNames.DisplayMember = "DocumentName"; + SushiNames.ValueMember = "Id"; + SushiNames.DataSource = list; + SushiNames.SelectedItem = null; + + } + catch (Exception ex) + { + _logger.LogError(ex, "Error on loading"); + MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } +} diff --git a/SushiBar/SushiBar/FormStoreSell.resx b/SushiBar/SushiBar/FormStoreSell.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/SushiBar/SushiBar/FormStoreSell.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/SushiBar/SushiBarBusinessLogic/BusinessLogics/OrderLogic.cs b/SushiBar/SushiBarBusinessLogic/BusinessLogics/OrderLogic.cs index 3f1ba18..c2ed8af 100644 --- a/SushiBar/SushiBarBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/SushiBar/SushiBarBusinessLogic/BusinessLogics/OrderLogic.cs @@ -12,11 +12,15 @@ namespace SushiBarBusinessLogic.BusinessLogics { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; + private readonly IStoreLogic _storeLogic; + private readonly ISushiStorage _sushiStorage; - public OrderLogic(ILogger logger, IOrderStorage orderStorage) + public OrderLogic(ILogger logger, IOrderStorage orderStorage, IStoreLogic storeLogic, ISushiStorage sushiStorage) { _logger = logger; _orderStorage = orderStorage; + _storeLogic = storeLogic; + _sushiStorage = sushiStorage; } public bool CreateOrder(OrderBindingModel model) @@ -78,12 +82,29 @@ namespace SushiBarBusinessLogic.BusinessLogics _logger.LogWarning("Status update operation failed"); return false; } + + if (status == OrderStatus.Ready) + { + var sushi = _sushiStorage.GetElement(new() { Id = model.SushiId }); + if (sushi == null) + { + _logger.LogWarning("Status update to " + status + " operation failed. Document not found."); + return false; + } + if (_storeLogic.CheckToSupply(sushi, model.Count) == false) + { + _logger.LogWarning("Status update to " + status + " operation failed. Shop supply error."); + return false; + } + } + model.Status = status; model.DateImplement = order?.DateImplement; - if (model.Status == OrderStatus.Ready) + if (model.Status == OrderStatus.Issued) { model.DateImplement = DateTime.Now; } + if (_orderStorage.Update(model) == null) { model.Status--; diff --git a/SushiBar/SushiBarBusinessLogic/BusinessLogics/StoreLogic.cs b/SushiBar/SushiBarBusinessLogic/BusinessLogics/StoreLogic.cs index 9bcc52c..2f05ae1 100644 --- a/SushiBar/SushiBarBusinessLogic/BusinessLogics/StoreLogic.cs +++ b/SushiBar/SushiBarBusinessLogic/BusinessLogics/StoreLogic.cs @@ -57,10 +57,11 @@ namespace SushiBarBusinessLogic.BusinessLogics _storeStorage.Update(new() { Id = element.Id, - StoreAdress = element.StoreAdress, + StoreAddress = element.StoreAddress, StoreName = element.StoreName, OpeningDate = element.OpeningDate, - Sushis = element.Sushis + Sushis = element.Sushis, + maxSushi = element.maxSushi }); return true; } @@ -93,6 +94,116 @@ namespace SushiBarBusinessLogic.BusinessLogics return true; } + public bool SupplySushi(StoreSearchModel model, ISushiModel sushi, int quantity) + { + if (model == null || sushi == null || quantity <= 0) + { + throw new ArgumentNullException(); + } + + var store = _storeStorage.GetElement(model); + if (store == null) + { + _logger.LogWarning("Required store element not found in storage"); + return false; + } + + _logger.LogInformation("Shop element found. ID: {0}, Name: {1}", store.Id, store.StoreName); + int countSushi = store.Sushis.Sum(s => s.Value.Item2); + if (store.maxSushi + countSushi >= quantity) + { + if (store.Sushis.TryGetValue(sushi.Id, out var pair)) + { + store.Sushis[sushi.Id] = (sushi, quantity + pair.Item2); + _logger.LogInformation("AddPackageInStore. Has been added {quantity} {package} in {StoreName}", quantity, sushi.SushiName, store.StoreName); + } + else + { + store.Sushis[sushi.Id] = (sushi, quantity); + _logger.LogInformation("AddPastryInShop. Has been added {quantity} new Package {package} in {StoreName}", quantity, sushi.SushiName, store.StoreName); + } + + _storeStorage.Update(new() + { + Id = store.Id, + StoreAddress = store.StoreAddress, + StoreName = store.StoreName, + OpeningDate = store.OpeningDate, + Sushis = store.Sushis, + maxSushi = store.maxSushi + }); + } + else + { + _logger.LogWarning("Required shop is overflowed"); + return false; + } + return true; + } + + public bool CheckToSupply(ISushiModel sushi, int quantity) + { + if (quantity <= 0) + { + _logger.LogWarning("Check then supply operation error. Document count < 0."); + return false; + } + + int countSushi = 0; + foreach (var store in _storeStorage.GetFullList()) + { + countSushi += store.maxSushi; + countSushi = store.Sushis.Values.Aggregate(countSushi, (current, valueTuple) => current - valueTuple.Item2); + } + + if (countSushi - quantity < 0) + { + _logger.LogWarning("Check then supply operation error. There's no place for new docs in stores."); + return false; + } + + foreach (var store in _storeStorage.GetFullList()) + { + countSushi = store.Sushis.Values.Aggregate(store.maxSushi, (current, valueTuple) => current - valueTuple.Item2); + if (countSushi == 0) + { + continue; + } + + if (countSushi - quantity >= 0) + { + if (SupplySushi(new() { Id = store.Id }, sushi, countSushi)) + countSushi = 0; + else + { + _logger.LogWarning("Supply error"); + return false; + } + } + if (countSushi - quantity < 0) + { + if (SupplySushi(new StoreSearchModel { Id = store.Id }, sushi, countSushi)) + quantity -= countSushi; + else + { + _logger.LogWarning("Supply error"); + return false; + } + } + if (countSushi <= 0) + { + return true; + } + } + + return false; + } + + public bool SellSushi(ISushiModel model, int quantity) + { + return _storeStorage.SellSushi(model, quantity); + } + public StoreViewModel? ReadElement(StoreSearchModel model) { if (model == null) @@ -117,7 +228,7 @@ namespace SushiBarBusinessLogic.BusinessLogics { _logger.LogInformation("ReadList. StoreName:{StoreName}.Id:{ Id} ", model?.StoreName, model?.Id); - var list = (model == null) ? _storeStorage.GetFullList() : _storeStorage.GetFilteredList(model); + var list = model == null ? _storeStorage.GetFullList() : _storeStorage.GetFilteredList(model); if (list == null) { @@ -163,7 +274,7 @@ namespace SushiBarBusinessLogic.BusinessLogics throw new ArgumentNullException("Store name is empty!", nameof(model.StoreName)); } - _logger.LogInformation("Store. StoreName:{0}.StoreAdress:{1}. Id: {2}", model.StoreName, model.StoreAdress, model.Id); + _logger.LogInformation("Store. StoreName:{0}.StoreAdress:{1}. Id: {2}", model.StoreName, model.StoreAddress, model.Id); var element = _storeStorage.GetElement(new StoreSearchModel { StoreName = model.StoreName diff --git a/SushiBar/SushiBarContracts/BindingModels/StoreBindingModel.cs b/SushiBar/SushiBarContracts/BindingModels/StoreBindingModel.cs index 90814f6..5ad457e 100644 --- a/SushiBar/SushiBarContracts/BindingModels/StoreBindingModel.cs +++ b/SushiBar/SushiBarContracts/BindingModels/StoreBindingModel.cs @@ -2,12 +2,13 @@ namespace SushiBarContracts.BindingModels { - public class StoreBindingModel + public class StoreBindingModel : IStoreModel { public string StoreName { get; set; } = string.Empty; - public string StoreAdress { get; set; } = string.Empty; + public string StoreAddress { get; set; } = string.Empty; public DateTime OpeningDate { get; set; } = DateTime.Now; public Dictionary Sushis { get; set; } = new(); + public int maxSushi { get; set; } public int Id { get; set; } } } diff --git a/SushiBar/SushiBarContracts/BusinessLogicsContracts/IStoreLogic.cs b/SushiBar/SushiBarContracts/BusinessLogicsContracts/IStoreLogic.cs index 9592818..ee423a2 100644 --- a/SushiBar/SushiBarContracts/BusinessLogicsContracts/IStoreLogic.cs +++ b/SushiBar/SushiBarContracts/BusinessLogicsContracts/IStoreLogic.cs @@ -12,6 +12,8 @@ namespace SushiBarContracts.BusinessLogicsContracts bool Create(StoreBindingModel model); bool Update(StoreBindingModel model); bool Delete(StoreBindingModel model); - bool AddPackage(StoreSearchModel model, ISushiModel sushi, int quantity); + bool SupplySushi(StoreSearchModel model, ISushiModel sushi, int quantity); + bool CheckToSupply(ISushiModel sushi, int quantity); + bool SellSushi(ISushiModel model, int quantity); } } diff --git a/SushiBar/SushiBarContracts/StoragesContracts/IStoreStorage.cs b/SushiBar/SushiBarContracts/StoragesContracts/IStoreStorage.cs index 1985f97..33b3c1c 100644 --- a/SushiBar/SushiBarContracts/StoragesContracts/IStoreStorage.cs +++ b/SushiBar/SushiBarContracts/StoragesContracts/IStoreStorage.cs @@ -1,6 +1,7 @@ using SushiBarContracts.BindingModels; using SushiBarContracts.SearchModels; using SushiBarContracts.ViewModels; +using SushiBarDataModels.Models; namespace SushiBarContracts.StoragesContracts { @@ -12,5 +13,6 @@ namespace SushiBarContracts.StoragesContracts StoreViewModel? Insert(StoreBindingModel model); StoreViewModel? Update(StoreBindingModel model); StoreViewModel? Delete(StoreBindingModel model); + bool SellSushi(ISushiModel model, int quantity); } } diff --git a/SushiBar/SushiBarContracts/ViewModels/StoreViewModel.cs b/SushiBar/SushiBarContracts/ViewModels/StoreViewModel.cs index f4e7543..96f0705 100644 --- a/SushiBar/SushiBarContracts/ViewModels/StoreViewModel.cs +++ b/SushiBar/SushiBarContracts/ViewModels/StoreViewModel.cs @@ -5,14 +5,15 @@ namespace SushiBarContracts.ViewModels { public class StoreViewModel : IStoreModel { - public Dictionary Sushis { get; set; } = new(); - public int Id { get; set; } + public Dictionary Sushis { get; init; } = new(); + public int maxSushi { get; set; } + public int Id { get; init; } [DisplayName("Name store")] - public string StoreName { get; set; } = string.Empty; - [DisplayName("Adress store")] - public string StoreAdress { get; set; } = string.Empty; + public string StoreName { get; init; } = string.Empty; + [DisplayName("Address store")] + public string StoreAddress { get; init; } = string.Empty; [DisplayName("Date opening")] - public DateTime OpeningDate { get; set; } = DateTime.Now; + public DateTime OpeningDate { get; init; } = DateTime.Now; } } diff --git a/SushiBar/SushiBarFileImplement/DataFileSingleton.cs b/SushiBar/SushiBarFileImplement/DataFileSingleton.cs index 4e670de..7870eff 100644 --- a/SushiBar/SushiBarFileImplement/DataFileSingleton.cs +++ b/SushiBar/SushiBarFileImplement/DataFileSingleton.cs @@ -9,9 +9,11 @@ namespace SushiBarFileImplement private readonly string ComponentFileName = "Component.xml"; private readonly string OrderFileName = "Order.xml"; private readonly string SushiFileName = "Sushi.xml"; - public List Components { get; private set; } - public List Orders { get; private set; } - public List Sushis { get; private set; } + private readonly string StoreFileName = "Store.xml"; + public List Components { get; } + public List Orders { get; } + public List Sushis { get; } + public List Stores { get; } public static DataFileSingleton GetInstance() { instance ??= new DataFileSingleton(); @@ -20,21 +22,20 @@ namespace SushiBarFileImplement public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement); public void SaveSushis() => SaveData(Sushis, SushiFileName, "Sushis", x => x.GetXElement); public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + public void SaveStores() => SaveData(Stores, StoreFileName, "Store", x => x.GetXElement); + private DataFileSingleton() { Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; Sushis = LoadData(SushiFileName, "Sushi", x => Sushi.Create(x)!)!; Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; + Stores = LoadData(StoreFileName, "Store", x => Store.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(); + return File.Exists(filename) ? XDocument.Load(filename)?.Root?.Elements(xmlNodeName)?.Select(selectFunction)?.ToList() : new List(); } - private static void SaveData(List data, string filename, string xmlNodeName, Func selectFunction) + private static void SaveData(IReadOnlyCollection data, string filename, string xmlNodeName, Func selectFunction) { if (data != null) { diff --git a/SushiBar/SushiBarFileImplement/Implements/StoreStorage.cs b/SushiBar/SushiBarFileImplement/Implements/StoreStorage.cs new file mode 100644 index 0000000..86f7046 --- /dev/null +++ b/SushiBar/SushiBarFileImplement/Implements/StoreStorage.cs @@ -0,0 +1,161 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.SearchModels; +using SushiBarContracts.StoragesContracts; +using SushiBarContracts.ViewModels; +using SushiBarDataModels.Models; +using SushiBarFileImplement.Models; + +namespace SushiBarFileImplement.Implements; + +public class StoreStorage : IStoreStorage +{ + private readonly DataFileSingleton _singleton; + + public StoreStorage() + { + _singleton = DataFileSingleton.GetInstance(); + } + + public List GetFullList() + { + return _singleton.Stores + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(StoreSearchModel model) + { + if (!model.Id.HasValue) + { + return new List(); + } + return _singleton.Stores + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + + public StoreViewModel? GetElement(StoreSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + return _singleton.Stores + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + } + + public StoreViewModel? Insert(StoreBindingModel model) + { + model.Id = _singleton.Stores.Count > 0 ? _singleton.Stores.Max(x => x.Id) + 1 : 1; + var store = Store.Create(model); + if (store == null) + { + return null; + } + _singleton.Stores.Add(store); + _singleton.SaveStores(); + return store.GetViewModel; + } + + public StoreViewModel? Update(StoreBindingModel model) + { + var store = _singleton.Stores.FirstOrDefault(x => x.Id == model.Id); + if (store == null) + { + return null; + } + store.Update(model); + _singleton.SaveStores(); + return store.GetViewModel; + } + + public StoreViewModel? Delete(StoreBindingModel model) + { + var element = _singleton.Stores.FirstOrDefault(x => x.Id == model.Id); + if (element == null) return null; + + _singleton.Stores.Remove(element); + _singleton.SaveStores(); + return element.GetViewModel; + } + + public bool SellSushi(ISushiModel model, int quantity) + { + var sushi = _singleton.Sushis.FirstOrDefault(x => x.Id == model.Id); + int countStore = quantity; + + if (sushi is null) + { + return false; + } + + foreach (var store in _singleton.Stores) + { + foreach (var valueTuple in store.Sushis.Values) + { + if (valueTuple.Item1.Id == sushi.Id) + { + quantity -= valueTuple.Item2; + } + + if (quantity <= 0) + { + break; + } + } + } + + if (quantity > 0) + { + return false; + } + + quantity = countStore; + foreach (var shop in _singleton.Stores) + { + var sushis = shop.Sushis; + + for (var j =1; j < sushis.Count + 1; j++) + { + if (sushis[j].Item1.Id == sushi.Id) + { + while (sushis[j].Item2 > 0 && quantity > 0) + { + var tempItem2 = sushis[j].Item2; + tempItem2--; + quantity--; + sushis[j] = (sushis[j].Item1, tempItem2); + } + } + + if (quantity > 0) continue; + shop.Update(new StoreBindingModel() + { + Id = shop.Id, + StoreName = shop.StoreName, + StoreAddress = shop.StoreAddress, + OpeningDate = shop.OpeningDate, + maxSushi = shop.maxSushi, + Sushis = sushis + }); + _singleton.SaveStores(); + return true; + } + + shop.Update(new StoreBindingModel() + { + Id = shop.Id, + StoreName = shop.StoreName, + StoreAddress = shop.StoreAddress, + OpeningDate = shop.OpeningDate, + maxSushi = shop.maxSushi, + Sushis = sushis + }); + _singleton.SaveStores(); + } + + return quantity <= 0; + } +} \ No newline at end of file diff --git a/SushiBar/SushiBarFileImplement/Models/Suhsi.cs b/SushiBar/SushiBarFileImplement/Models/Suhsi.cs index 005f303..c603262 100644 --- a/SushiBar/SushiBarFileImplement/Models/Suhsi.cs +++ b/SushiBar/SushiBarFileImplement/Models/Suhsi.cs @@ -78,8 +78,7 @@ namespace SushiBarFileImplement.Models new XAttribute("Id", Id), new XElement("SushiName", SushiName), new XElement("Price", Price.ToString()), - new XElement("SushiComponents", Components.Select(x => - new XElement("ProductComponent", + new XElement("SushiComponents", Components.Select(x => new XElement("ProductComponent", new XElement("Key", x.Key), new XElement("Value", x.Value)) ).ToArray()) diff --git a/SushiBar/SushiBarModels/Models/IStoreModel.cs b/SushiBar/SushiBarModels/Models/IStoreModel.cs index 2d31eda..5b3c8b9 100644 --- a/SushiBar/SushiBarModels/Models/IStoreModel.cs +++ b/SushiBar/SushiBarModels/Models/IStoreModel.cs @@ -3,8 +3,9 @@ public interface IStoreModel : IId { public string StoreName { get; } - public string StoreAdress { get; } + public string StoreAddress { get; } DateTime OpeningDate { get; } Dictionary Sushis { get; } + public int maxSushi { get; } } } diff --git a/SushiBar/SushibarListImplement/Implements/StoreStorage.cs b/SushiBar/SushibarListImplement/Implements/StoreStorage.cs index 1adbb11..45c4534 100644 --- a/SushiBar/SushibarListImplement/Implements/StoreStorage.cs +++ b/SushiBar/SushibarListImplement/Implements/StoreStorage.cs @@ -2,6 +2,7 @@ using SushiBarContracts.SearchModels; using SushiBarContracts.StoragesContracts; using SushiBarContracts.ViewModels; +using SushiBarDataModels.Models; using SushibarListImplement.Models; namespace SushibarListImplement.Implements @@ -27,6 +28,11 @@ namespace SushibarListImplement.Implements return null; } + public bool SellSushi(ISushiModel model, int quantity) + { + throw new NotImplementedException(); + } + public StoreViewModel? GetElement(StoreSearchModel model) { if (string.IsNullOrEmpty(model.StoreName) && !model.Id.HasValue) diff --git a/SushiBar/SushibarListImplement/Models/Store.cs b/SushiBar/SushibarListImplement/Models/Store.cs index 5d7300d..b0ad75f 100644 --- a/SushiBar/SushibarListImplement/Models/Store.cs +++ b/SushiBar/SushibarListImplement/Models/Store.cs @@ -7,9 +7,10 @@ namespace SushibarListImplement.Models public class Store : IStoreModel { public string StoreName { get; private set; } = string.Empty; - public string StoreAdress { get; private set; } = string.Empty; + public string StoreAddress { get; private set; } = string.Empty; public DateTime OpeningDate { get; private set; } public Dictionary Sushis { get; private set; } = new(); + public int maxSushi { get; } public int Id { get; private set; } public static Store? Create(StoreBindingModel? model) @@ -22,9 +23,9 @@ namespace SushibarListImplement.Models { Id = model.Id, StoreName = model.StoreName, - StoreAdress = model.StoreAdress, + StoreAddress = model.StoreAddress, OpeningDate = model.OpeningDate, - Sushis = new() + Sushis = new Dictionary() }; } @@ -35,7 +36,7 @@ namespace SushibarListImplement.Models return; } StoreName = model.StoreName; - StoreAdress = model.StoreAdress; + StoreAddress = model.StoreAddress; OpeningDate = model.OpeningDate; Sushis = model.Sushis; } @@ -44,7 +45,7 @@ namespace SushibarListImplement.Models { Id = Id, StoreName = StoreName, - StoreAdress = StoreAdress, + StoreAddress = StoreAddress, OpeningDate = OpeningDate, Sushis = Sushis };