PIbd-21_Putincev_D.M._FoodO.../FoodOrders/FoodOrdersView/FormCreateOrder.cs

147 lines
5.7 KiB
C#
Raw Permalink Normal View History

using FoodOrdersContracts.BindingModels;
using FoodOrdersContracts.BusinessLogicsContracts;
using FoodOrdersContracts.SearchModels;
using FoodOrdersContracts.ViewModels;
using Microsoft.Extensions.Logging;
using Microsoft.VisualBasic.Logging;
using System.Windows.Forms;
namespace FoodOrdersView
{
public partial class FormCreateOrder : Form
{
private readonly ILogger _logger;
private readonly IDishLogic _logicIC;
private readonly IOrderLogic _logicO;
private readonly IClientLogic _logicClient;
public FormCreateOrder(ILogger<FormCreateOrder> logger, IDishLogic logicIC, IOrderLogic logicO, IClientLogic logicClient)
{
InitializeComponent();
_logger = logger;
_logicIC = logicIC;
_logicO = logicO;
_logicClient = logicClient;
}
private void FormCreateOrder_Load(object sender, EventArgs e)
{
_logger.LogInformation("Загрузка блюда для заказа");
try
{
var FoodList = _logicIC.ReadList(null);
if (FoodList != null)
{
comboBoxFood.DisplayMember = "DishName";
comboBoxFood.ValueMember = "Id";
comboBoxFood.DataSource = FoodList;
comboBoxFood.SelectedItem = null;
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка загрузки блюда для заказа");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
_logger.LogInformation("Загрузка клиентов для заказа");
try
{
var clientList = _logicClient.ReadList(null);
if (clientList != null)
{
comboBoxClient.DisplayMember = "ClientFIO";
comboBoxClient.ValueMember = "Id";
comboBoxClient.DataSource = clientList;
comboBoxClient.SelectedItem = null;
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка загрузки клиентов для заказа");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void CalcSum()
{
if (comboBoxFood.SelectedValue != null && !string.IsNullOrEmpty(textBoxCount.Text))
{
try
{
int id = Convert.ToInt32(comboBoxFood.SelectedValue);
var product = _logicIC.ReadElement(new DishSearchModel { Id = id });
int count = Convert.ToInt32(textBoxCount.Text);
textBoxSum.Text = Math.Round(count * (product?.Price ?? 0), 2).ToString();
_logger.LogInformation("Расчет суммы заказа");
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка расчета суммы заказа");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void TextBoxCount_TextChanged(object sender, EventArgs e)
{
CalcSum();
}
private void ComboBoxFood_SelectedIndexChanged(object sender, EventArgs e)
{
CalcSum();
}
private void ButtonSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxCount.Text))
{
MessageBox.Show("Заполните поле Количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (comboBoxFood.SelectedValue == null)
{
MessageBox.Show("Выберите блюдо", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (comboBoxFood.SelectedValue == null)
{
MessageBox.Show("Выберите блюдо", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_logger.LogInformation("Order creation");
try
{
var operationResult = _logicO.CreateOrder(new OrderBindingModel
{
DishId = Convert.ToInt32(comboBoxFood.SelectedValue),
ClientId = Convert.ToInt32(comboBoxClient.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)
{
_logger.LogError(ex, "Order creation error");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ButtonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
}
}