diff --git a/FlowerShopBusinessLogic/ShopLogic.cs b/FlowerShopBusinessLogic/ShopLogic.cs new file mode 100644 index 0000000..9e064d1 --- /dev/null +++ b/FlowerShopBusinessLogic/ShopLogic.cs @@ -0,0 +1,159 @@ +using FlowerShopContracts.BindingModels; +using FlowerShopContracts.BusinessLogicsContracts; +using FlowerShopContracts.SearchModels; +using FlowerShopContracts.StoragesContracts; +using FlowerShopContracts.ViewModels; +using FlowerShopDataModels.Models; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FlowerShopBusinessLogic +{ + 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:{Name}. Id:{ Id}", model?.Name, 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 bool MakeSupply(ShopSearchModel model, IFlowerModel flower, int count) + { + if (model == null) + throw new ArgumentNullException(nameof(model)); + if (flower == null) + throw new ArgumentNullException(nameof(flower)); + if (count <= 0) + throw new ArgumentNullException("Количество должно быть положительным числом"); + + var curModel = _shopStorage.GetElement(model); + if (curModel == null) + throw new ArgumentNullException(nameof(curModel)); + if (curModel.ShopFlowers.TryGetValue(flower.Id, out var pair)) + { + curModel.ShopFlowers[flower.Id] = (pair.Item1, pair.Item2 + count); + } + else + { + curModel.ShopFlowers.Add(flower.Id, (flower, count)); + } + Update(new() + { + Id = curModel.Id, + ShopName = curModel.ShopName, + DateOpen = curModel.DateOpen, + Address = curModel.Address, + ShopFlowers = curModel.ShopFlowers, + }); + return true; + } + + public ShopViewModel ReadElement(ShopSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. ShopName:{ShopName}.Id:{ Id}", model.Name, 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; + } + + 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.ShopName)); + } + if (model.DateOpen == null) + { + throw new ArgumentNullException("Нет даты открытия магазина", + nameof(model.ShopName)); + } + _logger.LogInformation("Shop. ShopName:{ShopName}.Address:{Address}. DateOpen:{DateOpen}. Id: { Id}", model.ShopName, model.Address, model.DateOpen, model.Id); + var element = _shopStorage.GetElement(new ShopSearchModel + { + Name = model.ShopName + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Магазин с таким названием уже есть"); + } + } + } +} diff --git a/FlowerShopContracts/BindingModels/ShopBindingModel.cs b/FlowerShopContracts/BindingModels/ShopBindingModel.cs new file mode 100644 index 0000000..9afd7ed --- /dev/null +++ b/FlowerShopContracts/BindingModels/ShopBindingModel.cs @@ -0,0 +1,18 @@ +using FlowerShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FlowerShopContracts.BindingModels +{ + public class ShopBindingModel : IShopModel + { + public int Id { get; set; } + public string ShopName { get; set; } + public string Address { get; set; } + public DateTime DateOpen { get; set; } + public Dictionary ShopFlowers { get; set; } = new(); + } +} diff --git a/FlowerShopContracts/BusinessLogicsContracts/IShopLogic.cs b/FlowerShopContracts/BusinessLogicsContracts/IShopLogic.cs new file mode 100644 index 0000000..c9a8e57 --- /dev/null +++ b/FlowerShopContracts/BusinessLogicsContracts/IShopLogic.cs @@ -0,0 +1,22 @@ +using FlowerShopContracts.BindingModels; +using FlowerShopContracts.SearchModels; +using FlowerShopContracts.ViewModels; +using FlowerShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FlowerShopContracts.BusinessLogicsContracts +{ + public interface IShopLogic + { + List? ReadList(ShopSearchModel? model); + ShopViewModel? ReadElement(ShopSearchModel model); + bool Create(ShopBindingModel model); + bool Update(ShopBindingModel model); + bool Delete(ShopBindingModel model); + bool MakeSupply(ShopSearchModel model, IFlowerModel flower, int count); + } +} diff --git a/FlowerShopContracts/SearchModels/ShopSearchModel.cs b/FlowerShopContracts/SearchModels/ShopSearchModel.cs new file mode 100644 index 0000000..8a99201 --- /dev/null +++ b/FlowerShopContracts/SearchModels/ShopSearchModel.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FlowerShopContracts.SearchModels +{ + public class ShopSearchModel + { + public int? Id { get; set; } + public string? Name { get; set; } + } +} diff --git a/FlowerShopContracts/StoragesContracts/IShopStorage.cs b/FlowerShopContracts/StoragesContracts/IShopStorage.cs new file mode 100644 index 0000000..671f8c8 --- /dev/null +++ b/FlowerShopContracts/StoragesContracts/IShopStorage.cs @@ -0,0 +1,22 @@ +using FlowerShopContracts.ViewModels; +using FlowerShopContracts.SearchModels; +using FlowerShopContracts.BindingModels; +using FlowerShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FlowerShopContracts.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/FlowerShopContracts/ViewModels/ShopViewModel.cs b/FlowerShopContracts/ViewModels/ShopViewModel.cs new file mode 100644 index 0000000..bb7556f --- /dev/null +++ b/FlowerShopContracts/ViewModels/ShopViewModel.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using FlowerShopDataModels.Models; + +namespace FlowerShopContracts.ViewModels +{ + public class ShopViewModel : IShopModel + { + public int Id { get; set; } + [DisplayName("Название магазина")] + public string ShopName { get; set; } + [DisplayName("Адрес магазина")] + public string Address { get; set; } + [DisplayName("Дата открытия")] + public DateTime DateOpen { get; set; } + public Dictionary ShopFlowers { get; set; } = new(); + } +} diff --git a/FlowerShopDataModels/IShopModel.cs b/FlowerShopDataModels/IShopModel.cs new file mode 100644 index 0000000..54bba9f --- /dev/null +++ b/FlowerShopDataModels/IShopModel.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FlowerShopDataModels.Models +{ + public interface IShopModel : IId + { + string ShopName { get; } + string Address { get; } + DateTime DateOpen { get; } + Dictionary ShopFlowers { get; } + } +} diff --git a/FlowerShopListImplement/DataListSingleton.cs b/FlowerShopListImplement/DataListSingleton.cs index 117b155..92f37db 100644 --- a/FlowerShopListImplement/DataListSingleton.cs +++ b/FlowerShopListImplement/DataListSingleton.cs @@ -13,11 +13,13 @@ namespace FlowerShopListImplement public List Components { get; set; } public List Orders { get; set; } public List Flowers { get; set; } + public List Shops { get; set; } private DataListSingleton() { Components = new List(); Orders = new List(); Flowers = new List(); + Shops = new List(); } public static DataListSingleton GetInstance() { diff --git a/FlowerShopListImplement/Shop.cs b/FlowerShopListImplement/Shop.cs new file mode 100644 index 0000000..e7bba4a --- /dev/null +++ b/FlowerShopListImplement/Shop.cs @@ -0,0 +1,55 @@ +using FlowerShopContracts.BindingModels; +using FlowerShopContracts.ViewModels; +using FlowerShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FlowerShopListImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; private set; } + public string ShopName { get; private set; } + public string Address { get; private set; } + public DateTime DateOpen { get; private set; } + public Dictionary ShopFlowers { get; private set; } = new(); + + public static Shop? Create(ShopBindingModel model) + { + if (model == null) + return null; + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Address = model.Address, + DateOpen = model.DateOpen, + ShopFlowers = new() + }; + } + + public void Update(ShopBindingModel? model) + { + if (model == null) + { + return; + } + ShopName = model.ShopName; + Address = model.Address; + DateOpen = model.DateOpen; + ShopFlowers = model.ShopFlowers; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + DateOpen = DateOpen, + ShopFlowers = ShopFlowers + }; + } +} diff --git a/FlowerShopListImplement/ShopStorage.cs b/FlowerShopListImplement/ShopStorage.cs new file mode 100644 index 0000000..a97253b --- /dev/null +++ b/FlowerShopListImplement/ShopStorage.cs @@ -0,0 +1,110 @@ +using FlowerShopContracts.BindingModels; +using FlowerShopContracts.SearchModels; +using FlowerShopContracts.StoragesContracts; +using FlowerShopContracts.ViewModels; +using FlowerShopDataModels.Models; +using FlowerShopListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FlowerShopListImplement.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.Name)) + { + return result; + } + foreach (var shop in _source.Shops) + { + if (shop.ShopName.Contains(model.Name)) + { + result.Add(shop.GetViewModel); + } + } + return result; + } + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + { + return null; + } + foreach (var shop in _source.Shops) + { + if ((!string.IsNullOrEmpty(model.Name) && + shop.ShopName == model.Name) || + (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/ProjectFlowerShop/MainForm.Designer.cs b/ProjectFlowerShop/MainForm.Designer.cs index 56eb0e9..40db63c 100644 --- a/ProjectFlowerShop/MainForm.Designer.cs +++ b/ProjectFlowerShop/MainForm.Designer.cs @@ -32,6 +32,8 @@ ToolStripMenu = new ToolStripMenuItem(); КомпонентыStripMenuItem = new ToolStripMenuItem(); ЦветыStripMenuItem = new ToolStripMenuItem(); + магазиныToolStripMenuItem = new ToolStripMenuItem(); + поставкиToolStripMenuItem = new ToolStripMenuItem(); DataGridView = new DataGridView(); CreateOrderButton = new Button(); TakeInWorkButton = new Button(); @@ -54,7 +56,7 @@ // // ToolStripMenu // - ToolStripMenu.DropDownItems.AddRange(new ToolStripItem[] { КомпонентыStripMenuItem, ЦветыStripMenuItem }); + ToolStripMenu.DropDownItems.AddRange(new ToolStripItem[] { КомпонентыStripMenuItem, ЦветыStripMenuItem, магазиныToolStripMenuItem, поставкиToolStripMenuItem }); ToolStripMenu.Name = "ToolStripMenu"; ToolStripMenu.Size = new Size(117, 24); ToolStripMenu.Text = "Справочники"; @@ -73,6 +75,20 @@ ЦветыStripMenuItem.Text = "Цветы"; ЦветыStripMenuItem.Click += ЦветыStripMenuItem_Click; // + // магазиныToolStripMenuItem + // + магазиныToolStripMenuItem.Name = "магазиныToolStripMenuItem"; + магазиныToolStripMenuItem.Size = new Size(224, 26); + магазиныToolStripMenuItem.Text = "Магазины"; + магазиныToolStripMenuItem.Click += магазиныToolStripMenuItem_Click; + // + // поставкиToolStripMenuItem + // + поставкиToolStripMenuItem.Name = "поставкиToolStripMenuItem"; + поставкиToolStripMenuItem.Size = new Size(224, 26); + поставкиToolStripMenuItem.Text = "Поставки"; + поставкиToolStripMenuItem.Click += поставкиToolStripMenuItem_Click; + // // DataGridView // DataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; @@ -167,5 +183,7 @@ private Button ReadyButton; private Button IssuedButton; private Button RefreshButton; + private ToolStripMenuItem магазиныToolStripMenuItem; + private ToolStripMenuItem поставкиToolStripMenuItem; } } \ No newline at end of file diff --git a/ProjectFlowerShop/MainForm.cs b/ProjectFlowerShop/MainForm.cs index 13fab38..7b6b80d 100644 --- a/ProjectFlowerShop/MainForm.cs +++ b/ProjectFlowerShop/MainForm.cs @@ -37,6 +37,7 @@ namespace ProjectFlowerShop } + private void MainForm_Load(object sender, EventArgs e) { LoadData(); @@ -179,5 +180,23 @@ namespace ProjectFlowerShop { LoadData(); } + + private void магазиныToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(ShopsForm)); + if (service is ShopsForm form) + { + form.ShowDialog(); + } + } + + private void поставкиToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(SupplyForm)); + if (service is SupplyForm form) + { + form.ShowDialog(); + } + } } } diff --git a/ProjectFlowerShop/Program.cs b/ProjectFlowerShop/Program.cs index 67a1281..19c9645 100644 --- a/ProjectFlowerShop/Program.cs +++ b/ProjectFlowerShop/Program.cs @@ -8,6 +8,7 @@ using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; using System; using System.Drawing; +using FlowerShopBusinessLogic; namespace ProjectFlowerShop { @@ -40,6 +41,8 @@ namespace ProjectFlowerShop services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); @@ -47,6 +50,9 @@ namespace ProjectFlowerShop services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); } } diff --git a/ProjectFlowerShop/ShopForm.Designer.cs b/ProjectFlowerShop/ShopForm.Designer.cs new file mode 100644 index 0000000..ffdac20 --- /dev/null +++ b/ProjectFlowerShop/ShopForm.Designer.cs @@ -0,0 +1,193 @@ +namespace ProjectFlowerShop +{ + partial class ShopForm + { + /// + /// 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(); + buttonSave = new Button(); + buttonCancel = new Button(); + textBoxName = new TextBox(); + textBoxAddress = new TextBox(); + labelName = new Label(); + labelAddress = new Label(); + DateTimePicker = new DateTimePicker(); + labelDate = new Label(); + ColumnID = new DataGridViewTextBoxColumn(); + Name = new DataGridViewTextBoxColumn(); + Price = new DataGridViewTextBoxColumn(); + Number = new DataGridViewTextBoxColumn(); + ((System.ComponentModel.ISupportInitialize)DataGridView).BeginInit(); + SuspendLayout(); + // + // DataGridView + // + DataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + DataGridView.Columns.AddRange(new DataGridViewColumn[] { ColumnID, Name, Price, Number }); + DataGridView.Location = new Point(21, 12); + DataGridView.Name = "DataGridView"; + DataGridView.RowHeadersWidth = 51; + DataGridView.RowTemplate.Height = 29; + DataGridView.Size = new Size(397, 305); + DataGridView.TabIndex = 0; + // + // buttonSave + // + buttonSave.Location = new Point(424, 288); + buttonSave.Name = "buttonSave"; + buttonSave.Size = new Size(123, 29); + buttonSave.TabIndex = 1; + buttonSave.Text = "Сохранить"; + buttonSave.UseVisualStyleBackColor = true; + buttonSave.Click += buttonSave_Click; + // + // buttonCancel + // + buttonCancel.Location = new Point(553, 288); + buttonCancel.Name = "buttonCancel"; + buttonCancel.Size = new Size(116, 29); + buttonCancel.TabIndex = 2; + buttonCancel.Text = "Отмена"; + buttonCancel.UseVisualStyleBackColor = true; + buttonCancel.Click += buttonCancel_Click; + // + // textBoxName + // + textBoxName.Location = new Point(424, 34); + textBoxName.Name = "textBoxName"; + textBoxName.Size = new Size(245, 27); + textBoxName.TabIndex = 3; + // + // textBoxAddress + // + textBoxAddress.Location = new Point(424, 95); + textBoxAddress.Name = "textBoxAddress"; + textBoxAddress.Size = new Size(245, 27); + textBoxAddress.TabIndex = 4; + // + // labelName + // + labelName.AutoSize = true; + labelName.Location = new Point(424, 12); + labelName.Name = "labelName"; + labelName.Size = new Size(77, 20); + labelName.TabIndex = 5; + labelName.Text = "Название"; + // + // labelAddress + // + labelAddress.AutoSize = true; + labelAddress.Location = new Point(424, 72); + labelAddress.Name = "labelAddress"; + labelAddress.Size = new Size(51, 20); + labelAddress.TabIndex = 6; + labelAddress.Text = "Адрес"; + // + // DateTimePicker + // + DateTimePicker.Location = new Point(424, 148); + DateTimePicker.Name = "DateTimePicker"; + DateTimePicker.Size = new Size(245, 27); + DateTimePicker.TabIndex = 7; + // + // labelDate + // + labelDate.AutoSize = true; + labelDate.Location = new Point(424, 125); + labelDate.Name = "labelDate"; + labelDate.Size = new Size(41, 20); + labelDate.TabIndex = 8; + labelDate.Text = "Дата"; + // + // ColumnID + // + ColumnID.HeaderText = "ColumnID"; + ColumnID.MinimumWidth = 6; + ColumnID.Name = "ColumnID"; + ColumnID.Visible = false; + ColumnID.Width = 125; + // + // Name + // + Name.HeaderText = "Название"; + Name.MinimumWidth = 6; + Name.Name = "Name"; + Name.Width = 125; + // + // Price + // + Price.HeaderText = "Цена"; + Price.MinimumWidth = 6; + Price.Name = "Price"; + Price.Width = 125; + // + // Number + // + Number.HeaderText = "Количество"; + Number.MinimumWidth = 6; + Number.Name = "Number"; + Number.Width = 125; + // + // ShopForm + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(681, 329); + Controls.Add(labelDate); + Controls.Add(DateTimePicker); + Controls.Add(labelAddress); + Controls.Add(labelName); + Controls.Add(textBoxAddress); + Controls.Add(textBoxName); + Controls.Add(buttonCancel); + Controls.Add(buttonSave); + Controls.Add(DataGridView); + //Name = "ShopForm"; + Text = "ShopForm"; + Load += ShopForm_Load; + ((System.ComponentModel.ISupportInitialize)DataGridView).EndInit(); + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private DataGridView DataGridView; + private Button buttonSave; + private Button buttonCancel; + private TextBox textBoxName; + private TextBox textBoxAddress; + private Label labelName; + private Label labelAddress; + private DateTimePicker DateTimePicker; + private Label labelDate; + private DataGridViewTextBoxColumn ColumnID; + private DataGridViewTextBoxColumn Name; + private DataGridViewTextBoxColumn Price; + private DataGridViewTextBoxColumn Number; + } +} \ No newline at end of file diff --git a/ProjectFlowerShop/ShopForm.cs b/ProjectFlowerShop/ShopForm.cs new file mode 100644 index 0000000..90f1a02 --- /dev/null +++ b/ProjectFlowerShop/ShopForm.cs @@ -0,0 +1,124 @@ +using FlowerShopContracts.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; +using FlowerShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using FlowerShopContracts.BindingModels; +using FlowerShopContracts.SearchModels; + + +namespace ProjectFlowerShop +{ + public partial class ShopForm : Form + { + private readonly ILogger _logger; + private readonly IShopLogic _logic; + public int? _id; + private Dictionary _flowers; + public ShopForm(ILogger logger, IShopLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + } + + private void LoadData() + { + _logger.LogInformation("Загрузка товаров магазина"); + try + { + if (_flowers != null) + { + foreach (var flower in _flowers) + { + DataGridView.Rows.Add(new object[] { flower.Key, flower.Value.Item1.FlowerName, flower.Value.Item1.Price, flower.Value.Item2 }); + } + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки изделий магазина"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + + private void buttonSave_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxName.Text)) + { + MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (string.IsNullOrEmpty(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, + DateOpen = DateTimePicker.Value.Date, + ShopFlowers = _flowers + }; + var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model); + if (!operationResult) + { + throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); + } + MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + Close(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка сохранения магазина"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void buttonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + + private void ShopForm_Load(object sender, EventArgs e) + { + if (_id.HasValue) + { + _logger.LogInformation("Загрузка магазина"); + try + { + var shop = _logic.ReadElement(new ShopSearchModel { Id = _id }); + if (shop != null) + { + textBoxName.Text = shop.ShopName; + textBoxAddress.Text = shop.Address; + DateTimePicker.Text = shop.DateOpen.ToString(); + _flowers = shop.ShopFlowers; + } + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки магазина"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + } + } +} diff --git a/ProjectFlowerShop/ShopForm.resx b/ProjectFlowerShop/ShopForm.resx new file mode 100644 index 0000000..e4ea5ef --- /dev/null +++ b/ProjectFlowerShop/ShopForm.resx @@ -0,0 +1,132 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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 + + \ No newline at end of file diff --git a/ProjectFlowerShop/ShopsForm.Designer.cs b/ProjectFlowerShop/ShopsForm.Designer.cs new file mode 100644 index 0000000..431686d --- /dev/null +++ b/ProjectFlowerShop/ShopsForm.Designer.cs @@ -0,0 +1,114 @@ +namespace ProjectFlowerShop +{ + partial class ShopsForm + { + /// + /// 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(); + buttonAdd = new Button(); + buttonChange = new Button(); + buttonRemove = new Button(); + buttonRefresh = new Button(); + ((System.ComponentModel.ISupportInitialize)DataGridView).BeginInit(); + SuspendLayout(); + // + // DataGridView + // + DataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + DataGridView.Location = new Point(12, 12); + DataGridView.Name = "DataGridView"; + DataGridView.RowHeadersWidth = 51; + DataGridView.RowTemplate.Height = 29; + DataGridView.Size = new Size(531, 426); + DataGridView.TabIndex = 0; + // + // buttonAdd + // + buttonAdd.Location = new Point(549, 12); + buttonAdd.Name = "buttonAdd"; + buttonAdd.Size = new Size(239, 36); + buttonAdd.TabIndex = 1; + buttonAdd.Text = "Добавить"; + buttonAdd.UseVisualStyleBackColor = true; + buttonAdd.Click += buttonAdd_Click; + // + // buttonChange + // + buttonChange.Location = new Point(549, 54); + buttonChange.Name = "buttonChange"; + buttonChange.Size = new Size(239, 36); + buttonChange.TabIndex = 2; + buttonChange.Text = "Изменить"; + buttonChange.UseVisualStyleBackColor = true; + buttonChange.Click += buttonChange_Click; + // + // buttonRemove + // + buttonRemove.Location = new Point(549, 96); + buttonRemove.Name = "buttonRemove"; + buttonRemove.Size = new Size(239, 36); + buttonRemove.TabIndex = 3; + buttonRemove.Text = "Удалить"; + buttonRemove.UseVisualStyleBackColor = true; + buttonRemove.Click += buttonRemove_Click; + // + // buttonRefresh + // + buttonRefresh.Location = new Point(549, 138); + buttonRefresh.Name = "buttonRefresh"; + buttonRefresh.Size = new Size(239, 36); + buttonRefresh.TabIndex = 4; + buttonRefresh.Text = "Обновить"; + buttonRefresh.UseVisualStyleBackColor = true; + buttonRefresh.Click += buttonRefresh_Click; + // + // ShopsForm + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(800, 450); + Controls.Add(buttonRefresh); + Controls.Add(buttonRemove); + Controls.Add(buttonChange); + Controls.Add(buttonAdd); + Controls.Add(DataGridView); + Name = "ShopsForm"; + Text = "ShopsForm"; + Load += ShopsForm_Load; + ((System.ComponentModel.ISupportInitialize)DataGridView).EndInit(); + ResumeLayout(false); + } + + #endregion + + private DataGridView DataGridView; + private Button buttonAdd; + private Button buttonChange; + private Button buttonRemove; + private Button buttonRefresh; + } +} \ No newline at end of file diff --git a/ProjectFlowerShop/ShopsForm.cs b/ProjectFlowerShop/ShopsForm.cs new file mode 100644 index 0000000..4832033 --- /dev/null +++ b/ProjectFlowerShop/ShopsForm.cs @@ -0,0 +1,117 @@ +using FlowerShopContracts.BindingModels; +using FlowerShopContracts.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; +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 ProjectFlowerShop +{ + public partial class ShopsForm : Form + { + private readonly ILogger _logger; + private readonly IShopLogic _logic; + public ShopsForm(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["ShopFlowers"].Visible = false; + } + _logger.LogInformation("Загрузка магазинов"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки магазинов"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void buttonAdd_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(ShopForm)); + if (service is ShopForm form) + { + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + + private void ShopsForm_Load(object sender, EventArgs e) + { + LoadData(); + } + + private void buttonChange_Click(object sender, EventArgs e) + { + if (DataGridView.SelectedRows.Count == 1) + { + var service = Program.ServiceProvider?.GetService(typeof(ShopForm)); + if (service is ShopForm form) + { + var tmp = Convert.ToInt32(DataGridView.SelectedRows[0].Cells["Id"].Value); + form._id = Convert.ToInt32(DataGridView.SelectedRows[0].Cells["Id"].Value); + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + } + + private void buttonRemove_Click(object sender, EventArgs e) + { + if (DataGridView.SelectedRows.Count == 1) + { + if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + int id = Convert.ToInt32(DataGridView.SelectedRows[0].Cells["Id"].Value); + _logger.LogInformation("Удаление магазина"); + try + { + if (!_logic.Delete(new ShopBindingModel + { + Id = id + })) + { + throw new Exception("Ошибка при удалении. Дополнительная информация в логах."); + } + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка удаления магазина"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + } + + private void buttonRefresh_Click(object sender, EventArgs e) + { + LoadData(); + } + } +} diff --git a/ProjectFlowerShop/ShopsForm.resx b/ProjectFlowerShop/ShopsForm.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/ProjectFlowerShop/ShopsForm.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/ProjectFlowerShop/SupplyForm.Designer.cs b/ProjectFlowerShop/SupplyForm.Designer.cs new file mode 100644 index 0000000..87d9587 --- /dev/null +++ b/ProjectFlowerShop/SupplyForm.Designer.cs @@ -0,0 +1,141 @@ +namespace ProjectFlowerShop +{ + partial class SupplyForm + { + /// + /// 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(); + labelShop = new Label(); + labelFlower = new Label(); + labelNumber = new Label(); + comboBoxShop = new ComboBox(); + comboBoxFlower = new ComboBox(); + textBoxNumber = new TextBox(); + SuspendLayout(); + // + // buttonSave + // + buttonSave.Location = new Point(195, 186); + buttonSave.Name = "buttonSave"; + buttonSave.Size = new Size(111, 29); + buttonSave.TabIndex = 0; + buttonSave.Text = "Сохранить"; + buttonSave.UseVisualStyleBackColor = true; + buttonSave.Click += buttonSave_Click; + // + // buttonCancel + // + buttonCancel.Location = new Point(312, 186); + buttonCancel.Name = "buttonCancel"; + buttonCancel.Size = new Size(108, 29); + buttonCancel.TabIndex = 1; + buttonCancel.Text = "Отмена"; + buttonCancel.UseVisualStyleBackColor = true; + buttonCancel.Click += buttonCancel_Click; + // + // labelShop + // + labelShop.AutoSize = true; + labelShop.Location = new Point(12, 13); + labelShop.Name = "labelShop"; + labelShop.Size = new Size(69, 20); + labelShop.TabIndex = 2; + labelShop.Text = "Магазин"; + // + // labelFlower + // + labelFlower.AutoSize = true; + labelFlower.Location = new Point(12, 67); + labelFlower.Name = "labelFlower"; + labelFlower.Size = new Size(53, 20); + labelFlower.TabIndex = 3; + labelFlower.Text = "Цветы"; + // + // labelNumber + // + labelNumber.AutoSize = true; + labelNumber.Location = new Point(12, 121); + labelNumber.Name = "labelNumber"; + labelNumber.Size = new Size(90, 20); + labelNumber.TabIndex = 4; + labelNumber.Text = "Количество"; + // + // comboBoxShop + // + comboBoxShop.FormattingEnabled = true; + comboBoxShop.Location = new Point(12, 36); + comboBoxShop.Name = "comboBoxShop"; + comboBoxShop.Size = new Size(294, 28); + comboBoxShop.TabIndex = 5; + // + // comboBoxFlower + // + comboBoxFlower.FormattingEnabled = true; + comboBoxFlower.Location = new Point(12, 90); + comboBoxFlower.Name = "comboBoxFlower"; + comboBoxFlower.Size = new Size(294, 28); + comboBoxFlower.TabIndex = 6; + // + // textBoxNumber + // + textBoxNumber.Location = new Point(12, 144); + textBoxNumber.Name = "textBoxNumber"; + textBoxNumber.Size = new Size(151, 27); + textBoxNumber.TabIndex = 7; + // + // SupplyForm + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(430, 227); + Controls.Add(textBoxNumber); + Controls.Add(comboBoxFlower); + Controls.Add(comboBoxShop); + Controls.Add(labelNumber); + Controls.Add(labelFlower); + Controls.Add(labelShop); + Controls.Add(buttonCancel); + Controls.Add(buttonSave); + Name = "SupplyForm"; + Text = "SupplyForm"; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private Button buttonSave; + private Button buttonCancel; + private Label labelShop; + private Label labelFlower; + private Label labelNumber; + private ComboBox comboBoxShop; + private ComboBox comboBoxFlower; + private TextBox textBoxNumber; + } +} \ No newline at end of file diff --git a/ProjectFlowerShop/SupplyForm.cs b/ProjectFlowerShop/SupplyForm.cs new file mode 100644 index 0000000..e1e647e --- /dev/null +++ b/ProjectFlowerShop/SupplyForm.cs @@ -0,0 +1,146 @@ +using FlowerShopContracts.BusinessLogicsContracts; +using FlowerShopContracts.SearchModels; +using FlowerShopContracts.ViewModels; +using FlowerShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace ProjectFlowerShop +{ + public partial class SupplyForm : Form + { + private readonly List? _flowerList; + private readonly List? _shopsList; + IShopLogic _shopLogic; + IFlowerLogic _flowerLogic; + public SupplyForm(IFlowerLogic flowerLogic, IShopLogic shopLogic) + { + InitializeComponent(); + _shopLogic = shopLogic; + _flowerLogic = flowerLogic; + _flowerList = flowerLogic.ReadList(null); + _shopsList = shopLogic.ReadList(null); + if (_flowerList != null) + { + comboBoxFlower.DisplayMember = "FlowerName"; + comboBoxFlower.ValueMember = "Id"; + comboBoxFlower.DataSource = _flowerList; + comboBoxFlower.SelectedItem = null; + } + if (_shopsList != null) + { + comboBoxShop.DisplayMember = "ShopName"; + comboBoxShop.ValueMember = "Id"; + comboBoxShop.DataSource = _shopsList; + comboBoxShop.SelectedItem = null; + } + } + public int ShopId + { + get + { + return Convert.ToInt32(comboBoxShop.SelectedValue); + } + set + { + comboBoxShop.SelectedValue = value; + } + } + + public int FlowerId + { + get + { + return Convert.ToInt32(comboBoxFlower.SelectedValue); + } + set + { + comboBoxFlower.SelectedValue = value; + } + } + + public IFlowerModel? FlowerModel + { + get + { + if (_flowerList == null) + { + return null; + } + foreach (var elem in _flowerList) + { + if (elem.Id == FlowerId) + { + return elem; + } + } + return null; + } + } + public int Number + { + get { return Convert.ToInt32(textBoxNumber.Text); } + set { textBoxNumber.Text = value.ToString(); } + } + + private void buttonSave_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxNumber.Text)) + { + MessageBox.Show("Заполните поле Количество", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (comboBoxFlower.SelectedValue == null) + { + MessageBox.Show("Выберите цветы", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (comboBoxShop.SelectedValue == null) + { + MessageBox.Show("Выберите магазин", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + try + { + int count = Convert.ToInt32(textBoxNumber.Text); + + bool res = _shopLogic.MakeSupply( + new ShopSearchModel() { Id = Convert.ToInt32(comboBoxShop.SelectedValue) }, + _flowerLogic.ReadElement(new() { Id = Convert.ToInt32(comboBoxFlower.SelectedValue) }), + count + ); + + if (!res) + { + throw new Exception("Ошибка при пополнении. Дополнительная информация в логах"); + } + + MessageBox.Show("Пополнение прошло успешно"); + DialogResult = DialogResult.OK; + Close(); + + } + catch (Exception err) + { + MessageBox.Show("Ошибка пополнения"); + return; + } + } + + private void buttonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + } +} diff --git a/ProjectFlowerShop/SupplyForm.resx b/ProjectFlowerShop/SupplyForm.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/ProjectFlowerShop/SupplyForm.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