ISEbd_21_Kuklew_M.I._Softwa.../SoftwareInstallation/FormComponent.cs

85 lines
3.0 KiB
C#
Raw Normal View History

2024-05-12 14:38:32 +04:00
using SoftwareInstallationContracts.BindingModels;
2024-04-07 17:05:30 +04:00
using SoftwareInstallationContracts.BusinessLogicsContracts;
using SoftwareInstallationContracts.SearchModels;
2024-05-12 14:38:32 +04:00
using Microsoft.Extensions.Logging;
2024-04-07 16:54:02 +04:00
2024-05-12 14:38:32 +04:00
namespace SoftwareInstallationView
2024-04-07 16:54:02 +04:00
{
public partial class FormComponent : Form
{
private readonly ILogger _logger;
2024-05-12 14:38:32 +04:00
2024-04-07 16:54:02 +04:00
private readonly IComponentLogic _logic;
2024-05-12 14:38:32 +04:00
2024-04-07 16:54:02 +04:00
private int? _id;
2024-05-12 14:38:32 +04:00
2024-04-07 16:54:02 +04:00
public int Id { set { _id = value; } }
2024-05-12 14:38:32 +04:00
2024-04-07 16:54:02 +04:00
public FormComponent(ILogger<FormComponent> logger, IComponentLogic logic)
{
InitializeComponent();
_logger = logger;
_logic = logic;
}
private void FormComponent_Load(object sender, EventArgs e)
{
if (_id.HasValue)
{
try
{
2024-05-12 14:38:32 +04:00
_logger.LogInformation("Receiving component");
var view = _logic.ReadElement(new ComponentSearchModel { Id = _id.Value });
2024-04-07 16:54:02 +04:00
if (view != null)
{
textBoxName.Text = view.ComponentName;
textBoxCost.Text = view.Cost.ToString();
}
}
catch (Exception ex)
{
2024-05-12 14:38:32 +04:00
_logger.LogError(ex, "Component retrieval error");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
2024-04-07 16:54:02 +04:00
}
}
}
2024-05-12 14:38:32 +04:00
private void ButtonSave_Click(object sender, EventArgs e)
2024-04-07 16:54:02 +04:00
{
if (string.IsNullOrEmpty(textBoxName.Text))
{
MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
2024-05-12 14:38:32 +04:00
_logger.LogInformation("Saving component");
2024-04-07 16:54:02 +04:00
try
{
var model = new ComponentBindingModel
{
Id = _id ?? 0,
ComponentName = 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)
{
2024-05-12 14:38:32 +04:00
_logger.LogError(ex, "Component saving error");
2024-04-07 16:54:02 +04:00
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
2024-05-12 14:38:32 +04:00
private void ButtonCancel_Click(object sender, EventArgs e)
2024-04-07 16:54:02 +04:00
{
DialogResult = DialogResult.Cancel;
Close();
}
}
2024-05-12 14:38:32 +04:00
}