104 lines
3.3 KiB
C#
104 lines
3.3 KiB
C#
using FurnitureAssemblyContracts.BindingModels;
|
|
using FurnitureAssemblyContracts.BusinessLogicsContracts;
|
|
using FurnitureAssemblyContracts.SearchModels;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace FurnitureAssemblyView
|
|
{
|
|
// Ôîðìà "Çàãîòîâêà"
|
|
public partial class FormKomment : Form
|
|
{
|
|
private readonly ILogger _logger;
|
|
|
|
private readonly IKommentLogic _logic;
|
|
|
|
private int? _id;
|
|
|
|
public int Id { set { _id = value; } }
|
|
|
|
// Êîíñòðóêòîð
|
|
public FormKomment(ILogger<FormKomment> logger, IKommentLogic 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 KommentSearchModel { 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 KommentBindingModel
|
|
{
|
|
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();
|
|
}
|
|
}
|
|
} |