92 lines
3.1 KiB
C#
92 lines
3.1 KiB
C#
using IceCreamShopContracts.BindingModels;
|
|
using IceCreamShopContracts.BusinessLogicsContracts;
|
|
using IceCreamShopContracts.SearchModels;
|
|
using Microsoft.Extensions.Logging;
|
|
using System.Windows.Forms;
|
|
|
|
namespace IceCreamShop
|
|
{
|
|
public partial class ComponentForm : Form
|
|
{
|
|
private readonly ILogger _logger;
|
|
private readonly IComponentLogic _logic;
|
|
private int? _id;
|
|
public int Id { set { _id = value; } }
|
|
public ComponentForm(ILogger<ComponentForm> logger, IComponentLogic logic)
|
|
{
|
|
InitializeComponent();
|
|
_logger = logger;
|
|
_logic = logic;
|
|
}
|
|
|
|
private void ComponentForm_Load(object sender, EventArgs e)
|
|
{
|
|
if (_id.HasValue)
|
|
{
|
|
try
|
|
{
|
|
_logger.LogInformation("Ïîëó÷åíèå êîìïîíåíòà");
|
|
var view = _logic.ReadElement(new ComponentSearchModel
|
|
{
|
|
Id =
|
|
_id.Value
|
|
});
|
|
if (view != null)
|
|
{
|
|
NameTextBox.Text = view.ComponentName;
|
|
CostTextBox.Text = view.Cost.ToString();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Îøèáêà ïîëó÷åíèÿ êîìïîíåíòà");
|
|
MessageBox.Show(ex.Message, "Îøèáêà", MessageBoxButtons.OK,
|
|
MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void SaveButton_Click(object sender, EventArgs e)
|
|
{
|
|
if (string.IsNullOrEmpty(NameTextBox.Text))
|
|
{
|
|
MessageBox.Show("Çàïîëíèòå íàçâàíèå", "Îøèáêà",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
_logger.LogInformation("Ñîõðàíåíèå êîìïîíåíòà");
|
|
try
|
|
{
|
|
var model = new ComponentBindingModel
|
|
{
|
|
Id = _id ?? 0,
|
|
ComponentName = NameTextBox.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)
|
|
{
|
|
_logger.LogError(ex, "Îøèáêà ñîõðàíåíèÿ êîìïîíåíòà");
|
|
MessageBox.Show(ex.Message, "Îøèáêà", MessageBoxButtons.OK,
|
|
MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
|
|
private void CancelButton_Click(object sender, EventArgs e)
|
|
{
|
|
DialogResult = DialogResult.Cancel;
|
|
Close();
|
|
}
|
|
}
|
|
}
|