144 lines
5.1 KiB
C#
144 lines
5.1 KiB
C#
using IceCreamShopContracts.BindingModels;
|
|
using IceCreamShopContracts.BusinessLogicsContracts;
|
|
using IceCreamShopContracts.SearchModels;
|
|
using Microsoft.Extensions.Logging;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace IceCreamShop
|
|
{
|
|
public partial class OrderForm : Form
|
|
{
|
|
private readonly ILogger _logger;
|
|
private readonly IIceCreamLogic _logicI;
|
|
private readonly IOrderLogic _logicO;
|
|
public OrderForm(ILogger<OrderForm> logger, IIceCreamLogic
|
|
logicI, IOrderLogic logicO)
|
|
{
|
|
InitializeComponent();
|
|
_logger = logger;
|
|
_logicI = logicI;
|
|
_logicO = logicO;
|
|
}
|
|
|
|
private void OrderForm_Load(object sender, EventArgs e)
|
|
{
|
|
_logger.LogInformation("Загрузка мороженного для заказа");
|
|
try
|
|
{
|
|
var list = _logicI.ReadList(null);
|
|
if (list != null)
|
|
{
|
|
IceCreamComboBox.DisplayMember = "IceCreamName";
|
|
IceCreamComboBox.ValueMember = "Id";
|
|
IceCreamComboBox.DataSource = list;
|
|
IceCreamComboBox.SelectedItem = null;
|
|
}
|
|
_logger.LogInformation("Мороженное загружено");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка загрузки мороженного");
|
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
|
|
MessageBoxIcon.Error);
|
|
}
|
|
|
|
}
|
|
|
|
private void CalcSum()
|
|
{
|
|
if (IceCreamComboBox.SelectedValue != null &&
|
|
!string.IsNullOrEmpty(CountTextBox.Text))
|
|
{
|
|
try
|
|
{
|
|
int id = Convert.ToInt32(IceCreamComboBox.SelectedValue);
|
|
var iceCream = _logicI.ReadElement(new IceCreamSearchModel
|
|
{
|
|
Id
|
|
= id
|
|
});
|
|
int count = Convert.ToInt32(CountTextBox.Text);
|
|
SumTextBox.Text = Math.Round(count * (iceCream?.Price ?? 0),
|
|
2).ToString();
|
|
_logger.LogInformation("Расчет суммы заказа");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка расчета суммы заказа");
|
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
|
|
MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void CountTextBox_TextChanged(object sender, EventArgs e)
|
|
{
|
|
CalcSum();
|
|
}
|
|
|
|
private void SumTextBox_TextChanged(object sender, EventArgs e)
|
|
{
|
|
CalcSum();
|
|
}
|
|
|
|
private void SaveButton_Click(object sender, EventArgs e)
|
|
{
|
|
if (string.IsNullOrEmpty(CountTextBox.Text))
|
|
{
|
|
MessageBox.Show("Заполните поле Количество", "Ошибка",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
if (IceCreamComboBox.SelectedValue == null)
|
|
{
|
|
MessageBox.Show("Выберите мороженное", "Ошибка",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
_logger.LogInformation("Создание заказа");
|
|
try
|
|
{
|
|
var operationResult = _logicO.CreateOrder(new OrderBindingModel
|
|
{
|
|
IceCreamId = Convert.ToInt32(IceCreamComboBox.SelectedValue),
|
|
Count = Convert.ToInt32(CountTextBox.Text),
|
|
Sum = Convert.ToDouble(SumTextBox.Text)
|
|
});
|
|
if (!operationResult)
|
|
{
|
|
throw new Exception("Ошибка при создании заказа. Дополнительная информация в логах.");
|
|
}
|
|
MessageBox.Show("Сохранение прошло успешно", "Сообщение",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
DialogResult = DialogResult.OK;
|
|
Close();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка создания заказа");
|
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
|
|
MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
|
|
private void CancelButton_Click(object sender, EventArgs e)
|
|
{
|
|
DialogResult = DialogResult.Cancel;
|
|
Close();
|
|
}
|
|
|
|
private void IceCreamComboBox_SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
CalcSum();
|
|
}
|
|
}
|
|
}
|