2024-10-28 23:08:27 +04:00
|
|
|
|
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
|
|
|
|
|
using ShopContracts.BindingModels;
|
|
|
|
|
using ShopContracts.BusinessLogicsContracts;
|
|
|
|
|
using Lab1;
|
|
|
|
|
using System;
|
|
|
|
|
using Lab1.VisualComponents.Exceptions;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace ShopView
|
|
|
|
|
{
|
2024-11-07 14:19:38 +04:00
|
|
|
|
public partial class FormCustomer : Form
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly ICustomerLogic _customerLogic;
|
|
|
|
|
private readonly IProductCategoryLogic _productCategoryLogic;
|
|
|
|
|
private string imagePath;
|
|
|
|
|
BindingList<ProductCategoryBindingModel> _list;
|
|
|
|
|
private int? _id;
|
|
|
|
|
public int Id { set { _id = value; } }
|
|
|
|
|
private string? _item;
|
|
|
|
|
|
|
|
|
|
List<string> productCategory = new();
|
|
|
|
|
public FormCustomer(ILogger<FormCustomer> logger, ICustomerLogic customerLogic, IProductCategoryLogic productCategoryLogic)
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_customerLogic = customerLogic;
|
|
|
|
|
_productCategoryLogic = productCategoryLogic;
|
|
|
|
|
imagePath = "";
|
|
|
|
|
_list = new BindingList<ProductCategoryBindingModel>();
|
|
|
|
|
emailField1.EmailRegex = "^\\S+@\\S+\\.\\S+$";
|
|
|
|
|
emailField1.EmailExample = "example@mail.ru";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void buttonSave_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(textBoxFIO.Text))
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Заполните ФИО", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(emailField1.Value))
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Введите почту!", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (IsNotMatchRegexException)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Не соответствует шаблону", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pictureBox.Image == null)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Загрузите фото", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("Сохранение студента");
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_customerLogic.CreateOrUpdate(new CustomerBindingModel
|
|
|
|
|
{
|
|
|
|
|
Id = _id,
|
|
|
|
|
FIO = textBoxFIO.Text,
|
|
|
|
|
PhotoFilePath = imagePath,
|
|
|
|
|
Email = emailField1.Value,
|
|
|
|
|
ProductCategoryName = myListBox1.SelectedItem
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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 FormCustomer_Load(object sender, EventArgs e)
|
|
|
|
|
{
|
2024-10-28 23:08:27 +04:00
|
|
|
|
LoadData();
|
|
|
|
|
if (_id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var view = _customerLogic.Read(new CustomerBindingModel { Id = _id.Value })?.FirstOrDefault();
|
|
|
|
|
if (view != null)
|
|
|
|
|
{
|
2024-11-07 14:19:38 +04:00
|
|
|
|
textBoxFIO.Text = view.FIO;
|
|
|
|
|
imagePath = view.PhotoFilePath ?? string.Empty;
|
2024-10-28 23:08:27 +04:00
|
|
|
|
|
2024-11-07 14:19:38 +04:00
|
|
|
|
// Используем рефлексию для доступа к textBox1 в emailField1
|
2024-10-28 23:08:27 +04:00
|
|
|
|
var textBoxField = typeof(EmailField).GetField("textBox1",
|
|
|
|
|
System.Reflection.BindingFlags.NonPublic |
|
|
|
|
|
System.Reflection.BindingFlags.Instance);
|
|
|
|
|
|
|
|
|
|
if (textBoxField?.GetValue(emailField1) is TextBox innerTextBox)
|
|
|
|
|
{
|
2024-11-07 14:19:38 +04:00
|
|
|
|
innerTextBox.Text = view.Email ?? string.Empty; // Устанавливаем значение напрямую
|
2024-10-28 23:08:27 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-07 14:19:38 +04:00
|
|
|
|
// Устанавливаем другие значения, как и раньше
|
2024-10-28 23:08:27 +04:00
|
|
|
|
string[] dirs = view.ProductCategoryName.Split(";");
|
|
|
|
|
foreach (var dir in dirs)
|
|
|
|
|
{
|
|
|
|
|
myListBox1.SelectedItem = dir;
|
|
|
|
|
}
|
2024-11-07 14:19:38 +04:00
|
|
|
|
if (!string.IsNullOrEmpty(imagePath) && File.Exists(imagePath))
|
|
|
|
|
{
|
|
|
|
|
pictureBox.Image = Image.FromFile(imagePath);
|
|
|
|
|
}
|
2024-10-28 23:08:27 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-07 14:19:38 +04:00
|
|
|
|
private void LoadData()
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Загрузка категории товаров");
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var list = _productCategoryLogic.Read(null);
|
2024-10-28 23:08:27 +04:00
|
|
|
|
myListBox1.Clear();
|
2024-11-07 14:19:38 +04:00
|
|
|
|
productCategory.Clear();
|
|
|
|
|
_list.Clear();
|
|
|
|
|
if (list != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (var item in list)
|
|
|
|
|
{
|
|
|
|
|
productCategory.Add(item.Name);
|
|
|
|
|
_list.Add(new ProductCategoryBindingModel
|
2024-10-28 23:08:27 +04:00
|
|
|
|
{
|
2024-11-07 14:19:38 +04:00
|
|
|
|
Id = item.Id,
|
|
|
|
|
Name = item.Name,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-28 23:08:27 +04:00
|
|
|
|
myListBox1.AddItems(productCategory);
|
2024-11-07 14:19:38 +04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка загрузки категории товаров");
|
|
|
|
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void buttonAddPhoto_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
OpenFileDialog openFileDialog = new()
|
|
|
|
|
{
|
|
|
|
|
Filter = "Image Files(*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG|All files (*.*)|*.*"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
|
|
|
|
{
|
|
|
|
|
imagePath = openFileDialog.FileName;
|
|
|
|
|
pictureBox.Image = Image.FromFile(imagePath);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|