188 lines
6.0 KiB
C#
188 lines
6.0 KiB
C#
|
||
using Microsoft.Extensions.Logging;
|
||
using System.ComponentModel;
|
||
|
||
using ShopContracts.BindingModels;
|
||
using ShopContracts.BusinessLogicsContracts;
|
||
using Lab1;
|
||
using System;
|
||
using Lab1.VisualComponents.Exceptions;
|
||
|
||
|
||
|
||
namespace ShopView
|
||
{
|
||
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);
|
||
}
|
||
}
|
||
catch (IsNotMatchRegexException)
|
||
{
|
||
|
||
}
|
||
|
||
|
||
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)
|
||
{
|
||
LoadData();
|
||
if (_id.HasValue)
|
||
{
|
||
try
|
||
{
|
||
var view = _customerLogic.Read(new CustomerBindingModel { Id = _id.Value })?.FirstOrDefault();
|
||
if (view != null)
|
||
{
|
||
textBoxFIO.Text = view.FIO ;
|
||
|
||
var textBoxField = typeof(EmailField).GetField("textBox1",
|
||
System.Reflection.BindingFlags.NonPublic |
|
||
System.Reflection.BindingFlags.Instance);
|
||
|
||
if (textBoxField?.GetValue(emailField1) is TextBox innerTextBox)
|
||
{
|
||
innerTextBox.Text = view.Email;
|
||
}
|
||
|
||
|
||
imagePath = view.PhotoFilePath;
|
||
|
||
if (!string.IsNullOrEmpty(view.PhotoFilePath) && File.Exists(view.PhotoFilePath))
|
||
{
|
||
pictureBox.Image = Image.FromFile(view.PhotoFilePath);
|
||
}
|
||
|
||
string[] dirs = view.ProductCategoryName.Split(";");
|
||
foreach (var dir in dirs)
|
||
{
|
||
myListBox1.SelectedItem = dir;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
_logger.LogWarning("Клиент с указанным ID не найден.");
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError(ex, "Ошибка при загрузке данных клиента.");
|
||
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
}
|
||
|
||
private void LoadData()
|
||
{
|
||
_logger.LogInformation("Загрузка категории товаров");
|
||
try
|
||
{
|
||
var list = _productCategoryLogic.Read(null);
|
||
myListBox1.Clear();
|
||
productCategory.Clear();
|
||
_list.Clear();
|
||
if (list != null)
|
||
{
|
||
foreach (var item in list)
|
||
{
|
||
productCategory.Add(item.Name);
|
||
_list.Add(new ProductCategoryBindingModel
|
||
{
|
||
Id = item.Id,
|
||
Name = item.Name,
|
||
});
|
||
}
|
||
}
|
||
myListBox1.AddItems(productCategory);
|
||
}
|
||
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);
|
||
}
|
||
}
|
||
}
|
||
}
|