From 908c0ea4a55ba59123e31f63565707f3726037c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=BB=D1=8C=D1=8F?= <Илья@WIN-RANNDDD> Date: Sun, 11 Feb 2024 17:39:43 +0400 Subject: [PATCH 01/11] =?UTF-8?q?=D0=93=D0=BE=D1=82=D0=BE=D0=B2=D0=B0?= =?UTF-8?q?=D1=8F=201=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=BD=D0=B0=D1=8F=20(=D0=A3=D1=81=D0=BB=D0=BE=D0=B6?= =?UTF-8?q?=D0=BD=D0=B5=D0=BD=D0=BD=D0=B0=D1=8F)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/ShopLogic.cs | 166 ++++++++++++++ .../BindingModels/ShopBindingModel.cs | 21 ++ .../BusinessLogicsContracts/IShopLogic.cs | 22 ++ .../SearchModels/ShopSearchModel.cs | 9 + .../StoragesContracts/IShopStorage.cs | 21 ++ .../ViewModels/ShopViewModel.cs | 25 ++ .../Models/IShopModel.cs | 13 ++ .../DataListSingleton.cs | 3 + .../Implements/ShopStorage.cs | 109 +++++++++ .../IceCreamShopListImplement/Models/Shop.cs | 60 +++++ .../IceCreamShopView/FormCreateOrder.cs | 2 - IceCreamShop/IceCreamShopView/FormMain.cs | 18 ++ .../IceCreamShopView/FormMain.designer.cs | 22 +- .../FormMakeShipment.Designer.cs | 153 +++++++++++++ .../IceCreamShopView/FormMakeShipment.cs | 116 ++++++++++ .../IceCreamShopView/FormMakeShipment.resx | 120 ++++++++++ .../IceCreamShopView/FormShop.Designer.cs | 213 ++++++++++++++++++ IceCreamShop/IceCreamShopView/FormShop.cs | 124 ++++++++++ IceCreamShop/IceCreamShopView/FormShop.resx | 120 ++++++++++ .../IceCreamShopView/FormShops.Designer.cs | 126 +++++++++++ IceCreamShop/IceCreamShopView/FormShops.cs | 104 +++++++++ IceCreamShop/IceCreamShopView/FormShops.resx | 120 ++++++++++ IceCreamShop/IceCreamShopView/Program.cs | 5 + 23 files changed, 1688 insertions(+), 4 deletions(-) create mode 100644 IceCreamShop/IceCreamShopBusinessLogic/BusinessLogics/ShopLogic.cs create mode 100644 IceCreamShop/IceCreamShopContracts/BindingModels/ShopBindingModel.cs create mode 100644 IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IShopLogic.cs create mode 100644 IceCreamShop/IceCreamShopContracts/SearchModels/ShopSearchModel.cs create mode 100644 IceCreamShop/IceCreamShopContracts/StoragesContracts/IShopStorage.cs create mode 100644 IceCreamShop/IceCreamShopContracts/ViewModels/ShopViewModel.cs create mode 100644 IceCreamShop/IceCreamShopDataModels/Models/IShopModel.cs create mode 100644 IceCreamShop/IceCreamShopListImplement/Implements/ShopStorage.cs create mode 100644 IceCreamShop/IceCreamShopListImplement/Models/Shop.cs create mode 100644 IceCreamShop/IceCreamShopView/FormMakeShipment.Designer.cs create mode 100644 IceCreamShop/IceCreamShopView/FormMakeShipment.cs create mode 100644 IceCreamShop/IceCreamShopView/FormMakeShipment.resx create mode 100644 IceCreamShop/IceCreamShopView/FormShop.Designer.cs create mode 100644 IceCreamShop/IceCreamShopView/FormShop.cs create mode 100644 IceCreamShop/IceCreamShopView/FormShop.resx create mode 100644 IceCreamShop/IceCreamShopView/FormShops.Designer.cs create mode 100644 IceCreamShop/IceCreamShopView/FormShops.cs create mode 100644 IceCreamShop/IceCreamShopView/FormShops.resx diff --git a/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogics/ShopLogic.cs b/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogics/ShopLogic.cs new file mode 100644 index 0000000..4f05818 --- /dev/null +++ b/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogics/ShopLogic.cs @@ -0,0 +1,166 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.BusinessLogicsContracts; +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.StoragesContracts; +using IceCreamShopContracts.ViewModels; +using IceCreamShopDataModels.Models; +using Microsoft.Extensions.Logging; + +namespace IceCreamShopBusinessLogic.BusinessLogics +{ + public class ShopLogic : IShopLogic + { + private readonly ILogger _logger; + + private readonly IShopStorage _shopStorage; + + public ShopLogic(ILogger logger, IShopStorage shopStorage) + { + _logger = logger; + _shopStorage = shopStorage; + } + + 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 MakeShipment(ShopSearchModel shopModel, IIceCreamModel iceCream, int count) + { + if (shopModel == null) + { + throw new ArgumentNullException(nameof(shopModel)); + } + if (iceCream == null) + { + throw new ArgumentNullException(nameof(iceCream)); + } + if (count <= 0) + { + throw new ArgumentException("Количество товаров(мороженого) в магазине должно быть больше нуля", nameof(count)); + } + _logger.LogInformation("MakeShipment(GetElement). ShopName: {ShopName}. Id: {Id}", shopModel.ShopName, shopModel.Id); + var shop = _shopStorage.GetElement(shopModel); + if (shop == null) + { + _logger.LogWarning("MakeShipment(GetElement). Element not found"); + return false; + } + if (shop.ShopIceCreams.ContainsKey(iceCream.Id)) + { + var shopIC = shop.ShopIceCreams[iceCream.Id]; + shopIC.Item2 += count; + shop.ShopIceCreams[iceCream.Id] = shopIC; + _logger.LogInformation("MakeShipment. Added {count} '{iceCream}' to '{ShopName}' shop", count, iceCream.IceCreamName, + shop.ShopName); + } + else + { + shop.ShopIceCreams.Add(iceCream.Id, (iceCream, count)); + _logger.LogInformation("MakeShipment. Added {count} new '{iceCream}' to '{ShopName}' shop", count, iceCream.IceCreamName, + shop.ShopName); + } + if (_shopStorage.Update(new ShopBindingModel() + { + Id = shop.Id, + ShopName = shop.ShopName, + Address = shop.Address, + DateOpening = shop.DateOpening, + ShopIceCreams = shop.ShopIceCreams, + }) == null) + { + _logger.LogWarning("MakeShipment. Update operation failed"); + return false; + } + 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.ShopName)) + { + throw new ArgumentNullException("Нет названия магазина", nameof(model.ShopName)); + } + if (string.IsNullOrEmpty(model.Address)) + { + throw new ArgumentNullException("Нет адреса магазина", nameof(model.Address)); + } + _logger.LogInformation("Shop. ShopName: {ShopName}. Address: {Address}. Id: {Id}", model.ShopName, model.Address, model.Id); + var element = _shopStorage.GetElement(new ShopSearchModel + { + ShopName = model.ShopName + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Магазин с таким названием уже есть"); + } + } + } +} diff --git a/IceCreamShop/IceCreamShopContracts/BindingModels/ShopBindingModel.cs b/IceCreamShop/IceCreamShopContracts/BindingModels/ShopBindingModel.cs new file mode 100644 index 0000000..0710d3d --- /dev/null +++ b/IceCreamShop/IceCreamShopContracts/BindingModels/ShopBindingModel.cs @@ -0,0 +1,21 @@ +using IceCreamShopDataModels.Models; + +namespace IceCreamShopContracts.BindingModels +{ + public class ShopBindingModel : IShopModel + { + public int Id { get; set; } + + public string ShopName { get; set; } = string.Empty; + + public string Address { get; set; } = string.Empty; + + public DateTime DateOpening { get; set; } = DateTime.Now; + + public Dictionary ShopIceCreams + { + get; + set; + } = new(); + } +} diff --git a/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IShopLogic.cs b/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IShopLogic.cs new file mode 100644 index 0000000..8e9a7fa --- /dev/null +++ b/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IShopLogic.cs @@ -0,0 +1,22 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.ViewModels; +using IceCreamShopDataModels.Models; + +namespace IceCreamShopContracts.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 MakeShipment(ShopSearchModel shopModel, IIceCreamModel iceCream, int count); + } +} diff --git a/IceCreamShop/IceCreamShopContracts/SearchModels/ShopSearchModel.cs b/IceCreamShop/IceCreamShopContracts/SearchModels/ShopSearchModel.cs new file mode 100644 index 0000000..5c6f8cd --- /dev/null +++ b/IceCreamShop/IceCreamShopContracts/SearchModels/ShopSearchModel.cs @@ -0,0 +1,9 @@ +namespace IceCreamShopContracts.SearchModels +{ + public class ShopSearchModel + { + public int? Id { get; set; } + + public string? ShopName { get; set; } + } +} diff --git a/IceCreamShop/IceCreamShopContracts/StoragesContracts/IShopStorage.cs b/IceCreamShop/IceCreamShopContracts/StoragesContracts/IShopStorage.cs new file mode 100644 index 0000000..374366b --- /dev/null +++ b/IceCreamShop/IceCreamShopContracts/StoragesContracts/IShopStorage.cs @@ -0,0 +1,21 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.ViewModels; + +namespace IceCreamShopContracts.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); + } +} diff --git a/IceCreamShop/IceCreamShopContracts/ViewModels/ShopViewModel.cs b/IceCreamShop/IceCreamShopContracts/ViewModels/ShopViewModel.cs new file mode 100644 index 0000000..066a909 --- /dev/null +++ b/IceCreamShop/IceCreamShopContracts/ViewModels/ShopViewModel.cs @@ -0,0 +1,25 @@ +using IceCreamShopDataModels.Models; +using System.ComponentModel; + +namespace IceCreamShopContracts.ViewModels +{ + public class ShopViewModel : IShopModel + { + public int Id { get; set; } + + [DisplayName("Название магазина")] + public string ShopName { get; set; } = string.Empty; + + [DisplayName("Адрес")] + public string Address { get; set; } = string.Empty; + + [DisplayName("Дата открытия")] + public DateTime DateOpening { get; set; } = DateTime.Now; + + public Dictionary ShopIceCreams + { + get; + set; + } = new(); + } +} diff --git a/IceCreamShop/IceCreamShopDataModels/Models/IShopModel.cs b/IceCreamShop/IceCreamShopDataModels/Models/IShopModel.cs new file mode 100644 index 0000000..91542ec --- /dev/null +++ b/IceCreamShop/IceCreamShopDataModels/Models/IShopModel.cs @@ -0,0 +1,13 @@ +namespace IceCreamShopDataModels.Models +{ + public interface IShopModel : IId + { + string ShopName { get; } + + string Address { get; } + + DateTime DateOpening { get; } + + Dictionary ShopIceCreams { get; } + } +} diff --git a/IceCreamShop/IceCreamShopListImplement/DataListSingleton.cs b/IceCreamShop/IceCreamShopListImplement/DataListSingleton.cs index e3cac9e..cdd9baf 100644 --- a/IceCreamShop/IceCreamShopListImplement/DataListSingleton.cs +++ b/IceCreamShop/IceCreamShopListImplement/DataListSingleton.cs @@ -12,11 +12,14 @@ namespace IceCreamShopListImplement public List IceCreams { get; set; } + public List Shops { get; set; } + private DataListSingleton() { Components = new List(); Orders = new List(); IceCreams = new List(); + Shops = new List(); } public static DataListSingleton GetInstance() diff --git a/IceCreamShop/IceCreamShopListImplement/Implements/ShopStorage.cs b/IceCreamShop/IceCreamShopListImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..f39611e --- /dev/null +++ b/IceCreamShop/IceCreamShopListImplement/Implements/ShopStorage.cs @@ -0,0 +1,109 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.StoragesContracts; +using IceCreamShopContracts.ViewModels; +using IceCreamShopListImplement.Models; + +namespace IceCreamShopListImplement.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; + } + } +} diff --git a/IceCreamShop/IceCreamShopListImplement/Models/Shop.cs b/IceCreamShop/IceCreamShopListImplement/Models/Shop.cs new file mode 100644 index 0000000..4602d84 --- /dev/null +++ b/IceCreamShop/IceCreamShopListImplement/Models/Shop.cs @@ -0,0 +1,60 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.ViewModels; +using IceCreamShopDataModels.Models; + +namespace IceCreamShopListImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; private set; } + + public string ShopName { get; private set; } = string.Empty; + + public string Address { get; private set; } = string.Empty; + + public DateTime DateOpening { get; private set; } + + public Dictionary ShopIceCreams + { + get; + private set; + } = new Dictionary(); + + public static Shop? Create(ShopBindingModel? model) + { + if (model == null) + { + return null; + } + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Address = model.Address, + DateOpening = model.DateOpening, + ShopIceCreams = model.ShopIceCreams + }; + } + + public void Update(ShopBindingModel? model) + { + if (model == null) + { + return; + } + ShopName = model.ShopName; + Address = model.Address; + DateOpening = model.DateOpening; + ShopIceCreams = model.ShopIceCreams; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + DateOpening = DateOpening, + ShopIceCreams = ShopIceCreams + }; + } +} diff --git a/IceCreamShop/IceCreamShopView/FormCreateOrder.cs b/IceCreamShop/IceCreamShopView/FormCreateOrder.cs index f93a15b..b589fd6 100644 --- a/IceCreamShop/IceCreamShopView/FormCreateOrder.cs +++ b/IceCreamShop/IceCreamShopView/FormCreateOrder.cs @@ -3,8 +3,6 @@ using IceCreamShopContracts.BusinessLogicsContracts; using IceCreamShopContracts.SearchModels; using IceCreamShopContracts.ViewModels; using Microsoft.Extensions.Logging; -using Microsoft.VisualBasic.Logging; -using System.Windows.Forms; namespace IceCreamShopView { diff --git a/IceCreamShop/IceCreamShopView/FormMain.cs b/IceCreamShop/IceCreamShopView/FormMain.cs index a50d1ef..b72c9f7 100644 --- a/IceCreamShop/IceCreamShopView/FormMain.cs +++ b/IceCreamShop/IceCreamShopView/FormMain.cs @@ -60,6 +60,24 @@ namespace IceCreamShopView } } + private void МагазиныToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormShops)); + if (service is FormShops form) + { + form.ShowDialog(); + } + } + + private void ПополнениеМагазинаToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormMakeShipment)); + if (service is FormMakeShipment form) + { + form.ShowDialog(); + } + } + private void ButtonCreateOrder_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormCreateOrder)); diff --git a/IceCreamShop/IceCreamShopView/FormMain.designer.cs b/IceCreamShop/IceCreamShopView/FormMain.designer.cs index 7757958..93afb5c 100644 --- a/IceCreamShop/IceCreamShopView/FormMain.designer.cs +++ b/IceCreamShop/IceCreamShopView/FormMain.designer.cs @@ -32,6 +32,8 @@ справочникиToolStripMenuItem = new ToolStripMenuItem(); компонентыToolStripMenuItem = new ToolStripMenuItem(); мороженоеToolStripMenuItem = new ToolStripMenuItem(); + магазиныToolStripMenuItem = new ToolStripMenuItem(); + пополнениеМагазинаToolStripMenuItem = new ToolStripMenuItem(); buttonIssuedOrder = new Button(); buttonOrderReady = new Button(); buttonTakeOrderInWork = new Button(); @@ -44,7 +46,7 @@ // // menuStrip // - menuStrip.Items.AddRange(new ToolStripItem[] { справочникиToolStripMenuItem }); + menuStrip.Items.AddRange(new ToolStripItem[] { справочникиToolStripMenuItem, пополнениеМагазинаToolStripMenuItem }); menuStrip.Location = new Point(0, 0); menuStrip.Name = "menuStrip"; menuStrip.Padding = new Padding(7, 2, 0, 2); @@ -54,7 +56,7 @@ // // справочникиToolStripMenuItem // - справочникиToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { компонентыToolStripMenuItem, мороженоеToolStripMenuItem }); + справочникиToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { компонентыToolStripMenuItem, мороженоеToolStripMenuItem, магазиныToolStripMenuItem }); справочникиToolStripMenuItem.Name = "справочникиToolStripMenuItem"; справочникиToolStripMenuItem.Size = new Size(94, 20); справочникиToolStripMenuItem.Text = "Справочники"; @@ -73,6 +75,20 @@ мороженоеToolStripMenuItem.Text = "Мороженое"; мороженоеToolStripMenuItem.Click += МороженоеToolStripMenuItem_Click; // + // магазиныToolStripMenuItem + // + магазиныToolStripMenuItem.Name = "магазиныToolStripMenuItem"; + магазиныToolStripMenuItem.Size = new Size(180, 22); + магазиныToolStripMenuItem.Text = "Магазины"; + магазиныToolStripMenuItem.Click += МагазиныToolStripMenuItem_Click; + // + // пополнениеМагазинаToolStripMenuItem + // + пополнениеМагазинаToolStripMenuItem.Name = "пополнениеМагазинаToolStripMenuItem"; + пополнениеМагазинаToolStripMenuItem.Size = new Size(143, 20); + пополнениеМагазинаToolStripMenuItem.Text = "Пополнение магазина"; + пополнениеМагазинаToolStripMenuItem.Click += ПополнениеМагазинаToolStripMenuItem_Click; + // // buttonIssuedOrder // buttonIssuedOrder.Anchor = AnchorStyles.Top | AnchorStyles.Right; @@ -187,6 +203,8 @@ private System.Windows.Forms.Button buttonCreateOrder; private System.Windows.Forms.DataGridView dataGridView; private System.Windows.Forms.Button buttonUpd; + private ToolStripMenuItem магазиныToolStripMenuItem; + private ToolStripMenuItem пополнениеМагазинаToolStripMenuItem; } } diff --git a/IceCreamShop/IceCreamShopView/FormMakeShipment.Designer.cs b/IceCreamShop/IceCreamShopView/FormMakeShipment.Designer.cs new file mode 100644 index 0000000..77ac97a --- /dev/null +++ b/IceCreamShop/IceCreamShopView/FormMakeShipment.Designer.cs @@ -0,0 +1,153 @@ +namespace IceCreamShopView +{ + partial class FormMakeShipment + { + /// + /// 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() + { + labelShop = new Label(); + comboBoxShop = new ComboBox(); + labelIceCream = new Label(); + comboBoxIceCream = new ComboBox(); + labelCount = new Label(); + textBoxCount = new TextBox(); + buttonSave = new Button(); + buttonCancel = new Button(); + SuspendLayout(); + // + // labelShop + // + labelShop.AutoSize = true; + labelShop.Location = new Point(14, 13); + labelShop.Margin = new Padding(4, 0, 4, 0); + labelShop.Name = "labelShop"; + labelShop.Size = new Size(60, 15); + labelShop.TabIndex = 2; + labelShop.Text = "Магазин :"; + // + // comboBoxShop + // + comboBoxShop.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxShop.FormattingEnabled = true; + comboBoxShop.Location = new Point(102, 10); + comboBoxShop.Margin = new Padding(4, 3, 4, 3); + comboBoxShop.Name = "comboBoxShop"; + comboBoxShop.Size = new Size(252, 23); + comboBoxShop.TabIndex = 5; + // + // labelIceCream + // + labelIceCream.AutoSize = true; + labelIceCream.Location = new Point(14, 48); + labelIceCream.Margin = new Padding(4, 0, 4, 0); + labelIceCream.Name = "labelIceCream"; + labelIceCream.Size = new Size(80, 15); + labelIceCream.TabIndex = 6; + labelIceCream.Text = "Мороженое :"; + // + // comboBoxIceCream + // + comboBoxIceCream.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxIceCream.FormattingEnabled = true; + comboBoxIceCream.Location = new Point(102, 44); + comboBoxIceCream.Margin = new Padding(4, 3, 4, 3); + comboBoxIceCream.Name = "comboBoxIceCream"; + comboBoxIceCream.Size = new Size(252, 23); + comboBoxIceCream.TabIndex = 7; + // + // labelCount + // + labelCount.AutoSize = true; + labelCount.Location = new Point(14, 83); + labelCount.Margin = new Padding(4, 0, 4, 0); + labelCount.Name = "labelCount"; + labelCount.Size = new Size(78, 15); + labelCount.TabIndex = 8; + labelCount.Text = "Количество :"; + // + // textBoxCount + // + textBoxCount.Location = new Point(102, 80); + textBoxCount.Margin = new Padding(4, 3, 4, 3); + textBoxCount.Name = "textBoxCount"; + textBoxCount.Size = new Size(252, 23); + textBoxCount.TabIndex = 9; + // + // buttonSave + // + buttonSave.Location = new Point(160, 112); + buttonSave.Margin = new Padding(4, 3, 4, 3); + buttonSave.Name = "buttonSave"; + buttonSave.Size = new Size(88, 27); + buttonSave.TabIndex = 10; + buttonSave.Text = "Сохранить"; + buttonSave.UseVisualStyleBackColor = true; + buttonSave.Click += ButtonSave_Click; + // + // buttonCancel + // + buttonCancel.Location = new Point(254, 112); + buttonCancel.Margin = new Padding(4, 3, 4, 3); + buttonCancel.Name = "buttonCancel"; + buttonCancel.Size = new Size(88, 27); + buttonCancel.TabIndex = 11; + buttonCancel.Text = "Отмена"; + buttonCancel.UseVisualStyleBackColor = true; + buttonCancel.Click += ButtonCancel_Click; + // + // FormMakeShipment + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(373, 147); + Controls.Add(buttonCancel); + Controls.Add(buttonSave); + Controls.Add(textBoxCount); + Controls.Add(labelCount); + Controls.Add(comboBoxIceCream); + Controls.Add(labelIceCream); + Controls.Add(comboBoxShop); + Controls.Add(labelShop); + Name = "FormMakeShipment"; + StartPosition = FormStartPosition.CenterScreen; + Text = "Пополнение магазина"; + Load += FormMakeShipment_Load; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private Label labelShop; + private ComboBox comboBoxShop; + private Label labelIceCream; + private ComboBox comboBoxIceCream; + private Label labelCount; + private TextBox textBoxCount; + private Button buttonSave; + private Button buttonCancel; + } +} \ No newline at end of file diff --git a/IceCreamShop/IceCreamShopView/FormMakeShipment.cs b/IceCreamShop/IceCreamShopView/FormMakeShipment.cs new file mode 100644 index 0000000..1c1217d --- /dev/null +++ b/IceCreamShop/IceCreamShopView/FormMakeShipment.cs @@ -0,0 +1,116 @@ +using IceCreamShopContracts.BusinessLogicsContracts; +using IceCreamShopContracts.SearchModels; +using Microsoft.Extensions.Logging; + +namespace IceCreamShopView +{ + public partial class FormMakeShipment : Form + { + private readonly ILogger _logger; + + private readonly IIceCreamLogic _logicIceCream; + + private readonly IShopLogic _logicShop; + + public FormMakeShipment(ILogger logger, IIceCreamLogic logicIceCream, IShopLogic logicShop) + { + InitializeComponent(); + _logger = logger; + _logicIceCream = logicIceCream; + _logicShop = logicShop; + } + + private void FormMakeShipment_Load(object sender, EventArgs e) + { + _logger.LogInformation("Ice creams loading"); + try + { + var list = _logicIceCream.ReadList(null); + if (list != null) + { + comboBoxIceCream.DisplayMember = "IceCreamName"; + comboBoxIceCream.ValueMember = "Id"; + comboBoxIceCream.DataSource = list; + comboBoxIceCream.SelectedItem = null; + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ice creams loading error"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + _logger.LogInformation("Shops loading"); + try + { + var list = _logicShop.ReadList(null); + if (list != null) + { + comboBoxShop.DisplayMember = "ShopName"; + comboBoxShop.ValueMember = "Id"; + comboBoxShop.DataSource = list; + comboBoxShop.SelectedItem = null; + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Shops loading error"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void ButtonSave_Click(object sender, EventArgs e) + { + if (comboBoxShop.SelectedValue == null) + { + MessageBox.Show("Выберите магазин", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (comboBoxIceCream.SelectedValue == null) + { + MessageBox.Show("Выберите мороженое", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (string.IsNullOrEmpty(textBoxCount.Text)) + { + MessageBox.Show("Заполните поле Количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Shop replenishment"); + try + { + var iceCream = _logicIceCream.ReadElement(new IceCreamSearchModel + { Id = Convert.ToInt32(comboBoxIceCream.SelectedValue) }); + if (iceCream == null) + { + throw new Exception("Мороженое не найдено."); + } + var operationResult = _logicShop.MakeShipment(new ShopSearchModel + { + Id = Convert.ToInt32(comboBoxShop.SelectedValue) + }, + iceCream, + 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, "Shop replenishment error"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + DialogResult = DialogResult.OK; + Close(); + } + + private void ButtonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + } +} diff --git a/IceCreamShop/IceCreamShopView/FormMakeShipment.resx b/IceCreamShop/IceCreamShopView/FormMakeShipment.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/IceCreamShop/IceCreamShopView/FormMakeShipment.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/IceCreamShop/IceCreamShopView/FormShop.Designer.cs b/IceCreamShop/IceCreamShopView/FormShop.Designer.cs new file mode 100644 index 0000000..9fca672 --- /dev/null +++ b/IceCreamShop/IceCreamShopView/FormShop.Designer.cs @@ -0,0 +1,213 @@ +namespace IceCreamShopView +{ + 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() + { + labelName = new Label(); + textBoxName = new TextBox(); + labelAddress = new Label(); + textBoxAddress = new TextBox(); + dateTimePicker = new DateTimePicker(); + labelOpeningDate = new Label(); + groupBoxIceCreams = new GroupBox(); + dataGridView = new DataGridView(); + ColumnId = new DataGridViewTextBoxColumn(); + ColumnName = new DataGridViewTextBoxColumn(); + ColumnCount = new DataGridViewTextBoxColumn(); + buttonSave = new Button(); + buttonCancel = new Button(); + groupBoxIceCreams.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); + SuspendLayout(); + // + // labelName + // + labelName.AutoSize = true; + labelName.Location = new Point(14, 10); + labelName.Margin = new Padding(4, 0, 4, 0); + labelName.Name = "labelName"; + labelName.Size = new Size(65, 15); + labelName.TabIndex = 1; + labelName.Text = "Название :"; + // + // textBoxName + // + textBoxName.Location = new Point(92, 7); + textBoxName.Margin = new Padding(4, 3, 4, 3); + textBoxName.Name = "textBoxName"; + textBoxName.Size = new Size(252, 23); + textBoxName.TabIndex = 2; + // + // labelAddress + // + labelAddress.AutoSize = true; + labelAddress.Location = new Point(14, 40); + labelAddress.Margin = new Padding(4, 0, 4, 0); + labelAddress.Name = "labelAddress"; + labelAddress.Size = new Size(46, 15); + labelAddress.TabIndex = 3; + labelAddress.Text = "Адрес :"; + // + // textBoxAddress + // + textBoxAddress.Location = new Point(92, 37); + textBoxAddress.Margin = new Padding(4, 3, 4, 3); + textBoxAddress.Name = "textBoxAddress"; + textBoxAddress.Size = new Size(252, 23); + textBoxAddress.TabIndex = 4; + // + // dateTimePicker + // + dateTimePicker.Location = new Point(126, 66); + dateTimePicker.Name = "dateTimePicker"; + dateTimePicker.Size = new Size(218, 23); + dateTimePicker.TabIndex = 5; + // + // labelOpeningDate + // + labelOpeningDate.AutoSize = true; + labelOpeningDate.Location = new Point(14, 69); + labelOpeningDate.Margin = new Padding(4, 0, 4, 0); + labelOpeningDate.Name = "labelOpeningDate"; + labelOpeningDate.Size = new Size(93, 15); + labelOpeningDate.TabIndex = 6; + labelOpeningDate.Text = "Дата открытия :"; + // + // groupBoxIceCreams + // + groupBoxIceCreams.Controls.Add(dataGridView); + groupBoxIceCreams.Location = new Point(4, 100); + groupBoxIceCreams.Margin = new Padding(4, 3, 4, 3); + groupBoxIceCreams.Name = "groupBoxIceCreams"; + groupBoxIceCreams.Padding = new Padding(4, 3, 4, 3); + groupBoxIceCreams.Size = new Size(469, 288); + groupBoxIceCreams.TabIndex = 7; + groupBoxIceCreams.TabStop = false; + groupBoxIceCreams.Text = "Мороженое"; + // + // dataGridView + // + dataGridView.AllowUserToAddRows = false; + dataGridView.AllowUserToDeleteRows = false; + dataGridView.BackgroundColor = SystemColors.ControlLightLight; + dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + dataGridView.Columns.AddRange(new DataGridViewColumn[] { ColumnId, ColumnName, ColumnCount }); + dataGridView.Dock = DockStyle.Left; + dataGridView.Location = new Point(4, 19); + dataGridView.Margin = new Padding(4, 3, 4, 3); + dataGridView.MultiSelect = false; + dataGridView.Name = "dataGridView"; + dataGridView.ReadOnly = true; + dataGridView.RowHeadersVisible = false; + dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect; + dataGridView.Size = new Size(457, 266); + dataGridView.TabIndex = 0; + // + // ColumnId + // + ColumnId.HeaderText = "Id"; + ColumnId.Name = "ColumnId"; + ColumnId.ReadOnly = true; + ColumnId.Visible = false; + // + // ColumnName + // + ColumnName.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + ColumnName.HeaderText = "Название мороженого"; + ColumnName.Name = "ColumnName"; + ColumnName.ReadOnly = true; + // + // ColumnCount + // + ColumnCount.HeaderText = "Количество"; + ColumnCount.Name = "ColumnCount"; + ColumnCount.ReadOnly = true; + // + // buttonSave + // + buttonSave.Location = new Point(255, 394); + buttonSave.Margin = new Padding(4, 3, 4, 3); + buttonSave.Name = "buttonSave"; + buttonSave.Size = new Size(88, 27); + buttonSave.TabIndex = 8; + buttonSave.Text = "Сохранить"; + buttonSave.UseVisualStyleBackColor = true; + buttonSave.Click += ButtonSave_Click; + // + // buttonCancel + // + buttonCancel.Location = new Point(359, 394); + buttonCancel.Margin = new Padding(4, 3, 4, 3); + buttonCancel.Name = "buttonCancel"; + buttonCancel.Size = new Size(88, 27); + buttonCancel.TabIndex = 9; + buttonCancel.Text = "Отмена"; + buttonCancel.UseVisualStyleBackColor = true; + buttonCancel.Click += ButtonCancel_Click; + // + // FormShop + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(478, 432); + Controls.Add(buttonCancel); + Controls.Add(buttonSave); + Controls.Add(groupBoxIceCreams); + Controls.Add(labelOpeningDate); + Controls.Add(dateTimePicker); + Controls.Add(textBoxAddress); + Controls.Add(labelAddress); + Controls.Add(textBoxName); + Controls.Add(labelName); + Name = "FormShop"; + StartPosition = FormStartPosition.CenterScreen; + Text = "Магазин"; + Load += FormShop_Load; + groupBoxIceCreams.ResumeLayout(false); + ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private Label labelName; + private TextBox textBoxName; + private Label labelAddress; + private TextBox textBoxAddress; + private DateTimePicker dateTimePicker; + private Label labelOpeningDate; + private GroupBox groupBoxIceCreams; + private DataGridView dataGridView; + private DataGridViewTextBoxColumn ColumnId; + private DataGridViewTextBoxColumn ColumnName; + private DataGridViewTextBoxColumn ColumnCount; + private Button buttonSave; + private Button buttonCancel; + } +} \ No newline at end of file diff --git a/IceCreamShop/IceCreamShopView/FormShop.cs b/IceCreamShop/IceCreamShopView/FormShop.cs new file mode 100644 index 0000000..5b61e3f --- /dev/null +++ b/IceCreamShop/IceCreamShopView/FormShop.cs @@ -0,0 +1,124 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.BusinessLogicsContracts; +using IceCreamShopContracts.SearchModels; +using IceCreamShopDataModels.Models; +using Microsoft.Extensions.Logging; + +namespace IceCreamShopView +{ + public partial class FormShop : Form + { + private readonly ILogger _logger; + + private readonly IShopLogic _logic; + + private int? _id; + + private Dictionary _shopIceCreams; + + public int Id { set { _id = value; } } + + public FormShop(ILogger logger, IShopLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + _shopIceCreams = new Dictionary(); + } + + private void FormShop_Load(object sender, EventArgs e) + { + if (_id.HasValue) + { + _logger.LogInformation("Shop loading"); + try + { + var view = _logic.ReadElement(new ShopSearchModel { Id = _id.Value }); + if (view != null) + { + textBoxName.Text = view.ShopName; + textBoxAddress.Text = view.Address; + dateTimePicker.Value = view.DateOpening; + _shopIceCreams = view.ShopIceCreams ?? new Dictionary(); + LoadData(); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Shop loading error"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + + private void LoadData() + { + _logger.LogInformation("Shop ice creams loading"); + try + { + if (_shopIceCreams != null) + { + dataGridView.Rows.Clear(); + foreach (var iceCream in _shopIceCreams) + { + dataGridView.Rows.Add(new object[] { iceCream.Key, iceCream.Value.Item1.IceCreamName, iceCream.Value.Item2 }); + } + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Shop ice creams loading error"); + 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(textBoxAddress.Text)) + { + MessageBox.Show("Заполните адрес", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (string.IsNullOrEmpty(dateTimePicker.Text)) + { + MessageBox.Show("Заполните дату", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Shop saving"); + try + { + var model = new ShopBindingModel + { + Id = _id ?? 0, + ShopName = textBoxName.Text, + Address = textBoxAddress.Text, + DateOpening = dateTimePicker.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, "Shop saving error"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void ButtonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + } +} diff --git a/IceCreamShop/IceCreamShopView/FormShop.resx b/IceCreamShop/IceCreamShopView/FormShop.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/IceCreamShop/IceCreamShopView/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/IceCreamShop/IceCreamShopView/FormShops.Designer.cs b/IceCreamShop/IceCreamShopView/FormShops.Designer.cs new file mode 100644 index 0000000..5b61657 --- /dev/null +++ b/IceCreamShop/IceCreamShopView/FormShops.Designer.cs @@ -0,0 +1,126 @@ +namespace IceCreamShopView +{ + 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() + { + dataGridView = new DataGridView(); + buttonUpd = new Button(); + buttonDel = new Button(); + buttonEdit = new Button(); + buttonAdd = new Button(); + ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); + SuspendLayout(); + // + // dataGridView + // + dataGridView.AllowUserToAddRows = false; + dataGridView.AllowUserToDeleteRows = false; + dataGridView.BackgroundColor = SystemColors.ControlLightLight; + dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + dataGridView.Dock = DockStyle.Left; + dataGridView.Location = new Point(0, 0); + dataGridView.Margin = new Padding(4, 3, 4, 3); + dataGridView.MultiSelect = false; + dataGridView.Name = "dataGridView"; + dataGridView.ReadOnly = true; + dataGridView.RowHeadersVisible = false; + dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect; + dataGridView.Size = new Size(408, 360); + dataGridView.TabIndex = 2; + // + // buttonUpd + // + buttonUpd.Location = new Point(432, 152); + buttonUpd.Margin = new Padding(4, 3, 4, 3); + buttonUpd.Name = "buttonUpd"; + buttonUpd.Size = new Size(88, 27); + buttonUpd.TabIndex = 12; + buttonUpd.Text = "Обновить"; + buttonUpd.UseVisualStyleBackColor = true; + buttonUpd.Click += ButtonUpd_Click; + // + // buttonDel + // + buttonDel.Location = new Point(432, 105); + buttonDel.Margin = new Padding(4, 3, 4, 3); + buttonDel.Name = "buttonDel"; + buttonDel.Size = new Size(88, 27); + buttonDel.TabIndex = 11; + buttonDel.Text = "Удалить"; + buttonDel.UseVisualStyleBackColor = true; + buttonDel.Click += ButtonDel_Click; + // + // buttonEdit + // + buttonEdit.Location = new Point(432, 58); + buttonEdit.Margin = new Padding(4, 3, 4, 3); + buttonEdit.Name = "buttonEdit"; + buttonEdit.Size = new Size(88, 27); + buttonEdit.TabIndex = 10; + buttonEdit.Text = "Изменить"; + buttonEdit.UseVisualStyleBackColor = true; + buttonEdit.Click += ButtonEdit_Click; + // + // buttonAdd + // + buttonAdd.Location = new Point(432, 14); + buttonAdd.Margin = new Padding(4, 3, 4, 3); + buttonAdd.Name = "buttonAdd"; + buttonAdd.Size = new Size(88, 27); + buttonAdd.TabIndex = 9; + buttonAdd.Text = "Добавить"; + buttonAdd.UseVisualStyleBackColor = true; + buttonAdd.Click += ButtonAdd_Click; + // + // FormShops + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(541, 360); + Controls.Add(buttonUpd); + Controls.Add(buttonDel); + Controls.Add(buttonEdit); + Controls.Add(buttonAdd); + Controls.Add(dataGridView); + Name = "FormShops"; + StartPosition = FormStartPosition.CenterScreen; + Text = "Магазины"; + Load += FormShops_Load; + ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); + ResumeLayout(false); + } + + #endregion + + private DataGridView dataGridView; + private Button buttonUpd; + private Button buttonDel; + private Button buttonEdit; + private Button buttonAdd; + } +} \ No newline at end of file diff --git a/IceCreamShop/IceCreamShopView/FormShops.cs b/IceCreamShop/IceCreamShopView/FormShops.cs new file mode 100644 index 0000000..288f631 --- /dev/null +++ b/IceCreamShop/IceCreamShopView/FormShops.cs @@ -0,0 +1,104 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; + +namespace IceCreamShopView +{ + 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["ShopName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + dataGridView.Columns["ShopIceCreams"].Visible = false; + } + _logger.LogInformation("Shops loading"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Shops loading error"); + 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 ButtonEdit_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("Deletion of shop"); + try + { + if (!_logic.Delete(new ShopBindingModel { Id = id })) + { + throw new Exception("Ошибка при удалении. Дополнительная информация в логах."); + } + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Shop deletion error"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + } + + private void ButtonUpd_Click(object sender, EventArgs e) + { + LoadData(); + } + } +} diff --git a/IceCreamShop/IceCreamShopView/FormShops.resx b/IceCreamShop/IceCreamShopView/FormShops.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/IceCreamShop/IceCreamShopView/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/IceCreamShop/IceCreamShopView/Program.cs b/IceCreamShop/IceCreamShopView/Program.cs index d811e5a..486b6e9 100644 --- a/IceCreamShop/IceCreamShopView/Program.cs +++ b/IceCreamShop/IceCreamShopView/Program.cs @@ -38,10 +38,12 @@ namespace IceCreamShopView services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); @@ -50,6 +52,9 @@ namespace IceCreamShopView services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file -- 2.25.1 From 1b18777f228b55ec4b598ef6cc2ac4a0621b90d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=BB=D1=8C=D1=8F?= <Илья@WIN-RANNDDD> Date: Mon, 12 Feb 2024 12:53:03 +0400 Subject: [PATCH 02/11] =?UTF-8?q?=D0=A4=D0=B8=D0=BA=D1=81=20=D1=84=D0=BE?= =?UTF-8?q?=D1=80=D0=BC=D1=8B=20=D0=BA=D0=BE=D0=BC=D0=BF=D0=BE=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D1=82=D0=B0=20=D0=B8=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0?= =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BF=D1=80=D0=BE=D0=B1?= =?UTF-8?q?=D0=BB=D0=B5=D0=BC=D1=8B=20=D1=81=20=D0=BE=D0=B1=D0=BD=D0=BE?= =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=D0=BC=20=D0=BC=D0=B0=D0=B3?= =?UTF-8?q?=D0=B0=D0=B7=D0=B8=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- IceCreamShop/IceCreamShopView/FormComponent.Designer.cs | 1 + IceCreamShop/IceCreamShopView/FormShop.cs | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/IceCreamShop/IceCreamShopView/FormComponent.Designer.cs b/IceCreamShop/IceCreamShopView/FormComponent.Designer.cs index b15d0b3..0102b94 100644 --- a/IceCreamShop/IceCreamShopView/FormComponent.Designer.cs +++ b/IceCreamShop/IceCreamShopView/FormComponent.Designer.cs @@ -106,6 +106,7 @@ Name = "FormComponent"; StartPosition = FormStartPosition.CenterScreen; Text = "Компонент"; + Load += FormComponent_Load; ResumeLayout(false); PerformLayout(); } diff --git a/IceCreamShop/IceCreamShopView/FormShop.cs b/IceCreamShop/IceCreamShopView/FormShop.cs index 5b61e3f..44bdbac 100644 --- a/IceCreamShop/IceCreamShopView/FormShop.cs +++ b/IceCreamShop/IceCreamShopView/FormShop.cs @@ -97,7 +97,8 @@ namespace IceCreamShopView Id = _id ?? 0, ShopName = textBoxName.Text, Address = textBoxAddress.Text, - DateOpening = dateTimePicker.Value + DateOpening = dateTimePicker.Value, + ShopIceCreams = _shopIceCreams }; var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model); if (!operationResult) -- 2.25.1 From 63875c64d0acec2b16ccc3c4119ec64af5e6b39d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=BB=D1=8C=D1=8F?= <Илья@WIN-RANNDDD> Date: Mon, 12 Feb 2024 13:09:25 +0400 Subject: [PATCH 03/11] =?UTF-8?q?Revert=20"=D0=A4=D0=B8=D0=BA=D1=81=20?= =?UTF-8?q?=D1=84=D0=BE=D1=80=D0=BC=D1=8B=20=D0=BA=D0=BE=D0=BC=D0=BF=D0=BE?= =?UTF-8?q?=D0=BD=D0=B5=D0=BD=D1=82=D0=B0=20=D0=B8=20=D0=B8=D1=81=D0=BF?= =?UTF-8?q?=D1=80=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BF=D1=80?= =?UTF-8?q?=D0=BE=D0=B1=D0=BB=D0=B5=D0=BC=D1=8B=20=D1=81=20=D0=BE=D0=B1?= =?UTF-8?q?=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=D0=BC=20=D0=BC?= =?UTF-8?q?=D0=B0=D0=B3=D0=B0=D0=B7=D0=B8=D0=BD=D0=B0"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 1b18777f228b55ec4b598ef6cc2ac4a0621b90d7. --- IceCreamShop/IceCreamShopView/FormComponent.Designer.cs | 1 - IceCreamShop/IceCreamShopView/FormShop.cs | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/IceCreamShop/IceCreamShopView/FormComponent.Designer.cs b/IceCreamShop/IceCreamShopView/FormComponent.Designer.cs index 0102b94..b15d0b3 100644 --- a/IceCreamShop/IceCreamShopView/FormComponent.Designer.cs +++ b/IceCreamShop/IceCreamShopView/FormComponent.Designer.cs @@ -106,7 +106,6 @@ Name = "FormComponent"; StartPosition = FormStartPosition.CenterScreen; Text = "Компонент"; - Load += FormComponent_Load; ResumeLayout(false); PerformLayout(); } diff --git a/IceCreamShop/IceCreamShopView/FormShop.cs b/IceCreamShop/IceCreamShopView/FormShop.cs index 44bdbac..5b61e3f 100644 --- a/IceCreamShop/IceCreamShopView/FormShop.cs +++ b/IceCreamShop/IceCreamShopView/FormShop.cs @@ -97,8 +97,7 @@ namespace IceCreamShopView Id = _id ?? 0, ShopName = textBoxName.Text, Address = textBoxAddress.Text, - DateOpening = dateTimePicker.Value, - ShopIceCreams = _shopIceCreams + DateOpening = dateTimePicker.Value }; var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model); if (!operationResult) -- 2.25.1 From 39b63af0eca4936569690bd748358031780f1f9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=BB=D1=8C=D1=8F?= <Илья@WIN-RANNDDD> Date: Mon, 12 Feb 2024 13:15:06 +0400 Subject: [PATCH 04/11] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BF=D1=80=D0=BE=D0=B1=D0=BB?= =?UTF-8?q?=D0=B5=D0=BC=D1=8B=20=D1=81=20=D0=BE=D0=B1=D0=BD=D0=BE=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=D0=BC=20=D0=BC=D0=B0=D0=B3=D0=B0?= =?UTF-8?q?=D0=B7=D0=B8=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- IceCreamShop/IceCreamShopView/FormShop.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/IceCreamShop/IceCreamShopView/FormShop.cs b/IceCreamShop/IceCreamShopView/FormShop.cs index 5b61e3f..44bdbac 100644 --- a/IceCreamShop/IceCreamShopView/FormShop.cs +++ b/IceCreamShop/IceCreamShopView/FormShop.cs @@ -97,7 +97,8 @@ namespace IceCreamShopView Id = _id ?? 0, ShopName = textBoxName.Text, Address = textBoxAddress.Text, - DateOpening = dateTimePicker.Value + DateOpening = dateTimePicker.Value, + ShopIceCreams = _shopIceCreams }; var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model); if (!operationResult) -- 2.25.1 From 31bcb77f01ef12ff6790bddc09fd037604c0896d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=BB=D1=8C=D1=8F?= <Илья@WIN-RANNDDD> Date: Fri, 8 Mar 2024 23:03:33 +0400 Subject: [PATCH 05/11] =?UTF-8?q?=D0=93=D0=BE=D1=82=D0=BE=D0=B2=D0=B0?= =?UTF-8?q?=D1=8F=202=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=BD=D0=B0=D1=8F=20(=D0=A3=D1=81=D0=BB=D0=BE=D0=B6?= =?UTF-8?q?=D0=BD=D0=B5=D0=BD=D0=BD=D0=B0=D1=8F)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/OrderLogic.cs | 94 +++++++++++- .../BusinessLogics/ShopLogic.cs | 11 ++ .../BindingModels/ShopBindingModel.cs | 2 + .../BusinessLogicsContracts/IShopLogic.cs | 2 + .../StoragesContracts/IShopStorage.cs | 3 + .../ViewModels/ShopViewModel.cs | 3 + .../Models/IShopModel.cs | 2 + .../DataFileSingleton.cs | 7 + .../Implements/ShopStorage.cs | 134 ++++++++++++++++++ .../IceCreamShopFileImplement/Models/Shop.cs | 108 ++++++++++++++ .../Implements/ShopStorage.cs | 8 +- .../IceCreamShopListImplement/Models/Shop.cs | 9 +- .../FormIceCreamSale.Designer.cs | 127 +++++++++++++++++ .../IceCreamShopView/FormIceCreamSale.cs | 87 ++++++++++++ .../IceCreamShopView/FormIceCreamSale.resx | 120 ++++++++++++++++ IceCreamShop/IceCreamShopView/FormMain.cs | 9 ++ .../IceCreamShopView/FormMain.designer.cs | 17 ++- .../IceCreamShopView/FormShop.Designer.cs | 32 ++++- IceCreamShop/IceCreamShopView/FormShop.cs | 7 + IceCreamShop/IceCreamShopView/Program.cs | 1 + 20 files changed, 766 insertions(+), 17 deletions(-) create mode 100644 IceCreamShop/IceCreamShopFileImplement/Implements/ShopStorage.cs create mode 100644 IceCreamShop/IceCreamShopFileImplement/Models/Shop.cs create mode 100644 IceCreamShop/IceCreamShopView/FormIceCreamSale.Designer.cs create mode 100644 IceCreamShop/IceCreamShopView/FormIceCreamSale.cs create mode 100644 IceCreamShop/IceCreamShopView/FormIceCreamSale.resx diff --git a/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogics/OrderLogic.cs b/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogics/OrderLogic.cs index b8037ea..41b8928 100644 --- a/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogics/OrderLogic.cs @@ -4,6 +4,7 @@ using IceCreamShopContracts.SearchModels; using IceCreamShopContracts.StoragesContracts; using IceCreamShopContracts.ViewModels; using IceCreamShopDataModels.Enums; +using IceCreamShopDataModels.Models; using Microsoft.Extensions.Logging; namespace IceCreamShopBusinessLogic.BusinessLogics @@ -14,10 +15,20 @@ namespace IceCreamShopBusinessLogic.BusinessLogics private readonly IOrderStorage _orderStorage; - public OrderLogic(ILogger logger, IOrderStorage orderStorage) + private readonly IIceCreamStorage _iceCreamStorage; + + private readonly IShopStorage _shopStorage; + + private readonly IShopLogic _shopLogic; + + public OrderLogic(ILogger logger, IOrderStorage orderStorage, IIceCreamStorage iceCreamStorage, + IShopStorage shopStorage, IShopLogic shopLogic) { _logger = logger; _orderStorage = orderStorage; + _iceCreamStorage = iceCreamStorage; + _shopStorage = shopStorage; + _shopLogic = shopLogic; } public bool CreateOrder(OrderBindingModel model) @@ -106,15 +117,26 @@ namespace IceCreamShopBusinessLogic.BusinessLogics newStatus, order.Status); return false; } - model.IceCreamId = order.IceCreamId; - model.Count = order.Count; - model.Sum = order.Sum; - model.DateCreate = order.DateCreate; + if (newStatus == OrderStatus.Выдан) + { + var iceCream = _iceCreamStorage.GetElement(new IceCreamSearchModel() { Id = order.IceCreamId }); + if (iceCream == null) + { + _logger.LogWarning("Change status operation failed. Ice cream not found"); + return false; + } + if (!DeliverIceCreams(iceCream, order.Count)) + { + _logger.LogWarning("Change status operation failed. Ice creams delivery operation failed"); + return false; + } + } model.Status = newStatus; if (model.Status == OrderStatus.Готов) { model.DateImplement = DateTime.Now; - } else + } + else { model.DateImplement = order.DateImplement; } @@ -125,5 +147,65 @@ namespace IceCreamShopBusinessLogic.BusinessLogics } return true; } + + private bool DeliverIceCreams(IIceCreamModel iceCream, int count) + { + if (count <= 0) + { + _logger.LogWarning("Ice creams delivery operation failed. Ice cream count <= 0"); + return false; + } + + var shopList = _shopStorage.GetFullList(); + int shopsCapacity = shopList.Sum(x => x.IceCreamsMaximum); + int currentIceCreams = shopList.Select(x => x.ShopIceCreams.Select(y => y.Value.Item2).Sum()).Sum(); + int freePlaces = shopsCapacity - currentIceCreams; + + if (freePlaces < count) + { + _logger.LogWarning("Ice creams delivery operation failed. No space for new ice creams"); + return false; + } + + foreach (var shop in shopList) + { + freePlaces = shop.IceCreamsMaximum - shop.ShopIceCreams.Sum(x => x.Value.Item2); + if (freePlaces == 0) + { + continue; + } + if (freePlaces >= count) + { + if (_shopLogic.MakeShipment(new() { Id = shop.Id }, iceCream, count)) + { + count = 0; + } + else + { + _logger.LogWarning("Ice creams delivery operation failed"); + return false; + } + } + else + { + if (_shopLogic.MakeShipment(new() { Id = shop.Id }, iceCream, freePlaces)) + { + count -= freePlaces; + } + else + { + _logger.LogWarning("Ice creams delivery operation failed"); + return false; + } + } + if (count == 0) + { + return true; + } + } + return false; + } } + + } diff --git a/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogics/ShopLogic.cs b/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogics/ShopLogic.cs index 4f05818..e4ddd2d 100644 --- a/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogics/ShopLogic.cs +++ b/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogics/ShopLogic.cs @@ -105,6 +105,11 @@ namespace IceCreamShopBusinessLogic.BusinessLogics _logger.LogWarning("MakeShipment(GetElement). Element not found"); return false; } + if (shop.IceCreamsMaximum - shop.ShopIceCreams.Sum(x => x.Value.Item2) < count) + { + _logger.LogWarning("MakeShipment error. No space for new ice creams"); + return false; + } if (shop.ShopIceCreams.ContainsKey(iceCream.Id)) { var shopIC = shop.ShopIceCreams[iceCream.Id]; @@ -125,6 +130,7 @@ namespace IceCreamShopBusinessLogic.BusinessLogics ShopName = shop.ShopName, Address = shop.Address, DateOpening = shop.DateOpening, + IceCreamsMaximum = shop.IceCreamsMaximum, ShopIceCreams = shop.ShopIceCreams, }) == null) { @@ -134,6 +140,11 @@ namespace IceCreamShopBusinessLogic.BusinessLogics return true; } + public bool MakeSale(IIceCreamModel model, int count) + { + return _shopStorage.MakeSale(model, count); + } + private void CheckModel(ShopBindingModel model, bool withParams = true) { if (model == null) diff --git a/IceCreamShop/IceCreamShopContracts/BindingModels/ShopBindingModel.cs b/IceCreamShop/IceCreamShopContracts/BindingModels/ShopBindingModel.cs index 0710d3d..66393f0 100644 --- a/IceCreamShop/IceCreamShopContracts/BindingModels/ShopBindingModel.cs +++ b/IceCreamShop/IceCreamShopContracts/BindingModels/ShopBindingModel.cs @@ -17,5 +17,7 @@ namespace IceCreamShopContracts.BindingModels get; set; } = new(); + + public int IceCreamsMaximum { get; set; } } } diff --git a/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IShopLogic.cs b/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IShopLogic.cs index 8e9a7fa..9c2bd2c 100644 --- a/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IShopLogic.cs +++ b/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IShopLogic.cs @@ -18,5 +18,7 @@ namespace IceCreamShopContracts.BusinessLogicsContracts bool Delete(ShopBindingModel model); bool MakeShipment(ShopSearchModel shopModel, IIceCreamModel iceCream, int count); + + bool MakeSale(IIceCreamModel model, int count); } } diff --git a/IceCreamShop/IceCreamShopContracts/StoragesContracts/IShopStorage.cs b/IceCreamShop/IceCreamShopContracts/StoragesContracts/IShopStorage.cs index 374366b..746c4f3 100644 --- a/IceCreamShop/IceCreamShopContracts/StoragesContracts/IShopStorage.cs +++ b/IceCreamShop/IceCreamShopContracts/StoragesContracts/IShopStorage.cs @@ -1,6 +1,7 @@ using IceCreamShopContracts.BindingModels; using IceCreamShopContracts.SearchModels; using IceCreamShopContracts.ViewModels; +using IceCreamShopDataModels.Models; namespace IceCreamShopContracts.StoragesContracts { @@ -17,5 +18,7 @@ namespace IceCreamShopContracts.StoragesContracts ShopViewModel? Update(ShopBindingModel model); ShopViewModel? Delete(ShopBindingModel model); + + bool MakeSale(IIceCreamModel model, int count); } } diff --git a/IceCreamShop/IceCreamShopContracts/ViewModels/ShopViewModel.cs b/IceCreamShop/IceCreamShopContracts/ViewModels/ShopViewModel.cs index 066a909..b1873a2 100644 --- a/IceCreamShop/IceCreamShopContracts/ViewModels/ShopViewModel.cs +++ b/IceCreamShop/IceCreamShopContracts/ViewModels/ShopViewModel.cs @@ -21,5 +21,8 @@ namespace IceCreamShopContracts.ViewModels get; set; } = new(); + + [DisplayName("Максимальное количество мороженого")] + public int IceCreamsMaximum { get; set; } } } diff --git a/IceCreamShop/IceCreamShopDataModels/Models/IShopModel.cs b/IceCreamShop/IceCreamShopDataModels/Models/IShopModel.cs index 91542ec..84e0301 100644 --- a/IceCreamShop/IceCreamShopDataModels/Models/IShopModel.cs +++ b/IceCreamShop/IceCreamShopDataModels/Models/IShopModel.cs @@ -9,5 +9,7 @@ DateTime DateOpening { get; } Dictionary ShopIceCreams { get; } + + int IceCreamsMaximum { get; } } } diff --git a/IceCreamShop/IceCreamShopFileImplement/DataFileSingleton.cs b/IceCreamShop/IceCreamShopFileImplement/DataFileSingleton.cs index 2a184a8..550330b 100644 --- a/IceCreamShop/IceCreamShopFileImplement/DataFileSingleton.cs +++ b/IceCreamShop/IceCreamShopFileImplement/DataFileSingleton.cs @@ -13,12 +13,16 @@ namespace IceCreamShopFileImplement private readonly string IceCreamFileName = "IceCream.xml"; + private readonly string ShopFileName = "Shop.xml"; + public List Components { get; private set; } public List Orders { get; private set; } public List IceCreams { get; private set; } + public List Shops { get; private set; } + public static DataFileSingleton GetInstance() { if (instance == null) @@ -34,11 +38,14 @@ namespace IceCreamShopFileImplement public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + public void SaveShops() => SaveData(Shops, ShopFileName, "Shops", x => x.GetXElement); + private DataFileSingleton() { Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; IceCreams = LoadData(IceCreamFileName, "IceCream", x => IceCream.Create(x)!)!; Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; + Shops = LoadData(ShopFileName, "Shop", x => Shop.Create(x)!)!; } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) diff --git a/IceCreamShop/IceCreamShopFileImplement/Implements/ShopStorage.cs b/IceCreamShop/IceCreamShopFileImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..578f972 --- /dev/null +++ b/IceCreamShop/IceCreamShopFileImplement/Implements/ShopStorage.cs @@ -0,0 +1,134 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.StoragesContracts; +using IceCreamShopContracts.ViewModels; +using IceCreamShopDataModels.Models; +using IceCreamShopFileImplement.Models; +using System.Collections.Generic; + +namespace IceCreamShopFileImplement.Implements +{ + public class ShopStorage : IShopStorage + { + private readonly DataFileSingleton source; + + public ShopStorage() + { + source = DataFileSingleton.GetInstance(); + } + + public List GetFullList() + { + return source.Shops + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName)) + { + return new(); + } + return source.Shops + .Where(x => x.ShopName.Contains(model.ShopName)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue) + { + return null; + } + return source.Shops + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ShopName) && x.ShopName == model.ShopName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public ShopViewModel? Insert(ShopBindingModel model) + { + model.Id = source.Shops.Count > 0 ? source.Shops.Max(x => x.Id) + 1 : 1; + var newShop = Shop.Create(model); + if (newShop == null) + { + return null; + } + source.Shops.Add(newShop); + source.SaveShops(); + return newShop.GetViewModel; + } + + public ShopViewModel? Update(ShopBindingModel model) + { + var shop = source.Shops.FirstOrDefault(x => x.Id == model.Id); + if (shop == null) + { + return null; + } + shop.Update(model); + source.SaveShops(); + return shop.GetViewModel; + } + + public ShopViewModel? Delete(ShopBindingModel model) + { + var element = source.Shops.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + source.Shops.Remove(element); + source.SaveShops(); + return element.GetViewModel; + } + return null; + } + + public bool MakeSale(IIceCreamModel model, int count) + { + var iceCream = source.IceCreams.FirstOrDefault(x => x.Id == model.Id); + int countInShops = source.Shops.Select(x => x.ShopIceCreams.Any(y => y.Key == model.Id) + ? x.ShopIceCreams.Where(y => y.Key == model.Id).First().Value.Item2 : 0).Sum(); + + if (iceCream == null || countInShops < count) + { + return false; + } + + foreach (var shop in source.Shops) + { + var shopIceCreams = shop.ShopIceCreams.Where(x => x.Key == model.Id); + if (shopIceCreams.Any()) + { + var shopIceCream = shopIceCreams.First(); + int min = Math.Min(shopIceCream.Value.Item2, count); + if (min == shopIceCream.Value.Item2) + { + shop.ShopIceCreams.Remove(shopIceCream.Key); + } + else + { + shop.ShopIceCreams[shopIceCream.Key] = (shopIceCream.Value.Item1, shopIceCream.Value.Item2 - min); + } + shop.Update(new ShopBindingModel + { + Id = model.Id, + ShopName = shop.ShopName, + Address = shop.Address, + DateOpening = shop.DateOpening, + ShopIceCreams = shop.ShopIceCreams, + IceCreamsMaximum = shop.IceCreamsMaximum + }); + count -= min; + if (count <= 0) + { + break; + } + } + } + source.SaveShops(); + return true; + } + } +} diff --git a/IceCreamShop/IceCreamShopFileImplement/Models/Shop.cs b/IceCreamShop/IceCreamShopFileImplement/Models/Shop.cs new file mode 100644 index 0000000..ef5c368 --- /dev/null +++ b/IceCreamShop/IceCreamShopFileImplement/Models/Shop.cs @@ -0,0 +1,108 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.ViewModels; +using IceCreamShopDataModels.Models; +using System.Xml.Linq; + +namespace IceCreamShopFileImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; private set; } + + public string ShopName { get; private set; } = string.Empty; + + public string Address { get; private set; } = string.Empty; + + public DateTime DateOpening { get; private set; } + + public Dictionary IceCreams { get; private set; } = new(); + + private Dictionary? _shopIceCreams = null; + + public Dictionary ShopIceCreams + { + get + { + if (_shopIceCreams == null) + { + var source = DataFileSingleton.GetInstance(); + _shopIceCreams = IceCreams.ToDictionary(x => x.Key, + y => ((source.IceCreams.FirstOrDefault(z => z.Id == y.Key) as IIceCreamModel)!, y.Value)); + } + return _shopIceCreams; + } + } + + public int IceCreamsMaximum { get; private set; } + + public static Shop? Create(ShopBindingModel? model) + { + if (model == null) + { + return null; + } + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Address = model.Address, + DateOpening = model.DateOpening, + IceCreams = model.ShopIceCreams.ToDictionary(x => x.Key, x => x.Value.Item2), + IceCreamsMaximum = model.IceCreamsMaximum + }; + } + + public static Shop? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Shop() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ShopName = element.Element("ShopName")!.Value, + Address = element.Element("Address")!.Value, + DateOpening = Convert.ToDateTime(element.Element("DateOpening")!.Value), + IceCreamsMaximum = Convert.ToInt32(element.Element("IceCreamsMaximum")!.Value), + IceCreams = element.Element("ShopIceCreams")!.Elements("ShopIceCream") + .ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value)) + }; + } + + public void Update(ShopBindingModel? model) + { + if (model == null) + { + return; + } + ShopName = model.ShopName; + Address = model.Address; + DateOpening = model.DateOpening; + IceCreamsMaximum = model.IceCreamsMaximum; + IceCreams = model.ShopIceCreams.ToDictionary(x => x.Key, x => x.Value.Item2); + _shopIceCreams = null; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + DateOpening = DateOpening, + ShopIceCreams = ShopIceCreams, + IceCreamsMaximum = IceCreamsMaximum + }; + + public XElement GetXElement => new("Shop", + new XAttribute("Id", Id), + new XElement("ShopName", ShopName), + new XElement("Address", Address), + new XElement("DateOpening", DateOpening.ToString()), + new XElement("IceCreamsMaximum", IceCreamsMaximum.ToString()), + new XElement("ShopIceCreams", + IceCreams.Select(x => new XElement("ShopIceCream", + new XElement("Key", x.Key), + new XElement("Value", x.Value))).ToArray())); + } +} diff --git a/IceCreamShop/IceCreamShopListImplement/Implements/ShopStorage.cs b/IceCreamShop/IceCreamShopListImplement/Implements/ShopStorage.cs index f39611e..c866429 100644 --- a/IceCreamShop/IceCreamShopListImplement/Implements/ShopStorage.cs +++ b/IceCreamShop/IceCreamShopListImplement/Implements/ShopStorage.cs @@ -2,6 +2,7 @@ using IceCreamShopContracts.SearchModels; using IceCreamShopContracts.StoragesContracts; using IceCreamShopContracts.ViewModels; +using IceCreamShopDataModels.Models; using IceCreamShopListImplement.Models; namespace IceCreamShopListImplement.Implements @@ -104,6 +105,11 @@ namespace IceCreamShopListImplement.Implements } } return null; - } + } + + public bool MakeSale(IIceCreamModel model, int count) + { + throw new NotImplementedException(); + } } } diff --git a/IceCreamShop/IceCreamShopListImplement/Models/Shop.cs b/IceCreamShop/IceCreamShopListImplement/Models/Shop.cs index 4602d84..cca24fc 100644 --- a/IceCreamShop/IceCreamShopListImplement/Models/Shop.cs +++ b/IceCreamShop/IceCreamShopListImplement/Models/Shop.cs @@ -20,6 +20,8 @@ namespace IceCreamShopListImplement.Models private set; } = new Dictionary(); + public int IceCreamsMaximum { get; private set; } + public static Shop? Create(ShopBindingModel? model) { if (model == null) @@ -32,7 +34,8 @@ namespace IceCreamShopListImplement.Models ShopName = model.ShopName, Address = model.Address, DateOpening = model.DateOpening, - ShopIceCreams = model.ShopIceCreams + ShopIceCreams = model.ShopIceCreams, + IceCreamsMaximum = model.IceCreamsMaximum }; } @@ -46,6 +49,7 @@ namespace IceCreamShopListImplement.Models Address = model.Address; DateOpening = model.DateOpening; ShopIceCreams = model.ShopIceCreams; + IceCreamsMaximum -= model.IceCreamsMaximum; } public ShopViewModel GetViewModel => new() @@ -54,7 +58,8 @@ namespace IceCreamShopListImplement.Models ShopName = ShopName, Address = Address, DateOpening = DateOpening, - ShopIceCreams = ShopIceCreams + ShopIceCreams = ShopIceCreams, + IceCreamsMaximum = IceCreamsMaximum }; } } diff --git a/IceCreamShop/IceCreamShopView/FormIceCreamSale.Designer.cs b/IceCreamShop/IceCreamShopView/FormIceCreamSale.Designer.cs new file mode 100644 index 0000000..d520bb0 --- /dev/null +++ b/IceCreamShop/IceCreamShopView/FormIceCreamSale.Designer.cs @@ -0,0 +1,127 @@ +namespace IceCreamShopView +{ + partial class FormIceCreamSale + { + /// + /// 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() + { + buttonCancel = new Button(); + buttonSale = new Button(); + textBoxCount = new TextBox(); + labelCount = new Label(); + comboBoxIceCream = new ComboBox(); + labelIceCream = new Label(); + SuspendLayout(); + // + // buttonCancel + // + buttonCancel.Location = new Point(253, 83); + buttonCancel.Margin = new Padding(4, 3, 4, 3); + buttonCancel.Name = "buttonCancel"; + buttonCancel.Size = new Size(88, 27); + buttonCancel.TabIndex = 17; + buttonCancel.Text = "Отмена"; + buttonCancel.UseVisualStyleBackColor = true; + buttonCancel.Click += ButtonCancel_Click; + // + // buttonSale + // + buttonSale.Location = new Point(159, 83); + buttonSale.Margin = new Padding(4, 3, 4, 3); + buttonSale.Name = "buttonSale"; + buttonSale.Size = new Size(88, 27); + buttonSale.TabIndex = 16; + buttonSale.Text = "Продать"; + buttonSale.UseVisualStyleBackColor = true; + buttonSale.Click += ButtonSale_Click; + // + // textBoxCount + // + textBoxCount.Location = new Point(101, 51); + textBoxCount.Margin = new Padding(4, 3, 4, 3); + textBoxCount.Name = "textBoxCount"; + textBoxCount.Size = new Size(252, 23); + textBoxCount.TabIndex = 15; + // + // labelCount + // + labelCount.AutoSize = true; + labelCount.Location = new Point(13, 54); + labelCount.Margin = new Padding(4, 0, 4, 0); + labelCount.Name = "labelCount"; + labelCount.Size = new Size(78, 15); + labelCount.TabIndex = 14; + labelCount.Text = "Количество :"; + // + // comboBoxIceCream + // + comboBoxIceCream.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxIceCream.FormattingEnabled = true; + comboBoxIceCream.Location = new Point(101, 15); + comboBoxIceCream.Margin = new Padding(4, 3, 4, 3); + comboBoxIceCream.Name = "comboBoxIceCream"; + comboBoxIceCream.Size = new Size(252, 23); + comboBoxIceCream.TabIndex = 13; + // + // labelIceCream + // + labelIceCream.AutoSize = true; + labelIceCream.Location = new Point(13, 19); + labelIceCream.Margin = new Padding(4, 0, 4, 0); + labelIceCream.Name = "labelIceCream"; + labelIceCream.Size = new Size(80, 15); + labelIceCream.TabIndex = 12; + labelIceCream.Text = "Мороженое :"; + // + // FormIceCreamSale + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(373, 123); + Controls.Add(buttonCancel); + Controls.Add(buttonSale); + Controls.Add(textBoxCount); + Controls.Add(labelCount); + Controls.Add(comboBoxIceCream); + Controls.Add(labelIceCream); + Name = "FormIceCreamSale"; + StartPosition = FormStartPosition.CenterScreen; + Text = "Продажа мороженого"; + Load += FormIceCreamSale_Load; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private Button buttonCancel; + private Button buttonSale; + private TextBox textBoxCount; + private Label labelCount; + private ComboBox comboBoxIceCream; + private Label labelIceCream; + } +} \ No newline at end of file diff --git a/IceCreamShop/IceCreamShopView/FormIceCreamSale.cs b/IceCreamShop/IceCreamShopView/FormIceCreamSale.cs new file mode 100644 index 0000000..da861e9 --- /dev/null +++ b/IceCreamShop/IceCreamShopView/FormIceCreamSale.cs @@ -0,0 +1,87 @@ +using IceCreamShopContracts.BusinessLogicsContracts; +using IceCreamShopContracts.BindingModels; +using Microsoft.Extensions.Logging; + +namespace IceCreamShopView +{ + public partial class FormIceCreamSale : Form + { + private readonly ILogger _logger; + + private readonly IIceCreamLogic _logicIceCream; + + private readonly IShopLogic _logicShop; + + public FormIceCreamSale(ILogger logger, IIceCreamLogic logicIceCream, IShopLogic logicShop) + { + InitializeComponent(); + _logger = logger; + _logicIceCream = logicIceCream; + _logicShop = logicShop; + } + + private void FormIceCreamSale_Load(object sender, EventArgs e) + { + _logger.LogInformation("Ice creams loading"); + try + { + var list = _logicIceCream.ReadList(null); + if (list != null) + { + comboBoxIceCream.DisplayMember = "IceCreamName"; + comboBoxIceCream.ValueMember = "Id"; + comboBoxIceCream.DataSource = list; + comboBoxIceCream.SelectedItem = null; + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ice creams loading error"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void ButtonSale_Click(object sender, EventArgs e) + { + if (comboBoxIceCream.SelectedValue == null) + { + MessageBox.Show("Выберите мороженое", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (string.IsNullOrEmpty(textBoxCount.Text)) + { + MessageBox.Show("Заполните поле Количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Ice cream sale"); + try + { + var operationResult = _logicShop.MakeSale( + new IceCreamBindingModel + { + Id = Convert.ToInt32(comboBoxIceCream.SelectedValue) + }, + 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, "Ice cream sale error"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void ButtonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + } +} diff --git a/IceCreamShop/IceCreamShopView/FormIceCreamSale.resx b/IceCreamShop/IceCreamShopView/FormIceCreamSale.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/IceCreamShop/IceCreamShopView/FormIceCreamSale.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/IceCreamShop/IceCreamShopView/FormMain.cs b/IceCreamShop/IceCreamShopView/FormMain.cs index b72c9f7..8d1aefb 100644 --- a/IceCreamShop/IceCreamShopView/FormMain.cs +++ b/IceCreamShop/IceCreamShopView/FormMain.cs @@ -78,6 +78,15 @@ namespace IceCreamShopView } } + private void продажаМороженогоToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormIceCreamSale)); + if (service is FormIceCreamSale form) + { + form.ShowDialog(); + } + } + private void ButtonCreateOrder_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormCreateOrder)); diff --git a/IceCreamShop/IceCreamShopView/FormMain.designer.cs b/IceCreamShop/IceCreamShopView/FormMain.designer.cs index 93afb5c..d4fb313 100644 --- a/IceCreamShop/IceCreamShopView/FormMain.designer.cs +++ b/IceCreamShop/IceCreamShopView/FormMain.designer.cs @@ -40,13 +40,14 @@ buttonCreateOrder = new Button(); dataGridView = new DataGridView(); buttonUpd = new Button(); + продажаМороженогоToolStripMenuItem = new ToolStripMenuItem(); menuStrip.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); SuspendLayout(); // // menuStrip // - menuStrip.Items.AddRange(new ToolStripItem[] { справочникиToolStripMenuItem, пополнениеМагазинаToolStripMenuItem }); + menuStrip.Items.AddRange(new ToolStripItem[] { справочникиToolStripMenuItem, пополнениеМагазинаToolStripMenuItem, продажаМороженогоToolStripMenuItem }); menuStrip.Location = new Point(0, 0); menuStrip.Name = "menuStrip"; menuStrip.Padding = new Padding(7, 2, 0, 2); @@ -64,21 +65,21 @@ // компонентыToolStripMenuItem // компонентыToolStripMenuItem.Name = "компонентыToolStripMenuItem"; - компонентыToolStripMenuItem.Size = new Size(180, 22); + компонентыToolStripMenuItem.Size = new Size(145, 22); компонентыToolStripMenuItem.Text = "Компоненты"; компонентыToolStripMenuItem.Click += КомпонентыToolStripMenuItem_Click; // // мороженоеToolStripMenuItem // мороженоеToolStripMenuItem.Name = "мороженоеToolStripMenuItem"; - мороженоеToolStripMenuItem.Size = new Size(180, 22); + мороженоеToolStripMenuItem.Size = new Size(145, 22); мороженоеToolStripMenuItem.Text = "Мороженое"; мороженоеToolStripMenuItem.Click += МороженоеToolStripMenuItem_Click; // // магазиныToolStripMenuItem // магазиныToolStripMenuItem.Name = "магазиныToolStripMenuItem"; - магазиныToolStripMenuItem.Size = new Size(180, 22); + магазиныToolStripMenuItem.Size = new Size(145, 22); магазиныToolStripMenuItem.Text = "Магазины"; магазиныToolStripMenuItem.Click += МагазиныToolStripMenuItem_Click; // @@ -166,6 +167,13 @@ buttonUpd.UseVisualStyleBackColor = true; buttonUpd.Click += ButtonUpd_Click; // + // продажаМороженогоToolStripMenuItem + // + продажаМороженогоToolStripMenuItem.Name = "продажаМороженогоToolStripMenuItem"; + продажаМороженогоToolStripMenuItem.Size = new Size(143, 20); + продажаМороженогоToolStripMenuItem.Text = "Продажа мороженого"; + продажаМороженогоToolStripMenuItem.Click += продажаМороженогоToolStripMenuItem_Click; + // // FormMain // AutoScaleDimensions = new SizeF(7F, 15F); @@ -205,6 +213,7 @@ private System.Windows.Forms.Button buttonUpd; private ToolStripMenuItem магазиныToolStripMenuItem; private ToolStripMenuItem пополнениеМагазинаToolStripMenuItem; + private ToolStripMenuItem продажаМороженогоToolStripMenuItem; } } diff --git a/IceCreamShop/IceCreamShopView/FormShop.Designer.cs b/IceCreamShop/IceCreamShopView/FormShop.Designer.cs index 9fca672..26480fb 100644 --- a/IceCreamShop/IceCreamShopView/FormShop.Designer.cs +++ b/IceCreamShop/IceCreamShopView/FormShop.Designer.cs @@ -41,6 +41,8 @@ ColumnCount = new DataGridViewTextBoxColumn(); buttonSave = new Button(); buttonCancel = new Button(); + textBoxMaximum = new TextBox(); + labelMaximum = new Label(); groupBoxIceCreams.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); SuspendLayout(); @@ -101,7 +103,7 @@ // groupBoxIceCreams // groupBoxIceCreams.Controls.Add(dataGridView); - groupBoxIceCreams.Location = new Point(4, 100); + groupBoxIceCreams.Location = new Point(5, 138); groupBoxIceCreams.Margin = new Padding(4, 3, 4, 3); groupBoxIceCreams.Name = "groupBoxIceCreams"; groupBoxIceCreams.Padding = new Padding(4, 3, 4, 3); @@ -150,7 +152,7 @@ // // buttonSave // - buttonSave.Location = new Point(255, 394); + buttonSave.Location = new Point(256, 434); buttonSave.Margin = new Padding(4, 3, 4, 3); buttonSave.Name = "buttonSave"; buttonSave.Size = new Size(88, 27); @@ -161,7 +163,7 @@ // // buttonCancel // - buttonCancel.Location = new Point(359, 394); + buttonCancel.Location = new Point(360, 434); buttonCancel.Margin = new Padding(4, 3, 4, 3); buttonCancel.Name = "buttonCancel"; buttonCancel.Size = new Size(88, 27); @@ -170,11 +172,31 @@ buttonCancel.UseVisualStyleBackColor = true; buttonCancel.Click += ButtonCancel_Click; // + // textBoxMaximum + // + textBoxMaximum.Location = new Point(169, 97); + textBoxMaximum.Margin = new Padding(4, 3, 4, 3); + textBoxMaximum.Name = "textBoxMaximum"; + textBoxMaximum.Size = new Size(175, 23); + textBoxMaximum.TabIndex = 11; + // + // labelMaximum + // + labelMaximum.AutoSize = true; + labelMaximum.Location = new Point(14, 100); + labelMaximum.Margin = new Padding(4, 0, 4, 0); + labelMaximum.Name = "labelMaximum"; + labelMaximum.Size = new Size(147, 15); + labelMaximum.TabIndex = 10; + labelMaximum.Text = "Максимум мороженого :"; + // // FormShop // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(478, 432); + ClientSize = new Size(478, 468); + Controls.Add(textBoxMaximum); + Controls.Add(labelMaximum); Controls.Add(buttonCancel); Controls.Add(buttonSave); Controls.Add(groupBoxIceCreams); @@ -209,5 +231,7 @@ private DataGridViewTextBoxColumn ColumnCount; private Button buttonSave; private Button buttonCancel; + private TextBox textBoxMaximum; + private Label labelMaximum; } } \ No newline at end of file diff --git a/IceCreamShop/IceCreamShopView/FormShop.cs b/IceCreamShop/IceCreamShopView/FormShop.cs index 44bdbac..37eb53c 100644 --- a/IceCreamShop/IceCreamShopView/FormShop.cs +++ b/IceCreamShop/IceCreamShopView/FormShop.cs @@ -39,6 +39,7 @@ namespace IceCreamShopView textBoxName.Text = view.ShopName; textBoxAddress.Text = view.Address; dateTimePicker.Value = view.DateOpening; + textBoxMaximum.Text = view.IceCreamsMaximum.ToString(); _shopIceCreams = view.ShopIceCreams ?? new Dictionary(); LoadData(); } @@ -89,6 +90,11 @@ namespace IceCreamShopView MessageBox.Show("Заполните дату", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } + if (string.IsNullOrEmpty(textBoxMaximum.Text)) + { + MessageBox.Show("Заполните максимальное количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } _logger.LogInformation("Shop saving"); try { @@ -98,6 +104,7 @@ namespace IceCreamShopView ShopName = textBoxName.Text, Address = textBoxAddress.Text, DateOpening = dateTimePicker.Value, + IceCreamsMaximum = Convert.ToInt32(textBoxMaximum.Text), ShopIceCreams = _shopIceCreams }; var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model); diff --git a/IceCreamShop/IceCreamShopView/Program.cs b/IceCreamShop/IceCreamShopView/Program.cs index 1aabd01..b5b86d8 100644 --- a/IceCreamShop/IceCreamShopView/Program.cs +++ b/IceCreamShop/IceCreamShopView/Program.cs @@ -55,6 +55,7 @@ namespace IceCreamShopView services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file -- 2.25.1 From 7c11d70f60a14c8bdb342b77976fa9a901ea80f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=BB=D1=8C=D1=8F?= <Илья@WIN-RANNDDD> Date: Sun, 10 Mar 2024 16:23:06 +0400 Subject: [PATCH 06/11] =?UTF-8?q?=D0=93=D0=BE=D1=82=D0=BE=D0=B2=D0=B0?= =?UTF-8?q?=D1=8F=203=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=BD=D0=B0=D1=8F=20(=D0=B1=D0=B0=D0=B7=D0=BE=D0=B2?= =?UTF-8?q?=D0=B0=D1=8F)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- IceCreamShop/IceCreamShop.sln | 8 +- .../IceCreamShopDatabase.cs | 25 +++ .../IceCreamShopDatabaseImplement.csproj | 23 +++ .../Implements/ComponentStorage.cs | 84 +++++++++ .../Implements/IceCreamStorage.cs | 106 +++++++++++ .../Implements/OrderStorage.cs | 97 ++++++++++ .../20240309192813_InitialCreate.Designer.cs | 171 ++++++++++++++++++ .../20240309192813_InitialCreate.cs | 125 +++++++++++++ .../IceCreamShopDatabaseModelSnapshot.cs | 168 +++++++++++++++++ .../Models/Component.cs | 63 +++++++ .../Models/IceCream.cs | 99 ++++++++++ .../Models/IceCreamComponent.cs | 22 +++ .../Models/Order.cs | 72 ++++++++ .../IceCreamShopView/IceCreamShopView.csproj | 5 + IceCreamShop/IceCreamShopView/Program.cs | 2 +- 15 files changed, 1068 insertions(+), 2 deletions(-) create mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDatabase.cs create mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDatabaseImplement.csproj create mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Implements/ComponentStorage.cs create mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Implements/IceCreamStorage.cs create mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Implements/OrderStorage.cs create mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240309192813_InitialCreate.Designer.cs create mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240309192813_InitialCreate.cs create mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Migrations/IceCreamShopDatabaseModelSnapshot.cs create mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Models/Component.cs create mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Models/IceCream.cs create mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Models/IceCreamComponent.cs create mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Models/Order.cs diff --git a/IceCreamShop/IceCreamShop.sln b/IceCreamShop/IceCreamShop.sln index 5a64334..84842a0 100644 --- a/IceCreamShop/IceCreamShop.sln +++ b/IceCreamShop/IceCreamShop.sln @@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IceCreamShopListImplement", EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IceCreamShopBusinessLogic", "IceCreamShopBusinessLogic\IceCreamShopBusinessLogic.csproj", "{392488A9-9B1E-4DDA-95CE-C2FDC954DEE6}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IceCreamShopFileImplement", "IceCreamShopFileImplement\IceCreamShopFileImplement.csproj", "{5CA64E22-1AB5-4D9C-9C1E-8E336857456B}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IceCreamShopFileImplement", "IceCreamShopFileImplement\IceCreamShopFileImplement.csproj", "{5CA64E22-1AB5-4D9C-9C1E-8E336857456B}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IceCreamShopDatabaseImplement", "IceCreamShopDatabaseImplement\IceCreamShopDatabaseImplement.csproj", "{095BA089-0060-4A1E-A658-9E9061A6BB39}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {5CA64E22-1AB5-4D9C-9C1E-8E336857456B}.Debug|Any CPU.Build.0 = Debug|Any CPU {5CA64E22-1AB5-4D9C-9C1E-8E336857456B}.Release|Any CPU.ActiveCfg = Release|Any CPU {5CA64E22-1AB5-4D9C-9C1E-8E336857456B}.Release|Any CPU.Build.0 = Release|Any CPU + {095BA089-0060-4A1E-A658-9E9061A6BB39}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {095BA089-0060-4A1E-A658-9E9061A6BB39}.Debug|Any CPU.Build.0 = Debug|Any CPU + {095BA089-0060-4A1E-A658-9E9061A6BB39}.Release|Any CPU.ActiveCfg = Release|Any CPU + {095BA089-0060-4A1E-A658-9E9061A6BB39}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDatabase.cs b/IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDatabase.cs new file mode 100644 index 0000000..8aa8ad9 --- /dev/null +++ b/IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDatabase.cs @@ -0,0 +1,25 @@ +using IceCreamShopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace IceCreamShopDatabaseImplement +{ + public class IceCreamShopDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=.\SQLEXPRESS;Initial Catalog=IceCreamShopDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + + public virtual DbSet Components { set; get; } + + public virtual DbSet IceCreams { set; get; } + + public virtual DbSet IceCreamComponents { set; get; } + + public virtual DbSet Orders { set; get; } + } +} \ No newline at end of file diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDatabaseImplement.csproj b/IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDatabaseImplement.csproj new file mode 100644 index 0000000..8643ac1 --- /dev/null +++ b/IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDatabaseImplement.csproj @@ -0,0 +1,23 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Implements/ComponentStorage.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..3f4c63e --- /dev/null +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,84 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.StoragesContracts; +using IceCreamShopContracts.ViewModels; +using IceCreamShopDatabaseImplement.Models; + +namespace IceCreamShopDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new IceCreamShopDatabase(); + return context.Components + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new IceCreamShopDatabase(); + return context.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; + } + using var context = new IceCreamShopDatabase(); + return context.Components + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public ComponentViewModel? Insert(ComponentBindingModel model) + { + var newComponent = Component.Create(model); + if (newComponent == null) + { + return null; + } + using var context = new IceCreamShopDatabase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new IceCreamShopDatabase(); + var component = context.Components.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } + + public ComponentViewModel? Delete(ComponentBindingModel model) + { + using var context = new IceCreamShopDatabase(); + var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} \ No newline at end of file diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Implements/IceCreamStorage.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Implements/IceCreamStorage.cs new file mode 100644 index 0000000..1a0a5e0 --- /dev/null +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Implements/IceCreamStorage.cs @@ -0,0 +1,106 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.StoragesContracts; +using IceCreamShopContracts.ViewModels; +using IceCreamShopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace IceCreamShopDatabaseImplement.Implements +{ + public class IceCreamStorage : IIceCreamStorage + { + public List GetFullList() + { + using var context = new IceCreamShopDatabase(); + return context.IceCreams + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(IceCreamSearchModel model) + { + if (string.IsNullOrEmpty(model.IceCreamName)) + { + return new(); + } + using var context = new IceCreamShopDatabase(); + return context.IceCreams + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .Where(x => x.IceCreamName.Contains(model.IceCreamName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public IceCreamViewModel? GetElement(IceCreamSearchModel model) + { + if (string.IsNullOrEmpty(model.IceCreamName) && !model.Id.HasValue) + { + return null; + } + using var context = new IceCreamShopDatabase(); + return context.IceCreams + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.IceCreamName) && x.IceCreamName == model.IceCreamName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public IceCreamViewModel? Insert(IceCreamBindingModel model) + { + using var context = new IceCreamShopDatabase(); + var newIceCream = IceCream.Create(context, model); + if (newIceCream == null) + { + return null; + } + context.IceCreams.Add(newIceCream); + context.SaveChanges(); + return newIceCream.GetViewModel; + } + + public IceCreamViewModel? Update(IceCreamBindingModel model) + { + using var context = new IceCreamShopDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var iceCream = context.IceCreams.FirstOrDefault(rec => rec.Id == model.Id); + if (iceCream == null) + { + return null; + } + iceCream.Update(model); + context.SaveChanges(); + iceCream.UpdateComponents(context, model); + transaction.Commit(); + return iceCream.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public IceCreamViewModel? Delete(IceCreamBindingModel model) + { + using var context = new IceCreamShopDatabase(); + var element = context.IceCreams + .Include(x => x.Components) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.IceCreams.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} \ No newline at end of file diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Implements/OrderStorage.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..c701c00 --- /dev/null +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,97 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.StoragesContracts; +using IceCreamShopContracts.ViewModels; +using IceCreamShopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace IceCreamShopDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public List GetFullList() + { + using var context = new IceCreamShopDatabase(); + return context.Orders + .Include(x => x.IceCream) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new IceCreamShopDatabase(); + return context.Orders + .Include(x => x.IceCream) + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new IceCreamShopDatabase(); + return context.Orders + .Include(x => x.IceCream) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + using var context = new IceCreamShopDatabase(); + context.Orders.Add(newOrder); + context.SaveChanges(); + return context.Orders + .Include(x => x.IceCream) + .FirstOrDefault(x => x.Id == newOrder.Id) + ?.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new IceCreamShopDatabase(); + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + return context.Orders + .Include(x => x.IceCream) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + } + + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new IceCreamShopDatabase(); + var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + var deletedElement = context.Orders + .Include(x => x.IceCream) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + context.Orders.Remove(element); + context.SaveChanges(); + return deletedElement; + } + return null; + } + } +} \ No newline at end of file diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240309192813_InitialCreate.Designer.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240309192813_InitialCreate.Designer.cs new file mode 100644 index 0000000..adc6c10 --- /dev/null +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240309192813_InitialCreate.Designer.cs @@ -0,0 +1,171 @@ +// +using System; +using IceCreamShopDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace IceCreamShopDatabaseImplement.Migrations +{ + [DbContext(typeof(IceCreamShopDatabase))] + [Migration("20240309192813_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.16") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCream", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("IceCreamName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("IceCreams"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCreamComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("IceCreamId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("IceCreamId"); + + b.ToTable("IceCreamComponents"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("IceCreamId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("IceCreamId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCreamComponent", b => + { + b.HasOne("IceCreamShopDatabaseImplement.Models.Component", "Component") + .WithMany("IceCreamComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", "IceCream") + .WithMany("Components") + .HasForeignKey("IceCreamId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("IceCream"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Order", b => + { + b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", "IceCream") + .WithMany("Orders") + .HasForeignKey("IceCreamId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("IceCream"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Component", b => + { + b.Navigation("IceCreamComponents"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCream", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240309192813_InitialCreate.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240309192813_InitialCreate.cs new file mode 100644 index 0000000..562bd62 --- /dev/null +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240309192813_InitialCreate.cs @@ -0,0 +1,125 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace IceCreamShopDatabaseImplement.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Components", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ComponentName = table.Column(type: "nvarchar(max)", nullable: false), + Cost = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Components", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "IceCreams", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + IceCreamName = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_IceCreams", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "IceCreamComponents", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + IceCreamId = table.Column(type: "int", nullable: false), + ComponentId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_IceCreamComponents", x => x.Id); + table.ForeignKey( + name: "FK_IceCreamComponents_Components_ComponentId", + column: x => x.ComponentId, + principalTable: "Components", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_IceCreamComponents_IceCreams_IceCreamId", + column: x => x.IceCreamId, + principalTable: "IceCreams", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + IceCreamId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false), + Sum = table.Column(type: "float", nullable: false), + Status = table.Column(type: "int", nullable: false), + DateCreate = table.Column(type: "datetime2", nullable: false), + DateImplement = table.Column(type: "datetime2", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Orders", x => x.Id); + table.ForeignKey( + name: "FK_Orders_IceCreams_IceCreamId", + column: x => x.IceCreamId, + principalTable: "IceCreams", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_IceCreamComponents_ComponentId", + table: "IceCreamComponents", + column: "ComponentId"); + + migrationBuilder.CreateIndex( + name: "IX_IceCreamComponents_IceCreamId", + table: "IceCreamComponents", + column: "IceCreamId"); + + migrationBuilder.CreateIndex( + name: "IX_Orders_IceCreamId", + table: "Orders", + column: "IceCreamId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "IceCreamComponents"); + + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "Components"); + + migrationBuilder.DropTable( + name: "IceCreams"); + } + } +} diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/IceCreamShopDatabaseModelSnapshot.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/IceCreamShopDatabaseModelSnapshot.cs new file mode 100644 index 0000000..3563fed --- /dev/null +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/IceCreamShopDatabaseModelSnapshot.cs @@ -0,0 +1,168 @@ +// +using System; +using IceCreamShopDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace IceCreamShopDatabaseImplement.Migrations +{ + [DbContext(typeof(IceCreamShopDatabase))] + partial class IceCreamShopDatabaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.16") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCream", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("IceCreamName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("IceCreams"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCreamComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("IceCreamId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("IceCreamId"); + + b.ToTable("IceCreamComponents"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("IceCreamId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("IceCreamId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCreamComponent", b => + { + b.HasOne("IceCreamShopDatabaseImplement.Models.Component", "Component") + .WithMany("IceCreamComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", "IceCream") + .WithMany("Components") + .HasForeignKey("IceCreamId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("IceCream"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Order", b => + { + b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", "IceCream") + .WithMany("Orders") + .HasForeignKey("IceCreamId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("IceCream"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Component", b => + { + b.Navigation("IceCreamComponents"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCream", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Models/Component.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..c295a25 --- /dev/null +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Models/Component.cs @@ -0,0 +1,63 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.ViewModels; +using IceCreamShopDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace IceCreamShopDatabaseImplement.Models +{ + public class Component : IComponentModel + { + public int Id { get; private set; } + + [Required] + public string ComponentName { get; private set; } = string.Empty; + + [Required] + public double Cost { get; set; } + + [ForeignKey("ComponentId")] + public virtual List IceCreamComponents { get; set; } = new(); + + 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(ComponentViewModel model) + { + return new Component + { + Id = model.Id, + ComponentName = model.ComponentName, + Cost = model.Cost + }; + } + + 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 + }; + } +} \ No newline at end of file diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Models/IceCream.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Models/IceCream.cs new file mode 100644 index 0000000..4105f97 --- /dev/null +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Models/IceCream.cs @@ -0,0 +1,99 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.ViewModels; +using IceCreamShopDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace IceCreamShopDatabaseImplement.Models +{ + public class IceCream : IIceCreamModel + { + public int Id { get; set; } + + [Required] + public string IceCreamName { get; set; } = string.Empty; + + [Required] + public double Price { get; set; } + + private Dictionary? _iceCreamComponents = null; + + [NotMapped] + public Dictionary IceCreamComponents + { + get + { + if (_iceCreamComponents == null) + { + _iceCreamComponents = Components + .ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count)); + } + return _iceCreamComponents; + } + } + + [ForeignKey("IceCreamId")] + public virtual List Components { get; set; } = new(); + + [ForeignKey("IceCreamId")] + public virtual List Orders { get; set; } = new(); + + public static IceCream Create(IceCreamShopDatabase context, IceCreamBindingModel model) + { + return new IceCream() + { + Id = model.Id, + IceCreamName = model.IceCreamName, + Price = model.Price, + Components = model.IceCreamComponents.Select(x => new IceCreamComponent + { + Component = context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + + public void Update(IceCreamBindingModel model) + { + IceCreamName = model.IceCreamName; + Price = model.Price; + } + + public IceCreamViewModel GetViewModel => new() + { + Id = Id, + IceCreamName = IceCreamName, + Price = Price, + IceCreamComponents = IceCreamComponents + }; + + public void UpdateComponents(IceCreamShopDatabase context, IceCreamBindingModel model) + { + var iceCreamComponents = context.IceCreamComponents.Where(rec => rec.IceCreamId == model.Id).ToList(); + if (iceCreamComponents != null && iceCreamComponents.Count > 0) + { // удалили те, которых нет в модели + context.IceCreamComponents.RemoveRange(iceCreamComponents.Where(rec => !model.IceCreamComponents.ContainsKey(rec.ComponentId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateComponent in iceCreamComponents) + { + updateComponent.Count = model.IceCreamComponents[updateComponent.ComponentId].Item2; + model.IceCreamComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + var iceCream = context.IceCreams.First(x => x.Id == Id); + foreach (var ic in model.IceCreamComponents) + { + context.IceCreamComponents.Add(new IceCreamComponent + { + IceCream = iceCream, + Component = context.Components.First(x => x.Id == ic.Key), + Count = ic.Value.Item2 + }); + context.SaveChanges(); + } + _iceCreamComponents = null; + } + } +} \ No newline at end of file diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Models/IceCreamComponent.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Models/IceCreamComponent.cs new file mode 100644 index 0000000..50ba298 --- /dev/null +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Models/IceCreamComponent.cs @@ -0,0 +1,22 @@ +using System.ComponentModel.DataAnnotations; + +namespace IceCreamShopDatabaseImplement.Models +{ + public class IceCreamComponent + { + public int Id { get; set; } + + [Required] + public int IceCreamId { get; set; } + + [Required] + public int ComponentId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Component Component { get; set; } = new(); + + public virtual IceCream IceCream { get; set; } = new(); + } +} \ No newline at end of file diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Models/Order.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..76edee3 --- /dev/null +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Models/Order.cs @@ -0,0 +1,72 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.ViewModels; +using IceCreamShopDataModels.Enums; +using IceCreamShopDataModels.Models; +using System.ComponentModel.DataAnnotations; + +namespace IceCreamShopDatabaseImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; set; } + + [Required] + public int IceCreamId { get; set; } + + [Required] + public int Count { get; set; } + + [Required] + public double Sum { get; set; } + + [Required] + public OrderStatus Status { get; set; } + + [Required] + public DateTime DateCreate { get; set; } + + public DateTime? DateImplement { get; set; } + + public virtual IceCream IceCream { get; set; } + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + IceCreamId = model.IceCreamId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement + }; + } + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + Id = Id, + IceCreamId = IceCreamId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + IceCreamName = IceCream.IceCreamName + }; + } +} \ No newline at end of file diff --git a/IceCreamShop/IceCreamShopView/IceCreamShopView.csproj b/IceCreamShop/IceCreamShopView/IceCreamShopView.csproj index 1456ac2..a816493 100644 --- a/IceCreamShop/IceCreamShopView/IceCreamShopView.csproj +++ b/IceCreamShop/IceCreamShopView/IceCreamShopView.csproj @@ -19,6 +19,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -27,6 +31,7 @@ + diff --git a/IceCreamShop/IceCreamShopView/Program.cs b/IceCreamShop/IceCreamShopView/Program.cs index bb53b29..831bbe4 100644 --- a/IceCreamShop/IceCreamShopView/Program.cs +++ b/IceCreamShop/IceCreamShopView/Program.cs @@ -1,7 +1,7 @@ using IceCreamShopBusinessLogic.BusinessLogics; using IceCreamShopContracts.BusinessLogicsContracts; using IceCreamShopContracts.StoragesContracts; -using IceCreamShopFileImplement.Implements; +using IceCreamShopDatabaseImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; -- 2.25.1 From 69123995cdf49f863a3d02e6ffb4842cdb28e8a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=BB=D1=8C=D1=8F?= <Илья@WIN-RANNDDD> Date: Sun, 10 Mar 2024 16:54:32 +0400 Subject: [PATCH 07/11] =?UTF-8?q?=D0=9C=D0=B5=D0=BB=D0=BA=D0=B8=D0=B5=20?= =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../IceCreamShopBusinessLogic/BusinessLogics/OrderLogic.cs | 2 +- .../IceCreamShopFileImplement/Implements/ShopStorage.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogics/OrderLogic.cs b/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogics/OrderLogic.cs index 41b8928..7dd184b 100644 --- a/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogics/OrderLogic.cs @@ -158,7 +158,7 @@ namespace IceCreamShopBusinessLogic.BusinessLogics var shopList = _shopStorage.GetFullList(); int shopsCapacity = shopList.Sum(x => x.IceCreamsMaximum); - int currentIceCreams = shopList.Select(x => x.ShopIceCreams.Select(y => y.Value.Item2).Sum()).Sum(); + int currentIceCreams = shopList.Select(x => x.ShopIceCreams.Sum(y => y.Value.Item2)).Sum(); int freePlaces = shopsCapacity - currentIceCreams; if (freePlaces < count) diff --git a/IceCreamShop/IceCreamShopFileImplement/Implements/ShopStorage.cs b/IceCreamShop/IceCreamShopFileImplement/Implements/ShopStorage.cs index 578f972..f4f565c 100644 --- a/IceCreamShop/IceCreamShopFileImplement/Implements/ShopStorage.cs +++ b/IceCreamShop/IceCreamShopFileImplement/Implements/ShopStorage.cs @@ -113,7 +113,7 @@ namespace IceCreamShopFileImplement.Implements } shop.Update(new ShopBindingModel { - Id = model.Id, + Id = shop.Id, ShopName = shop.ShopName, Address = shop.Address, DateOpening = shop.DateOpening, -- 2.25.1 From d7ecf984f1218527a0fcaf8c5fea2e3a14c3d346 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=BB=D1=8C=D1=8F?= <Илья@WIN-RANNDDD> Date: Mon, 11 Mar 2024 23:00:37 +0400 Subject: [PATCH 08/11] =?UTF-8?q?=D0=A3=D0=BF=D1=80=D0=BE=D1=89=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA?= =?UTF-8?q?=D0=B8=20=D0=BF=D1=80=D0=B8=20=D0=BF=D1=80=D0=BE=D0=B4=D0=B0?= =?UTF-8?q?=D0=B6=D0=B5=20=D0=B8=D0=B7=D0=B4=D0=B5=D0=BB=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../IceCreamShopFileImplement/Implements/ShopStorage.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/IceCreamShop/IceCreamShopFileImplement/Implements/ShopStorage.cs b/IceCreamShop/IceCreamShopFileImplement/Implements/ShopStorage.cs index f4f565c..a79aadb 100644 --- a/IceCreamShop/IceCreamShopFileImplement/Implements/ShopStorage.cs +++ b/IceCreamShop/IceCreamShopFileImplement/Implements/ShopStorage.cs @@ -88,9 +88,8 @@ namespace IceCreamShopFileImplement.Implements public bool MakeSale(IIceCreamModel model, int count) { var iceCream = source.IceCreams.FirstOrDefault(x => x.Id == model.Id); - int countInShops = source.Shops.Select(x => x.ShopIceCreams.Any(y => y.Key == model.Id) - ? x.ShopIceCreams.Where(y => y.Key == model.Id).First().Value.Item2 : 0).Sum(); - + int countInShops = source.Shops.SelectMany(x => x.ShopIceCreams).Sum(y => y.Key == model.Id ? y.Value.Item2 : 0); + if (iceCream == null || countInShops < count) { return false; -- 2.25.1 From 4e70f15021ed9bed1915012b8518f2251feaec67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=BB=D1=8C=D1=8F?= <Илья@WIN-RANNDDD> Date: Mon, 11 Mar 2024 23:14:15 +0400 Subject: [PATCH 09/11] =?UTF-8?q?=D0=A4=D0=B8=D0=BA=D1=81=20=D0=BC=D0=BE?= =?UTF-8?q?=D0=B4=D0=B5=D0=BB=D0=B8=20=D0=BC=D0=B0=D0=B3=D0=B0=D0=B7=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- IceCreamShop/IceCreamShopListImplement/Models/Shop.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/IceCreamShop/IceCreamShopListImplement/Models/Shop.cs b/IceCreamShop/IceCreamShopListImplement/Models/Shop.cs index cca24fc..d85b820 100644 --- a/IceCreamShop/IceCreamShopListImplement/Models/Shop.cs +++ b/IceCreamShop/IceCreamShopListImplement/Models/Shop.cs @@ -49,7 +49,7 @@ namespace IceCreamShopListImplement.Models Address = model.Address; DateOpening = model.DateOpening; ShopIceCreams = model.ShopIceCreams; - IceCreamsMaximum -= model.IceCreamsMaximum; + IceCreamsMaximum = model.IceCreamsMaximum; } public ShopViewModel GetViewModel => new() -- 2.25.1 From 85d2bd617a78878ccc6fa325aead081b16e68ccf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=BB=D1=8C=D1=8F?= <Илья@WIN-RANNDDD> Date: Sun, 7 Apr 2024 20:28:28 +0400 Subject: [PATCH 10/11] =?UTF-8?q?Revert=20"=D0=93=D0=BE=D1=82=D0=BE=D0=B2?= =?UTF-8?q?=D0=B0=D1=8F=203=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20(=D0=B1=D0=B0=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D1=8F)"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 7c11d70f60a14c8bdb342b77976fa9a901ea80f8. --- IceCreamShop/IceCreamShop.sln | 8 +- .../IceCreamShopDatabase.cs | 25 --- .../IceCreamShopDatabaseImplement.csproj | 23 --- .../Implements/ComponentStorage.cs | 84 --------- .../Implements/IceCreamStorage.cs | 106 ----------- .../Implements/OrderStorage.cs | 97 ---------- .../20240309192813_InitialCreate.Designer.cs | 171 ------------------ .../20240309192813_InitialCreate.cs | 125 ------------- .../IceCreamShopDatabaseModelSnapshot.cs | 168 ----------------- .../Models/Component.cs | 63 ------- .../Models/IceCream.cs | 99 ---------- .../Models/IceCreamComponent.cs | 22 --- .../Models/Order.cs | 72 -------- .../IceCreamShopView/IceCreamShopView.csproj | 5 - IceCreamShop/IceCreamShopView/Program.cs | 2 +- 15 files changed, 2 insertions(+), 1068 deletions(-) delete mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDatabase.cs delete mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDatabaseImplement.csproj delete mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Implements/ComponentStorage.cs delete mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Implements/IceCreamStorage.cs delete mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Implements/OrderStorage.cs delete mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240309192813_InitialCreate.Designer.cs delete mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240309192813_InitialCreate.cs delete mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Migrations/IceCreamShopDatabaseModelSnapshot.cs delete mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Models/Component.cs delete mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Models/IceCream.cs delete mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Models/IceCreamComponent.cs delete mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Models/Order.cs diff --git a/IceCreamShop/IceCreamShop.sln b/IceCreamShop/IceCreamShop.sln index 84842a0..5a64334 100644 --- a/IceCreamShop/IceCreamShop.sln +++ b/IceCreamShop/IceCreamShop.sln @@ -13,9 +13,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IceCreamShopListImplement", EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IceCreamShopBusinessLogic", "IceCreamShopBusinessLogic\IceCreamShopBusinessLogic.csproj", "{392488A9-9B1E-4DDA-95CE-C2FDC954DEE6}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IceCreamShopFileImplement", "IceCreamShopFileImplement\IceCreamShopFileImplement.csproj", "{5CA64E22-1AB5-4D9C-9C1E-8E336857456B}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IceCreamShopDatabaseImplement", "IceCreamShopDatabaseImplement\IceCreamShopDatabaseImplement.csproj", "{095BA089-0060-4A1E-A658-9E9061A6BB39}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IceCreamShopFileImplement", "IceCreamShopFileImplement\IceCreamShopFileImplement.csproj", "{5CA64E22-1AB5-4D9C-9C1E-8E336857456B}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -47,10 +45,6 @@ Global {5CA64E22-1AB5-4D9C-9C1E-8E336857456B}.Debug|Any CPU.Build.0 = Debug|Any CPU {5CA64E22-1AB5-4D9C-9C1E-8E336857456B}.Release|Any CPU.ActiveCfg = Release|Any CPU {5CA64E22-1AB5-4D9C-9C1E-8E336857456B}.Release|Any CPU.Build.0 = Release|Any CPU - {095BA089-0060-4A1E-A658-9E9061A6BB39}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {095BA089-0060-4A1E-A658-9E9061A6BB39}.Debug|Any CPU.Build.0 = Debug|Any CPU - {095BA089-0060-4A1E-A658-9E9061A6BB39}.Release|Any CPU.ActiveCfg = Release|Any CPU - {095BA089-0060-4A1E-A658-9E9061A6BB39}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDatabase.cs b/IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDatabase.cs deleted file mode 100644 index 8aa8ad9..0000000 --- a/IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDatabase.cs +++ /dev/null @@ -1,25 +0,0 @@ -using IceCreamShopDatabaseImplement.Models; -using Microsoft.EntityFrameworkCore; - -namespace IceCreamShopDatabaseImplement -{ - public class IceCreamShopDatabase : DbContext - { - protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) - { - if (optionsBuilder.IsConfigured == false) - { - optionsBuilder.UseSqlServer(@"Data Source=.\SQLEXPRESS;Initial Catalog=IceCreamShopDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); - } - base.OnConfiguring(optionsBuilder); - } - - public virtual DbSet Components { set; get; } - - public virtual DbSet IceCreams { set; get; } - - public virtual DbSet IceCreamComponents { set; get; } - - public virtual DbSet Orders { set; get; } - } -} \ No newline at end of file diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDatabaseImplement.csproj b/IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDatabaseImplement.csproj deleted file mode 100644 index 8643ac1..0000000 --- a/IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDatabaseImplement.csproj +++ /dev/null @@ -1,23 +0,0 @@ - - - - net6.0 - enable - enable - - - - - - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - - - - - - - - - diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Implements/ComponentStorage.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Implements/ComponentStorage.cs deleted file mode 100644 index 3f4c63e..0000000 --- a/IceCreamShop/IceCreamShopDatabaseImplement/Implements/ComponentStorage.cs +++ /dev/null @@ -1,84 +0,0 @@ -using IceCreamShopContracts.BindingModels; -using IceCreamShopContracts.SearchModels; -using IceCreamShopContracts.StoragesContracts; -using IceCreamShopContracts.ViewModels; -using IceCreamShopDatabaseImplement.Models; - -namespace IceCreamShopDatabaseImplement.Implements -{ - public class ComponentStorage : IComponentStorage - { - public List GetFullList() - { - using var context = new IceCreamShopDatabase(); - return context.Components - .Select(x => x.GetViewModel) - .ToList(); - } - - public List GetFilteredList(ComponentSearchModel model) - { - if (string.IsNullOrEmpty(model.ComponentName)) - { - return new(); - } - using var context = new IceCreamShopDatabase(); - return context.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; - } - using var context = new IceCreamShopDatabase(); - return context.Components - .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName) || - (model.Id.HasValue && x.Id == model.Id)) - ?.GetViewModel; - } - - public ComponentViewModel? Insert(ComponentBindingModel model) - { - var newComponent = Component.Create(model); - if (newComponent == null) - { - return null; - } - using var context = new IceCreamShopDatabase(); - context.Components.Add(newComponent); - context.SaveChanges(); - return newComponent.GetViewModel; - } - - public ComponentViewModel? Update(ComponentBindingModel model) - { - using var context = new IceCreamShopDatabase(); - var component = context.Components.FirstOrDefault(x => x.Id == model.Id); - if (component == null) - { - return null; - } - component.Update(model); - context.SaveChanges(); - return component.GetViewModel; - } - - public ComponentViewModel? Delete(ComponentBindingModel model) - { - using var context = new IceCreamShopDatabase(); - var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id); - if (element != null) - { - context.Components.Remove(element); - context.SaveChanges(); - return element.GetViewModel; - } - return null; - } - } -} \ No newline at end of file diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Implements/IceCreamStorage.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Implements/IceCreamStorage.cs deleted file mode 100644 index 1a0a5e0..0000000 --- a/IceCreamShop/IceCreamShopDatabaseImplement/Implements/IceCreamStorage.cs +++ /dev/null @@ -1,106 +0,0 @@ -using IceCreamShopContracts.BindingModels; -using IceCreamShopContracts.SearchModels; -using IceCreamShopContracts.StoragesContracts; -using IceCreamShopContracts.ViewModels; -using IceCreamShopDatabaseImplement.Models; -using Microsoft.EntityFrameworkCore; - -namespace IceCreamShopDatabaseImplement.Implements -{ - public class IceCreamStorage : IIceCreamStorage - { - public List GetFullList() - { - using var context = new IceCreamShopDatabase(); - return context.IceCreams - .Include(x => x.Components) - .ThenInclude(x => x.Component) - .ToList() - .Select(x => x.GetViewModel) - .ToList(); - } - - public List GetFilteredList(IceCreamSearchModel model) - { - if (string.IsNullOrEmpty(model.IceCreamName)) - { - return new(); - } - using var context = new IceCreamShopDatabase(); - return context.IceCreams - .Include(x => x.Components) - .ThenInclude(x => x.Component) - .Where(x => x.IceCreamName.Contains(model.IceCreamName)) - .ToList() - .Select(x => x.GetViewModel) - .ToList(); - } - - public IceCreamViewModel? GetElement(IceCreamSearchModel model) - { - if (string.IsNullOrEmpty(model.IceCreamName) && !model.Id.HasValue) - { - return null; - } - using var context = new IceCreamShopDatabase(); - return context.IceCreams - .Include(x => x.Components) - .ThenInclude(x => x.Component) - .FirstOrDefault(x => (!string.IsNullOrEmpty(model.IceCreamName) && x.IceCreamName == model.IceCreamName) || - (model.Id.HasValue && x.Id == model.Id)) - ?.GetViewModel; - } - - public IceCreamViewModel? Insert(IceCreamBindingModel model) - { - using var context = new IceCreamShopDatabase(); - var newIceCream = IceCream.Create(context, model); - if (newIceCream == null) - { - return null; - } - context.IceCreams.Add(newIceCream); - context.SaveChanges(); - return newIceCream.GetViewModel; - } - - public IceCreamViewModel? Update(IceCreamBindingModel model) - { - using var context = new IceCreamShopDatabase(); - using var transaction = context.Database.BeginTransaction(); - try - { - var iceCream = context.IceCreams.FirstOrDefault(rec => rec.Id == model.Id); - if (iceCream == null) - { - return null; - } - iceCream.Update(model); - context.SaveChanges(); - iceCream.UpdateComponents(context, model); - transaction.Commit(); - return iceCream.GetViewModel; - } - catch - { - transaction.Rollback(); - throw; - } - } - - public IceCreamViewModel? Delete(IceCreamBindingModel model) - { - using var context = new IceCreamShopDatabase(); - var element = context.IceCreams - .Include(x => x.Components) - .FirstOrDefault(rec => rec.Id == model.Id); - if (element != null) - { - context.IceCreams.Remove(element); - context.SaveChanges(); - return element.GetViewModel; - } - return null; - } - } -} \ No newline at end of file diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Implements/OrderStorage.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Implements/OrderStorage.cs deleted file mode 100644 index c701c00..0000000 --- a/IceCreamShop/IceCreamShopDatabaseImplement/Implements/OrderStorage.cs +++ /dev/null @@ -1,97 +0,0 @@ -using IceCreamShopContracts.BindingModels; -using IceCreamShopContracts.SearchModels; -using IceCreamShopContracts.StoragesContracts; -using IceCreamShopContracts.ViewModels; -using IceCreamShopDatabaseImplement.Models; -using Microsoft.EntityFrameworkCore; - -namespace IceCreamShopDatabaseImplement.Implements -{ - public class OrderStorage : IOrderStorage - { - public List GetFullList() - { - using var context = new IceCreamShopDatabase(); - return context.Orders - .Include(x => x.IceCream) - .Select(x => x.GetViewModel) - .ToList(); - } - - public List GetFilteredList(OrderSearchModel model) - { - if (!model.Id.HasValue) - { - return new(); - } - using var context = new IceCreamShopDatabase(); - return context.Orders - .Include(x => x.IceCream) - .Where(x => x.Id == model.Id) - .Select(x => x.GetViewModel) - .ToList(); - } - - public OrderViewModel? GetElement(OrderSearchModel model) - { - if (!model.Id.HasValue) - { - return null; - } - using var context = new IceCreamShopDatabase(); - return context.Orders - .Include(x => x.IceCream) - .FirstOrDefault(x => x.Id == model.Id) - ?.GetViewModel; - } - - public OrderViewModel? Insert(OrderBindingModel model) - { - var newOrder = Order.Create(model); - if (newOrder == null) - { - return null; - } - using var context = new IceCreamShopDatabase(); - context.Orders.Add(newOrder); - context.SaveChanges(); - return context.Orders - .Include(x => x.IceCream) - .FirstOrDefault(x => x.Id == newOrder.Id) - ?.GetViewModel; - } - - public OrderViewModel? Update(OrderBindingModel model) - { - using var context = new IceCreamShopDatabase(); - var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); - if (order == null) - { - return null; - } - order.Update(model); - context.SaveChanges(); - return context.Orders - .Include(x => x.IceCream) - .FirstOrDefault(x => x.Id == model.Id) - ?.GetViewModel; - } - - public OrderViewModel? Delete(OrderBindingModel model) - { - using var context = new IceCreamShopDatabase(); - var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); - if (element != null) - { - var deletedElement = context.Orders - .Include(x => x.IceCream) - .FirstOrDefault(x => x.Id == model.Id) - ?.GetViewModel; - context.Orders.Remove(element); - context.SaveChanges(); - return deletedElement; - } - return null; - } - } -} \ No newline at end of file diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240309192813_InitialCreate.Designer.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240309192813_InitialCreate.Designer.cs deleted file mode 100644 index adc6c10..0000000 --- a/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240309192813_InitialCreate.Designer.cs +++ /dev/null @@ -1,171 +0,0 @@ -// -using System; -using IceCreamShopDatabaseImplement; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Metadata; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -#nullable disable - -namespace IceCreamShopDatabaseImplement.Migrations -{ - [DbContext(typeof(IceCreamShopDatabase))] - [Migration("20240309192813_InitialCreate")] - partial class InitialCreate - { - /// - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder - .HasAnnotation("ProductVersion", "7.0.16") - .HasAnnotation("Relational:MaxIdentifierLength", 128); - - SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); - - modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Component", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); - - b.Property("ComponentName") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.Property("Cost") - .HasColumnType("float"); - - b.HasKey("Id"); - - b.ToTable("Components"); - }); - - modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCream", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); - - b.Property("IceCreamName") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.Property("Price") - .HasColumnType("float"); - - b.HasKey("Id"); - - b.ToTable("IceCreams"); - }); - - modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCreamComponent", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); - - b.Property("ComponentId") - .HasColumnType("int"); - - b.Property("Count") - .HasColumnType("int"); - - b.Property("IceCreamId") - .HasColumnType("int"); - - b.HasKey("Id"); - - b.HasIndex("ComponentId"); - - b.HasIndex("IceCreamId"); - - b.ToTable("IceCreamComponents"); - }); - - modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Order", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); - - b.Property("Count") - .HasColumnType("int"); - - b.Property("DateCreate") - .HasColumnType("datetime2"); - - b.Property("DateImplement") - .HasColumnType("datetime2"); - - b.Property("IceCreamId") - .HasColumnType("int"); - - b.Property("Status") - .HasColumnType("int"); - - b.Property("Sum") - .HasColumnType("float"); - - b.HasKey("Id"); - - b.HasIndex("IceCreamId"); - - b.ToTable("Orders"); - }); - - modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCreamComponent", b => - { - b.HasOne("IceCreamShopDatabaseImplement.Models.Component", "Component") - .WithMany("IceCreamComponents") - .HasForeignKey("ComponentId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", "IceCream") - .WithMany("Components") - .HasForeignKey("IceCreamId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("Component"); - - b.Navigation("IceCream"); - }); - - modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Order", b => - { - b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", "IceCream") - .WithMany("Orders") - .HasForeignKey("IceCreamId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("IceCream"); - }); - - modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Component", b => - { - b.Navigation("IceCreamComponents"); - }); - - modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCream", b => - { - b.Navigation("Components"); - - b.Navigation("Orders"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240309192813_InitialCreate.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240309192813_InitialCreate.cs deleted file mode 100644 index 562bd62..0000000 --- a/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240309192813_InitialCreate.cs +++ /dev/null @@ -1,125 +0,0 @@ -using System; -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -namespace IceCreamShopDatabaseImplement.Migrations -{ - /// - public partial class InitialCreate : Migration - { - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "Components", - columns: table => new - { - Id = table.Column(type: "int", nullable: false) - .Annotation("SqlServer:Identity", "1, 1"), - ComponentName = table.Column(type: "nvarchar(max)", nullable: false), - Cost = table.Column(type: "float", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_Components", x => x.Id); - }); - - migrationBuilder.CreateTable( - name: "IceCreams", - columns: table => new - { - Id = table.Column(type: "int", nullable: false) - .Annotation("SqlServer:Identity", "1, 1"), - IceCreamName = table.Column(type: "nvarchar(max)", nullable: false), - Price = table.Column(type: "float", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_IceCreams", x => x.Id); - }); - - migrationBuilder.CreateTable( - name: "IceCreamComponents", - columns: table => new - { - Id = table.Column(type: "int", nullable: false) - .Annotation("SqlServer:Identity", "1, 1"), - IceCreamId = table.Column(type: "int", nullable: false), - ComponentId = table.Column(type: "int", nullable: false), - Count = table.Column(type: "int", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_IceCreamComponents", x => x.Id); - table.ForeignKey( - name: "FK_IceCreamComponents_Components_ComponentId", - column: x => x.ComponentId, - principalTable: "Components", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_IceCreamComponents_IceCreams_IceCreamId", - column: x => x.IceCreamId, - principalTable: "IceCreams", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateTable( - name: "Orders", - columns: table => new - { - Id = table.Column(type: "int", nullable: false) - .Annotation("SqlServer:Identity", "1, 1"), - IceCreamId = table.Column(type: "int", nullable: false), - Count = table.Column(type: "int", nullable: false), - Sum = table.Column(type: "float", nullable: false), - Status = table.Column(type: "int", nullable: false), - DateCreate = table.Column(type: "datetime2", nullable: false), - DateImplement = table.Column(type: "datetime2", nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_Orders", x => x.Id); - table.ForeignKey( - name: "FK_Orders_IceCreams_IceCreamId", - column: x => x.IceCreamId, - principalTable: "IceCreams", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateIndex( - name: "IX_IceCreamComponents_ComponentId", - table: "IceCreamComponents", - column: "ComponentId"); - - migrationBuilder.CreateIndex( - name: "IX_IceCreamComponents_IceCreamId", - table: "IceCreamComponents", - column: "IceCreamId"); - - migrationBuilder.CreateIndex( - name: "IX_Orders_IceCreamId", - table: "Orders", - column: "IceCreamId"); - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "IceCreamComponents"); - - migrationBuilder.DropTable( - name: "Orders"); - - migrationBuilder.DropTable( - name: "Components"); - - migrationBuilder.DropTable( - name: "IceCreams"); - } - } -} diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/IceCreamShopDatabaseModelSnapshot.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/IceCreamShopDatabaseModelSnapshot.cs deleted file mode 100644 index 3563fed..0000000 --- a/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/IceCreamShopDatabaseModelSnapshot.cs +++ /dev/null @@ -1,168 +0,0 @@ -// -using System; -using IceCreamShopDatabaseImplement; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Metadata; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -#nullable disable - -namespace IceCreamShopDatabaseImplement.Migrations -{ - [DbContext(typeof(IceCreamShopDatabase))] - partial class IceCreamShopDatabaseModelSnapshot : ModelSnapshot - { - protected override void BuildModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder - .HasAnnotation("ProductVersion", "7.0.16") - .HasAnnotation("Relational:MaxIdentifierLength", 128); - - SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); - - modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Component", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); - - b.Property("ComponentName") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.Property("Cost") - .HasColumnType("float"); - - b.HasKey("Id"); - - b.ToTable("Components"); - }); - - modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCream", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); - - b.Property("IceCreamName") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.Property("Price") - .HasColumnType("float"); - - b.HasKey("Id"); - - b.ToTable("IceCreams"); - }); - - modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCreamComponent", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); - - b.Property("ComponentId") - .HasColumnType("int"); - - b.Property("Count") - .HasColumnType("int"); - - b.Property("IceCreamId") - .HasColumnType("int"); - - b.HasKey("Id"); - - b.HasIndex("ComponentId"); - - b.HasIndex("IceCreamId"); - - b.ToTable("IceCreamComponents"); - }); - - modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Order", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); - - b.Property("Count") - .HasColumnType("int"); - - b.Property("DateCreate") - .HasColumnType("datetime2"); - - b.Property("DateImplement") - .HasColumnType("datetime2"); - - b.Property("IceCreamId") - .HasColumnType("int"); - - b.Property("Status") - .HasColumnType("int"); - - b.Property("Sum") - .HasColumnType("float"); - - b.HasKey("Id"); - - b.HasIndex("IceCreamId"); - - b.ToTable("Orders"); - }); - - modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCreamComponent", b => - { - b.HasOne("IceCreamShopDatabaseImplement.Models.Component", "Component") - .WithMany("IceCreamComponents") - .HasForeignKey("ComponentId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", "IceCream") - .WithMany("Components") - .HasForeignKey("IceCreamId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("Component"); - - b.Navigation("IceCream"); - }); - - modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Order", b => - { - b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", "IceCream") - .WithMany("Orders") - .HasForeignKey("IceCreamId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("IceCream"); - }); - - modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Component", b => - { - b.Navigation("IceCreamComponents"); - }); - - modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCream", b => - { - b.Navigation("Components"); - - b.Navigation("Orders"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Models/Component.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Models/Component.cs deleted file mode 100644 index c295a25..0000000 --- a/IceCreamShop/IceCreamShopDatabaseImplement/Models/Component.cs +++ /dev/null @@ -1,63 +0,0 @@ -using IceCreamShopContracts.BindingModels; -using IceCreamShopContracts.ViewModels; -using IceCreamShopDataModels.Models; -using System.ComponentModel.DataAnnotations; -using System.ComponentModel.DataAnnotations.Schema; - -namespace IceCreamShopDatabaseImplement.Models -{ - public class Component : IComponentModel - { - public int Id { get; private set; } - - [Required] - public string ComponentName { get; private set; } = string.Empty; - - [Required] - public double Cost { get; set; } - - [ForeignKey("ComponentId")] - public virtual List IceCreamComponents { get; set; } = new(); - - 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(ComponentViewModel model) - { - return new Component - { - Id = model.Id, - ComponentName = model.ComponentName, - Cost = model.Cost - }; - } - - 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 - }; - } -} \ No newline at end of file diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Models/IceCream.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Models/IceCream.cs deleted file mode 100644 index 4105f97..0000000 --- a/IceCreamShop/IceCreamShopDatabaseImplement/Models/IceCream.cs +++ /dev/null @@ -1,99 +0,0 @@ -using IceCreamShopContracts.BindingModels; -using IceCreamShopContracts.ViewModels; -using IceCreamShopDataModels.Models; -using System.ComponentModel.DataAnnotations; -using System.ComponentModel.DataAnnotations.Schema; - -namespace IceCreamShopDatabaseImplement.Models -{ - public class IceCream : IIceCreamModel - { - public int Id { get; set; } - - [Required] - public string IceCreamName { get; set; } = string.Empty; - - [Required] - public double Price { get; set; } - - private Dictionary? _iceCreamComponents = null; - - [NotMapped] - public Dictionary IceCreamComponents - { - get - { - if (_iceCreamComponents == null) - { - _iceCreamComponents = Components - .ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count)); - } - return _iceCreamComponents; - } - } - - [ForeignKey("IceCreamId")] - public virtual List Components { get; set; } = new(); - - [ForeignKey("IceCreamId")] - public virtual List Orders { get; set; } = new(); - - public static IceCream Create(IceCreamShopDatabase context, IceCreamBindingModel model) - { - return new IceCream() - { - Id = model.Id, - IceCreamName = model.IceCreamName, - Price = model.Price, - Components = model.IceCreamComponents.Select(x => new IceCreamComponent - { - Component = context.Components.First(y => y.Id == x.Key), - Count = x.Value.Item2 - }).ToList() - }; - } - - public void Update(IceCreamBindingModel model) - { - IceCreamName = model.IceCreamName; - Price = model.Price; - } - - public IceCreamViewModel GetViewModel => new() - { - Id = Id, - IceCreamName = IceCreamName, - Price = Price, - IceCreamComponents = IceCreamComponents - }; - - public void UpdateComponents(IceCreamShopDatabase context, IceCreamBindingModel model) - { - var iceCreamComponents = context.IceCreamComponents.Where(rec => rec.IceCreamId == model.Id).ToList(); - if (iceCreamComponents != null && iceCreamComponents.Count > 0) - { // удалили те, которых нет в модели - context.IceCreamComponents.RemoveRange(iceCreamComponents.Where(rec => !model.IceCreamComponents.ContainsKey(rec.ComponentId))); - context.SaveChanges(); - // обновили количество у существующих записей - foreach (var updateComponent in iceCreamComponents) - { - updateComponent.Count = model.IceCreamComponents[updateComponent.ComponentId].Item2; - model.IceCreamComponents.Remove(updateComponent.ComponentId); - } - context.SaveChanges(); - } - var iceCream = context.IceCreams.First(x => x.Id == Id); - foreach (var ic in model.IceCreamComponents) - { - context.IceCreamComponents.Add(new IceCreamComponent - { - IceCream = iceCream, - Component = context.Components.First(x => x.Id == ic.Key), - Count = ic.Value.Item2 - }); - context.SaveChanges(); - } - _iceCreamComponents = null; - } - } -} \ No newline at end of file diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Models/IceCreamComponent.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Models/IceCreamComponent.cs deleted file mode 100644 index 50ba298..0000000 --- a/IceCreamShop/IceCreamShopDatabaseImplement/Models/IceCreamComponent.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System.ComponentModel.DataAnnotations; - -namespace IceCreamShopDatabaseImplement.Models -{ - public class IceCreamComponent - { - public int Id { get; set; } - - [Required] - public int IceCreamId { get; set; } - - [Required] - public int ComponentId { get; set; } - - [Required] - public int Count { get; set; } - - public virtual Component Component { get; set; } = new(); - - public virtual IceCream IceCream { get; set; } = new(); - } -} \ No newline at end of file diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Models/Order.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Models/Order.cs deleted file mode 100644 index 76edee3..0000000 --- a/IceCreamShop/IceCreamShopDatabaseImplement/Models/Order.cs +++ /dev/null @@ -1,72 +0,0 @@ -using IceCreamShopContracts.BindingModels; -using IceCreamShopContracts.ViewModels; -using IceCreamShopDataModels.Enums; -using IceCreamShopDataModels.Models; -using System.ComponentModel.DataAnnotations; - -namespace IceCreamShopDatabaseImplement.Models -{ - public class Order : IOrderModel - { - public int Id { get; set; } - - [Required] - public int IceCreamId { get; set; } - - [Required] - public int Count { get; set; } - - [Required] - public double Sum { get; set; } - - [Required] - public OrderStatus Status { get; set; } - - [Required] - public DateTime DateCreate { get; set; } - - public DateTime? DateImplement { get; set; } - - public virtual IceCream IceCream { get; set; } - - public static Order? Create(OrderBindingModel? model) - { - if (model == null) - { - return null; - } - return new Order() - { - Id = model.Id, - IceCreamId = model.IceCreamId, - Count = model.Count, - Sum = model.Sum, - Status = model.Status, - DateCreate = model.DateCreate, - DateImplement = model.DateImplement - }; - } - - public void Update(OrderBindingModel? model) - { - if (model == null) - { - return; - } - Status = model.Status; - DateImplement = model.DateImplement; - } - - public OrderViewModel GetViewModel => new() - { - Id = Id, - IceCreamId = IceCreamId, - Count = Count, - Sum = Sum, - Status = Status, - DateCreate = DateCreate, - DateImplement = DateImplement, - IceCreamName = IceCream.IceCreamName - }; - } -} \ No newline at end of file diff --git a/IceCreamShop/IceCreamShopView/IceCreamShopView.csproj b/IceCreamShop/IceCreamShopView/IceCreamShopView.csproj index a816493..1456ac2 100644 --- a/IceCreamShop/IceCreamShopView/IceCreamShopView.csproj +++ b/IceCreamShop/IceCreamShopView/IceCreamShopView.csproj @@ -19,10 +19,6 @@ - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - @@ -31,7 +27,6 @@ - diff --git a/IceCreamShop/IceCreamShopView/Program.cs b/IceCreamShop/IceCreamShopView/Program.cs index 50eb3ce..b5b86d8 100644 --- a/IceCreamShop/IceCreamShopView/Program.cs +++ b/IceCreamShop/IceCreamShopView/Program.cs @@ -1,7 +1,7 @@ using IceCreamShopBusinessLogic.BusinessLogics; using IceCreamShopContracts.BusinessLogicsContracts; using IceCreamShopContracts.StoragesContracts; -using IceCreamShopDatabaseImplement.Implements; +using IceCreamShopFileImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; -- 2.25.1 From 7f6ae000bbb1dda240ea1c47314cba05d9b348aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=BB=D1=8C=D1=8F?= <Илья@WIN-RANNDDD> Date: Sun, 7 Apr 2024 21:10:13 +0400 Subject: [PATCH 11/11] =?UTF-8?q?=D0=A4=D0=B8=D0=BA=D1=81=20=D1=81=D0=BB?= =?UTF-8?q?=D0=B8=D1=8F=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- IceCreamShop/IceCreamShop.sln | 8 +- .../IceCreamShopDatabase.cs | 25 --- .../IceCreamShopDatabaseImplement.csproj | 23 --- .../Implements/ComponentStorage.cs | 84 --------- .../Implements/IceCreamStorage.cs | 106 ----------- .../Implements/OrderStorage.cs | 97 ---------- .../20240309192813_InitialCreate.Designer.cs | 171 ------------------ .../20240309192813_InitialCreate.cs | 125 ------------- .../IceCreamShopDatabaseModelSnapshot.cs | 168 ----------------- .../Models/Component.cs | 63 ------- .../Models/IceCream.cs | 99 ---------- .../Models/IceCreamComponent.cs | 22 --- .../Models/Order.cs | 72 -------- .../IceCreamShopView/IceCreamShopView.csproj | 5 - IceCreamShop/IceCreamShopView/Program.cs | 2 +- 15 files changed, 2 insertions(+), 1068 deletions(-) delete mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDatabase.cs delete mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDatabaseImplement.csproj delete mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Implements/ComponentStorage.cs delete mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Implements/IceCreamStorage.cs delete mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Implements/OrderStorage.cs delete mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240309192813_InitialCreate.Designer.cs delete mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240309192813_InitialCreate.cs delete mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Migrations/IceCreamShopDatabaseModelSnapshot.cs delete mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Models/Component.cs delete mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Models/IceCream.cs delete mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Models/IceCreamComponent.cs delete mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Models/Order.cs diff --git a/IceCreamShop/IceCreamShop.sln b/IceCreamShop/IceCreamShop.sln index 84842a0..5a64334 100644 --- a/IceCreamShop/IceCreamShop.sln +++ b/IceCreamShop/IceCreamShop.sln @@ -13,9 +13,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IceCreamShopListImplement", EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IceCreamShopBusinessLogic", "IceCreamShopBusinessLogic\IceCreamShopBusinessLogic.csproj", "{392488A9-9B1E-4DDA-95CE-C2FDC954DEE6}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IceCreamShopFileImplement", "IceCreamShopFileImplement\IceCreamShopFileImplement.csproj", "{5CA64E22-1AB5-4D9C-9C1E-8E336857456B}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IceCreamShopDatabaseImplement", "IceCreamShopDatabaseImplement\IceCreamShopDatabaseImplement.csproj", "{095BA089-0060-4A1E-A658-9E9061A6BB39}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IceCreamShopFileImplement", "IceCreamShopFileImplement\IceCreamShopFileImplement.csproj", "{5CA64E22-1AB5-4D9C-9C1E-8E336857456B}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -47,10 +45,6 @@ Global {5CA64E22-1AB5-4D9C-9C1E-8E336857456B}.Debug|Any CPU.Build.0 = Debug|Any CPU {5CA64E22-1AB5-4D9C-9C1E-8E336857456B}.Release|Any CPU.ActiveCfg = Release|Any CPU {5CA64E22-1AB5-4D9C-9C1E-8E336857456B}.Release|Any CPU.Build.0 = Release|Any CPU - {095BA089-0060-4A1E-A658-9E9061A6BB39}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {095BA089-0060-4A1E-A658-9E9061A6BB39}.Debug|Any CPU.Build.0 = Debug|Any CPU - {095BA089-0060-4A1E-A658-9E9061A6BB39}.Release|Any CPU.ActiveCfg = Release|Any CPU - {095BA089-0060-4A1E-A658-9E9061A6BB39}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDatabase.cs b/IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDatabase.cs deleted file mode 100644 index 8aa8ad9..0000000 --- a/IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDatabase.cs +++ /dev/null @@ -1,25 +0,0 @@ -using IceCreamShopDatabaseImplement.Models; -using Microsoft.EntityFrameworkCore; - -namespace IceCreamShopDatabaseImplement -{ - public class IceCreamShopDatabase : DbContext - { - protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) - { - if (optionsBuilder.IsConfigured == false) - { - optionsBuilder.UseSqlServer(@"Data Source=.\SQLEXPRESS;Initial Catalog=IceCreamShopDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); - } - base.OnConfiguring(optionsBuilder); - } - - public virtual DbSet Components { set; get; } - - public virtual DbSet IceCreams { set; get; } - - public virtual DbSet IceCreamComponents { set; get; } - - public virtual DbSet Orders { set; get; } - } -} \ No newline at end of file diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDatabaseImplement.csproj b/IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDatabaseImplement.csproj deleted file mode 100644 index 8643ac1..0000000 --- a/IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDatabaseImplement.csproj +++ /dev/null @@ -1,23 +0,0 @@ - - - - net6.0 - enable - enable - - - - - - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - - - - - - - - - diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Implements/ComponentStorage.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Implements/ComponentStorage.cs deleted file mode 100644 index 3f4c63e..0000000 --- a/IceCreamShop/IceCreamShopDatabaseImplement/Implements/ComponentStorage.cs +++ /dev/null @@ -1,84 +0,0 @@ -using IceCreamShopContracts.BindingModels; -using IceCreamShopContracts.SearchModels; -using IceCreamShopContracts.StoragesContracts; -using IceCreamShopContracts.ViewModels; -using IceCreamShopDatabaseImplement.Models; - -namespace IceCreamShopDatabaseImplement.Implements -{ - public class ComponentStorage : IComponentStorage - { - public List GetFullList() - { - using var context = new IceCreamShopDatabase(); - return context.Components - .Select(x => x.GetViewModel) - .ToList(); - } - - public List GetFilteredList(ComponentSearchModel model) - { - if (string.IsNullOrEmpty(model.ComponentName)) - { - return new(); - } - using var context = new IceCreamShopDatabase(); - return context.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; - } - using var context = new IceCreamShopDatabase(); - return context.Components - .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName) || - (model.Id.HasValue && x.Id == model.Id)) - ?.GetViewModel; - } - - public ComponentViewModel? Insert(ComponentBindingModel model) - { - var newComponent = Component.Create(model); - if (newComponent == null) - { - return null; - } - using var context = new IceCreamShopDatabase(); - context.Components.Add(newComponent); - context.SaveChanges(); - return newComponent.GetViewModel; - } - - public ComponentViewModel? Update(ComponentBindingModel model) - { - using var context = new IceCreamShopDatabase(); - var component = context.Components.FirstOrDefault(x => x.Id == model.Id); - if (component == null) - { - return null; - } - component.Update(model); - context.SaveChanges(); - return component.GetViewModel; - } - - public ComponentViewModel? Delete(ComponentBindingModel model) - { - using var context = new IceCreamShopDatabase(); - var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id); - if (element != null) - { - context.Components.Remove(element); - context.SaveChanges(); - return element.GetViewModel; - } - return null; - } - } -} \ No newline at end of file diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Implements/IceCreamStorage.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Implements/IceCreamStorage.cs deleted file mode 100644 index 1a0a5e0..0000000 --- a/IceCreamShop/IceCreamShopDatabaseImplement/Implements/IceCreamStorage.cs +++ /dev/null @@ -1,106 +0,0 @@ -using IceCreamShopContracts.BindingModels; -using IceCreamShopContracts.SearchModels; -using IceCreamShopContracts.StoragesContracts; -using IceCreamShopContracts.ViewModels; -using IceCreamShopDatabaseImplement.Models; -using Microsoft.EntityFrameworkCore; - -namespace IceCreamShopDatabaseImplement.Implements -{ - public class IceCreamStorage : IIceCreamStorage - { - public List GetFullList() - { - using var context = new IceCreamShopDatabase(); - return context.IceCreams - .Include(x => x.Components) - .ThenInclude(x => x.Component) - .ToList() - .Select(x => x.GetViewModel) - .ToList(); - } - - public List GetFilteredList(IceCreamSearchModel model) - { - if (string.IsNullOrEmpty(model.IceCreamName)) - { - return new(); - } - using var context = new IceCreamShopDatabase(); - return context.IceCreams - .Include(x => x.Components) - .ThenInclude(x => x.Component) - .Where(x => x.IceCreamName.Contains(model.IceCreamName)) - .ToList() - .Select(x => x.GetViewModel) - .ToList(); - } - - public IceCreamViewModel? GetElement(IceCreamSearchModel model) - { - if (string.IsNullOrEmpty(model.IceCreamName) && !model.Id.HasValue) - { - return null; - } - using var context = new IceCreamShopDatabase(); - return context.IceCreams - .Include(x => x.Components) - .ThenInclude(x => x.Component) - .FirstOrDefault(x => (!string.IsNullOrEmpty(model.IceCreamName) && x.IceCreamName == model.IceCreamName) || - (model.Id.HasValue && x.Id == model.Id)) - ?.GetViewModel; - } - - public IceCreamViewModel? Insert(IceCreamBindingModel model) - { - using var context = new IceCreamShopDatabase(); - var newIceCream = IceCream.Create(context, model); - if (newIceCream == null) - { - return null; - } - context.IceCreams.Add(newIceCream); - context.SaveChanges(); - return newIceCream.GetViewModel; - } - - public IceCreamViewModel? Update(IceCreamBindingModel model) - { - using var context = new IceCreamShopDatabase(); - using var transaction = context.Database.BeginTransaction(); - try - { - var iceCream = context.IceCreams.FirstOrDefault(rec => rec.Id == model.Id); - if (iceCream == null) - { - return null; - } - iceCream.Update(model); - context.SaveChanges(); - iceCream.UpdateComponents(context, model); - transaction.Commit(); - return iceCream.GetViewModel; - } - catch - { - transaction.Rollback(); - throw; - } - } - - public IceCreamViewModel? Delete(IceCreamBindingModel model) - { - using var context = new IceCreamShopDatabase(); - var element = context.IceCreams - .Include(x => x.Components) - .FirstOrDefault(rec => rec.Id == model.Id); - if (element != null) - { - context.IceCreams.Remove(element); - context.SaveChanges(); - return element.GetViewModel; - } - return null; - } - } -} \ No newline at end of file diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Implements/OrderStorage.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Implements/OrderStorage.cs deleted file mode 100644 index c701c00..0000000 --- a/IceCreamShop/IceCreamShopDatabaseImplement/Implements/OrderStorage.cs +++ /dev/null @@ -1,97 +0,0 @@ -using IceCreamShopContracts.BindingModels; -using IceCreamShopContracts.SearchModels; -using IceCreamShopContracts.StoragesContracts; -using IceCreamShopContracts.ViewModels; -using IceCreamShopDatabaseImplement.Models; -using Microsoft.EntityFrameworkCore; - -namespace IceCreamShopDatabaseImplement.Implements -{ - public class OrderStorage : IOrderStorage - { - public List GetFullList() - { - using var context = new IceCreamShopDatabase(); - return context.Orders - .Include(x => x.IceCream) - .Select(x => x.GetViewModel) - .ToList(); - } - - public List GetFilteredList(OrderSearchModel model) - { - if (!model.Id.HasValue) - { - return new(); - } - using var context = new IceCreamShopDatabase(); - return context.Orders - .Include(x => x.IceCream) - .Where(x => x.Id == model.Id) - .Select(x => x.GetViewModel) - .ToList(); - } - - public OrderViewModel? GetElement(OrderSearchModel model) - { - if (!model.Id.HasValue) - { - return null; - } - using var context = new IceCreamShopDatabase(); - return context.Orders - .Include(x => x.IceCream) - .FirstOrDefault(x => x.Id == model.Id) - ?.GetViewModel; - } - - public OrderViewModel? Insert(OrderBindingModel model) - { - var newOrder = Order.Create(model); - if (newOrder == null) - { - return null; - } - using var context = new IceCreamShopDatabase(); - context.Orders.Add(newOrder); - context.SaveChanges(); - return context.Orders - .Include(x => x.IceCream) - .FirstOrDefault(x => x.Id == newOrder.Id) - ?.GetViewModel; - } - - public OrderViewModel? Update(OrderBindingModel model) - { - using var context = new IceCreamShopDatabase(); - var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); - if (order == null) - { - return null; - } - order.Update(model); - context.SaveChanges(); - return context.Orders - .Include(x => x.IceCream) - .FirstOrDefault(x => x.Id == model.Id) - ?.GetViewModel; - } - - public OrderViewModel? Delete(OrderBindingModel model) - { - using var context = new IceCreamShopDatabase(); - var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); - if (element != null) - { - var deletedElement = context.Orders - .Include(x => x.IceCream) - .FirstOrDefault(x => x.Id == model.Id) - ?.GetViewModel; - context.Orders.Remove(element); - context.SaveChanges(); - return deletedElement; - } - return null; - } - } -} \ No newline at end of file diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240309192813_InitialCreate.Designer.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240309192813_InitialCreate.Designer.cs deleted file mode 100644 index adc6c10..0000000 --- a/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240309192813_InitialCreate.Designer.cs +++ /dev/null @@ -1,171 +0,0 @@ -// -using System; -using IceCreamShopDatabaseImplement; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Metadata; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -#nullable disable - -namespace IceCreamShopDatabaseImplement.Migrations -{ - [DbContext(typeof(IceCreamShopDatabase))] - [Migration("20240309192813_InitialCreate")] - partial class InitialCreate - { - /// - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder - .HasAnnotation("ProductVersion", "7.0.16") - .HasAnnotation("Relational:MaxIdentifierLength", 128); - - SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); - - modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Component", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); - - b.Property("ComponentName") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.Property("Cost") - .HasColumnType("float"); - - b.HasKey("Id"); - - b.ToTable("Components"); - }); - - modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCream", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); - - b.Property("IceCreamName") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.Property("Price") - .HasColumnType("float"); - - b.HasKey("Id"); - - b.ToTable("IceCreams"); - }); - - modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCreamComponent", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); - - b.Property("ComponentId") - .HasColumnType("int"); - - b.Property("Count") - .HasColumnType("int"); - - b.Property("IceCreamId") - .HasColumnType("int"); - - b.HasKey("Id"); - - b.HasIndex("ComponentId"); - - b.HasIndex("IceCreamId"); - - b.ToTable("IceCreamComponents"); - }); - - modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Order", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); - - b.Property("Count") - .HasColumnType("int"); - - b.Property("DateCreate") - .HasColumnType("datetime2"); - - b.Property("DateImplement") - .HasColumnType("datetime2"); - - b.Property("IceCreamId") - .HasColumnType("int"); - - b.Property("Status") - .HasColumnType("int"); - - b.Property("Sum") - .HasColumnType("float"); - - b.HasKey("Id"); - - b.HasIndex("IceCreamId"); - - b.ToTable("Orders"); - }); - - modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCreamComponent", b => - { - b.HasOne("IceCreamShopDatabaseImplement.Models.Component", "Component") - .WithMany("IceCreamComponents") - .HasForeignKey("ComponentId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", "IceCream") - .WithMany("Components") - .HasForeignKey("IceCreamId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("Component"); - - b.Navigation("IceCream"); - }); - - modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Order", b => - { - b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", "IceCream") - .WithMany("Orders") - .HasForeignKey("IceCreamId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("IceCream"); - }); - - modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Component", b => - { - b.Navigation("IceCreamComponents"); - }); - - modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCream", b => - { - b.Navigation("Components"); - - b.Navigation("Orders"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240309192813_InitialCreate.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240309192813_InitialCreate.cs deleted file mode 100644 index 562bd62..0000000 --- a/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240309192813_InitialCreate.cs +++ /dev/null @@ -1,125 +0,0 @@ -using System; -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -namespace IceCreamShopDatabaseImplement.Migrations -{ - /// - public partial class InitialCreate : Migration - { - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "Components", - columns: table => new - { - Id = table.Column(type: "int", nullable: false) - .Annotation("SqlServer:Identity", "1, 1"), - ComponentName = table.Column(type: "nvarchar(max)", nullable: false), - Cost = table.Column(type: "float", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_Components", x => x.Id); - }); - - migrationBuilder.CreateTable( - name: "IceCreams", - columns: table => new - { - Id = table.Column(type: "int", nullable: false) - .Annotation("SqlServer:Identity", "1, 1"), - IceCreamName = table.Column(type: "nvarchar(max)", nullable: false), - Price = table.Column(type: "float", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_IceCreams", x => x.Id); - }); - - migrationBuilder.CreateTable( - name: "IceCreamComponents", - columns: table => new - { - Id = table.Column(type: "int", nullable: false) - .Annotation("SqlServer:Identity", "1, 1"), - IceCreamId = table.Column(type: "int", nullable: false), - ComponentId = table.Column(type: "int", nullable: false), - Count = table.Column(type: "int", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_IceCreamComponents", x => x.Id); - table.ForeignKey( - name: "FK_IceCreamComponents_Components_ComponentId", - column: x => x.ComponentId, - principalTable: "Components", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_IceCreamComponents_IceCreams_IceCreamId", - column: x => x.IceCreamId, - principalTable: "IceCreams", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateTable( - name: "Orders", - columns: table => new - { - Id = table.Column(type: "int", nullable: false) - .Annotation("SqlServer:Identity", "1, 1"), - IceCreamId = table.Column(type: "int", nullable: false), - Count = table.Column(type: "int", nullable: false), - Sum = table.Column(type: "float", nullable: false), - Status = table.Column(type: "int", nullable: false), - DateCreate = table.Column(type: "datetime2", nullable: false), - DateImplement = table.Column(type: "datetime2", nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_Orders", x => x.Id); - table.ForeignKey( - name: "FK_Orders_IceCreams_IceCreamId", - column: x => x.IceCreamId, - principalTable: "IceCreams", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateIndex( - name: "IX_IceCreamComponents_ComponentId", - table: "IceCreamComponents", - column: "ComponentId"); - - migrationBuilder.CreateIndex( - name: "IX_IceCreamComponents_IceCreamId", - table: "IceCreamComponents", - column: "IceCreamId"); - - migrationBuilder.CreateIndex( - name: "IX_Orders_IceCreamId", - table: "Orders", - column: "IceCreamId"); - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "IceCreamComponents"); - - migrationBuilder.DropTable( - name: "Orders"); - - migrationBuilder.DropTable( - name: "Components"); - - migrationBuilder.DropTable( - name: "IceCreams"); - } - } -} diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/IceCreamShopDatabaseModelSnapshot.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/IceCreamShopDatabaseModelSnapshot.cs deleted file mode 100644 index 3563fed..0000000 --- a/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/IceCreamShopDatabaseModelSnapshot.cs +++ /dev/null @@ -1,168 +0,0 @@ -// -using System; -using IceCreamShopDatabaseImplement; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Metadata; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -#nullable disable - -namespace IceCreamShopDatabaseImplement.Migrations -{ - [DbContext(typeof(IceCreamShopDatabase))] - partial class IceCreamShopDatabaseModelSnapshot : ModelSnapshot - { - protected override void BuildModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder - .HasAnnotation("ProductVersion", "7.0.16") - .HasAnnotation("Relational:MaxIdentifierLength", 128); - - SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); - - modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Component", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); - - b.Property("ComponentName") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.Property("Cost") - .HasColumnType("float"); - - b.HasKey("Id"); - - b.ToTable("Components"); - }); - - modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCream", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); - - b.Property("IceCreamName") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.Property("Price") - .HasColumnType("float"); - - b.HasKey("Id"); - - b.ToTable("IceCreams"); - }); - - modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCreamComponent", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); - - b.Property("ComponentId") - .HasColumnType("int"); - - b.Property("Count") - .HasColumnType("int"); - - b.Property("IceCreamId") - .HasColumnType("int"); - - b.HasKey("Id"); - - b.HasIndex("ComponentId"); - - b.HasIndex("IceCreamId"); - - b.ToTable("IceCreamComponents"); - }); - - modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Order", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); - - b.Property("Count") - .HasColumnType("int"); - - b.Property("DateCreate") - .HasColumnType("datetime2"); - - b.Property("DateImplement") - .HasColumnType("datetime2"); - - b.Property("IceCreamId") - .HasColumnType("int"); - - b.Property("Status") - .HasColumnType("int"); - - b.Property("Sum") - .HasColumnType("float"); - - b.HasKey("Id"); - - b.HasIndex("IceCreamId"); - - b.ToTable("Orders"); - }); - - modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCreamComponent", b => - { - b.HasOne("IceCreamShopDatabaseImplement.Models.Component", "Component") - .WithMany("IceCreamComponents") - .HasForeignKey("ComponentId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", "IceCream") - .WithMany("Components") - .HasForeignKey("IceCreamId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("Component"); - - b.Navigation("IceCream"); - }); - - modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Order", b => - { - b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", "IceCream") - .WithMany("Orders") - .HasForeignKey("IceCreamId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("IceCream"); - }); - - modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Component", b => - { - b.Navigation("IceCreamComponents"); - }); - - modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCream", b => - { - b.Navigation("Components"); - - b.Navigation("Orders"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Models/Component.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Models/Component.cs deleted file mode 100644 index c295a25..0000000 --- a/IceCreamShop/IceCreamShopDatabaseImplement/Models/Component.cs +++ /dev/null @@ -1,63 +0,0 @@ -using IceCreamShopContracts.BindingModels; -using IceCreamShopContracts.ViewModels; -using IceCreamShopDataModels.Models; -using System.ComponentModel.DataAnnotations; -using System.ComponentModel.DataAnnotations.Schema; - -namespace IceCreamShopDatabaseImplement.Models -{ - public class Component : IComponentModel - { - public int Id { get; private set; } - - [Required] - public string ComponentName { get; private set; } = string.Empty; - - [Required] - public double Cost { get; set; } - - [ForeignKey("ComponentId")] - public virtual List IceCreamComponents { get; set; } = new(); - - 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(ComponentViewModel model) - { - return new Component - { - Id = model.Id, - ComponentName = model.ComponentName, - Cost = model.Cost - }; - } - - 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 - }; - } -} \ No newline at end of file diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Models/IceCream.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Models/IceCream.cs deleted file mode 100644 index 4105f97..0000000 --- a/IceCreamShop/IceCreamShopDatabaseImplement/Models/IceCream.cs +++ /dev/null @@ -1,99 +0,0 @@ -using IceCreamShopContracts.BindingModels; -using IceCreamShopContracts.ViewModels; -using IceCreamShopDataModels.Models; -using System.ComponentModel.DataAnnotations; -using System.ComponentModel.DataAnnotations.Schema; - -namespace IceCreamShopDatabaseImplement.Models -{ - public class IceCream : IIceCreamModel - { - public int Id { get; set; } - - [Required] - public string IceCreamName { get; set; } = string.Empty; - - [Required] - public double Price { get; set; } - - private Dictionary? _iceCreamComponents = null; - - [NotMapped] - public Dictionary IceCreamComponents - { - get - { - if (_iceCreamComponents == null) - { - _iceCreamComponents = Components - .ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count)); - } - return _iceCreamComponents; - } - } - - [ForeignKey("IceCreamId")] - public virtual List Components { get; set; } = new(); - - [ForeignKey("IceCreamId")] - public virtual List Orders { get; set; } = new(); - - public static IceCream Create(IceCreamShopDatabase context, IceCreamBindingModel model) - { - return new IceCream() - { - Id = model.Id, - IceCreamName = model.IceCreamName, - Price = model.Price, - Components = model.IceCreamComponents.Select(x => new IceCreamComponent - { - Component = context.Components.First(y => y.Id == x.Key), - Count = x.Value.Item2 - }).ToList() - }; - } - - public void Update(IceCreamBindingModel model) - { - IceCreamName = model.IceCreamName; - Price = model.Price; - } - - public IceCreamViewModel GetViewModel => new() - { - Id = Id, - IceCreamName = IceCreamName, - Price = Price, - IceCreamComponents = IceCreamComponents - }; - - public void UpdateComponents(IceCreamShopDatabase context, IceCreamBindingModel model) - { - var iceCreamComponents = context.IceCreamComponents.Where(rec => rec.IceCreamId == model.Id).ToList(); - if (iceCreamComponents != null && iceCreamComponents.Count > 0) - { // удалили те, которых нет в модели - context.IceCreamComponents.RemoveRange(iceCreamComponents.Where(rec => !model.IceCreamComponents.ContainsKey(rec.ComponentId))); - context.SaveChanges(); - // обновили количество у существующих записей - foreach (var updateComponent in iceCreamComponents) - { - updateComponent.Count = model.IceCreamComponents[updateComponent.ComponentId].Item2; - model.IceCreamComponents.Remove(updateComponent.ComponentId); - } - context.SaveChanges(); - } - var iceCream = context.IceCreams.First(x => x.Id == Id); - foreach (var ic in model.IceCreamComponents) - { - context.IceCreamComponents.Add(new IceCreamComponent - { - IceCream = iceCream, - Component = context.Components.First(x => x.Id == ic.Key), - Count = ic.Value.Item2 - }); - context.SaveChanges(); - } - _iceCreamComponents = null; - } - } -} \ No newline at end of file diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Models/IceCreamComponent.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Models/IceCreamComponent.cs deleted file mode 100644 index 50ba298..0000000 --- a/IceCreamShop/IceCreamShopDatabaseImplement/Models/IceCreamComponent.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System.ComponentModel.DataAnnotations; - -namespace IceCreamShopDatabaseImplement.Models -{ - public class IceCreamComponent - { - public int Id { get; set; } - - [Required] - public int IceCreamId { get; set; } - - [Required] - public int ComponentId { get; set; } - - [Required] - public int Count { get; set; } - - public virtual Component Component { get; set; } = new(); - - public virtual IceCream IceCream { get; set; } = new(); - } -} \ No newline at end of file diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Models/Order.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Models/Order.cs deleted file mode 100644 index 76edee3..0000000 --- a/IceCreamShop/IceCreamShopDatabaseImplement/Models/Order.cs +++ /dev/null @@ -1,72 +0,0 @@ -using IceCreamShopContracts.BindingModels; -using IceCreamShopContracts.ViewModels; -using IceCreamShopDataModels.Enums; -using IceCreamShopDataModels.Models; -using System.ComponentModel.DataAnnotations; - -namespace IceCreamShopDatabaseImplement.Models -{ - public class Order : IOrderModel - { - public int Id { get; set; } - - [Required] - public int IceCreamId { get; set; } - - [Required] - public int Count { get; set; } - - [Required] - public double Sum { get; set; } - - [Required] - public OrderStatus Status { get; set; } - - [Required] - public DateTime DateCreate { get; set; } - - public DateTime? DateImplement { get; set; } - - public virtual IceCream IceCream { get; set; } - - public static Order? Create(OrderBindingModel? model) - { - if (model == null) - { - return null; - } - return new Order() - { - Id = model.Id, - IceCreamId = model.IceCreamId, - Count = model.Count, - Sum = model.Sum, - Status = model.Status, - DateCreate = model.DateCreate, - DateImplement = model.DateImplement - }; - } - - public void Update(OrderBindingModel? model) - { - if (model == null) - { - return; - } - Status = model.Status; - DateImplement = model.DateImplement; - } - - public OrderViewModel GetViewModel => new() - { - Id = Id, - IceCreamId = IceCreamId, - Count = Count, - Sum = Sum, - Status = Status, - DateCreate = DateCreate, - DateImplement = DateImplement, - IceCreamName = IceCream.IceCreamName - }; - } -} \ No newline at end of file diff --git a/IceCreamShop/IceCreamShopView/IceCreamShopView.csproj b/IceCreamShop/IceCreamShopView/IceCreamShopView.csproj index a816493..1456ac2 100644 --- a/IceCreamShop/IceCreamShopView/IceCreamShopView.csproj +++ b/IceCreamShop/IceCreamShopView/IceCreamShopView.csproj @@ -19,10 +19,6 @@ - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - @@ -31,7 +27,6 @@ - diff --git a/IceCreamShop/IceCreamShopView/Program.cs b/IceCreamShop/IceCreamShopView/Program.cs index 50eb3ce..b5b86d8 100644 --- a/IceCreamShop/IceCreamShopView/Program.cs +++ b/IceCreamShop/IceCreamShopView/Program.cs @@ -1,7 +1,7 @@ using IceCreamShopBusinessLogic.BusinessLogics; using IceCreamShopContracts.BusinessLogicsContracts; using IceCreamShopContracts.StoragesContracts; -using IceCreamShopDatabaseImplement.Implements; +using IceCreamShopFileImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; -- 2.25.1