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

Нужно доделать добавление продукта
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,17 +1,17 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<root> <root>
<!-- <!--
Microsoft ResX Schema Microsoft ResX Schema
Version 2.0 Version 2.0
The primary goals of this format is to allow a simple XML format The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes various data types are done through the TypeConverter classes
associated with the data types. associated with the data types.
Example: Example:
... ado.net/XML headers & schema ... ... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader> <resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader> <resheader name="version">2.0</resheader>
@ -26,36 +26,36 @@
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value> <value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment> <comment>This is a comment</comment>
</data> </data>
There are any number of "resheader" rows that contain simple There are any number of "resheader" rows that contain simple
name/value pairs. name/value pairs.
Each data row contains a name, and value. The row also contains a Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture. text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the Classes that don't support this are serialized and stored with the
mimetype set. mimetype set.
The mimetype is used for serialized objects, and tells the The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly: extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below. read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64 mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding. : and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64 mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter : System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding. : and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64 mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter : using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding. : and then encoded with base64 encoding.
--> -->

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;
}
} }
} }
} }

View File

@ -1,17 +1,17 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<root> <root>
<!-- <!--
Microsoft ResX Schema Microsoft ResX Schema
Version 2.0 Version 2.0
The primary goals of this format is to allow a simple XML format The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes various data types are done through the TypeConverter classes
associated with the data types. associated with the data types.
Example: Example:
... ado.net/XML headers & schema ... ... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader> <resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader> <resheader name="version">2.0</resheader>
@ -26,36 +26,36 @@
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value> <value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment> <comment>This is a comment</comment>
</data> </data>
There are any number of "resheader" rows that contain simple There are any number of "resheader" rows that contain simple
name/value pairs. name/value pairs.
Each data row contains a name, and value. The row also contains a Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture. text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the Classes that don't support this are serialized and stored with the
mimetype set. mimetype set.
The mimetype is used for serialized objects, and tells the The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly: extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below. read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64 mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding. : and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64 mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter : System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding. : and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64 mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter : using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding. : and then encoded with base64 encoding.
--> -->