Начал реализацию логики форм;

Нужно доделать добавление продукта
This commit is contained in:
Никита Потапов 2024-11-20 18:03:33 +04:00
parent de4a43be41
commit 63668122c2
9 changed files with 252 additions and 84 deletions

View File

@ -5,10 +5,15 @@ namespace InternetShopContracts.DataViewModels
{ {
public class OrderViewModel : IOrderModel public class OrderViewModel : IOrderModel
{ {
[DisplayName("ФИО заказчика")]
public string CustomerFIO { get; set; } = string.Empty; public string CustomerFIO { get; set; } = string.Empty;
[DisplayName("Email заказчика")]
public string CustomerEmail { get; set; } = string.Empty; public string CustomerEmail { get; set; } = string.Empty;
[DisplayName("Изображение")]
public string ImagePath { get; set; } = string.Empty; public string ImagePath { get; set; } = string.Empty;
[DisplayName("Товары")]
public List<string> ProductNames { get; set; } = new List<string>(); public List<string> ProductNames { get; set; } = new List<string>();
[DisplayName("ID")]
public int Id { get; set; } public int Id { get; set; }
} }
} }

View File

@ -1,10 +1,13 @@
using InternetShopDataModels.Models; using InternetShopDataModels.Models;
using System.ComponentModel;
namespace InternetShopContracts.DataViewModels namespace InternetShopContracts.DataViewModels
{ {
public class ProductViewModel : IProductModel public class ProductViewModel : IProductModel
{ {
[DisplayName("Название")]
public string Name { get; set; } = string.Empty; public string Name { get; set; } = string.Empty;
[DisplayName("ID")]
public int Id { get; set; } public int Id { get; set; }
} }
} }

View File

@ -28,10 +28,16 @@
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
this.components = new System.ComponentModel.Container(); SuspendLayout();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; //
this.ClientSize = new System.Drawing.Size(800, 450); // FormOrderEdit
this.Text = "FormOrderEdit"; //
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450);
Name = "FormOrderEdit";
Text = "Заказ";
ResumeLayout(false);
} }
#endregion #endregion

View File

@ -1,20 +1,14 @@
using System; using InternetShopContracts.LogicsContracts;
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 InternetShopForms.Orders namespace InternetShopForms.Orders
{ {
public partial class FormOrderEdit : Form public partial class FormOrderEdit : Form
{ {
public FormOrderEdit() private IOrderLogic _orderLogic;
public FormOrderEdit(IOrderLogic orderLogic)
{ {
InitializeComponent(); InitializeComponent();
_orderLogic = orderLogic;
} }
} }
} }

View File

@ -1,15 +1,24 @@
using InternetShopContracts.LogicsContracts;
using InternetShopForms.Products;
namespace InternetShopForms namespace InternetShopForms
{ {
public partial class FormOrdersList : Form public partial class FormOrdersList : Form
{ {
public FormOrdersList() private IOrderLogic _orderLogic;
public FormOrdersList(IOrderLogic orderLogic)
{ {
InitializeComponent(); InitializeComponent();
_orderLogic = orderLogic;
} }
private void productsToolStripMenuItem_Click(object sender, EventArgs e) private void productsToolStripMenuItem_Click(object sender, EventArgs e)
{ {
// todo открыть окно со списком продуктов var service = Program.ServiceProvider?.GetService(typeof(FormProductsList));
if (service is FormProductsList form)
{
form.ShowDialog();
}
} }
} }
} }

View File

@ -28,12 +28,46 @@
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
this.components = new System.ComponentModel.Container(); dataGridView = new DataGridView();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit();
this.ClientSize = new System.Drawing.Size(800, 450); SuspendLayout();
this.Text = "FormProductsList"; //
// 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 #endregion
private DataGridView dataGridView;
} }
} }

View File

@ -1,20 +1,137 @@
using System; using InternetShopContracts.DataBindingModels;
using System.Collections.Generic; using InternetShopContracts.DataSearchModels;
using System.ComponentModel; using InternetShopContracts.DataViewModels;
using System.Data; using InternetShopContracts.LogicsContracts;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace InternetShopForms.Products namespace InternetShopForms.Products
{ {
public partial class FormProductsList : Form public partial class FormProductsList : Form
{ {
public FormProductsList() private IProductLogic _productLogic;
private List<ProductViewModel> _products;
public FormProductsList(IProductLogic productLogic)
{ {
InitializeComponent(); 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;
}
} }
} }
} }