PIbd-21_Anisin_R.S._CarRepa.../CarRepairShop/CarRepairShopView/FormMakeShipment.cs
2024-04-14 02:36:43 +04:00

117 lines
4.3 KiB
C#

using CarRepairShopContracts.BusinessLogicsContracts;
using CarRepairShopContracts.SearchModels;
using Microsoft.Extensions.Logging;
namespace CarRepairShopView
{
public partial class FormMakeShipment : Form
{
private readonly ILogger _logger;
private readonly IRepairLogic _logicRepair;
private readonly IShopLogic _logicShop;
public FormMakeShipment(ILogger<FormMakeShipment> logger, IRepairLogic logicRepair, IShopLogic logicShop)
{
InitializeComponent();
_logger = logger;
_logicRepair = logicRepair;
_logicShop = logicShop;
}
private void FormMakeShipment_Load(object sender, EventArgs e)
{
_logger.LogInformation("Reapirs loading");
try
{
var list = _logicRepair.ReadList(null);
if (list != null)
{
comboBoxRepair.DisplayMember = "RepairName";
comboBoxRepair.ValueMember = "Id";
comboBoxRepair.DataSource = list;
comboBoxRepair.SelectedItem = null;
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Repairs 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 (comboBoxRepair.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 repair = _logicRepair.ReadElement(new RepairSearchModel
{ Id = Convert.ToInt32(comboBoxRepair.SelectedValue) });
if (repair == null)
{
throw new Exception("Ремонт не найден.");
}
var operationResult = _logicShop.MakeShipment(new ShopSearchModel
{
Id = Convert.ToInt32(comboBoxShop.SelectedValue)
},
repair,
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();
}
}
}