using DressAtelierContracts.BindingModels; using DressAtelierContracts.BusinessLogicContracts; using DressAtelierContracts.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 SewingDresses { public partial class FormOrderCreation : Form { private readonly ILogger _logger; private readonly IDressLogic _logicDress; private readonly IOrderLogic _logicOrder; public FormOrderCreation(ILogger logger, IDressLogic logicP, IOrderLogic logicO) { InitializeComponent(); _logger = logger; _logicDress = logicP; _logicOrder = logicO; } private void FormOrderCreation_Load(object sender, EventArgs e) { _logger.LogInformation("Downloading dresses for order"); try { var _list = _logicDress.ReadList(null); if (_list != null) { dressComboBox.DisplayMember = "DressName"; dressComboBox.ValueMember = "ID"; dressComboBox.DataSource = _list; dressComboBox.SelectedItem = null; } } catch (Exception ex) { _logger.LogError(ex, "Downloading dresses for order error"); MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void CalcSum() { if (dressComboBox.SelectedValue != null && !string.IsNullOrEmpty(quantityTextBox.Text)) { try { int id = Convert.ToInt32(dressComboBox.SelectedValue); var dress = _logicDress.ReadElement(new DressSearchModel { ID = id, }); int count = Convert.ToInt32(quantityTextBox.Text); priceTextBox.Text = Math.Round(count * (dress?.Price ?? 0), 2).ToString(); _logger.LogInformation("Calculating total of order"); } catch (Exception ex) { _logger.LogError(ex, "Calculation total of order error"); MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,MessageBoxIcon.Error); } } } private void QuantityTextBox_TextChanged(object sender, EventArgs e) { CalcSum(); } private void DressComboBox_SelectedIndexChanged(object sender, EventArgs e) { CalcSum(); } private void ButtonSave_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(quantityTextBox.Text)) { MessageBox.Show("Fill quantity field", "Error",MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (dressComboBox.SelectedValue == null) { MessageBox.Show("Choose dress", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } _logger.LogInformation("Creation of order"); try { var operationResult = _logicOrder.CreateOrder(new OrderBindingModel { DressID = Convert.ToInt32(dressComboBox.SelectedValue), Count = Convert.ToInt32(quantityTextBox.Text), Sum = Convert.ToDouble(priceTextBox.Text) }); if (!operationResult) { throw new Exception("Creating of order error.Extra information in logs."); } MessageBox.Show("Saving was succesfull", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information); DialogResult = DialogResult.OK; Close(); } catch (Exception ex) { _logger.LogError(ex, "Creation of order error"); MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void ButtonCancel_Click(object sender, EventArgs e) { DialogResult = DialogResult.Cancel; Close(); } } }