SUBD_PIbd-21_Balberova_D.N./BeautySaloon/BeautySaloonView/FormClient.cs

93 lines
3.4 KiB
C#
Raw Permalink Normal View History

2023-03-29 20:00:53 +04:00
using BeautySaloonContracts.BindingModels;
using BeautySaloonContracts.BusinessLogicsContracts;
using BeautySaloonContracts.SearchModels;
namespace BeautySaloonView
{
public partial class FormClient : Form
{
private readonly IClientLogic _logicC;
private readonly IOrderLogic _logicO;
private int? _id;
public int Id { set { _id = value; } }
public FormClient(IClientLogic logicС, IOrderLogic logicO)
{
InitializeComponent();
_logicC = logicС;
_logicO = logicO;
}
private void FormClient_Load(object sender, EventArgs e)
{
if (_id.HasValue)
{
try
{
var view = _logicC.ReadElement(new ClientSearchModel
{
Id = _id.Value
});
if (view != null)
{
textBoxName.Text = view.Name;
textBoxSurname.Text = view.Surname;
textBoxPatronymic.Text = view.Patronymic;
maskedTextBox.Text = view.Phone;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
private void ButtonSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxName.Text))
{
MessageBox.Show("Заполните имя", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (string.IsNullOrEmpty(textBoxSurname.Text))
{
MessageBox.Show("Заполните фамилию", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (!maskedTextBox.MaskFull)
{
MessageBox.Show("Заполните номер телефона", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
var model = new ClientBindingModel
{
Id = _id ?? 0,
Name = textBoxName.Text,
Surname = textBoxSurname.Text,
Patronymic = textBoxPatronymic.Text,
Phone = maskedTextBox.Text
};
var operationResult = _id.HasValue ? _logicC.Update(model) : _logicC.Create(model);
if (!operationResult)
{
throw new Exception("Ошибка при сохранении.");
}
MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
DialogResult = DialogResult.OK;
Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ButtonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
}
}