SUBD_PIbd-21_Balberova_D.N./BeautySaloon/BeautySaloonView/FormPosition.cs
2023-03-29 20:00:53 +04:00

76 lines
2.5 KiB
C#

using BeautySaloonContracts.BindingModels;
using BeautySaloonContracts.BusinessLogicsContracts;
using BeautySaloonContracts.SearchModels;
using System.Windows.Forms;
namespace BeautySaloonView
{
public partial class FormPosition : Form
{
private readonly IPositionLogic _logic;
private int? _id;
public int Id { set { _id = value; } }
public FormPosition(IPositionLogic logic)
{
InitializeComponent();
_logic = logic;
}
private void FormPosition_Load(object sender, EventArgs e)
{
if (_id.HasValue)
{
try
{
var view = _logic.ReadElement(new PositionSearchModel
{
Id = _id.Value
});
if (view != null)
{
textBox.Text = view.Name;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
private void ButtonSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox.Text))
{
MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
var model = new PositionBindingModel
{
Id = _id ?? 0,
Name = textBox.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)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ButtonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
}
}