81 lines
2.7 KiB
C#
81 lines
2.7 KiB
C#
using SushiBarBusinessLogic;
|
|
using SushiBarContracts.BindingModels;
|
|
using SushiBarContracts.SearchModels;
|
|
|
|
namespace SushiBarView.Forms
|
|
{
|
|
public partial class FormPromotion : Form
|
|
{
|
|
private readonly PromotionLogic _logic;
|
|
|
|
private int? _id;
|
|
public int Id { set { _id = value; } }
|
|
|
|
public FormPromotion(PromotionLogic Logic)
|
|
{
|
|
InitializeComponent();
|
|
_logic = Logic;
|
|
}
|
|
|
|
private void FormPromotion_Load(object sender, EventArgs e)
|
|
{
|
|
if (_id.HasValue)
|
|
{
|
|
try
|
|
{
|
|
var View = _logic.ReadElement(new PromotionSearchModel
|
|
{
|
|
Id = _id.Value
|
|
});
|
|
|
|
if (View != null)
|
|
{
|
|
NameTextBox.Text = View.PromotionName;
|
|
DiscountTextBox.Text = View.Discount.ToString();
|
|
TriggeringSumTextBox.Text = View.TriggeringSum.ToString();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void ButtonSave_Click(object sender, EventArgs e)
|
|
{
|
|
if (string.IsNullOrEmpty(NameTextBox.Text))
|
|
{
|
|
MessageBox.Show("Заполните ФИО", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
var Model = new PromotionBindingModel
|
|
{
|
|
Id = _id ?? 0,
|
|
PromotionName = NameTextBox.Text,
|
|
Discount = float.Parse(DiscountTextBox.Text),
|
|
TriggeringSum = Double.Parse(TriggeringSumTextBox.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);
|
|
}
|
|
}
|
|
}
|
|
}
|