PIbd-21_Belianin_N.N._Furni.../FurnitureAssembly/FurnitureAssemblyView/FormWorkPiece.cs
2024-02-26 10:12:21 +04:00

104 lines
3.4 KiB
C#

using FurnitureAssemblyContracts.BindingModels;
using FurnitureAssemblyContracts.BusinessLogicsContracts;
using FurnitureAssemblyContracts.SearchModels;
using Microsoft.Extensions.Logging;
namespace FurnitureAssemblyView
{
// Ôîðìà "Çàãîòîâêà"
public partial class FormWorkPiece : Form
{
private readonly ILogger _logger;
private readonly IWorkPieceLogic _logic;
private int? _id;
public int Id { set { _id = value; } }
// Êîíñòðóêòîð
public FormWorkPiece(ILogger<FormWorkPiece> logger, IWorkPieceLogic logic)
{
InitializeComponent();
_logger = logger;
_logic = logic;
}
// Ïðè çàãðóçêå ôîðìû
private void FormWorkPiece_Load(object sender, EventArgs e)
{
// Ïðîâåðêà íà çàïîëíåíèå ïîëÿ id. Åñëè îíî çàïîëíåíî, òî ïðîáóåì ïîëó÷èòü çàïèñü è âûâåñòè å¸ íà ýêðàí
if (_id.HasValue)
{
try
{
_logger.LogInformation("Ïîëó÷åíèå çàãîòîâêè");
var view = _logic.ReadElement(new WorkPieceSearchModel { Id = _id.Value });
if(view != null)
{
textBoxName.Text = view.WorkPieceName;
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 WorkPieceBindingModel
{
Id = _id ?? 0,
WorkPieceName = 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();
}
}
}