diff --git a/InternetShop/InternetShopContracts/DataViewModels/OrderViewModel.cs b/InternetShop/InternetShopContracts/DataViewModels/OrderViewModel.cs index 9920364..d311afc 100644 --- a/InternetShop/InternetShopContracts/DataViewModels/OrderViewModel.cs +++ b/InternetShop/InternetShopContracts/DataViewModels/OrderViewModel.cs @@ -5,10 +5,15 @@ namespace InternetShopContracts.DataViewModels { public class OrderViewModel : IOrderModel { + [DisplayName("ФИО заказчика")] public string CustomerFIO { get; set; } = string.Empty; + [DisplayName("Email заказчика")] public string CustomerEmail { get; set; } = string.Empty; + [DisplayName("Изображение")] public string ImagePath { get; set; } = string.Empty; + [DisplayName("Товары")] public List ProductNames { get; set; } = new List(); + [DisplayName("ID")] public int Id { get; set; } } } diff --git a/InternetShop/InternetShopContracts/DataViewModels/ProductViewModel.cs b/InternetShop/InternetShopContracts/DataViewModels/ProductViewModel.cs index 08d9d8b..558fd49 100644 --- a/InternetShop/InternetShopContracts/DataViewModels/ProductViewModel.cs +++ b/InternetShop/InternetShopContracts/DataViewModels/ProductViewModel.cs @@ -1,10 +1,13 @@ using InternetShopDataModels.Models; +using System.ComponentModel; namespace InternetShopContracts.DataViewModels { public class ProductViewModel : IProductModel { + [DisplayName("Название")] public string Name { get; set; } = string.Empty; + [DisplayName("ID")] public int Id { get; set; } } } diff --git a/InternetShop/InternetShopForms/Orders/FormOrderEdit.Designer.cs b/InternetShop/InternetShopForms/Orders/FormOrderEdit.Designer.cs index 4d7e418..68fffeb 100644 --- a/InternetShop/InternetShopForms/Orders/FormOrderEdit.Designer.cs +++ b/InternetShop/InternetShopForms/Orders/FormOrderEdit.Designer.cs @@ -28,10 +28,16 @@ /// private void InitializeComponent() { - this.components = new System.ComponentModel.Container(); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 450); - this.Text = "FormOrderEdit"; + SuspendLayout(); + // + // FormOrderEdit + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(800, 450); + Name = "FormOrderEdit"; + Text = "Заказ"; + ResumeLayout(false); } #endregion diff --git a/InternetShop/InternetShopForms/Orders/FormOrderEdit.cs b/InternetShop/InternetShopForms/Orders/FormOrderEdit.cs index 7982a35..374b5c0 100644 --- a/InternetShop/InternetShopForms/Orders/FormOrderEdit.cs +++ b/InternetShop/InternetShopForms/Orders/FormOrderEdit.cs @@ -1,20 +1,14 @@ -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 InternetShopContracts.LogicsContracts; namespace InternetShopForms.Orders { public partial class FormOrderEdit : Form { - public FormOrderEdit() + private IOrderLogic _orderLogic; + public FormOrderEdit(IOrderLogic orderLogic) { InitializeComponent(); + _orderLogic = orderLogic; } } } diff --git a/InternetShop/InternetShopForms/Orders/FormOrderEdit.resx b/InternetShop/InternetShopForms/Orders/FormOrderEdit.resx index 1af7de1..8b2ff64 100644 --- a/InternetShop/InternetShopForms/Orders/FormOrderEdit.resx +++ b/InternetShop/InternetShopForms/Orders/FormOrderEdit.resx @@ -1,17 +1,17 @@  - diff --git a/InternetShop/InternetShopForms/Orders/FormOrdersList.cs b/InternetShop/InternetShopForms/Orders/FormOrdersList.cs index 5193f9f..c70b7c7 100644 --- a/InternetShop/InternetShopForms/Orders/FormOrdersList.cs +++ b/InternetShop/InternetShopForms/Orders/FormOrdersList.cs @@ -1,15 +1,24 @@ +using InternetShopContracts.LogicsContracts; +using InternetShopForms.Products; + namespace InternetShopForms { public partial class FormOrdersList : Form { - public FormOrdersList() + private IOrderLogic _orderLogic; + public FormOrdersList(IOrderLogic orderLogic) { InitializeComponent(); + _orderLogic = orderLogic; } private void productsToolStripMenuItem_Click(object sender, EventArgs e) { - // todo + var service = Program.ServiceProvider?.GetService(typeof(FormProductsList)); + if (service is FormProductsList form) + { + form.ShowDialog(); + } } } } diff --git a/InternetShop/InternetShopForms/Products/FormProductsList.Designer.cs b/InternetShop/InternetShopForms/Products/FormProductsList.Designer.cs index a8a2564..ad98c61 100644 --- a/InternetShop/InternetShopForms/Products/FormProductsList.Designer.cs +++ b/InternetShop/InternetShopForms/Products/FormProductsList.Designer.cs @@ -28,12 +28,46 @@ /// private void InitializeComponent() { - this.components = new System.ComponentModel.Container(); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 450); - this.Text = "FormProductsList"; + dataGridView = new DataGridView(); + ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); + SuspendLayout(); + // + // dataGridView + // + dataGridView.AllowUserToAddRows = false; + dataGridView.AllowUserToDeleteRows = false; + dataGridView.AllowUserToResizeColumns = false; + dataGridView.AllowUserToResizeRows = false; + dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; + dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + dataGridView.Dock = DockStyle.Fill; + dataGridView.Location = new Point(0, 0); + dataGridView.MultiSelect = false; + dataGridView.Name = "dataGridView"; + dataGridView.RowHeadersVisible = false; + dataGridView.RowHeadersWidth = 51; + dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect; + dataGridView.Size = new Size(310, 450); + dataGridView.TabIndex = 0; + dataGridView.CellEndEdit += dataGridView_CellEndEdit; + dataGridView.CellValidating += dataGridView_CellValidating; + // + // FormProductsList + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(310, 450); + Controls.Add(dataGridView); + Name = "FormProductsList"; + Text = "Список товаров"; + Load += FormProductsList_Load; + KeyDown += FormProductsList_KeyDown; + ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); + ResumeLayout(false); } #endregion + + private DataGridView dataGridView; } } \ No newline at end of file diff --git a/InternetShop/InternetShopForms/Products/FormProductsList.cs b/InternetShop/InternetShopForms/Products/FormProductsList.cs index c201045..3215cbe 100644 --- a/InternetShop/InternetShopForms/Products/FormProductsList.cs +++ b/InternetShop/InternetShopForms/Products/FormProductsList.cs @@ -1,20 +1,137 @@ -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 InternetShopContracts.DataBindingModels; +using InternetShopContracts.DataSearchModels; +using InternetShopContracts.DataViewModels; +using InternetShopContracts.LogicsContracts; namespace InternetShopForms.Products { public partial class FormProductsList : Form { - public FormProductsList() + private IProductLogic _productLogic; + private List _products; + public FormProductsList(IProductLogic productLogic) { InitializeComponent(); + _productLogic = productLogic; + KeyPreview = true; + } + + private void FormProductsList_Load(object sender, EventArgs e) + { + var idColumn = new DataGridViewColumn(); + var nameColumn = new DataGridViewColumn(); + var dataGridViewCell = new DataGridViewTextBoxCell(); + idColumn.Visible = false; + idColumn.DataPropertyName = "Id"; + idColumn.Name = "Id"; + idColumn.CellTemplate = dataGridViewCell; + nameColumn.DataPropertyName = "Name"; + nameColumn.HeaderText = "Название"; + nameColumn.Name = "Name"; + nameColumn.CellTemplate = dataGridViewCell; + dataGridView.Columns.Add(idColumn); + dataGridView.Columns.Add(nameColumn); + LoadData(); + } + + private void LoadData() + { + try + { + _products = _productLogic.ReadList(); + dataGridView.Rows.Clear(); + foreach (var product in _products) + { + dataGridView.Rows.Add(product); + } + } + catch (Exception ex) + { + MessageBox.Show("Произошла ошибка при загрузке данных:\n" + ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void FormProductsList_KeyDown(object sender, KeyEventArgs e) + { + if (e.KeyCode == Keys.Insert) + { + dataGridView.Rows.Add(); + dataGridView.CurrentCell = dataGridView.Rows[dataGridView.RowCount - 1].Cells[1]; + e.Handled = true; + } + else if (e.KeyCode == Keys.Delete) + { + if (dataGridView.SelectedRows.Count == 1) + { + DataGridViewRow selectedRow = dataGridView.SelectedRows[0]; + var selectedItemIdValue = selectedRow.Cells["Id"].Value; + if (selectedItemIdValue == null) + { + dataGridView.Rows.Remove(selectedRow); + return; + } + int selectedItemId = Convert.ToInt32(selectedItemIdValue); + string selectedItemName = (string)selectedRow.Cells["Name"].Value; + var result = MessageBox.Show( + $"Удалить товар \"{selectedRow.Cells["Id"].Value}\"?", + "Подтверждение", + MessageBoxButtons.YesNo, + MessageBoxIcon.Question + ); + + if (result == DialogResult.Yes) + { + if (_productLogic.Delete(new ProductSearchModel { Id = selectedItemId })) + { + MessageBox.Show($"Товар \"{selectedItemName}\" удален"); + LoadData(); + } + else + { + MessageBox.Show("Ошибка удаления", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + } + } + + private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e) + { + try + { + DataGridViewRow currentRow = dataGridView.Rows[e.RowIndex]; + if (currentRow.Cells["Id"].Value == null || Convert.ToInt32(currentRow.Cells["Id"].Value) == 0) + { + _productLogic.Create(new ProductBindingModel + { + Name = currentRow.Cells["Name"].Value.ToString()!, + }); + } + else + { + _productLogic.Update(new ProductBindingModel + { + Id = Convert.ToInt32(currentRow.Cells["Id"].Value), + Name = currentRow.Cells["Name"].Value.ToString(), + }); + } + MessageBox.Show("Запись сохранена"); + } + catch (Exception ex) + { + MessageBox.Show("Ошибка сохранения:\n" + ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) + { + string? userInput = e.FormattedValue.ToString(); + + if (string.IsNullOrWhiteSpace(userInput)) + { + MessageBox.Show("Нельзя сохранить пустую строку", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + e.Cancel = true; + } } } } diff --git a/InternetShop/InternetShopForms/Products/FormProductsList.resx b/InternetShop/InternetShopForms/Products/FormProductsList.resx index 1af7de1..8b2ff64 100644 --- a/InternetShop/InternetShopForms/Products/FormProductsList.resx +++ b/InternetShop/InternetShopForms/Products/FormProductsList.resx @@ -1,17 +1,17 @@  -