From 5433ec4b6d65a2dec88a799067f6a4ca83658a36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D1=8F=D1=87=D0=B5=D1=81=D0=BB=D0=B0=D0=B2=20=D0=98?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=BE=D0=B2?= Date: Tue, 6 Feb 2024 14:51:10 +0400 Subject: [PATCH 1/9] done hard 1 --- .../BusinessLogics/ShopLogic.cs | 160 +++++++++++++++ .../BindingModels/ShopBindingModel.cs | 18 ++ .../BindingModels/SupplyBindingModel.cs | 16 ++ .../BusinessLogicsContracts/IShopLogic.cs | 21 ++ .../SearchModels/ShopSearchModel.cs | 14 ++ .../StoragesContracts/IShopStorage.cs | 21 ++ .../ViewModels/ShopViewModel.cs | 22 ++ .../PizzeriaDataModels/Models/IShopModel.cs | 18 ++ .../PizzeriaDataModels/Models/ISupplyModel.cs | 15 ++ .../DataListSingleton.cs | 2 + .../Implements/ShopStorage.cs | 113 ++++++++++ Pizzeria/PizzeriaListImplement/Models/Shop.cs | 55 +++++ .../PizzeriaView/FormCreateSupply.Designer.cs | 143 +++++++++++++ Pizzeria/PizzeriaView/FormCreateSupply.cs | 99 +++++++++ Pizzeria/PizzeriaView/FormCreateSupply.resx | 120 +++++++++++ Pizzeria/PizzeriaView/FormMain.Designer.cs | 34 ++- Pizzeria/PizzeriaView/FormMain.cs | 18 ++ Pizzeria/PizzeriaView/FormShop.Designer.cs | 193 ++++++++++++++++++ Pizzeria/PizzeriaView/FormShop.cs | 128 ++++++++++++ Pizzeria/PizzeriaView/FormShop.resx | 120 +++++++++++ Pizzeria/PizzeriaView/FormShops.Designer.cs | 130 ++++++++++++ Pizzeria/PizzeriaView/FormShops.cs | 117 +++++++++++ Pizzeria/PizzeriaView/FormShops.resx | 120 +++++++++++ Pizzeria/PizzeriaView/Program.cs | 5 + 24 files changed, 1700 insertions(+), 2 deletions(-) create mode 100644 Pizzeria/PizzeriaBusinessLogic/BusinessLogics/ShopLogic.cs create mode 100644 Pizzeria/PizzeriaContracts/BindingModels/ShopBindingModel.cs create mode 100644 Pizzeria/PizzeriaContracts/BindingModels/SupplyBindingModel.cs create mode 100644 Pizzeria/PizzeriaContracts/BusinessLogicsContracts/IShopLogic.cs create mode 100644 Pizzeria/PizzeriaContracts/SearchModels/ShopSearchModel.cs create mode 100644 Pizzeria/PizzeriaContracts/StoragesContracts/IShopStorage.cs create mode 100644 Pizzeria/PizzeriaContracts/ViewModels/ShopViewModel.cs create mode 100644 Pizzeria/PizzeriaDataModels/Models/IShopModel.cs create mode 100644 Pizzeria/PizzeriaDataModels/Models/ISupplyModel.cs create mode 100644 Pizzeria/PizzeriaListImplement/Implements/ShopStorage.cs create mode 100644 Pizzeria/PizzeriaListImplement/Models/Shop.cs create mode 100644 Pizzeria/PizzeriaView/FormCreateSupply.Designer.cs create mode 100644 Pizzeria/PizzeriaView/FormCreateSupply.cs create mode 100644 Pizzeria/PizzeriaView/FormCreateSupply.resx create mode 100644 Pizzeria/PizzeriaView/FormShop.Designer.cs create mode 100644 Pizzeria/PizzeriaView/FormShop.cs create mode 100644 Pizzeria/PizzeriaView/FormShop.resx create mode 100644 Pizzeria/PizzeriaView/FormShops.Designer.cs create mode 100644 Pizzeria/PizzeriaView/FormShops.cs create mode 100644 Pizzeria/PizzeriaView/FormShops.resx diff --git a/Pizzeria/PizzeriaBusinessLogic/BusinessLogics/ShopLogic.cs b/Pizzeria/PizzeriaBusinessLogic/BusinessLogics/ShopLogic.cs new file mode 100644 index 0000000..88dab54 --- /dev/null +++ b/Pizzeria/PizzeriaBusinessLogic/BusinessLogics/ShopLogic.cs @@ -0,0 +1,160 @@ +using Microsoft.Extensions.Logging; +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.BusinessLogicsContracts; +using PizzeriaContracts.SearchModels; +using PizzeriaContracts.StoragesContracts; +using PizzeriaContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PizzeriaBusinessLogic.BusinessLogics +{ + public class ShopLogic : IShopLogic + { + private readonly ILogger _logger; + private readonly IShopStorage _shopStorage; + private readonly IPizzaStorage _pizzaStorage; + + public ShopLogic(ILogger logger, IShopStorage shopStorage, IPizzaStorage pizzaStorage) + { + _logger = logger; + _shopStorage = shopStorage; + _pizzaStorage = pizzaStorage; + } + + public List? ReadList(ShopSearchModel? model) + { + _logger.LogInformation("ReadList. ShopName:{ShopName}.Id:{ Id}", model?.ShopName, model?.Id); + var list = model == null ? _shopStorage.GetFullList() : _shopStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + public ShopViewModel? ReadElement(ShopSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. ShopName:{ShopName}.Id:{ Id}", model.ShopName, model.Id); + var element = _shopStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); + return element; + } + + public bool Create(ShopBindingModel model) + { + CheckModel(model); + if (_shopStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(ShopBindingModel model) + { + CheckModel(model); + if (_shopStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + public bool Delete(ShopBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_shopStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + public bool MakeSupply(SupplyBindingModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (model.Count <= 0) + { + throw new ArgumentException("Количество изделий должно быть больше 0"); + } + var shop = _shopStorage.GetElement(new ShopSearchModel + { + Id = model.ShopId + }); + if (shop == null) + { + throw new ArgumentException("Магазина не существует"); + } + if (shop.ShopPizzas.ContainsKey(model.PizzaId)) + { + var oldValue = shop.ShopPizzas[model.PizzaId]; + oldValue.Item2 += model.Count; + shop.ShopPizzas[model.PizzaId] = oldValue; + } + else + { + var pizza = _pizzaStorage.GetElement(new PizzaSearchModel + { + Id = model.PizzaId + }); + if (pizza == null) + { + throw new ArgumentException($"Поставка: Товар с id:{model.PizzaId} не найденн"); + } + shop.ShopPizzas.Add(model.PizzaId, (pizza, model.Count)); + } + return true; + } + + private void CheckModel(ShopBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.Adress)) + { + throw new ArgumentException("Адрес магазина длжен быть заполнен", nameof(model.Adress)); + } + if (string.IsNullOrEmpty(model.ShopName)) + { + throw new ArgumentException("Название магазина должно быть заполнено", nameof(model.ShopName)); + } + _logger.LogInformation("Shop. ShopName:{ShopName}.Adres:{Adres}.OpeningDate:{OpeningDate}.Id:{ Id}", model.ShopName, model.Adress, model.OpeningDate, model.Id); + var element = _shopStorage.GetElement(new ShopSearchModel + { + ShopName = model.ShopName + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Магазин с таким названием уже есть"); + } + } + } +} diff --git a/Pizzeria/PizzeriaContracts/BindingModels/ShopBindingModel.cs b/Pizzeria/PizzeriaContracts/BindingModels/ShopBindingModel.cs new file mode 100644 index 0000000..a3e34fe --- /dev/null +++ b/Pizzeria/PizzeriaContracts/BindingModels/ShopBindingModel.cs @@ -0,0 +1,18 @@ +using PizzeriaDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PizzeriaContracts.BindingModels +{ + public class ShopBindingModel : IShopModel + { + public int Id { get; set; } + public string ShopName { get; set; } = string.Empty; + public string Adress { get; set; } = string.Empty; + public DateTime OpeningDate { get; set; } = DateTime.Now; + public Dictionary ShopPizzas { get; set; } = new(); + } +} \ No newline at end of file diff --git a/Pizzeria/PizzeriaContracts/BindingModels/SupplyBindingModel.cs b/Pizzeria/PizzeriaContracts/BindingModels/SupplyBindingModel.cs new file mode 100644 index 0000000..2a7b3c7 --- /dev/null +++ b/Pizzeria/PizzeriaContracts/BindingModels/SupplyBindingModel.cs @@ -0,0 +1,16 @@ +using PizzeriaDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PizzeriaContracts.BindingModels +{ + public class SupplyBindingModel : ISupplyModel + { + public int ShopId { get; set; } + public int PizzaId { get; set; } + public int Count { get; set; } + } +} \ No newline at end of file diff --git a/Pizzeria/PizzeriaContracts/BusinessLogicsContracts/IShopLogic.cs b/Pizzeria/PizzeriaContracts/BusinessLogicsContracts/IShopLogic.cs new file mode 100644 index 0000000..ad7a4f7 --- /dev/null +++ b/Pizzeria/PizzeriaContracts/BusinessLogicsContracts/IShopLogic.cs @@ -0,0 +1,21 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.SearchModels; +using PizzeriaContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PizzeriaContracts.BusinessLogicsContracts +{ + public interface IShopLogic + { + List? ReadList(ShopSearchModel? model); + ShopViewModel? ReadElement(ShopSearchModel model); + bool Create(ShopBindingModel model); + bool Update(ShopBindingModel model); + bool Delete(ShopBindingModel model); + bool MakeSupply(SupplyBindingModel model); + } +} \ No newline at end of file diff --git a/Pizzeria/PizzeriaContracts/SearchModels/ShopSearchModel.cs b/Pizzeria/PizzeriaContracts/SearchModels/ShopSearchModel.cs new file mode 100644 index 0000000..cf9b916 --- /dev/null +++ b/Pizzeria/PizzeriaContracts/SearchModels/ShopSearchModel.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 ShopSearchModel + { + public int? Id { get; set; } + public string? ShopName { get; set; } + } +} \ No newline at end of file diff --git a/Pizzeria/PizzeriaContracts/StoragesContracts/IShopStorage.cs b/Pizzeria/PizzeriaContracts/StoragesContracts/IShopStorage.cs new file mode 100644 index 0000000..5ed1d83 --- /dev/null +++ b/Pizzeria/PizzeriaContracts/StoragesContracts/IShopStorage.cs @@ -0,0 +1,21 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.SearchModels; +using PizzeriaContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PizzeriaContracts.StoragesContracts +{ + public interface IShopStorage + { + List GetFullList(); + List GetFilteredList(ShopSearchModel model); + ShopViewModel? GetElement(ShopSearchModel model); + ShopViewModel? Insert(ShopBindingModel model); + ShopViewModel? Update(ShopBindingModel model); + ShopViewModel? Delete(ShopBindingModel model); + } +} \ No newline at end of file diff --git a/Pizzeria/PizzeriaContracts/ViewModels/ShopViewModel.cs b/Pizzeria/PizzeriaContracts/ViewModels/ShopViewModel.cs new file mode 100644 index 0000000..49fe315 --- /dev/null +++ b/Pizzeria/PizzeriaContracts/ViewModels/ShopViewModel.cs @@ -0,0 +1,22 @@ +using PizzeriaDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PizzeriaContracts.ViewModels +{ + public class ShopViewModel : IShopModel + { + public int Id { get; set; } + [DisplayName("Название")] + public string ShopName { get; set; } = string.Empty; + [DisplayName("Адрес")] + public string Adress { get; set; } = string.Empty; + [DisplayName("Дата открытия")] + public DateTime OpeningDate { get; set; } + public Dictionary ShopPizzas { get; set; } = new(); + } +} \ No newline at end of file diff --git a/Pizzeria/PizzeriaDataModels/Models/IShopModel.cs b/Pizzeria/PizzeriaDataModels/Models/IShopModel.cs new file mode 100644 index 0000000..94f43db --- /dev/null +++ b/Pizzeria/PizzeriaDataModels/Models/IShopModel.cs @@ -0,0 +1,18 @@ +using PizzeriaDataModels; +using PizzeriaDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PizzeriaDataModels.Models +{ + public interface IShopModel : IId + { + string ShopName { get; } + string Adress { get; } + DateTime OpeningDate { get; } + Dictionary ShopPizzas { get; } + } +} \ No newline at end of file diff --git a/Pizzeria/PizzeriaDataModels/Models/ISupplyModel.cs b/Pizzeria/PizzeriaDataModels/Models/ISupplyModel.cs new file mode 100644 index 0000000..103ab7b --- /dev/null +++ b/Pizzeria/PizzeriaDataModels/Models/ISupplyModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PizzeriaDataModels.Models +{ + public interface ISupplyModel + { + int ShopId { get; } + int PizzaId { get; } + int Count { get; } + } +} \ No newline at end of file diff --git a/Pizzeria/PizzeriaListImplement/DataListSingleton.cs b/Pizzeria/PizzeriaListImplement/DataListSingleton.cs index f61d7cd..06a7262 100644 --- a/Pizzeria/PizzeriaListImplement/DataListSingleton.cs +++ b/Pizzeria/PizzeriaListImplement/DataListSingleton.cs @@ -13,12 +13,14 @@ namespace PizzeriaListImplement public List Components { get; set; } public List Orders { get; set; } public List Pizzas { get; set; } + public List Shops { get; set; } private DataListSingleton() { Components = new List(); Orders = new List(); Pizzas = new List(); + Shops = new List(); } public static DataListSingleton GetInstance() diff --git a/Pizzeria/PizzeriaListImplement/Implements/ShopStorage.cs b/Pizzeria/PizzeriaListImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..532e44c --- /dev/null +++ b/Pizzeria/PizzeriaListImplement/Implements/ShopStorage.cs @@ -0,0 +1,113 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.SearchModels; +using PizzeriaContracts.StoragesContracts; +using PizzeriaContracts.ViewModels; +using PizzeriaListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PizzeriaListImplement.Implements +{ + public class ShopStorage : IShopStorage + { + private readonly DataListSingleton _source; + + public ShopStorage() + { + _source = DataListSingleton.GetInstance(); + } + + public List GetFullList() + { + var result = new List(); + foreach (var shop in _source.Shops) + { + result.Add(shop.GetViewModel); + } + return result; + } + + public List GetFilteredList(ShopSearchModel model) + { + var result = new List(); + if (string.IsNullOrEmpty(model.ShopName)) + { + return result; + } + foreach (var shop in _source.Shops) + { + if (shop.ShopName.Contains(model.ShopName)) + { + result.Add(shop.GetViewModel); + } + } + return result; + } + + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue) + { + return null; + } + foreach (var shop in _source.Shops) + { + if ((!string.IsNullOrEmpty(model.ShopName) && shop.ShopName == model.ShopName) || + (model.Id.HasValue && shop.Id == model.Id)) + { + return shop.GetViewModel; + } + } + return null; + } + + public ShopViewModel? Insert(ShopBindingModel model) + { + model.Id = 1; + foreach (var shop in _source.Shops) + { + if (model.Id <= shop.Id) + { + model.Id = shop.Id + 1; + } + } + var newShop = Shop.Create(model); + if (newShop == null) + { + return null; + } + _source.Shops.Add(newShop); + return newShop.GetViewModel; + } + + public ShopViewModel? Update(ShopBindingModel model) + { + foreach (var shop in _source.Shops) + { + if (shop.Id == model.Id) + { + shop.Update(model); + return shop.GetViewModel; + } + } + return null; + } + + public ShopViewModel? Delete(ShopBindingModel model) + { + for (int i = 0; i < _source.Shops.Count; ++i) + { + if (_source.Shops[i].Id == model.Id) + { + var element = _source.Shops[i]; + _source.Shops.RemoveAt(i); + return element.GetViewModel; + } + } + return null; + } + } +} \ No newline at end of file diff --git a/Pizzeria/PizzeriaListImplement/Models/Shop.cs b/Pizzeria/PizzeriaListImplement/Models/Shop.cs new file mode 100644 index 0000000..1011e4f --- /dev/null +++ b/Pizzeria/PizzeriaListImplement/Models/Shop.cs @@ -0,0 +1,55 @@ +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; + +namespace PizzeriaListImplement.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 ShopPizzas { get; private set; } = new(); + + 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 + }; + } + + public void Update(ShopBindingModel? model) + { + if (model == null) + { + return; + } + ShopName = model.ShopName; + Adress = model.Adress; + OpeningDate = model.OpeningDate; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Adress = Adress, + OpeningDate = OpeningDate, + ShopPizzas = ShopPizzas + }; + } +} \ No newline at end of file diff --git a/Pizzeria/PizzeriaView/FormCreateSupply.Designer.cs b/Pizzeria/PizzeriaView/FormCreateSupply.Designer.cs new file mode 100644 index 0000000..52fde78 --- /dev/null +++ b/Pizzeria/PizzeriaView/FormCreateSupply.Designer.cs @@ -0,0 +1,143 @@ +namespace PizzeriaView +{ + partial class FormCreateSupply + { + /// + /// 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.comboBoxShop = new System.Windows.Forms.ComboBox(); + this.labelShop = new System.Windows.Forms.Label(); + 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.buttonCancel = new System.Windows.Forms.Button(); + this.buttonSave = new System.Windows.Forms.Button(); + this.SuspendLayout(); + // + // comboBoxShop + // + this.comboBoxShop.FormattingEnabled = true; + this.comboBoxShop.Location = new System.Drawing.Point(115, 12); + this.comboBoxShop.Name = "comboBoxShop"; + this.comboBoxShop.Size = new System.Drawing.Size(344, 28); + this.comboBoxShop.TabIndex = 0; + // + // labelShop + // + this.labelShop.AutoSize = true; + this.labelShop.Location = new System.Drawing.Point(12, 15); + this.labelShop.Name = "labelShop"; + this.labelShop.Size = new System.Drawing.Size(76, 20); + this.labelShop.TabIndex = 1; + this.labelShop.Text = "Магазин: "; + // + // labelPizza + // + this.labelPizza.AutoSize = true; + this.labelPizza.Location = new System.Drawing.Point(12, 49); + this.labelPizza.Name = "labelPizza"; + this.labelPizza.Size = new System.Drawing.Size(75, 20); + this.labelPizza.TabIndex = 2; + this.labelPizza.Text = "Изделие: "; + // + // comboBoxPizza + // + this.comboBoxPizza.FormattingEnabled = true; + this.comboBoxPizza.Location = new System.Drawing.Point(115, 46); + this.comboBoxPizza.Name = "comboBoxPizza"; + this.comboBoxPizza.Size = new System.Drawing.Size(344, 28); + this.comboBoxPizza.TabIndex = 3; + // + // labelCount + // + this.labelCount.AutoSize = true; + this.labelCount.Location = new System.Drawing.Point(12, 83); + this.labelCount.Name = "labelCount"; + this.labelCount.Size = new System.Drawing.Size(97, 20); + this.labelCount.TabIndex = 4; + this.labelCount.Text = "Количество: "; + // + // textBoxCount + // + this.textBoxCount.Location = new System.Drawing.Point(115, 80); + this.textBoxCount.Name = "textBoxCount"; + this.textBoxCount.Size = new System.Drawing.Size(344, 27); + this.textBoxCount.TabIndex = 5; + // + // buttonCancel + // + this.buttonCancel.Location = new System.Drawing.Point(300, 113); + this.buttonCancel.Name = "buttonCancel"; + this.buttonCancel.Size = new System.Drawing.Size(116, 39); + this.buttonCancel.TabIndex = 6; + this.buttonCancel.Text = "Отмена"; + this.buttonCancel.UseVisualStyleBackColor = true; + this.buttonCancel.Click += new System.EventHandler(this.ButtonCancel_Click); + // + // buttonSave + // + this.buttonSave.Location = new System.Drawing.Point(168, 113); + this.buttonSave.Name = "buttonSave"; + this.buttonSave.Size = new System.Drawing.Size(116, 39); + this.buttonSave.TabIndex = 7; + this.buttonSave.Text = "Сохранить"; + this.buttonSave.UseVisualStyleBackColor = true; + this.buttonSave.Click += new System.EventHandler(this.ButtonSave_Click); + // + // FormCreateSupply + // + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(471, 164); + this.Controls.Add(this.buttonSave); + this.Controls.Add(this.buttonCancel); + this.Controls.Add(this.textBoxCount); + this.Controls.Add(this.labelCount); + this.Controls.Add(this.comboBoxPizza); + this.Controls.Add(this.labelPizza); + this.Controls.Add(this.labelShop); + this.Controls.Add(this.comboBoxShop); + this.Name = "FormCreateSupply"; + this.Text = "Создание поставки"; + this.Load += new System.EventHandler(this.FormCreateSupply_Load); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private ComboBox comboBoxShop; + private Label labelShop; + private Label labelPizza; + private ComboBox comboBoxPizza; + private Label labelCount; + private TextBox textBoxCount; + private Button buttonCancel; + private Button buttonSave; + } +} \ No newline at end of file diff --git a/Pizzeria/PizzeriaView/FormCreateSupply.cs b/Pizzeria/PizzeriaView/FormCreateSupply.cs new file mode 100644 index 0000000..1ffd6a5 --- /dev/null +++ b/Pizzeria/PizzeriaView/FormCreateSupply.cs @@ -0,0 +1,99 @@ +using Microsoft.Extensions.Logging; +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.BusinessLogicsContracts; +using PizzeriaContracts.ViewModels; +using System; +using System.Collections; +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; +using static System.Windows.Forms.VisualStyles.VisualStyleElement; + +namespace PizzeriaView +{ + public partial class FormCreateSupply : Form + { + private readonly ILogger _logger; + private readonly IPizzaLogic _logicP; + private readonly IShopLogic _logicS; + private List _shopList = new List(); + private List _pizzaList = new List(); + + public FormCreateSupply(ILogger logger, IPizzaLogic logicP, IShopLogic logicS) + { + InitializeComponent(); + _logger = logger; + _logicP = logicP; + _logicS = logicS; + } + + private void FormCreateSupply_Load(object sender, EventArgs e) + { + _shopList = _logicS.ReadList(null); + _pizzaList = _logicP.ReadList(null); + if (_shopList != null) + { + comboBoxShop.DisplayMember = "ShopName"; + comboBoxShop.ValueMember = "Id"; + comboBoxShop.DataSource = _shopList; + comboBoxShop.SelectedItem = null; + _logger.LogInformation("Загрузка магазинов для поставок"); + } + if (_pizzaList != null) + { + comboBoxPizza.DisplayMember = "PizzaName"; + comboBoxPizza.ValueMember = "Id"; + comboBoxPizza.DataSource = _pizzaList; + comboBoxPizza.SelectedItem = null; + _logger.LogInformation("Загрузка пиццы для поставок"); + } + } + + private void ButtonSave_Click(object sender, EventArgs e) + { + if (comboBoxShop.SelectedValue == null) + { + MessageBox.Show("Выберите магазин", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (comboBoxPizza.SelectedValue == null) + { + MessageBox.Show("Выберите изделие", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Создание поставки"); + try + { + var operationResult = _logicS.MakeSupply(new SupplyBindingModel + { + ShopId = Convert.ToInt32(comboBoxShop.SelectedValue), + PizzaId = Convert.ToInt32(comboBoxPizza.SelectedValue), + Count = Convert.ToInt32(textBoxCount.Text) + }); + if (!operationResult) + { + throw new Exception("Ошибка при создании поставки. Дополнительная информация в логах."); + } + MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + Close(); + } + 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/PizzeriaView/FormCreateSupply.resx b/Pizzeria/PizzeriaView/FormCreateSupply.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/Pizzeria/PizzeriaView/FormCreateSupply.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Pizzeria/PizzeriaView/FormMain.Designer.cs b/Pizzeria/PizzeriaView/FormMain.Designer.cs index 572f0a2..c54717f 100644 --- a/Pizzeria/PizzeriaView/FormMain.Designer.cs +++ b/Pizzeria/PizzeriaView/FormMain.Designer.cs @@ -32,6 +32,9 @@ this.bookToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.ingridientsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.pizzasToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.shopsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.operationToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.transactionToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.dataGridView = new System.Windows.Forms.DataGridView(); this.buttonCreateOrder = new System.Windows.Forms.Button(); this.buttonTakeOrderInWork = new System.Windows.Forms.Button(); @@ -46,7 +49,8 @@ // this.menuStrip1.ImageScalingSize = new System.Drawing.Size(20, 20); this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { - this.bookToolStripMenuItem}); + this.bookToolStripMenuItem, + this.operationToolStripMenuItem}); this.menuStrip1.Location = new System.Drawing.Point(0, 0); this.menuStrip1.Name = "menuStrip1"; this.menuStrip1.Padding = new System.Windows.Forms.Padding(5, 2, 0, 2); @@ -58,7 +62,8 @@ // this.bookToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.ingridientsToolStripMenuItem, - this.pizzasToolStripMenuItem}); + this.pizzasToolStripMenuItem, + this.shopsToolStripMenuItem}); this.bookToolStripMenuItem.Name = "bookToolStripMenuItem"; this.bookToolStripMenuItem.Size = new System.Drawing.Size(87, 20); this.bookToolStripMenuItem.Text = "Справочник"; @@ -77,6 +82,28 @@ 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.Text = "Магазины"; + this.shopsToolStripMenuItem.Click += new System.EventHandler(this.shopsToolStripMenuItem_Click); + // + // operationToolStripMenuItem + // + this.operationToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.transactionToolStripMenuItem}); + this.operationToolStripMenuItem.Name = "operationToolStripMenuItem"; + this.operationToolStripMenuItem.Size = new System.Drawing.Size(75, 20); + this.operationToolStripMenuItem.Text = "Операции"; + // + // transactionToolStripMenuItem + // + this.transactionToolStripMenuItem.Name = "transactionToolStripMenuItem"; + this.transactionToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.transactionToolStripMenuItem.Text = "Поставка"; + this.transactionToolStripMenuItem.Click += new System.EventHandler(this.transactionToolStripMenuItem_Click); + // // dataGridView // this.dataGridView.AllowUserToAddRows = false; @@ -183,5 +210,8 @@ private Button buttonOrderReady; private Button buttonIssuedOrder; private Button buttonRef; + private ToolStripMenuItem shopsToolStripMenuItem; + private ToolStripMenuItem operationToolStripMenuItem; + private ToolStripMenuItem transactionToolStripMenuItem; } } \ No newline at end of file diff --git a/Pizzeria/PizzeriaView/FormMain.cs b/Pizzeria/PizzeriaView/FormMain.cs index 02484c4..9381de1 100644 --- a/Pizzeria/PizzeriaView/FormMain.cs +++ b/Pizzeria/PizzeriaView/FormMain.cs @@ -154,5 +154,23 @@ namespace PizzeriaView { LoadData(); } + + private void shopsToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormShops)); + if (service is FormShops form) + { + form.ShowDialog(); + } + } + + private void transactionToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormCreateSupply)); + if (service is FormCreateSupply form) + { + form.ShowDialog(); + } + } } } diff --git a/Pizzeria/PizzeriaView/FormShop.Designer.cs b/Pizzeria/PizzeriaView/FormShop.Designer.cs new file mode 100644 index 0000000..010c144 --- /dev/null +++ b/Pizzeria/PizzeriaView/FormShop.Designer.cs @@ -0,0 +1,193 @@ +namespace PizzeriaView +{ + partial class FormShop + { + /// + /// 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.labelName = new System.Windows.Forms.Label(); + this.textBoxName = new System.Windows.Forms.TextBox(); + this.textBoxAdress = new System.Windows.Forms.TextBox(); + this.labelAdress = new System.Windows.Forms.Label(); + this.buttonCancel = new System.Windows.Forms.Button(); + this.buttonSave = new System.Windows.Forms.Button(); + this.dataGridView = new System.Windows.Forms.DataGridView(); + this.id = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.PizzaName = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.Count = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.label1 = new System.Windows.Forms.Label(); + this.dateTimeOpen = new System.Windows.Forms.DateTimePicker(); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); + this.SuspendLayout(); + // + // labelName + // + this.labelName.AutoSize = true; + this.labelName.Location = new System.Drawing.Point(11, 15); + this.labelName.Name = "labelName"; + this.labelName.Size = new System.Drawing.Size(84, 20); + this.labelName.TabIndex = 0; + this.labelName.Text = "Название: "; + // + // textBoxName + // + this.textBoxName.Location = new System.Drawing.Point(102, 12); + this.textBoxName.Name = "textBoxName"; + this.textBoxName.Size = new System.Drawing.Size(276, 27); + this.textBoxName.TabIndex = 1; + // + // textBoxAdress + // + this.textBoxAdress.Location = new System.Drawing.Point(102, 59); + this.textBoxAdress.Name = "textBoxAdress"; + this.textBoxAdress.Size = new System.Drawing.Size(427, 27); + this.textBoxAdress.TabIndex = 3; + // + // labelAdress + // + this.labelAdress.AutoSize = true; + this.labelAdress.Location = new System.Drawing.Point(11, 61); + this.labelAdress.Name = "labelAdress"; + this.labelAdress.Size = new System.Drawing.Size(58, 20); + this.labelAdress.TabIndex = 2; + this.labelAdress.Text = "Адрес: "; + // + // buttonCancel + // + this.buttonCancel.Location = new System.Drawing.Point(451, 457); + this.buttonCancel.Name = "buttonCancel"; + this.buttonCancel.Size = new System.Drawing.Size(130, 44); + this.buttonCancel.TabIndex = 5; + this.buttonCancel.Text = "Отмена"; + this.buttonCancel.UseVisualStyleBackColor = true; + this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click); + // + // buttonSave + // + this.buttonSave.Location = new System.Drawing.Point(315, 457); + this.buttonSave.Name = "buttonSave"; + this.buttonSave.Size = new System.Drawing.Size(130, 44); + this.buttonSave.TabIndex = 6; + this.buttonSave.Text = "Сохранить"; + this.buttonSave.UseVisualStyleBackColor = true; + this.buttonSave.Click += new System.EventHandler(this.buttonSave_Click); + // + // dataGridView + // + this.dataGridView.AllowUserToAddRows = false; + this.dataGridView.AllowUserToDeleteRows = false; + this.dataGridView.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill; + this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { + this.id, + this.PizzaName, + this.Count}); + this.dataGridView.Location = new System.Drawing.Point(12, 144); + 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.TabIndex = 7; + // + // id + // + this.id.HeaderText = "id"; + this.id.MinimumWidth = 6; + this.id.Name = "id"; + this.id.ReadOnly = true; + this.id.Visible = false; + // + // PizzaName + // + this.PizzaName.HeaderText = "Пицца"; + this.PizzaName.MinimumWidth = 6; + this.PizzaName.Name = "PizzaName"; + this.PizzaName.ReadOnly = true; + // + // Count + // + this.Count.HeaderText = "Количество"; + this.Count.MinimumWidth = 6; + this.Count.Name = "Count"; + this.Count.ReadOnly = true; + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(12, 103); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(110, 20); + this.label1.TabIndex = 8; + this.label1.Text = "Дата открытия"; + // + // dateTimeOpen + // + this.dateTimeOpen.Location = new System.Drawing.Point(128, 103); + this.dateTimeOpen.Name = "dateTimeOpen"; + this.dateTimeOpen.Size = new System.Drawing.Size(401, 27); + this.dateTimeOpen.TabIndex = 9; + // + // 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.dateTimeOpen); + this.Controls.Add(this.label1); + this.Controls.Add(this.dataGridView); + this.Controls.Add(this.buttonSave); + this.Controls.Add(this.buttonCancel); + this.Controls.Add(this.textBoxAdress); + this.Controls.Add(this.labelAdress); + this.Controls.Add(this.textBoxName); + this.Controls.Add(this.labelName); + this.Name = "FormShop"; + this.Text = "Магазин"; + this.Load += new System.EventHandler(this.FormShop_Load); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private Label labelName; + private TextBox textBoxName; + private TextBox textBoxAdress; + private Label labelAdress; + private Button buttonCancel; + private Button buttonSave; + private DataGridView dataGridView; + private DataGridViewTextBoxColumn id; + private DataGridViewTextBoxColumn PizzaName; + private DataGridViewTextBoxColumn Count; + private Label label1; + private DateTimePicker dateTimeOpen; + } +} \ No newline at end of file diff --git a/Pizzeria/PizzeriaView/FormShop.cs b/Pizzeria/PizzeriaView/FormShop.cs new file mode 100644 index 0000000..5d4cde8 --- /dev/null +++ b/Pizzeria/PizzeriaView/FormShop.cs @@ -0,0 +1,128 @@ +using PizzeriaDataModels.Models; +using Microsoft.Extensions.Logging; +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.BusinessLogicsContracts; +using PizzeriaContracts.SearchModels; +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 FormShop : Form + { + private readonly ILogger _logger; + private readonly IShopLogic _logic; + private int? _id; + public int Id { set { _id = value; } } + private Dictionary _ShopPizzas; + private DateTime? _openingDate = null; + + public FormShop(ILogger logger, IShopLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + _ShopPizzas = new Dictionary(); + } + + private void FormShop_Load(object sender, EventArgs e) + { + if (_id.HasValue) + { + _logger.LogInformation("Загрузка магазина"); + try + { + var view = _logic.ReadElement(new ShopSearchModel + { + Id = _id.Value + }); + if (view != null) + { + textBoxName.Text = view.ShopName; + textBoxAdress.Text = view.Adress; + dateTimeOpen.Value = view.OpeningDate; + _ShopPizzas = view.ShopPizzas ?? new Dictionary(); + LoadData(); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки магазина"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + + private void LoadData() + { + _logger.LogInformation("Загрузка изделий в магазине"); + try + { + if (_ShopPizzas != null) + { + dataGridView.Rows.Clear(); + foreach (var sr in _ShopPizzas) + { + dataGridView.Rows.Add(new object[] { sr.Key, sr.Value.Item1.PizzaName, sr.Value.Item2 }); + } + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки изделий магазина"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void buttonSave_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxName.Text)) + { + MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (string.IsNullOrEmpty(textBoxAdress.Text)) + { + MessageBox.Show("Заполните адрес", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Сохранение магазина"); + try + { + var model = new ShopBindingModel + { + Id = _id ?? 0, + ShopName = textBoxName.Text, + Adress = textBoxAdress.Text, + OpeningDate = dateTimeOpen.Value + }; + var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model); + if (!operationResult) + { + throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); + } + MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + Close(); + } + 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/PizzeriaView/FormShop.resx b/Pizzeria/PizzeriaView/FormShop.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/Pizzeria/PizzeriaView/FormShop.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Pizzeria/PizzeriaView/FormShops.Designer.cs b/Pizzeria/PizzeriaView/FormShops.Designer.cs new file mode 100644 index 0000000..3af7c45 --- /dev/null +++ b/Pizzeria/PizzeriaView/FormShops.Designer.cs @@ -0,0 +1,130 @@ +namespace PizzeriaView +{ + partial class FormShops + { + /// + /// 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.ToolsPanel = new System.Windows.Forms.Panel(); + this.buttonRef = new System.Windows.Forms.Button(); + this.buttonDel = new System.Windows.Forms.Button(); + this.buttonUpd = new System.Windows.Forms.Button(); + this.buttonAdd = new System.Windows.Forms.Button(); + this.dataGridView = new System.Windows.Forms.DataGridView(); + this.ToolsPanel.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); + this.SuspendLayout(); + // + // ToolsPanel + // + this.ToolsPanel.Controls.Add(this.buttonRef); + this.ToolsPanel.Controls.Add(this.buttonDel); + this.ToolsPanel.Controls.Add(this.buttonUpd); + this.ToolsPanel.Controls.Add(this.buttonAdd); + this.ToolsPanel.Location = new System.Drawing.Point(608, 12); + this.ToolsPanel.Name = "ToolsPanel"; + this.ToolsPanel.Size = new System.Drawing.Size(180, 426); + this.ToolsPanel.TabIndex = 3; + // + // buttonRef + // + this.buttonRef.Location = new System.Drawing.Point(31, 206); + this.buttonRef.Name = "buttonRef"; + this.buttonRef.Size = new System.Drawing.Size(126, 36); + this.buttonRef.TabIndex = 3; + this.buttonRef.Text = "Обновить"; + this.buttonRef.UseVisualStyleBackColor = true; + this.buttonRef.Click += new System.EventHandler(this.ButtonRef_Click); + // + // buttonDel + // + this.buttonDel.Location = new System.Drawing.Point(31, 142); + this.buttonDel.Name = "buttonDel"; + this.buttonDel.Size = new System.Drawing.Size(126, 36); + this.buttonDel.TabIndex = 2; + this.buttonDel.Text = "Удалить"; + this.buttonDel.UseVisualStyleBackColor = true; + this.buttonDel.Click += new System.EventHandler(this.ButtonDel_Click); + // + // buttonUpd + // + this.buttonUpd.Location = new System.Drawing.Point(31, 76); + this.buttonUpd.Name = "buttonUpd"; + this.buttonUpd.Size = new System.Drawing.Size(126, 36); + this.buttonUpd.TabIndex = 1; + this.buttonUpd.Text = "Изменить"; + this.buttonUpd.UseVisualStyleBackColor = true; + this.buttonUpd.Click += new System.EventHandler(this.ButtonUpd_Click); + // + // buttonAdd + // + this.buttonAdd.Location = new System.Drawing.Point(31, 16); + this.buttonAdd.Name = "buttonAdd"; + this.buttonAdd.Size = new System.Drawing.Size(126, 36); + this.buttonAdd.TabIndex = 0; + this.buttonAdd.Text = "Добавить"; + this.buttonAdd.UseVisualStyleBackColor = true; + this.buttonAdd.Click += new System.EventHandler(this.ButtonAdd_Click); + // + // dataGridView + // + this.dataGridView.AllowUserToAddRows = false; + this.dataGridView.AllowUserToDeleteRows = false; + this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dataGridView.Location = new System.Drawing.Point(12, 12); + this.dataGridView.Name = "dataGridView"; + this.dataGridView.ReadOnly = true; + this.dataGridView.RowHeadersWidth = 51; + this.dataGridView.RowTemplate.Height = 29; + this.dataGridView.Size = new System.Drawing.Size(590, 426); + this.dataGridView.TabIndex = 2; + // + // FormShops + // + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(800, 450); + this.Controls.Add(this.ToolsPanel); + this.Controls.Add(this.dataGridView); + this.Name = "FormShops"; + this.Text = "Магазины"; + this.Load += new System.EventHandler(this.FormShops_Load); + this.ToolsPanel.ResumeLayout(false); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); + this.ResumeLayout(false); + + } + + #endregion + + private Panel ToolsPanel; + private Button buttonRef; + private Button buttonDel; + private Button buttonUpd; + private Button buttonAdd; + private DataGridView dataGridView; + } +} \ No newline at end of file diff --git a/Pizzeria/PizzeriaView/FormShops.cs b/Pizzeria/PizzeriaView/FormShops.cs new file mode 100644 index 0000000..b1f946e --- /dev/null +++ b/Pizzeria/PizzeriaView/FormShops.cs @@ -0,0 +1,117 @@ +using Microsoft.Extensions.Logging; +using Pizzeria; +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.BusinessLogicsContracts; +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 FormShops : Form + { + private readonly ILogger _logger; + private readonly IShopLogic _logic; + + public FormShops(ILogger logger, IShopLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + } + + private void FormShops_Load(object sender, EventArgs e) + { + LoadData(); + } + + private void LoadData() + { + try + { + var list = _logic.ReadList(null); + if (list != null) + { + dataGridView.DataSource = list; + dataGridView.Columns["Id"].Visible = false; + dataGridView.Columns["ShopPizzas"].Visible = false; + dataGridView.Columns["ShopName"].AutoSizeMode = + DataGridViewAutoSizeColumnMode.Fill; + } + _logger.LogInformation("Загрузка магазинов"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки магазинов"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void ButtonAdd_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormShop)); + if (service is FormShop form) + { + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + + private void ButtonUpd_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + var service = Program.ServiceProvider?.GetService(typeof(FormShop)); + if (service is FormShop form) + { + form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + } + + private void ButtonDel_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + _logger.LogInformation("Удаление магазина"); + try + { + if (!_logic.Delete(new ShopBindingModel + { + Id = id + })) + { + throw new Exception("Ошибка при удалении. Дополнительная информация в логах."); + } + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка удаления магазина"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + } + + private void ButtonRef_Click(object sender, EventArgs e) + { + LoadData(); + } + } +} diff --git a/Pizzeria/PizzeriaView/FormShops.resx b/Pizzeria/PizzeriaView/FormShops.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/Pizzeria/PizzeriaView/FormShops.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Pizzeria/PizzeriaView/Program.cs b/Pizzeria/PizzeriaView/Program.cs index 702b489..ae88579 100644 --- a/Pizzeria/PizzeriaView/Program.cs +++ b/Pizzeria/PizzeriaView/Program.cs @@ -49,6 +49,11 @@ namespace Pizzeria services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file -- 2.25.1 From 8f48a325a5f541493df9810f7d60819a8c19bd2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D1=8F=D1=87=D0=B5=D1=81=D0=BB=D0=B0=D0=B2=20=D0=98?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=BE=D0=B2?= Date: Sat, 17 Feb 2024 13:31:30 +0400 Subject: [PATCH 2/9] done --- Pizzeria/Pizzeria.sln | 14 ++- .../DataFileSingleton.cs | 58 ++++++++++++ .../Implements/ComponentStorage.cs | 86 +++++++++++++++++ .../Implements/OrderStorage.cs | 94 +++++++++++++++++++ .../Implements/PizzaStorage.cs | 86 +++++++++++++++++ .../PizzeriaFileImplement/Models/Component.cs | 69 ++++++++++++++ .../PizzeriaFileImplement/Models/Order.cs | 92 ++++++++++++++++++ .../PizzeriaFileImplement/Models/Pizza.cs | 92 ++++++++++++++++++ .../PizzeriaFileImplement.csproj | 14 +++ Pizzeria/PizzeriaView/PizzeriaView.csproj | 1 + Pizzeria/PizzeriaView/Program.cs | 2 +- 11 files changed, 603 insertions(+), 5 deletions(-) create mode 100644 Pizzeria/PizzeriaFileImplement/DataFileSingleton.cs create mode 100644 Pizzeria/PizzeriaFileImplement/Implements/ComponentStorage.cs create mode 100644 Pizzeria/PizzeriaFileImplement/Implements/OrderStorage.cs create mode 100644 Pizzeria/PizzeriaFileImplement/Implements/PizzaStorage.cs create mode 100644 Pizzeria/PizzeriaFileImplement/Models/Component.cs create mode 100644 Pizzeria/PizzeriaFileImplement/Models/Order.cs create mode 100644 Pizzeria/PizzeriaFileImplement/Models/Pizza.cs create mode 100644 Pizzeria/PizzeriaFileImplement/PizzeriaFileImplement.csproj diff --git a/Pizzeria/Pizzeria.sln b/Pizzeria/Pizzeria.sln index d40c84e..94cd565 100644 --- a/Pizzeria/Pizzeria.sln +++ b/Pizzeria/Pizzeria.sln @@ -5,13 +5,15 @@ VisualStudioVersion = 17.7.34024.191 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaView", "PizzeriaView\PizzeriaView.csproj", "{C3B647C4-306F-43B5-BDF2-FF5F3A34364F}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PizzeriaBusinessLogic", "PizzeriaBusinessLogic\PizzeriaBusinessLogic.csproj", "{906C7D67-BBCC-48D7-BB34-62A9A35779A8}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaBusinessLogic", "PizzeriaBusinessLogic\PizzeriaBusinessLogic.csproj", "{906C7D67-BBCC-48D7-BB34-62A9A35779A8}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PizzeriaContracts", "PizzeriaContracts\PizzeriaContracts.csproj", "{A625183B-6EEA-4995-B06D-E10835CEFE9C}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaContracts", "PizzeriaContracts\PizzeriaContracts.csproj", "{A625183B-6EEA-4995-B06D-E10835CEFE9C}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PizzeriaDataModels", "PizzeriaDataModels\PizzeriaDataModels.csproj", "{D0318436-6887-4AD1-92B2-19A7F37240DC}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaDataModels", "PizzeriaDataModels\PizzeriaDataModels.csproj", "{D0318436-6887-4AD1-92B2-19A7F37240DC}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PizzeriaListImplement", "PizzeriaListImplement\PizzeriaListImplement.csproj", "{190E2EDD-BFA6-4213-9F8A-1B1D4FBBB8E4}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaListImplement", "PizzeriaListImplement\PizzeriaListImplement.csproj", "{190E2EDD-BFA6-4213-9F8A-1B1D4FBBB8E4}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PizzeriaFileImplement", "PizzeriaFileImplement\PizzeriaFileImplement.csproj", "{678CE6E1-EEBF-4D54-AFCA-B1DEAF300C88}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -39,6 +41,10 @@ Global {190E2EDD-BFA6-4213-9F8A-1B1D4FBBB8E4}.Debug|Any CPU.Build.0 = Debug|Any CPU {190E2EDD-BFA6-4213-9F8A-1B1D4FBBB8E4}.Release|Any CPU.ActiveCfg = Release|Any CPU {190E2EDD-BFA6-4213-9F8A-1B1D4FBBB8E4}.Release|Any CPU.Build.0 = Release|Any CPU + {678CE6E1-EEBF-4D54-AFCA-B1DEAF300C88}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {678CE6E1-EEBF-4D54-AFCA-B1DEAF300C88}.Debug|Any CPU.Build.0 = Debug|Any CPU + {678CE6E1-EEBF-4D54-AFCA-B1DEAF300C88}.Release|Any CPU.ActiveCfg = Release|Any CPU + {678CE6E1-EEBF-4D54-AFCA-B1DEAF300C88}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Pizzeria/PizzeriaFileImplement/DataFileSingleton.cs b/Pizzeria/PizzeriaFileImplement/DataFileSingleton.cs new file mode 100644 index 0000000..91526a5 --- /dev/null +++ b/Pizzeria/PizzeriaFileImplement/DataFileSingleton.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using PizzeriaFileImplement.Models; +using System.Xml.Linq; + +namespace PizzeriaFileImplement +{ + public class DataFileSingleton + { + private static DataFileSingleton? instance; + private readonly string ComponentFileName = "Component.xml"; + private readonly string OrderFileName = "Order.xml"; + private readonly string PizzaFileName = "Pizza.xml"; + public List Components { get; private set; } + public List Orders { get; private set; } + public List Pizzas { 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 SavePizzas() => SaveData(Pizzas, PizzaFileName, "Pizzas", x => x.GetXElement); + public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", 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)!)!; + } + + 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); + } + } + } +} diff --git a/Pizzeria/PizzeriaFileImplement/Implements/ComponentStorage.cs b/Pizzeria/PizzeriaFileImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..06b8716 --- /dev/null +++ b/Pizzeria/PizzeriaFileImplement/Implements/ComponentStorage.cs @@ -0,0 +1,86 @@ +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 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/Pizzeria/PizzeriaFileImplement/Implements/OrderStorage.cs b/Pizzeria/PizzeriaFileImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..4fecc2f --- /dev/null +++ b/Pizzeria/PizzeriaFileImplement/Implements/OrderStorage.cs @@ -0,0 +1,94 @@ +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 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 pizza = source.Pizzas.FirstOrDefault(x => x.Id == model.PizzaId); + if (pizza != null) + { + model.PizzaName = pizza.PizzaName; + } + return model; + } + } +} diff --git a/Pizzeria/PizzeriaFileImplement/Implements/PizzaStorage.cs b/Pizzeria/PizzeriaFileImplement/Implements/PizzaStorage.cs new file mode 100644 index 0000000..3c17ade --- /dev/null +++ b/Pizzeria/PizzeriaFileImplement/Implements/PizzaStorage.cs @@ -0,0 +1,86 @@ +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 PizzaStorage : IPizzaStorage + { + private readonly DataFileSingleton source; + + public PizzaStorage() + { + source = DataFileSingleton.GetInstance(); + } + + public List GetFullList() + { + return source.Pizzas.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(PizzaSearchModel model) + { + if (string.IsNullOrEmpty(model.PizzaName)) + { + return new(); + } + return source.Pizzas.Where(x => x.PizzaName.Contains(model.PizzaName)).Select(x => x.GetViewModel).ToList(); + } + + public PizzaViewModel? GetElement(PizzaSearchModel model) + { + if (string.IsNullOrEmpty(model.PizzaName) && !model.Id.HasValue) + { + return null; + } + return source.Pizzas.FirstOrDefault(x => + (!string.IsNullOrEmpty(model.PizzaName) && x.PizzaName == model.PizzaName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public PizzaViewModel? Insert(PizzaBindingModel model) + { + model.Id = source.Pizzas.Count > 0 ? source.Pizzas.Max(x => x.Id) + 1 : 1; + var newPizza = Pizza.Create(model); + if (newPizza == null) + { + return null; + } + source.Pizzas.Add(newPizza); + source.SavePizzas(); + return newPizza.GetViewModel; + } + + public PizzaViewModel? Update(PizzaBindingModel model) + { + var Pizza = source.Pizzas.FirstOrDefault(x => x.Id == model.Id); + if (Pizza == null) + { + return null; + } + Pizza.Update(model); + source.SavePizzas(); + return Pizza.GetViewModel; + } + + public PizzaViewModel? Delete(PizzaBindingModel model) + { + var Pizza = source.Pizzas.FirstOrDefault(x => x.Id == model.Id); + if (Pizza != null) + { + source.Pizzas.Remove(Pizza); + source.SavePizzas(); + return Pizza.GetViewModel; + } + return null; + } + } +} diff --git a/Pizzeria/PizzeriaFileImplement/Models/Component.cs b/Pizzeria/PizzeriaFileImplement/Models/Component.cs new file mode 100644 index 0000000..1efe080 --- /dev/null +++ b/Pizzeria/PizzeriaFileImplement/Models/Component.cs @@ -0,0 +1,69 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.ViewModels; +using PizzeriaDataModels.Models; +using System.Xml.Linq; + +namespace PizzeriaFileImplement.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/Pizzeria/PizzeriaFileImplement/Models/Order.cs b/Pizzeria/PizzeriaFileImplement/Models/Order.cs new file mode 100644 index 0000000..af29cef --- /dev/null +++ b/Pizzeria/PizzeriaFileImplement/Models/Order.cs @@ -0,0 +1,92 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.ViewModels; +using PizzeriaDataModels.Enums; +using PizzeriaDataModels.Models; +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 Order : IOrderModel + { + public int Id { get; private set; } + public int PizzaId { 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, + PizzaId = model.PizzaId, + 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), + PizzaId = Convert.ToInt32(element.Element("PizzaId")!.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, + PizzaId = PizzaId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + }; + + public XElement GetXElement => new("Order", + new XAttribute("Id", Id), + new XElement("PizzaId", PizzaId.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/Pizzeria/PizzeriaFileImplement/Models/Pizza.cs b/Pizzeria/PizzeriaFileImplement/Models/Pizza.cs new file mode 100644 index 0000000..240a5d6 --- /dev/null +++ b/Pizzeria/PizzeriaFileImplement/Models/Pizza.cs @@ -0,0 +1,92 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.ViewModels; +using PizzeriaDataModels.Models; +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 Pizza : IPizzaModel + { + public int Id { get; private set; } + public string PizzaName { get; private set; } = string.Empty; + public double Price { get; private set; } + public Dictionary Components { get; private set; } = new(); + private Dictionary? _pizzaComponents = null; + + public Dictionary PizzaComponents + { + get + { + if (_pizzaComponents == null) + { + var source = DataFileSingleton.GetInstance(); + _pizzaComponents = Components.ToDictionary(x => x.Key, y => ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, y.Value)); + } + return _pizzaComponents; + } + } + + public static Pizza? Create(PizzaBindingModel model) + { + if (model == null) + { + return null; + } + return new Pizza() + { + Id = model.Id, + PizzaName = model.PizzaName, + Price = model.Price, + Components = model.PizzaComponents.ToDictionary(x => x.Key, x => x.Value.Item2) + }; + } + + public static Pizza? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Pizza() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + PizzaName = element.Element("PizzaName")!.Value, + Price = Convert.ToDouble(element.Element("Price")!.Value), + Components = element.Element("PizzaComponents")!.Elements("PizzaComponent").ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), + x => Convert.ToInt32(x.Element("Value")?.Value)) + }; + } + + public void Update(PizzaBindingModel model) + { + if (model == null) + { + return; + } + PizzaName = model.PizzaName; + Price = model.Price; + Components = model.PizzaComponents.ToDictionary(x => x.Key, x => x.Value.Item2); + _pizzaComponents = null; + } + + public PizzaViewModel GetViewModel => new() + { + Id = Id, + PizzaName = PizzaName, + Price = Price, + PizzaComponents = PizzaComponents + }; + + public XElement GetXElement => new("Pizza", + new XAttribute("Id", Id), + new XElement("PizzaName", PizzaName), + new XElement("Price", Price.ToString()), + new XElement("PizzaComponents", Components.Select( + x => new XElement("PizzaComponent", new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray())); + } +} diff --git a/Pizzeria/PizzeriaFileImplement/PizzeriaFileImplement.csproj b/Pizzeria/PizzeriaFileImplement/PizzeriaFileImplement.csproj new file mode 100644 index 0000000..b612a23 --- /dev/null +++ b/Pizzeria/PizzeriaFileImplement/PizzeriaFileImplement.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + enable + enable + + + + + + + + diff --git a/Pizzeria/PizzeriaView/PizzeriaView.csproj b/Pizzeria/PizzeriaView/PizzeriaView.csproj index 6f8feb1..fd4aa2c 100644 --- a/Pizzeria/PizzeriaView/PizzeriaView.csproj +++ b/Pizzeria/PizzeriaView/PizzeriaView.csproj @@ -28,6 +28,7 @@ + diff --git a/Pizzeria/PizzeriaView/Program.cs b/Pizzeria/PizzeriaView/Program.cs index ae88579..8ce3d2d 100644 --- a/Pizzeria/PizzeriaView/Program.cs +++ b/Pizzeria/PizzeriaView/Program.cs @@ -4,7 +4,7 @@ using NLog.Extensions.Logging; using PizzeriaBusinessLogic.BusinessLogics; using PizzeriaContracts.BusinessLogicsContracts; using PizzeriaContracts.StoragesContracts; -using PizzeriaListImplement.Implements; +using PizzeriaFileImplement.Implements; using PizzeriaView; namespace Pizzeria -- 2.25.1 From 010efbacc2f6c561ce3895154b67e3d261aaae99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D1=8F=D1=87=D0=B5=D1=81=D0=BB=D0=B0=D0=B2=20=D0=98?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=BE=D0=B2?= Date: Sat, 17 Feb 2024 16:33:25 +0400 Subject: [PATCH 3/9] fix --- Pizzeria/PizzeriaView/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pizzeria/PizzeriaView/Program.cs b/Pizzeria/PizzeriaView/Program.cs index 8ce3d2d..ae88579 100644 --- a/Pizzeria/PizzeriaView/Program.cs +++ b/Pizzeria/PizzeriaView/Program.cs @@ -4,7 +4,7 @@ using NLog.Extensions.Logging; using PizzeriaBusinessLogic.BusinessLogics; using PizzeriaContracts.BusinessLogicsContracts; using PizzeriaContracts.StoragesContracts; -using PizzeriaFileImplement.Implements; +using PizzeriaListImplement.Implements; using PizzeriaView; namespace Pizzeria -- 2.25.1 From 8c4233d1a6fe4f940bdc91f0eb9a975258a3009d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D1=8F=D1=87=D0=B5=D1=81=D0=BB=D0=B0=D0=B2=20=D0=98?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=BE=D0=B2?= Date: Sat, 17 Feb 2024 16:37:01 +0400 Subject: [PATCH 4/9] Revert "fix" This reverts commit 010efbacc2f6c561ce3895154b67e3d261aaae99. --- Pizzeria/PizzeriaView/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pizzeria/PizzeriaView/Program.cs b/Pizzeria/PizzeriaView/Program.cs index ae88579..8ce3d2d 100644 --- a/Pizzeria/PizzeriaView/Program.cs +++ b/Pizzeria/PizzeriaView/Program.cs @@ -4,7 +4,7 @@ using NLog.Extensions.Logging; using PizzeriaBusinessLogic.BusinessLogics; using PizzeriaContracts.BusinessLogicsContracts; using PizzeriaContracts.StoragesContracts; -using PizzeriaListImplement.Implements; +using PizzeriaFileImplement.Implements; using PizzeriaView; namespace Pizzeria -- 2.25.1 From 2ce7c826e8691e26bd0efb70cb1856065c622b56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D1=8F=D1=87=D0=B5=D1=81=D0=BB=D0=B0=D0=B2=20=D0=98?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=BE=D0=B2?= Date: Sat, 17 Feb 2024 16:37:07 +0400 Subject: [PATCH 5/9] Revert "Revert "fix"" This reverts commit 8c4233d1a6fe4f940bdc91f0eb9a975258a3009d. --- Pizzeria/PizzeriaView/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pizzeria/PizzeriaView/Program.cs b/Pizzeria/PizzeriaView/Program.cs index 8ce3d2d..ae88579 100644 --- a/Pizzeria/PizzeriaView/Program.cs +++ b/Pizzeria/PizzeriaView/Program.cs @@ -4,7 +4,7 @@ using NLog.Extensions.Logging; using PizzeriaBusinessLogic.BusinessLogics; using PizzeriaContracts.BusinessLogicsContracts; using PizzeriaContracts.StoragesContracts; -using PizzeriaFileImplement.Implements; +using PizzeriaListImplement.Implements; using PizzeriaView; namespace Pizzeria -- 2.25.1 From 91c55357c85a149d24feaf0cfa8ce4c3554d42c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D1=8F=D1=87=D0=B5=D1=81=D0=BB=D0=B0=D0=B2=20=D0=98?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=BE=D0=B2?= Date: Sat, 17 Feb 2024 16:37:24 +0400 Subject: [PATCH 6/9] q --- Pizzeria/Pizzeria.sln | 6 ------ Pizzeria/PizzeriaView/PizzeriaView.csproj | 1 - 2 files changed, 7 deletions(-) diff --git a/Pizzeria/Pizzeria.sln b/Pizzeria/Pizzeria.sln index 94cd565..c868b9f 100644 --- a/Pizzeria/Pizzeria.sln +++ b/Pizzeria/Pizzeria.sln @@ -13,8 +13,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaDataModels", "Pizze EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaListImplement", "PizzeriaListImplement\PizzeriaListImplement.csproj", "{190E2EDD-BFA6-4213-9F8A-1B1D4FBBB8E4}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PizzeriaFileImplement", "PizzeriaFileImplement\PizzeriaFileImplement.csproj", "{678CE6E1-EEBF-4D54-AFCA-B1DEAF300C88}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -41,10 +39,6 @@ Global {190E2EDD-BFA6-4213-9F8A-1B1D4FBBB8E4}.Debug|Any CPU.Build.0 = Debug|Any CPU {190E2EDD-BFA6-4213-9F8A-1B1D4FBBB8E4}.Release|Any CPU.ActiveCfg = Release|Any CPU {190E2EDD-BFA6-4213-9F8A-1B1D4FBBB8E4}.Release|Any CPU.Build.0 = Release|Any CPU - {678CE6E1-EEBF-4D54-AFCA-B1DEAF300C88}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {678CE6E1-EEBF-4D54-AFCA-B1DEAF300C88}.Debug|Any CPU.Build.0 = Debug|Any CPU - {678CE6E1-EEBF-4D54-AFCA-B1DEAF300C88}.Release|Any CPU.ActiveCfg = Release|Any CPU - {678CE6E1-EEBF-4D54-AFCA-B1DEAF300C88}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Pizzeria/PizzeriaView/PizzeriaView.csproj b/Pizzeria/PizzeriaView/PizzeriaView.csproj index fd4aa2c..6f8feb1 100644 --- a/Pizzeria/PizzeriaView/PizzeriaView.csproj +++ b/Pizzeria/PizzeriaView/PizzeriaView.csproj @@ -28,7 +28,6 @@ - -- 2.25.1 From 8f9a53c6e60805b3e78938ac748a8bde38a5a232 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D1=8F=D1=87=D0=B5=D1=81=D0=BB=D0=B0=D0=B2=20=D0=98?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=BE=D0=B2?= Date: Sat, 17 Feb 2024 16:39:16 +0400 Subject: [PATCH 7/9] d --- Pizzeria/Pizzeria.sln | 8 +- .../DataFileSingleton.cs | 58 ------------ .../Implements/ComponentStorage.cs | 86 ----------------- .../Implements/OrderStorage.cs | 94 ------------------- .../Implements/PizzaStorage.cs | 86 ----------------- .../PizzeriaFileImplement/Models/Component.cs | 69 -------------- .../PizzeriaFileImplement/Models/Order.cs | 92 ------------------ .../PizzeriaFileImplement/Models/Pizza.cs | 92 ------------------ .../PizzeriaFileImplement.csproj | 14 --- 9 files changed, 5 insertions(+), 594 deletions(-) delete mode 100644 Pizzeria/PizzeriaFileImplement/DataFileSingleton.cs delete mode 100644 Pizzeria/PizzeriaFileImplement/Implements/ComponentStorage.cs delete mode 100644 Pizzeria/PizzeriaFileImplement/Implements/OrderStorage.cs delete mode 100644 Pizzeria/PizzeriaFileImplement/Implements/PizzaStorage.cs delete mode 100644 Pizzeria/PizzeriaFileImplement/Models/Component.cs delete mode 100644 Pizzeria/PizzeriaFileImplement/Models/Order.cs delete mode 100644 Pizzeria/PizzeriaFileImplement/Models/Pizza.cs delete mode 100644 Pizzeria/PizzeriaFileImplement/PizzeriaFileImplement.csproj diff --git a/Pizzeria/Pizzeria.sln b/Pizzeria/Pizzeria.sln index c868b9f..1c18c22 100644 --- a/Pizzeria/Pizzeria.sln +++ b/Pizzeria/Pizzeria.sln @@ -5,14 +5,16 @@ VisualStudioVersion = 17.7.34024.191 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaView", "PizzeriaView\PizzeriaView.csproj", "{C3B647C4-306F-43B5-BDF2-FF5F3A34364F}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaBusinessLogic", "PizzeriaBusinessLogic\PizzeriaBusinessLogic.csproj", "{906C7D67-BBCC-48D7-BB34-62A9A35779A8}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PizzeriaBusinessLogic", "PizzeriaBusinessLogic\PizzeriaBusinessLogic.csproj", "{906C7D67-BBCC-48D7-BB34-62A9A35779A8}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaContracts", "PizzeriaContracts\PizzeriaContracts.csproj", "{A625183B-6EEA-4995-B06D-E10835CEFE9C}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PizzeriaContracts", "PizzeriaContracts\PizzeriaContracts.csproj", "{A625183B-6EEA-4995-B06D-E10835CEFE9C}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaDataModels", "PizzeriaDataModels\PizzeriaDataModels.csproj", "{D0318436-6887-4AD1-92B2-19A7F37240DC}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PizzeriaDataModels", "PizzeriaDataModels\PizzeriaDataModels.csproj", "{D0318436-6887-4AD1-92B2-19A7F37240DC}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaListImplement", "PizzeriaListImplement\PizzeriaListImplement.csproj", "{190E2EDD-BFA6-4213-9F8A-1B1D4FBBB8E4}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PizzeriaFileImplement", "PizzeriaFileImplement\PizzeriaFileImplement.csproj", "{678CE6E1-EEBF-4D54-AFCA-B1DEAF300C88}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU diff --git a/Pizzeria/PizzeriaFileImplement/DataFileSingleton.cs b/Pizzeria/PizzeriaFileImplement/DataFileSingleton.cs deleted file mode 100644 index 91526a5..0000000 --- a/Pizzeria/PizzeriaFileImplement/DataFileSingleton.cs +++ /dev/null @@ -1,58 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using PizzeriaFileImplement.Models; -using System.Xml.Linq; - -namespace PizzeriaFileImplement -{ - public class DataFileSingleton - { - private static DataFileSingleton? instance; - private readonly string ComponentFileName = "Component.xml"; - private readonly string OrderFileName = "Order.xml"; - private readonly string PizzaFileName = "Pizza.xml"; - public List Components { get; private set; } - public List Orders { get; private set; } - public List Pizzas { 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 SavePizzas() => SaveData(Pizzas, PizzaFileName, "Pizzas", x => x.GetXElement); - public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", 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)!)!; - } - - 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); - } - } - } -} diff --git a/Pizzeria/PizzeriaFileImplement/Implements/ComponentStorage.cs b/Pizzeria/PizzeriaFileImplement/Implements/ComponentStorage.cs deleted file mode 100644 index 06b8716..0000000 --- a/Pizzeria/PizzeriaFileImplement/Implements/ComponentStorage.cs +++ /dev/null @@ -1,86 +0,0 @@ -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 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/Pizzeria/PizzeriaFileImplement/Implements/OrderStorage.cs b/Pizzeria/PizzeriaFileImplement/Implements/OrderStorage.cs deleted file mode 100644 index 4fecc2f..0000000 --- a/Pizzeria/PizzeriaFileImplement/Implements/OrderStorage.cs +++ /dev/null @@ -1,94 +0,0 @@ -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 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 pizza = source.Pizzas.FirstOrDefault(x => x.Id == model.PizzaId); - if (pizza != null) - { - model.PizzaName = pizza.PizzaName; - } - return model; - } - } -} diff --git a/Pizzeria/PizzeriaFileImplement/Implements/PizzaStorage.cs b/Pizzeria/PizzeriaFileImplement/Implements/PizzaStorage.cs deleted file mode 100644 index 3c17ade..0000000 --- a/Pizzeria/PizzeriaFileImplement/Implements/PizzaStorage.cs +++ /dev/null @@ -1,86 +0,0 @@ -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 PizzaStorage : IPizzaStorage - { - private readonly DataFileSingleton source; - - public PizzaStorage() - { - source = DataFileSingleton.GetInstance(); - } - - public List GetFullList() - { - return source.Pizzas.Select(x => x.GetViewModel).ToList(); - } - - public List GetFilteredList(PizzaSearchModel model) - { - if (string.IsNullOrEmpty(model.PizzaName)) - { - return new(); - } - return source.Pizzas.Where(x => x.PizzaName.Contains(model.PizzaName)).Select(x => x.GetViewModel).ToList(); - } - - public PizzaViewModel? GetElement(PizzaSearchModel model) - { - if (string.IsNullOrEmpty(model.PizzaName) && !model.Id.HasValue) - { - return null; - } - return source.Pizzas.FirstOrDefault(x => - (!string.IsNullOrEmpty(model.PizzaName) && x.PizzaName == model.PizzaName) || - (model.Id.HasValue && x.Id == model.Id)) - ?.GetViewModel; - } - - public PizzaViewModel? Insert(PizzaBindingModel model) - { - model.Id = source.Pizzas.Count > 0 ? source.Pizzas.Max(x => x.Id) + 1 : 1; - var newPizza = Pizza.Create(model); - if (newPizza == null) - { - return null; - } - source.Pizzas.Add(newPizza); - source.SavePizzas(); - return newPizza.GetViewModel; - } - - public PizzaViewModel? Update(PizzaBindingModel model) - { - var Pizza = source.Pizzas.FirstOrDefault(x => x.Id == model.Id); - if (Pizza == null) - { - return null; - } - Pizza.Update(model); - source.SavePizzas(); - return Pizza.GetViewModel; - } - - public PizzaViewModel? Delete(PizzaBindingModel model) - { - var Pizza = source.Pizzas.FirstOrDefault(x => x.Id == model.Id); - if (Pizza != null) - { - source.Pizzas.Remove(Pizza); - source.SavePizzas(); - return Pizza.GetViewModel; - } - return null; - } - } -} diff --git a/Pizzeria/PizzeriaFileImplement/Models/Component.cs b/Pizzeria/PizzeriaFileImplement/Models/Component.cs deleted file mode 100644 index 1efe080..0000000 --- a/Pizzeria/PizzeriaFileImplement/Models/Component.cs +++ /dev/null @@ -1,69 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using PizzeriaContracts.BindingModels; -using PizzeriaContracts.ViewModels; -using PizzeriaDataModels.Models; -using System.Xml.Linq; - -namespace PizzeriaFileImplement.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/Pizzeria/PizzeriaFileImplement/Models/Order.cs b/Pizzeria/PizzeriaFileImplement/Models/Order.cs deleted file mode 100644 index af29cef..0000000 --- a/Pizzeria/PizzeriaFileImplement/Models/Order.cs +++ /dev/null @@ -1,92 +0,0 @@ -using PizzeriaContracts.BindingModels; -using PizzeriaContracts.ViewModels; -using PizzeriaDataModels.Enums; -using PizzeriaDataModels.Models; -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 Order : IOrderModel - { - public int Id { get; private set; } - public int PizzaId { 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, - PizzaId = model.PizzaId, - 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), - PizzaId = Convert.ToInt32(element.Element("PizzaId")!.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, - PizzaId = PizzaId, - Count = Count, - Sum = Sum, - Status = Status, - DateCreate = DateCreate, - DateImplement = DateImplement, - }; - - public XElement GetXElement => new("Order", - new XAttribute("Id", Id), - new XElement("PizzaId", PizzaId.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/Pizzeria/PizzeriaFileImplement/Models/Pizza.cs b/Pizzeria/PizzeriaFileImplement/Models/Pizza.cs deleted file mode 100644 index 240a5d6..0000000 --- a/Pizzeria/PizzeriaFileImplement/Models/Pizza.cs +++ /dev/null @@ -1,92 +0,0 @@ -using PizzeriaContracts.BindingModels; -using PizzeriaContracts.ViewModels; -using PizzeriaDataModels.Models; -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 Pizza : IPizzaModel - { - public int Id { get; private set; } - public string PizzaName { get; private set; } = string.Empty; - public double Price { get; private set; } - public Dictionary Components { get; private set; } = new(); - private Dictionary? _pizzaComponents = null; - - public Dictionary PizzaComponents - { - get - { - if (_pizzaComponents == null) - { - var source = DataFileSingleton.GetInstance(); - _pizzaComponents = Components.ToDictionary(x => x.Key, y => ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, y.Value)); - } - return _pizzaComponents; - } - } - - public static Pizza? Create(PizzaBindingModel model) - { - if (model == null) - { - return null; - } - return new Pizza() - { - Id = model.Id, - PizzaName = model.PizzaName, - Price = model.Price, - Components = model.PizzaComponents.ToDictionary(x => x.Key, x => x.Value.Item2) - }; - } - - public static Pizza? Create(XElement element) - { - if (element == null) - { - return null; - } - return new Pizza() - { - Id = Convert.ToInt32(element.Attribute("Id")!.Value), - PizzaName = element.Element("PizzaName")!.Value, - Price = Convert.ToDouble(element.Element("Price")!.Value), - Components = element.Element("PizzaComponents")!.Elements("PizzaComponent").ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), - x => Convert.ToInt32(x.Element("Value")?.Value)) - }; - } - - public void Update(PizzaBindingModel model) - { - if (model == null) - { - return; - } - PizzaName = model.PizzaName; - Price = model.Price; - Components = model.PizzaComponents.ToDictionary(x => x.Key, x => x.Value.Item2); - _pizzaComponents = null; - } - - public PizzaViewModel GetViewModel => new() - { - Id = Id, - PizzaName = PizzaName, - Price = Price, - PizzaComponents = PizzaComponents - }; - - public XElement GetXElement => new("Pizza", - new XAttribute("Id", Id), - new XElement("PizzaName", PizzaName), - new XElement("Price", Price.ToString()), - new XElement("PizzaComponents", Components.Select( - x => new XElement("PizzaComponent", new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray())); - } -} diff --git a/Pizzeria/PizzeriaFileImplement/PizzeriaFileImplement.csproj b/Pizzeria/PizzeriaFileImplement/PizzeriaFileImplement.csproj deleted file mode 100644 index b612a23..0000000 --- a/Pizzeria/PizzeriaFileImplement/PizzeriaFileImplement.csproj +++ /dev/null @@ -1,14 +0,0 @@ - - - - net6.0 - enable - enable - - - - - - - - -- 2.25.1 From b1b0fa02e659da890257afd384548014587d1063 Mon Sep 17 00:00:00 2001 From: Vyacheslav Date: Sat, 17 Feb 2024 16:44:33 +0400 Subject: [PATCH 8/9] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8=D0=BB(?= =?UTF-8?q?=D0=B0)=20=D0=BD=D0=B0=20'Pizzeria/Pizzeria.sln'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Pizzeria/Pizzeria.sln | 2 -- 1 file changed, 2 deletions(-) diff --git a/Pizzeria/Pizzeria.sln b/Pizzeria/Pizzeria.sln index 1c18c22..c8119b1 100644 --- a/Pizzeria/Pizzeria.sln +++ b/Pizzeria/Pizzeria.sln @@ -13,8 +13,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PizzeriaDataModels", "Pizze EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaListImplement", "PizzeriaListImplement\PizzeriaListImplement.csproj", "{190E2EDD-BFA6-4213-9F8A-1B1D4FBBB8E4}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PizzeriaFileImplement", "PizzeriaFileImplement\PizzeriaFileImplement.csproj", "{678CE6E1-EEBF-4D54-AFCA-B1DEAF300C88}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU -- 2.25.1 From 0250879758c4536cc7e11f6f95aea0ba0e70f29b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D1=8F=D1=87=D0=B5=D1=81=D0=BB=D0=B0=D0=B2=20=D0=98?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=BE=D0=B2?= Date: Sat, 17 Feb 2024 17:57:28 +0400 Subject: [PATCH 9/9] q --- Pizzeria/Pizzeria.sln | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Pizzeria/Pizzeria.sln b/Pizzeria/Pizzeria.sln index c8119b1..c868b9f 100644 --- a/Pizzeria/Pizzeria.sln +++ b/Pizzeria/Pizzeria.sln @@ -5,11 +5,11 @@ VisualStudioVersion = 17.7.34024.191 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaView", "PizzeriaView\PizzeriaView.csproj", "{C3B647C4-306F-43B5-BDF2-FF5F3A34364F}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PizzeriaBusinessLogic", "PizzeriaBusinessLogic\PizzeriaBusinessLogic.csproj", "{906C7D67-BBCC-48D7-BB34-62A9A35779A8}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaBusinessLogic", "PizzeriaBusinessLogic\PizzeriaBusinessLogic.csproj", "{906C7D67-BBCC-48D7-BB34-62A9A35779A8}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PizzeriaContracts", "PizzeriaContracts\PizzeriaContracts.csproj", "{A625183B-6EEA-4995-B06D-E10835CEFE9C}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaContracts", "PizzeriaContracts\PizzeriaContracts.csproj", "{A625183B-6EEA-4995-B06D-E10835CEFE9C}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PizzeriaDataModels", "PizzeriaDataModels\PizzeriaDataModels.csproj", "{D0318436-6887-4AD1-92B2-19A7F37240DC}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaDataModels", "PizzeriaDataModels\PizzeriaDataModels.csproj", "{D0318436-6887-4AD1-92B2-19A7F37240DC}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaListImplement", "PizzeriaListImplement\PizzeriaListImplement.csproj", "{190E2EDD-BFA6-4213-9F8A-1B1D4FBBB8E4}" EndProject -- 2.25.1