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

82 lines
2.8 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 FormService : Form
{
private readonly IServiceLogic _logic;
private int? _id;
public int Id { set { _id = value; } }
public FormService(IServiceLogic logic)
{
InitializeComponent();
_logic = logic;
}
private void FormService_Load(object sender, EventArgs e)
{
if (_id.HasValue)
{
try
{
var view = _logic.ReadElement(new ServiceSearchModel
{
Id = _id.Value
});
if (view != null)
{
textBox.Text = view.Name;
numericUpDown.Value = view.Price;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
private void ButtonSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox.Text) || numericUpDown.Value <= 0)
{
MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (numericUpDown.Value <= 0)
{
MessageBox.Show("Заполните цену", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
var model = new ServiceBindingModel
{
Id = _id ?? 0,
Name = textBox.Text,
Price = numericUpDown.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);
}
}
private void ButtonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
}
}