PIbd-21_Rodionov_I.A._IceCr.../IceCreamShop/IceCreamShopView/FormIceCreamSale.cs

88 lines
3.1 KiB
C#

using IceCreamShopContracts.BusinessLogicsContracts;
using IceCreamShopContracts.BindingModels;
using Microsoft.Extensions.Logging;
namespace IceCreamShopView
{
public partial class FormIceCreamSale : Form
{
private readonly ILogger _logger;
private readonly IIceCreamLogic _logicIceCream;
private readonly IShopLogic _logicShop;
public FormIceCreamSale(ILogger<FormMakeShipment> logger, IIceCreamLogic logicIceCream, IShopLogic logicShop)
{
InitializeComponent();
_logger = logger;
_logicIceCream = logicIceCream;
_logicShop = logicShop;
}
private void FormIceCreamSale_Load(object sender, EventArgs e)
{
_logger.LogInformation("Ice creams loading");
try
{
var list = _logicIceCream.ReadList(null);
if (list != null)
{
comboBoxIceCream.DisplayMember = "IceCreamName";
comboBoxIceCream.ValueMember = "Id";
comboBoxIceCream.DataSource = list;
comboBoxIceCream.SelectedItem = null;
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Ice creams loading error");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ButtonSale_Click(object sender, EventArgs e)
{
if (comboBoxIceCream.SelectedValue == null)
{
MessageBox.Show("Выберите мороженое", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (string.IsNullOrEmpty(textBoxCount.Text))
{
MessageBox.Show("Заполните поле Количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_logger.LogInformation("Ice cream sale");
try
{
var operationResult = _logicShop.MakeSale(
new IceCreamBindingModel
{
Id = Convert.ToInt32(comboBoxIceCream.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, "Ice cream sale error");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ButtonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
}
}