PIbd-21_Pyatakov_K.M._Secure/Secure/SecureShop/FormFacilitie.cs
2023-03-09 08:41:05 +04:00

86 lines
3.1 KiB
C#

using SecureShopContracts.BindingModels;
using SecureShopContracts.BusinessLogicsContracts;
using SecureShopContracts.SearchModels;
using Microsoft.Extensions.Logging;
namespace SecureShopView
{
public partial class FormFacilitie : Form
{
private readonly ILogger _logger;
private readonly IFacilitiesLogic _logic;
private int? _id;
public int Id { set { _id = value; } }
public FormFacilitie(ILogger<FormFacilitie> logger, IFacilitiesLogic logic)
{
InitializeComponent();
_logger = logger;
_logic = logic;
}
private void FormFacilities_Load(object sender, EventArgs e)
{
if (_id.HasValue)
{
try
{
_logger.LogInformation("Получение добавки");
var view = _logic.ReadElement(new FacilitiesSearchModel
{
Id = _id.Value
});
if (view != null)
{
textBoxName.Text = view.FacilitiesName;
textBoxCost.Text = view.Cost.ToString();
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения добавки");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void ButtonSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxName.Text))
{
MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_logger.LogInformation("Сохранение добавки");
try
{
var model = new FacilitiesBindingModel
{
Id = _id ?? 0,
FacilitiesName = textBoxName.Text,
Cost = Convert.ToDouble(textBoxCost.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)
{
_logger.LogError(ex, "Ошибка сохранения добавки");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
private void ButtonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
}
}