diff --git a/JewelryStore/FormCreateOrder.Designer.cs b/JewelryStore/FormCreateOrder.Designer.cs new file mode 100644 index 0000000..57c334d --- /dev/null +++ b/JewelryStore/FormCreateOrder.Designer.cs @@ -0,0 +1,144 @@ +namespace JewelryStore +{ + partial class FormCreateOrder + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.comboBoxJewel = new System.Windows.Forms.ComboBox(); + this.labelJewel = new System.Windows.Forms.Label(); + this.labelCount = new System.Windows.Forms.Label(); + this.labelSum = new System.Windows.Forms.Label(); + this.textBoxCount = new System.Windows.Forms.TextBox(); + this.textBoxSum = new System.Windows.Forms.TextBox(); + this.buttonSave = new System.Windows.Forms.Button(); + this.buttonCancel = new System.Windows.Forms.Button(); + this.SuspendLayout(); + // + // comboBoxJewel + // + this.comboBoxJewel.FormattingEnabled = true; + this.comboBoxJewel.Location = new System.Drawing.Point(115, 14); + this.comboBoxJewel.Name = "comboBoxJewel"; + this.comboBoxJewel.Size = new System.Drawing.Size(250, 33); + this.comboBoxJewel.TabIndex = 0; + // + // labelJewel + // + this.labelJewel.AutoSize = true; + this.labelJewel.Location = new System.Drawing.Point(12, 22); + this.labelJewel.Name = "labelJewel"; + this.labelJewel.Size = new System.Drawing.Size(80, 25); + this.labelJewel.TabIndex = 1; + this.labelJewel.Text = "Изделие"; + // + // labelCount + // + this.labelCount.AutoSize = true; + this.labelCount.Location = new System.Drawing.Point(12, 67); + this.labelCount.Name = "labelCount"; + this.labelCount.Size = new System.Drawing.Size(107, 25); + this.labelCount.TabIndex = 2; + this.labelCount.Text = "Количество"; + // + // labelSum + // + this.labelSum.AutoSize = true; + this.labelSum.Location = new System.Drawing.Point(12, 118); + this.labelSum.Name = "labelSum"; + this.labelSum.Size = new System.Drawing.Size(67, 25); + this.labelSum.TabIndex = 3; + this.labelSum.Text = "Сумма"; + // + // textBoxCount + // + this.textBoxCount.Location = new System.Drawing.Point(115, 64); + this.textBoxCount.Name = "textBoxCount"; + this.textBoxCount.Size = new System.Drawing.Size(250, 31); + this.textBoxCount.TabIndex = 4; + this.textBoxCount.TextChanged += new System.EventHandler(this.textBoxCount_TextChanged); + // + // textBoxSum + // + this.textBoxSum.Location = new System.Drawing.Point(115, 115); + this.textBoxSum.Name = "textBoxSum"; + this.textBoxSum.ReadOnly = true; + this.textBoxSum.Size = new System.Drawing.Size(250, 31); + this.textBoxSum.TabIndex = 5; + // + // buttonSave + // + this.buttonSave.Location = new System.Drawing.Point(31, 161); + this.buttonSave.Name = "buttonSave"; + this.buttonSave.Size = new System.Drawing.Size(112, 34); + this.buttonSave.TabIndex = 6; + this.buttonSave.Text = "Сохранить"; + this.buttonSave.UseVisualStyleBackColor = true; + this.buttonSave.Click += new System.EventHandler(this.buttonSave_Click); + // + // buttonCancel + // + this.buttonCancel.Location = new System.Drawing.Point(253, 161); + this.buttonCancel.Name = "buttonCancel"; + this.buttonCancel.Size = new System.Drawing.Size(112, 34); + this.buttonCancel.TabIndex = 7; + this.buttonCancel.Text = "Отмена"; + this.buttonCancel.UseVisualStyleBackColor = true; + this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click); + // + // FormCreateOrder + // + this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(401, 207); + this.Controls.Add(this.buttonCancel); + this.Controls.Add(this.buttonSave); + this.Controls.Add(this.textBoxSum); + this.Controls.Add(this.textBoxCount); + this.Controls.Add(this.labelSum); + this.Controls.Add(this.labelCount); + this.Controls.Add(this.labelJewel); + this.Controls.Add(this.comboBoxJewel); + this.Name = "FormCreateOrder"; + this.Text = "Заказ"; + this.Load += new System.EventHandler(this.OrderForm_Load); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private ComboBox comboBoxJewel; + private Label labelJewel; + private Label labelCount; + private Label labelSum; + private TextBox textBoxCount; + private TextBox textBoxSum; + private Button buttonSave; + private Button buttonCancel; + } +} \ No newline at end of file diff --git a/JewelryStore/FormCreateOrder.cs b/JewelryStore/FormCreateOrder.cs new file mode 100644 index 0000000..b6c1d21 --- /dev/null +++ b/JewelryStore/FormCreateOrder.cs @@ -0,0 +1,127 @@ +using JewelryStoreContracts.BindingModels; +using JewelryStoreContracts.BusinessLogicsContracts; +using JewelryStoreContracts.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 JewelryStore +{ + public partial class FormCreateOrder : Form + { + private readonly ILogger _logger; + private readonly IJewelLogic _logicP; + private readonly IOrderLogic _logicO; + public FormCreateOrder(ILogger logger, IJewelLogic logicP, IOrderLogic logicO) + { + InitializeComponent(); + _logger = logger; + _logicP = logicP; + _logicO = logicO; + } + + private void OrderForm_Load(object sender, EventArgs e) + { + _logger.LogInformation("Загрузка драгоценностей для заказа"); + try + { + var list = _logicP.ReadList(null); + if (list != null) + { + comboBoxJewel.DisplayMember = "Драгоценность"; + comboBoxJewel.ValueMember = "Id"; + comboBoxJewel.DataSource = list.Select(c => c.JewelName).ToList(); + comboBoxJewel.SelectedItem = null; + } + + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки списка драгоценностей"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + private void CalcSum() + { + if (comboBoxJewel.SelectedValue != null && !string.IsNullOrEmpty(textBoxCount.Text)) + { + try + { + int id = Convert.ToInt32(comboBoxJewel.SelectedIndex) + 1; + var Jewel = _logicP.ReadElement(new JewelSearchModel + { + Id = id + }); + int count = Convert.ToInt32(textBoxCount.Text); + textBoxSum.Text = Math.Round(count * (Jewel?.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 buttonSave_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxCount.Text)) + { + MessageBox.Show("Заполните поле Количество", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (comboBoxJewel.SelectedValue == null) + { + MessageBox.Show("Выберите драгоценность", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Создание заказа"); + try + { + var operationResult = _logicO.CreateOrder(new OrderBindingModel + { + JewelId = Convert.ToInt32(comboBoxJewel.SelectedIndex) + 1, + JewelName = comboBoxJewel.SelectedValue.ToString(), + 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, "Ошибка создания заказа"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + + } + + private void buttonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + } +} diff --git a/JewelryStore/FormCreateOrder.resx b/JewelryStore/FormCreateOrder.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/JewelryStore/FormCreateOrder.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/JewelryStore/FormJewel.cs b/JewelryStore/FormJewel.cs index a54a46c..c820b68 100644 --- a/JewelryStore/FormJewel.cs +++ b/JewelryStore/FormJewel.cs @@ -112,8 +112,8 @@ namespace JewelryStore { if (dataGridView.SelectedRows.Count == 1) { - var service = Program.ServiceProvider?.GetService(typeof(JewelComponentForm)); - if (service is JewelComponentForm form) + var service = Program.ServiceProvider?.GetService(typeof(FormJewelComponent)); + if (service is FormJewelComponent form) { int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value); diff --git a/JewelryStore/FormJewels.cs b/JewelryStore/FormJewels.cs index 411c317..dc4f263 100644 --- a/JewelryStore/FormJewels.cs +++ b/JewelryStore/FormJewels.cs @@ -10,7 +10,7 @@ using System.Windows.Forms; namespace JewelryStore { - public partial class FormJewels : Form + public partial class FormJewels : Form //TODO { public FormJewels() { diff --git a/JewelryStoreContracts/BindingModels/OrderBindingModel.cs b/JewelryStoreContracts/BindingModels/OrderBindingModel.cs index be96d50..37e87a9 100644 --- a/JewelryStoreContracts/BindingModels/OrderBindingModel.cs +++ b/JewelryStoreContracts/BindingModels/OrderBindingModel.cs @@ -12,6 +12,7 @@ namespace JewelryStoreContracts.BindingModels { public int Id { get; set; } public int JewelId { get; set; } + public string JewelName { get; set; } public int Count { get; set; } public double Sum { get; set; } public OrderStatus Status { get; set; } = OrderStatus.Неизвестен;