PIbd-21_Anisin_R.S._CarRepa.../CarRepairShop/CarRepairShopView/FormSale.cs
2024-04-27 22:23:26 +04:00

88 lines
3.1 KiB
C#

using CarRepairShopContracts.BusinessLogicsContracts;
using CarRepairShopContracts.BindingModels;
using Microsoft.Extensions.Logging;
namespace CarRepairShopView
{
public partial class FormSale : Form
{
private readonly ILogger _logger;
private readonly IRepairLogic _logicRepair;
private readonly IShopLogic _logicShop;
public FormSale(ILogger<FormMakeShipment> logger, IRepairLogic logicRepair, IShopLogic logicShop)
{
InitializeComponent();
_logger = logger;
_logicRepair = logicRepair;
_logicShop = logicShop;
}
private void FormRepairSale_Load(object sender, EventArgs e)
{
_logger.LogInformation("Repairs 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);
}
}
private void ButtonSale_Click(object sender, EventArgs e)
{
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("Repair sale");
try
{
var operationResult = _logicShop.MakeSale(
new RepairBindingModel
{
Id = Convert.ToInt32(comboBoxRepair.SelectedValue)
},
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, "Repair sale error");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ButtonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
}
}