95 lines
3.5 KiB
C#
95 lines
3.5 KiB
C#
|
using SYBDContracts.BindingModels;
|
|||
|
using SYBDContracts.BusinessLogicsContracts;
|
|||
|
using SYBDContracts.SearchModels;
|
|||
|
using Microsoft.Extensions.Logging;
|
|||
|
|
|||
|
namespace SYBDView
|
|||
|
{
|
|||
|
public partial class FormClient : Form
|
|||
|
{
|
|||
|
private readonly ILogger _logger;
|
|||
|
|
|||
|
private readonly IClientLogic _logic;
|
|||
|
|
|||
|
private int? _id;
|
|||
|
public int Id { set { _id = value; } }
|
|||
|
|
|||
|
public FormClient(ILogger<FormClient> logger, IClientLogic logic)
|
|||
|
{
|
|||
|
InitializeComponent();
|
|||
|
_logger = logger;
|
|||
|
_logic = logic;
|
|||
|
}
|
|||
|
|
|||
|
private void FormClient_Load(object sender, EventArgs e)
|
|||
|
{
|
|||
|
if (_id.HasValue)
|
|||
|
{
|
|||
|
_logger.LogInformation("Загрузка клиентов");
|
|||
|
try
|
|||
|
{
|
|||
|
var view = _logic.ReadElement(new ClientSearchModel { Id = _id.Value });
|
|||
|
if (view != null)
|
|||
|
{
|
|||
|
textBoxFullname.Text = view.Fullname;
|
|||
|
textBoxEmail.Text = view.Email;
|
|||
|
textBoxPhone.Text = view.Phone;
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
_logger.LogError(ex, "Ошибка загрузки клиента");
|
|||
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
private void ButtonSave_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
if (string.IsNullOrEmpty(textBoxFullname.Text))
|
|||
|
{
|
|||
|
MessageBox.Show("Заполните ФИО", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
return;
|
|||
|
}
|
|||
|
if (string.IsNullOrEmpty(textBoxEmail.Text))
|
|||
|
{
|
|||
|
MessageBox.Show("Заполните адрес", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
return;
|
|||
|
}
|
|||
|
if (string.IsNullOrEmpty(textBoxPhone.Text))
|
|||
|
{
|
|||
|
MessageBox.Show("Заполните телефон", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
return;
|
|||
|
}
|
|||
|
_logger.LogInformation("Сохранение изделия");
|
|||
|
try
|
|||
|
{
|
|||
|
var model = new ClientBindingModel
|
|||
|
{
|
|||
|
Id = _id ?? 0,
|
|||
|
Fullname = textBoxFullname.Text,
|
|||
|
Email = textBoxEmail.Text,
|
|||
|
Phone = textBoxPhone.Text,
|
|||
|
};
|
|||
|
var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model);
|
|||
|
if (!operationResult)
|
|||
|
{
|
|||
|
throw new Exception("Ошибка при сохранении. Дополнительная информация в логах.");
|
|||
|
}
|
|||
|
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();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|