2024-05-24 01:56:34 +04:00

79 lines
2.5 KiB
C#

using SushiBarBusinessLogic;
using SushiBarContracts.BindingModels;
using SushiBarContracts.SearchModels;
namespace SushiBarView.Forms
{
public partial class FormCook : Form
{
private readonly CookLogic _logic;
private int? _id;
public int Id { set { _id = value; } }
public FormCook(CookLogic Logic)
{
InitializeComponent();
_logic = Logic;
}
private void FormCook_Load(object sender, EventArgs e)
{
if (_id.HasValue)
{
try
{
var View = _logic.ReadElement(new CookSearchModel
{
Id = _id.Value
});
if (View != null)
{
FioTextBox.Text = View.Fio;
EmploymentDateTimePicker.Value = View.EmploymentDate;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void ButtonSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(FioTextBox.Text))
{
MessageBox.Show("Заполните ФИО", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
var Model = new CookBindingModel
{
Id = _id ?? 0,
Fio = FioTextBox.Text,
EmploymentDate = EmploymentDateTimePicker.Value,
};
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);
}
}
}
}