PIbd-23-Lisov-N.A-SewingDre.../SewingDresses/FormMaterial.cs

98 lines
3.2 KiB
C#

using DressAtelierContracts.BusinessLogicContracts;
using DressAtelierContracts.SearchModels;
using DressAtelierContracts.BindingModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SewingDresses
{
public partial class FormMaterial : Form
{
private readonly ILogger _logger;
private readonly IMaterialLogic _logic;
private int? _id;
public int ID { set { _id = value; } }
public FormMaterial(ILogger<FormMaterial> logger, IMaterialLogic logic)
{
InitializeComponent();
_logger = logger;
_logic = logic;
}
private void FormComponent_Load(object sender, EventArgs e)
{
if (_id.HasValue)
{
try
{
_logger.LogInformation("Receiving component");
var view = _logic.ReadElement(new MaterialSearchModel
{
ID = _id.Value
});
if (view != null)
{
componentNameTextBox.Text = view.ComponentName;
componentPriceTextBox.Text = view.Cost.ToString();
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Error receiving component");
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
private void ButtonSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(componentNameTextBox.Text))
{
MessageBox.Show("Fill name field", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_logger.LogInformation("Saving component");
try
{
var model = new MaterialBindingModel
{
ID = _id ?? 0,
ComponentName = componentNameTextBox.Text,
Cost = Convert.ToDouble(componentPriceTextBox.Text)
};
var operationResult = _id.HasValue ? _logic.Update(model) :
_logic.Create(model);
if (!operationResult)
{
throw new Exception("Error during saving. Extra info in logs.");
}
MessageBox.Show("Saving was succesful", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
DialogResult = DialogResult.OK;
Close();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error saving component");
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
private void ButtonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
}
}