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

82 lines
2.7 KiB
C#

using SushiBarBusinessLogic;
using SushiBarContracts.BindingModels;
using SushiBarContracts.SearchModels;
namespace SushiBarView.Forms
{
public partial class FormIngredient : Form
{
private readonly IngredientLogic _logic;
private int? _id;
public int Id { set { _id = value; } }
public FormIngredient(IngredientLogic Logic)
{
InitializeComponent();
_logic = Logic;
}
private void FormIngredient_Load(object sender, EventArgs e)
{
if (_id.HasValue)
{
try
{
var View = _logic.ReadElement(new IngredientSearchModel
{
Id = _id.Value
});
if (View != null)
{
IngredientNameTextBox.Text = View.IngredientName;
UnitTextBox.Text = View.Unit;
CostTextBox.Text = View.Cost.ToString();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void ButtonSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(IngredientNameTextBox.Text))
{
MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
var Model = new IngredientBindingModel
{
Id = _id ?? 0,
IngredientName = IngredientNameTextBox.Text,
Unit = UnitTextBox.Text,
Cost = Convert.ToDouble(CostTextBox.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);
}
}
}
}