PIbd-21_RazubaevSM_Plumbing.../PlumbingRepair/PlumbingRepairView/FormStoreReplenishment.cs

118 lines
4.3 KiB
C#

using Microsoft.Extensions.Logging;
using PlumbingRepairContracts.BusinessLogicsContracts;
using PlumbingRepairContracts.SearchModels;
namespace PlumbingRepairView
{
public partial class FormStoreReplenishment : Form
{
private readonly ILogger _logger;
private readonly IWorkLogic _workLogic;
private readonly IShopLogic _logicShop;
public FormStoreReplenishment(ILogger<FormStoreReplenishment> logger, IWorkLogic workLogic, IShopLogic logicShop)
{
InitializeComponent();
_logger = logger;
_workLogic = workLogic;
_logicShop = logicShop;
}
private void FormStoreReplenishment_Load(object sender, EventArgs e)
{
_logger.LogInformation("Works loading");
try
{
var list = _workLogic.ReadList(null);
if (list != null)
{
comboBoxWork.DisplayMember = "WorkName";
comboBoxWork.ValueMember = "Id";
comboBoxWork.DataSource = list;
comboBoxWork.SelectedItem = null;
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Works loading error");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
_logger.LogInformation("Shops loading");
try
{
var list = _logicShop.ReadList(null);
if (list != null)
{
comboBoxShop.DisplayMember = "ShopName";
comboBoxShop.ValueMember = "Id";
comboBoxShop.DataSource = list;
comboBoxShop.SelectedItem = null;
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Shops loading error");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ButtonSave_Click(object sender, EventArgs e)
{
if (comboBoxShop.SelectedValue == null)
{
MessageBox.Show("Выберите магазин", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (comboBoxWork.SelectedValue == null)
{
MessageBox.Show("Выберите работу", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (string.IsNullOrEmpty(textBoxCount.Text))
{
MessageBox.Show("Заполните поле Количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_logger.LogInformation("Shop replenishment");
try
{
var work = _workLogic.ReadElement(new WorkSearchModel
{ Id = Convert.ToInt32(comboBoxWork.SelectedValue) });
if (work == null)
{
throw new Exception("Работа не найдена.");
}
var operationResult = _logicShop.StoreReplenishment(new ShopSearchModel
{
Id = Convert.ToInt32(comboBoxShop.SelectedValue)
},
work,
Convert.ToInt32(textBoxCount.Text));
if (!operationResult)
{
throw new Exception("Ошибка при проведении поставки.");
}
MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
DialogResult = DialogResult.OK;
Close();
}
catch (Exception ex)
{
_logger.LogError(ex, "Shop replenishment error");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
DialogResult = DialogResult.OK;
Close();
}
private void ButtonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
}
}