ISEbd_21_Kuklew_M.I._Softwa.../SoftwareInstallation/FormCreateOrder.cs

119 lines
4.5 KiB
C#
Raw Normal View History

2024-04-07 17:05:30 +04:00
using SoftwareInstallationContracts.BindingModels;
2024-04-07 16:54:02 +04:00
using SoftwareInstallationContracts.BusinessLogicsContracts;
using SoftwareInstallationContracts.SearchModels;
2024-05-12 14:38:32 +04:00
using SoftwareInstallationContracts.ViewModels;
2024-04-07 17:05:30 +04:00
using Microsoft.Extensions.Logging;
2024-04-07 16:54:02 +04:00
2024-05-12 14:38:32 +04:00
namespace SoftwareInstallationView
2024-04-07 16:54:02 +04:00
{
public partial class FormCreateOrder : Form
{
private readonly ILogger _logger;
2024-05-12 14:38:32 +04:00
private readonly IPackageLogic _logicIC;
2024-04-07 16:54:02 +04:00
private readonly IOrderLogic _logicO;
2024-05-12 14:38:32 +04:00
public FormCreateOrder(ILogger<FormCreateOrder> logger, IPackageLogic logicIC, IOrderLogic logicO)
2024-04-07 16:54:02 +04:00
{
InitializeComponent();
_logger = logger;
2024-05-12 14:38:32 +04:00
_logicIC = logicIC;
2024-04-07 16:54:02 +04:00
_logicO = logicO;
}
private void FormCreateOrder_Load(object sender, EventArgs e)
{
2024-05-12 14:38:32 +04:00
_logger.LogInformation("Loading ice cream for order");
2024-04-07 17:05:30 +04:00
try
{
2024-05-12 14:38:32 +04:00
var iceCreamList = _logicIC.ReadList(null);
if (iceCreamList != null)
2024-04-07 17:05:30 +04:00
{
comboBoxPackage.DisplayMember = "PackageName";
comboBoxPackage.ValueMember = "Id";
2024-05-12 14:38:32 +04:00
comboBoxPackage.DataSource = iceCreamList;
2024-04-07 17:05:30 +04:00
comboBoxPackage.SelectedItem = null;
}
}
catch (Exception ex)
2024-04-07 16:54:02 +04:00
{
2024-05-12 14:38:32 +04:00
_logger.LogError(ex, "Error during loading ice cream for order");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
2024-04-07 16:54:02 +04:00
}
}
2024-05-12 14:38:32 +04:00
2024-04-07 16:54:02 +04:00
private void CalcSum()
{
if (comboBoxPackage.SelectedValue != null && !string.IsNullOrEmpty(textBoxCount.Text))
{
try
{
int id = Convert.ToInt32(comboBoxPackage.SelectedValue);
2024-05-12 14:38:32 +04:00
var product = _logicIC.ReadElement(new PackageSearchModel { Id = id });
2024-04-07 16:54:02 +04:00
int count = Convert.ToInt32(textBoxCount.Text);
2024-05-12 14:38:32 +04:00
textBoxSum.Text = Math.Round(count * (product?.Price ?? 0), 2).ToString();
_logger.LogInformation("Calculation of order sum");
2024-04-07 16:54:02 +04:00
}
catch (Exception ex)
{
2024-05-12 14:38:32 +04:00
_logger.LogError(ex, "Calculation of order sum error");
2024-04-07 16:54:02 +04:00
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
2024-05-12 14:38:32 +04:00
private void TextBoxCount_TextChanged(object sender, EventArgs e)
2024-04-07 16:54:02 +04:00
{
CalcSum();
}
private void ComboBoxPackage_SelectedIndexChanged(object sender, EventArgs e)
{
CalcSum();
}
2024-05-12 14:38:32 +04:00
private void ButtonSave_Click(object sender, EventArgs e)
2024-04-07 16:54:02 +04:00
{
if (string.IsNullOrEmpty(textBoxCount.Text))
{
MessageBox.Show("Заполните поле Количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (comboBoxPackage.SelectedValue == null)
{
2024-05-12 14:38:32 +04:00
MessageBox.Show("Выберите мороженое", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
2024-04-07 16:54:02 +04:00
return;
}
2024-05-12 14:38:32 +04:00
_logger.LogInformation("Order creation");
2024-04-07 16:54:02 +04:00
try
{
var operationResult = _logicO.CreateOrder(new OrderBindingModel
{
PackageId = Convert.ToInt32(comboBoxPackage.SelectedValue),
Count = Convert.ToInt32(textBoxCount.Text),
Sum = Convert.ToDouble(textBoxSum.Text)
});
if (!operationResult)
{
throw new Exception("Ошибка при создании заказа. Дополнительная информация в логах.");
}
MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
DialogResult = DialogResult.OK;
Close();
}
catch (Exception ex)
{
2024-05-12 14:38:32 +04:00
_logger.LogError(ex, "Order creation error");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
2024-04-07 16:54:02 +04:00
}
}
2024-05-12 14:38:32 +04:00
private void ButtonCancel_Click(object sender, EventArgs e)
2024-04-07 16:54:02 +04:00
{
DialogResult = DialogResult.Cancel;
Close();
}
}
}