diff --git a/SushiBar/SushiBar/Forms/FormShop.Designer.cs b/SushiBar/SushiBar/Forms/FormShop.Designer.cs new file mode 100644 index 0000000..0c92cbf --- /dev/null +++ b/SushiBar/SushiBar/Forms/FormShop.Designer.cs @@ -0,0 +1,178 @@ +namespace SushiBar.Forms +{ + 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() + { + ButtonSave = new Button(); + ButtonCancel = new Button(); + label1 = new Label(); + label2 = new Label(); + textBoxName = new TextBox(); + textBoxAddress = new TextBox(); + dataGridView = new DataGridView(); + Id = new DataGridViewTextBoxColumn(); + SushiName = new DataGridViewTextBoxColumn(); + Sum = new DataGridViewTextBoxColumn(); + Count = new DataGridViewTextBoxColumn(); + ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); + SuspendLayout(); + // + // ButtonSave + // + ButtonSave.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; + ButtonSave.Location = new Point(536, 418); + ButtonSave.Name = "ButtonSave"; + ButtonSave.Size = new Size(123, 29); + ButtonSave.TabIndex = 0; + ButtonSave.Text = "Сохранить"; + ButtonSave.UseVisualStyleBackColor = true; + ButtonSave.Click += ButtonSave_Click; + // + // ButtonCancel + // + ButtonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; + ButtonCancel.Location = new Point(665, 418); + ButtonCancel.Name = "ButtonCancel"; + ButtonCancel.Size = new Size(123, 29); + ButtonCancel.TabIndex = 1; + ButtonCancel.Text = "Отмена"; + ButtonCancel.UseVisualStyleBackColor = true; + // + // label1 + // + label1.AutoSize = true; + label1.Location = new Point(22, 28); + label1.Name = "label1"; + label1.Size = new Size(77, 20); + label1.TabIndex = 2; + label1.Text = "Название\r\n"; + // + // label2 + // + label2.AutoSize = true; + label2.Location = new Point(22, 71); + label2.Name = "label2"; + label2.Size = new Size(51, 20); + label2.TabIndex = 3; + label2.Text = "Адрес"; + // + // textBoxName + // + textBoxName.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + textBoxName.Location = new Point(150, 25); + textBoxName.Name = "textBoxName"; + textBoxName.Size = new Size(631, 27); + textBoxName.TabIndex = 5; + // + // textBoxAddress + // + textBoxAddress.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + textBoxAddress.Location = new Point(150, 68); + textBoxAddress.Name = "textBoxAddress"; + textBoxAddress.Size = new Size(631, 27); + textBoxAddress.TabIndex = 6; + // + // dataGridView + // + dataGridView.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right; + dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + dataGridView.Columns.AddRange(new DataGridViewColumn[] { Id, SushiName, Sum, Count }); + dataGridView.Location = new Point(23, 111); + dataGridView.Name = "dataGridView"; + dataGridView.RowHeadersWidth = 51; + dataGridView.RowTemplate.Height = 29; + dataGridView.Size = new Size(759, 302); + dataGridView.TabIndex = 7; + // + // Id + // + Id.HeaderText = "Id"; + Id.MinimumWidth = 6; + Id.Name = "Id"; + Id.Visible = false; + Id.Width = 125; + // + // SushiName + // + SushiName.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + SushiName.HeaderText = "Суши"; + SushiName.MinimumWidth = 6; + SushiName.Name = "SushiName"; + SushiName.ReadOnly = true; + // + // Sum + // + Sum.HeaderText = "Стоимость"; + Sum.MinimumWidth = 6; + Sum.Name = "Sum"; + Sum.ReadOnly = true; + Sum.Width = 125; + // + // Count + // + Count.HeaderText = "Количество"; + Count.MinimumWidth = 6; + Count.Name = "Count"; + Count.ReadOnly = true; + Count.Width = 125; + // + // FormShop + // + AllowDrop = true; + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(793, 453); + Controls.Add(dataGridView); + Controls.Add(textBoxAddress); + Controls.Add(textBoxName); + Controls.Add(label2); + Controls.Add(label1); + Controls.Add(ButtonCancel); + Controls.Add(ButtonSave); + Name = "FormShop"; + Text = "Форма магазина"; + ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private Button ButtonSave; + private Button ButtonCancel; + private Label label1; + private Label label2; + private TextBox textBoxName; + private TextBox textBoxAddress; + private DataGridView dataGridView; + private DataGridViewTextBoxColumn Id; + private DataGridViewTextBoxColumn SushiName; + private DataGridViewTextBoxColumn Sum; + private DataGridViewTextBoxColumn Count; + } +} \ No newline at end of file diff --git a/SushiBar/SushiBar/Forms/FormShop.cs b/SushiBar/SushiBar/Forms/FormShop.cs new file mode 100644 index 0000000..a1709ab --- /dev/null +++ b/SushiBar/SushiBar/Forms/FormShop.cs @@ -0,0 +1,60 @@ +using Microsoft.Extensions.Logging; +using SushiBarContracts.BindingModels; +using SushiBarContracts.BusinessLogicsContracts; +using SushiBarDataModels.Models; + +namespace SushiBar.Forms +{ + public partial class FormShop : Form + { + private readonly ILogger _logger; + private readonly IShopLogic _logic; + public int? _id; + private Dictionary _sushis; + public FormShop(ILogger logger, IShopLogic logic) + { + _logger = logger; + _logic = logic; + InitializeComponent(); + } + + 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; + } + _logger.LogInformation("Сохранение магазина"); + try + { + var model = new ShopBindingModel + { + Id = _id ?? 0, + ShopName = textBoxName.Text, + Address = textBoxAddress.Text, + DateCreate = DateTime.Now, + ShopSushi = _sushis + }; + var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model); + if (!operationResult) + { + throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); + } + MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + Close(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка сохранения магазина"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } +} diff --git a/SushiBar/SushiBar/Forms/FormShop.resx b/SushiBar/SushiBar/Forms/FormShop.resx new file mode 100644 index 0000000..2b321a8 --- /dev/null +++ b/SushiBar/SushiBar/Forms/FormShop.resx @@ -0,0 +1,144 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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 + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + \ No newline at end of file diff --git a/SushiBar/SushiBar/Forms/FormShops.Designer.cs b/SushiBar/SushiBar/Forms/FormShops.Designer.cs new file mode 100644 index 0000000..5145450 --- /dev/null +++ b/SushiBar/SushiBar/Forms/FormShops.Designer.cs @@ -0,0 +1,116 @@ +namespace SushiBar.Forms +{ + 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(); + buttonCreate = new Button(); + buttonUpdate = new Button(); + buttonRemove = new Button(); + buttonRef = new Button(); + ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); + SuspendLayout(); + // + // dataGridView + // + dataGridView.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right; + dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + dataGridView.Location = new Point(12, 12); + dataGridView.Name = "dataGridView"; + dataGridView.RowHeadersWidth = 51; + dataGridView.RowTemplate.Height = 29; + dataGridView.Size = new Size(606, 426); + dataGridView.TabIndex = 0; + // + // buttonCreate + // + buttonCreate.Anchor = AnchorStyles.Top | AnchorStyles.Right; + buttonCreate.Location = new Point(635, 12); + buttonCreate.Name = "buttonCreate"; + buttonCreate.Size = new Size(153, 29); + buttonCreate.TabIndex = 1; + buttonCreate.Text = "Добавить"; + buttonCreate.UseVisualStyleBackColor = true; + // + // buttonUpdate + // + buttonUpdate.Anchor = AnchorStyles.Top | AnchorStyles.Right; + buttonUpdate.Location = new Point(635, 56); + buttonUpdate.Name = "buttonUpdate"; + buttonUpdate.Size = new Size(153, 29); + buttonUpdate.TabIndex = 2; + buttonUpdate.Text = "Изменить"; + buttonUpdate.UseVisualStyleBackColor = true; + // + // buttonRemove + // + buttonRemove.Anchor = AnchorStyles.Top | AnchorStyles.Right; + buttonRemove.Location = new Point(635, 102); + buttonRemove.Name = "buttonRemove"; + buttonRemove.Size = new Size(153, 29); + buttonRemove.TabIndex = 3; + buttonRemove.Text = "Удалить"; + buttonRemove.UseVisualStyleBackColor = true; + // + // buttonRef + // + buttonRef.Anchor = AnchorStyles.Top | AnchorStyles.Right; + buttonRef.Location = new Point(635, 146); + buttonRef.Name = "buttonRef"; + buttonRef.Size = new Size(153, 29); + buttonRef.TabIndex = 4; + buttonRef.Text = "Обновить"; + buttonRef.UseVisualStyleBackColor = true; + // + // FormShops + // + AllowDrop = true; + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(800, 450); + Controls.Add(buttonRef); + Controls.Add(buttonRemove); + Controls.Add(buttonUpdate); + Controls.Add(buttonCreate); + Controls.Add(dataGridView); + Name = "FormShops"; + Text = "FormShops"; + Load += FormShops_Load; + ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); + ResumeLayout(false); + } + + #endregion + + private DataGridView dataGridView; + private Button buttonCreate; + private Button buttonUpdate; + private Button buttonRemove; + private Button buttonRef; + } +} \ No newline at end of file diff --git a/SushiBar/SushiBar/Forms/FormShops.cs b/SushiBar/SushiBar/Forms/FormShops.cs new file mode 100644 index 0000000..df25fef --- /dev/null +++ b/SushiBar/SushiBar/Forms/FormShops.cs @@ -0,0 +1,54 @@ +using Microsoft.Extensions.Logging; +using SushiBarContracts.BusinessLogicsContracts; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace SushiBar.Forms +{ + 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 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["Address"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + dataGridView.Columns["DateOpen"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + dataGridView.Columns["ShopIceCreams"].Visible = false; + } + _logger.LogInformation("Загрузка магазинов"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки магазинов"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void FormShops_Load(object sender, EventArgs e) + { + + } + } +} diff --git a/SushiBar/SushiBar/Forms/FormShops.resx b/SushiBar/SushiBar/Forms/FormShops.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/SushiBar/SushiBar/Forms/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/SushiBar/SushiBarBusinessLogic/BusinessLogics/ShopLogic.cs b/SushiBar/SushiBarBusinessLogic/BusinessLogics/ShopLogic.cs index a6235b2..ed34557 100644 --- a/SushiBar/SushiBarBusinessLogic/BusinessLogics/ShopLogic.cs +++ b/SushiBar/SushiBarBusinessLogic/BusinessLogics/ShopLogic.cs @@ -4,7 +4,6 @@ using SushiBarContracts.BusinessLogicsContracts; using SushiBarContracts.SearchModels; using SushiBarContracts.StoragesContracts; using SushiBarContracts.ViewModels; -using SushiBarDataModels.Models; namespace SushiBarBusinessLogic.BusinessLogics { @@ -91,6 +90,11 @@ namespace SushiBarBusinessLogic.BusinessLogics { throw new ArgumentException("Нет названия магазина", nameof(model.ShopName)); } + if (string.IsNullOrEmpty(model.Address)) + { + throw new ArgumentNullException("Нет адресса магазина", nameof(model.ShopName)); + } + _logger.LogTrace("Shop. ShopName:{ShopName}.Address:{Address}. DateCreate:{DateCreate}. Id: { Id}", model.ShopName, model.Address, model.DateCreate, model.Id); var element = _shopStorage.GetElement(new ShopSearchModel { ShopName = model.ShopName @@ -100,15 +104,37 @@ namespace SushiBarBusinessLogic.BusinessLogics throw new InvalidOperationException("Магазин с таким названием уже есть"); } } - - public bool supplySushi(ShopBindingModel shopModel, SushiBindingModel sushiModel) + public bool MakeSupply(ShopSearchModel model, SushiBindingModel sushi, int count) { - CheckModel(shopModel, false); - ShopViewModel? shop = _shopStorage.GetElement(new ShopSearchModel { Id = shopModel.Id}); + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (sushi == null){ + throw new ArgumentNullException(nameof(sushi)); + } + ShopViewModel? shop = _shopStorage.GetElement(new ShopSearchModel { Id = model.Id }); if (shop == null) { - return false; + throw new ArgumentNullException(nameof(shop)); } - ShopViewModel? shop = _shopStorage.Update(S) + if (shop.ShopSushi.TryGetValue(sushi.Id, out var pair)) + { + shop.ShopSushi[sushi.Id] = (pair.Item1, pair.Item2 + count); + } + else + { + shop.ShopSushi.Add(sushi.Id, (sushi, count)); + } + Update(new() + { + Id = shop.Id, + ShopName = shop.ShopName, + DateCreate = shop.DateCreate, + Address = shop.Address, + ShopSushi = shop.ShopSushi + }); + return true; + } } -} +} \ No newline at end of file diff --git a/SushiBar/SushiBarContracts/BusinessLogicsContracts/IShopLogic.cs b/SushiBar/SushiBarContracts/BusinessLogicsContracts/IShopLogic.cs index cdbc503..9e8eb73 100644 --- a/SushiBar/SushiBarContracts/BusinessLogicsContracts/IShopLogic.cs +++ b/SushiBar/SushiBarContracts/BusinessLogicsContracts/IShopLogic.cs @@ -11,6 +11,6 @@ namespace SushiBarContracts.BusinessLogicsContracts bool Create(ShopBindingModel model); bool Update(ShopBindingModel model); bool Delete(ShopBindingModel model); - bool supplySushi(ShopBindingModel shopModel, SushiBindingModel sushiModel, int count); + bool MakeSupply(ShopSearchModel model, SushiBindingModel sushi, int count); } } diff --git a/SushiBar/SushiBarContracts/ViewModels/ShopViewModel.cs b/SushiBar/SushiBarContracts/ViewModels/ShopViewModel.cs index e067415..6532c86 100644 --- a/SushiBar/SushiBarContracts/ViewModels/ShopViewModel.cs +++ b/SushiBar/SushiBarContracts/ViewModels/ShopViewModel.cs @@ -1,4 +1,5 @@ -using System.ComponentModel; +using SushiBarDataModels.Models; +using System.ComponentModel; namespace SushiBarContracts.ViewModels { @@ -12,5 +13,6 @@ namespace SushiBarContracts.ViewModels public string Address { get; set; } = string.Empty; [DisplayName("Дата открытия")] public DateTime DateCreate { get; set; } = DateTime.Now; + public Dictionary ShopSushi { get; set; } = new(); } } diff --git a/SushiBar/SushiBarListImplement/DataListSingleton.cs b/SushiBar/SushiBarListImplement/DataListSingleton.cs index 4844ad6..12eabea 100644 --- a/SushiBar/SushiBarListImplement/DataListSingleton.cs +++ b/SushiBar/SushiBarListImplement/DataListSingleton.cs @@ -4,7 +4,7 @@ namespace SushiBarListImplement { public class DataListSingleton { - private static DataListSingleton _instance; + private static DataListSingleton? _instance; public List Components { get; set; } public List Orders { get; set; } public List Sushis { get; set; } diff --git a/SushiBar/SushiBarListImplement/Implements/ShopStorage.cs b/SushiBar/SushiBarListImplement/Implements/ShopStorage.cs index 4be4f2d..ec3deef 100644 --- a/SushiBar/SushiBarListImplement/Implements/ShopStorage.cs +++ b/SushiBar/SushiBarListImplement/Implements/ShopStorage.cs @@ -2,10 +2,7 @@ using SushiBarContracts.SearchModels; using SushiBarContracts.StoragesContracts; using SushiBarContracts.ViewModels; -using SushiBarDataModels.Models; using SushiBarListImplement.Models; -using System.ComponentModel.Design; -using System.Reflection; namespace SushiBarListImplement.Implements { @@ -101,33 +98,5 @@ namespace SushiBarListImplement.Implements } return null; } - - public ShopViewModel? supplySushi(ShopBindingModel model) - { - int id = -1; - for (int i = 0; i < _source.Shops.Count; ++i) - { - if (_source.Shops[i].Id == model.Id) - { - id = i; - break; - } - } - if (id < 0) - { - return null; - } - model.ShopName = _source.Shops[id].ShopName; - model.Address = _source.Shops[id].Address; - model.DateCreate = _source.Shops[id].DateCreate; - foreach (var key in model.ShopSushi.Keys) - { - if (_source.Shops[id].ShopSushi.ContainsKey(key)){ - model.ShopSushi[key] = (model.ShopSushi[key].Item1, model.ShopSushi[key].Item2 + _source.Shops[id].ShopSushi[key].Item2); - } - } - _source.Shops[id].Update(model); - return _source.Shops[id].GetViewModel; - } } } diff --git a/SushiBar/SushiBarListImplement/Models/Shop.cs b/SushiBar/SushiBarListImplement/Models/Shop.cs index 3bc0cd2..034a382 100644 --- a/SushiBar/SushiBarListImplement/Models/Shop.cs +++ b/SushiBar/SushiBarListImplement/Models/Shop.cs @@ -47,6 +47,7 @@ namespace SushiBarListImplement.Models ShopName = ShopName, Address = Address, DateCreate = DateCreate, + ShopSushi = ShopSushi }; } }