diff --git a/JewelryStore/FormJewel.Designer.cs b/JewelryStore/FormJewel.Designer.cs new file mode 100644 index 0000000..ffaa849 --- /dev/null +++ b/JewelryStore/FormJewel.Designer.cs @@ -0,0 +1,221 @@ +namespace JewelryStore +{ + partial class FormJewel + { + /// + /// 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.labelName = new System.Windows.Forms.Label(); + this.labelPrice = new System.Windows.Forms.Label(); + this.textBoxName = new System.Windows.Forms.TextBox(); + this.textBoxPrice = new System.Windows.Forms.TextBox(); + this.groupBoxComponent = new System.Windows.Forms.GroupBox(); + this.buttonSave = new System.Windows.Forms.Button(); + this.buttonCancel = new System.Windows.Forms.Button(); + this.dataGridView = new System.Windows.Forms.DataGridView(); + this.ColumnComponent = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.ColumnCount = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.buttonAdd = new System.Windows.Forms.Button(); + this.buttonChange = new System.Windows.Forms.Button(); + this.buttonDelete = new System.Windows.Forms.Button(); + this.buttonUpdate = new System.Windows.Forms.Button(); + this.groupBoxComponent.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); + this.SuspendLayout(); + // + // labelName + // + this.labelName.AutoSize = true; + this.labelName.Location = new System.Drawing.Point(19, 19); + this.labelName.Name = "labelName"; + this.labelName.Size = new System.Drawing.Size(94, 25); + this.labelName.TabIndex = 0; + this.labelName.Text = "Название:"; + // + // labelPrice + // + this.labelPrice.AutoSize = true; + this.labelPrice.Location = new System.Drawing.Point(19, 59); + this.labelPrice.Name = "labelPrice"; + this.labelPrice.Size = new System.Drawing.Size(108, 25); + this.labelPrice.TabIndex = 1; + this.labelPrice.Text = "Стоимость: "; + // + // textBoxName + // + this.textBoxName.Location = new System.Drawing.Point(119, 16); + this.textBoxName.Name = "textBoxName"; + this.textBoxName.Size = new System.Drawing.Size(340, 31); + this.textBoxName.TabIndex = 2; + // + // textBoxPrice + // + this.textBoxPrice.Location = new System.Drawing.Point(119, 53); + this.textBoxPrice.Name = "textBoxPrice"; + this.textBoxPrice.Size = new System.Drawing.Size(150, 31); + this.textBoxPrice.TabIndex = 3; + // + // groupBoxComponent + // + this.groupBoxComponent.Controls.Add(this.buttonUpdate); + this.groupBoxComponent.Controls.Add(this.buttonDelete); + this.groupBoxComponent.Controls.Add(this.buttonChange); + this.groupBoxComponent.Controls.Add(this.buttonAdd); + this.groupBoxComponent.Controls.Add(this.dataGridView); + this.groupBoxComponent.Location = new System.Drawing.Point(14, 108); + this.groupBoxComponent.Name = "groupBoxComponent"; + this.groupBoxComponent.Size = new System.Drawing.Size(763, 330); + this.groupBoxComponent.TabIndex = 4; + this.groupBoxComponent.TabStop = false; + this.groupBoxComponent.Text = "Компоненты"; + // + // buttonSave + // + this.buttonSave.Location = new System.Drawing.Point(431, 452); + this.buttonSave.Name = "buttonSave"; + this.buttonSave.Size = new System.Drawing.Size(112, 34); + this.buttonSave.TabIndex = 5; + this.buttonSave.Text = "Сохранить"; + this.buttonSave.UseVisualStyleBackColor = true; + this.buttonSave.Click += new System.EventHandler(this.buttonSave_Click); + // + // buttonCancel + // + this.buttonCancel.Location = new System.Drawing.Point(562, 454); + this.buttonCancel.Name = "buttonCancel"; + this.buttonCancel.Size = new System.Drawing.Size(112, 34); + this.buttonCancel.TabIndex = 6; + this.buttonCancel.Text = "Отмена"; + this.buttonCancel.UseVisualStyleBackColor = true; + this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click); + // + // dataGridView + // + this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { + this.ColumnComponent, + this.ColumnCount}); + this.dataGridView.Location = new System.Drawing.Point(25, 30); + this.dataGridView.Name = "dataGridView"; + this.dataGridView.RowHeadersWidth = 62; + this.dataGridView.RowTemplate.Height = 33; + this.dataGridView.Size = new System.Drawing.Size(554, 281); + this.dataGridView.TabIndex = 0; + // + // ColumnComponent + // + this.ColumnComponent.HeaderText = "Компонент"; + this.ColumnComponent.MinimumWidth = 8; + this.ColumnComponent.Name = "ColumnComponent"; + this.ColumnComponent.Width = 150; + // + // ColumnCount + // + this.ColumnCount.HeaderText = "Количество"; + this.ColumnCount.MinimumWidth = 8; + this.ColumnCount.Name = "ColumnCount"; + this.ColumnCount.Width = 150; + // + // buttonAdd + // + this.buttonAdd.Location = new System.Drawing.Point(602, 35); + this.buttonAdd.Name = "buttonAdd"; + this.buttonAdd.Size = new System.Drawing.Size(112, 34); + this.buttonAdd.TabIndex = 1; + this.buttonAdd.Text = "Добавить"; + this.buttonAdd.UseVisualStyleBackColor = true; + this.buttonAdd.Click += new System.EventHandler(this.buttonAdd_Click); + // + // buttonChange + // + this.buttonChange.Location = new System.Drawing.Point(602, 81); + this.buttonChange.Name = "buttonChange"; + this.buttonChange.Size = new System.Drawing.Size(112, 34); + this.buttonChange.TabIndex = 2; + this.buttonChange.Text = "Изменить"; + this.buttonChange.UseVisualStyleBackColor = true; + this.buttonChange.Click += new System.EventHandler(this.buttonChange_Click); + // + // buttonDelete + // + this.buttonDelete.Location = new System.Drawing.Point(604, 124); + this.buttonDelete.Name = "buttonDelete"; + this.buttonDelete.Size = new System.Drawing.Size(112, 34); + this.buttonDelete.TabIndex = 3; + this.buttonDelete.Text = "Удалить"; + this.buttonDelete.UseVisualStyleBackColor = true; + this.buttonDelete.Click += new System.EventHandler(this.buttonDelete_Click); + // + // buttonUpdate + // + this.buttonUpdate.Location = new System.Drawing.Point(604, 164); + this.buttonUpdate.Name = "buttonUpdate"; + this.buttonUpdate.Size = new System.Drawing.Size(112, 34); + this.buttonUpdate.TabIndex = 4; + this.buttonUpdate.Text = "Обновить"; + this.buttonUpdate.UseVisualStyleBackColor = true; + this.buttonUpdate.Click += new System.EventHandler(this.buttonUpdate_Click); + // + // FormJewel + // + this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(800, 495); + this.Controls.Add(this.buttonCancel); + this.Controls.Add(this.buttonSave); + this.Controls.Add(this.groupBoxComponent); + this.Controls.Add(this.textBoxPrice); + this.Controls.Add(this.textBoxName); + this.Controls.Add(this.labelPrice); + this.Controls.Add(this.labelName); + this.Name = "FormJewel"; + this.Text = "Драгоценность"; + this.Load += new System.EventHandler(this.FormJewel_Load); + this.groupBoxComponent.ResumeLayout(false); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private Label labelName; + private Label labelPrice; + private TextBox textBoxName; + private TextBox textBoxPrice; + private GroupBox groupBoxComponent; + private Button buttonUpdate; + private Button buttonDelete; + private Button buttonChange; + private Button buttonAdd; + private DataGridView dataGridView; + private DataGridViewTextBoxColumn ColumnComponent; + private DataGridViewTextBoxColumn ColumnCount; + private Button buttonSave; + private Button buttonCancel; + } +} \ No newline at end of file diff --git a/JewelryStore/FormJewel.cs b/JewelryStore/FormJewel.cs new file mode 100644 index 0000000..a54a46c --- /dev/null +++ b/JewelryStore/FormJewel.cs @@ -0,0 +1,229 @@ +using JewelryStoreContracts.BindingModels; +using JewelryStoreContracts.BusinessLogicsContracts; +using JewelryStoreContracts.SearchModels; +using JewelryStoreDataModels.Models; +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 FormJewel : Form + { + private readonly ILogger _logger; + private readonly IJewelLogic _logic; + private int? _id; + private Dictionary _JewelComponents; + public int Id { set { _id = value; } } + public FormJewel(ILogger logger, IJewelLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + _JewelComponents = new Dictionary(); + } + + private void FormJewel_Load(object sender, EventArgs e) + { + if (_id.HasValue) + { + _logger.LogInformation("Загрузка драгоценности"); + try + { + var view = _logic.ReadElement(new JewelSearchModel + { + Id = _id.Value + }); + if (view != null) + { + textBoxName.Text = view.JewelName; + textBoxPrice.Text = view.Price.ToString(); + _JewelComponents = view.JewelComponents ?? new + Dictionary(); + LoadData(); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки драгоценности"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + + } + private void LoadData() + { + _logger.LogInformation("Загрузка компонент драгоценности"); + try + { + if (_JewelComponents != null) + { + dataGridView.Rows.Clear(); + foreach (var pc in _JewelComponents) + { + dataGridView.Rows.Add(new object[] { pc.Value.Item1.ComponentName, pc.Value.Item2 }); + } + textBoxPrice.Text = CalcPrice().ToString(); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки компонент драгоценности"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + + private void buttonAdd_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormJewelComponent)); + if (service is FormJewelComponent form) + { + if (form.ShowDialog() == DialogResult.OK) + { + if (form.ComponentModel == null) + { + return; + } + _logger.LogInformation("Добавление нового компонента:{ ComponentName} - { Count}", form.ComponentModel.ComponentName, form.Count); + if (_JewelComponents.ContainsKey(form.Id)) + { + _JewelComponents[form.Id] = (form.ComponentModel, form.Count); + } + else + { + _JewelComponents.Add(form.Id, (form.ComponentModel, form.Count)); + } + LoadData(); + } + } + + } + + private void buttonChange_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + var service = Program.ServiceProvider?.GetService(typeof(JewelComponentForm)); + if (service is JewelComponentForm form) + { + int id = + Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value); + form.Id = id; + form.Count = _JewelComponents[id].Item2; + if (form.ShowDialog() == DialogResult.OK) + { + if (form.ComponentModel == null) + { + return; + } + _logger.LogInformation("Изменение компонента: { ComponentName} - { Count}", form.ComponentModel.ComponentName, form.Count); + _JewelComponents[form.Id] = (form.ComponentModel, form.Count); + LoadData(); + } + } + } + + } + + private void buttonDelete_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + if (MessageBox.Show("Удалить запись?", "Вопрос", + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + try + { + _logger.LogInformation("Удаление компонента: { ComponentName} - { Count}", dataGridView.SelectedRows[0].Cells[1].Value); + _JewelComponents?.Remove(Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value)); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + LoadData(); + } + } + + } + + private void buttonUpdate_Click(object sender, EventArgs e) + { + LoadData(); + } + + private void buttonSave_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxName.Text)) + { + MessageBox.Show("Заполните название", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (string.IsNullOrEmpty(textBoxPrice.Text)) + { + MessageBox.Show("Заполните цену", "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + return; + } + if (_JewelComponents == null || _JewelComponents.Count == 0) + { + MessageBox.Show("Заполните компоненты", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Сохранение драгоценности"); + try + { + var model = new JewelBindingModel + { + Id = _id ?? 0, + JewelName = textBoxName.Text, + Price = Convert.ToDouble(textBoxPrice.Text), + JewelComponents = _JewelComponents + }; + var operationResult = _id.HasValue ? _logic.Update(model) : + _logic.Create(model); + 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(); + } + private double CalcPrice() + { + double price = 0; + foreach (var elem in _JewelComponents) + { + price += ((elem.Value.Item1?.Cost ?? 0) * elem.Value.Item2); + } + return Math.Round(price * 1.1, 2); + } + } +} diff --git a/JewelryStore/FormJewel.resx b/JewelryStore/FormJewel.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/JewelryStore/FormJewel.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/FormJewels.Designer.cs b/JewelryStore/FormJewels.Designer.cs new file mode 100644 index 0000000..1f178f8 --- /dev/null +++ b/JewelryStore/FormJewels.Designer.cs @@ -0,0 +1,39 @@ +namespace JewelryStore +{ + partial class FormJewels + { + /// + /// 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.components = new System.ComponentModel.Container(); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(800, 450); + this.Text = "FormJewels"; + } + + #endregion + } +} \ No newline at end of file diff --git a/JewelryStore/FormJewels.cs b/JewelryStore/FormJewels.cs new file mode 100644 index 0000000..411c317 --- /dev/null +++ b/JewelryStore/FormJewels.cs @@ -0,0 +1,20 @@ +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 FormJewels : Form + { + public FormJewels() + { + InitializeComponent(); + } + } +} diff --git a/JewelryStore/FormJewels.resx b/JewelryStore/FormJewels.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/JewelryStore/FormJewels.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