From 9a7e0e839a38806971082f23a091aea81148fe4d Mon Sep 17 00:00:00 2001 From: VictoriaPresnyakova Date: Mon, 6 Feb 2023 17:46:35 +0400 Subject: [PATCH] full --- JewelryStore/FormJewel.Designer.cs | 127 +++++++++++++++------------- JewelryStore/FormJewel.cs | 5 +- JewelryStore/FormJewel.resx | 65 +------------- JewelryStore/FormJewels.Designer.cs | 88 ++++++++++++++++++- JewelryStore/FormJewels.cs | 107 ++++++++++++++++++++++- JewelryStore/FormMain.cs | 4 +- 6 files changed, 266 insertions(+), 130 deletions(-) diff --git a/JewelryStore/FormJewel.Designer.cs b/JewelryStore/FormJewel.Designer.cs index ffaa849..3111cd6 100644 --- a/JewelryStore/FormJewel.Designer.cs +++ b/JewelryStore/FormJewel.Designer.cs @@ -33,15 +33,16 @@ this.textBoxName = new System.Windows.Forms.TextBox(); this.textBoxPrice = new System.Windows.Forms.TextBox(); this.groupBoxComponent = new System.Windows.Forms.GroupBox(); + this.buttonUpdate = new System.Windows.Forms.Button(); + this.buttonDelete = new System.Windows.Forms.Button(); + this.buttonChange = new System.Windows.Forms.Button(); + this.buttonAdd = new System.Windows.Forms.Button(); + this.dataGridView = new System.Windows.Forms.DataGridView(); this.buttonSave = new System.Windows.Forms.Button(); this.buttonCancel = new System.Windows.Forms.Button(); - this.dataGridView = new System.Windows.Forms.DataGridView(); + this.ID = new System.Windows.Forms.DataGridViewTextBoxColumn(); 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(); @@ -92,6 +93,60 @@ this.groupBoxComponent.TabStop = false; this.groupBoxComponent.Text = "Компоненты"; // + // 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); + // + // 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); + // + // 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); + // + // 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); + // + // dataGridView + // + this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { + this.ID, + 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; + // // buttonSave // this.buttonSave.Location = new System.Drawing.Point(431, 452); @@ -112,18 +167,13 @@ this.buttonCancel.UseVisualStyleBackColor = true; this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click); // - // dataGridView + // ID // - 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; + this.ID.HeaderText = "Column1"; + this.ID.MinimumWidth = 8; + this.ID.Name = "ID"; + this.ID.Visible = false; + this.ID.Width = 150; // // ColumnComponent // @@ -139,46 +189,6 @@ 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); @@ -213,9 +223,10 @@ private Button buttonChange; private Button buttonAdd; private DataGridView dataGridView; - private DataGridViewTextBoxColumn ColumnComponent; - private DataGridViewTextBoxColumn ColumnCount; private Button buttonSave; private Button buttonCancel; + private DataGridViewTextBoxColumn ID; + private DataGridViewTextBoxColumn ColumnComponent; + private DataGridViewTextBoxColumn ColumnCount; } } \ No newline at end of file diff --git a/JewelryStore/FormJewel.cs b/JewelryStore/FormJewel.cs index c820b68..584076e 100644 --- a/JewelryStore/FormJewel.cs +++ b/JewelryStore/FormJewel.cs @@ -69,7 +69,7 @@ namespace JewelryStore dataGridView.Rows.Clear(); foreach (var pc in _JewelComponents) { - dataGridView.Rows.Add(new object[] { pc.Value.Item1.ComponentName, pc.Value.Item2 }); + dataGridView.Rows.Add(new object[] { pc.Key,pc.Value.Item1.ComponentName, pc.Value.Item2 }); } textBoxPrice.Text = CalcPrice().ToString(); } @@ -115,8 +115,7 @@ namespace JewelryStore var service = Program.ServiceProvider?.GetService(typeof(FormJewelComponent)); if (service is FormJewelComponent form) { - int id = - Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value); + int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value); form.Id = id; form.Count = _JewelComponents[id].Item2; if (form.ShowDialog() == DialogResult.OK) diff --git a/JewelryStore/FormJewel.resx b/JewelryStore/FormJewel.resx index 1af7de1..bb23025 100644 --- a/JewelryStore/FormJewel.resx +++ b/JewelryStore/FormJewel.resx @@ -1,64 +1,4 @@ - - - + @@ -117,4 +57,7 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + True + \ No newline at end of file diff --git a/JewelryStore/FormJewels.Designer.cs b/JewelryStore/FormJewels.Designer.cs index 1f178f8..54f228a 100644 --- a/JewelryStore/FormJewels.Designer.cs +++ b/JewelryStore/FormJewels.Designer.cs @@ -28,12 +28,94 @@ /// private void InitializeComponent() { - this.components = new System.ComponentModel.Container(); + this.DataGridView = new System.Windows.Forms.DataGridView(); + this.AddButton = new System.Windows.Forms.Button(); + this.ChangeButton = new System.Windows.Forms.Button(); + this.DeleteButton = new System.Windows.Forms.Button(); + this.UpdateButton = new System.Windows.Forms.Button(); + ((System.ComponentModel.ISupportInitialize)(this.DataGridView)).BeginInit(); + this.SuspendLayout(); + // + // DataGridView + // + this.DataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.DataGridView.Location = new System.Drawing.Point(1, 2); + this.DataGridView.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); + this.DataGridView.Name = "DataGridView"; + this.DataGridView.RowHeadersWidth = 62; + this.DataGridView.RowTemplate.Height = 25; + this.DataGridView.Size = new System.Drawing.Size(597, 547); + this.DataGridView.TabIndex = 0; + // + // AddButton + // + this.AddButton.Location = new System.Drawing.Point(615, 12); + this.AddButton.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); + this.AddButton.Name = "AddButton"; + this.AddButton.Size = new System.Drawing.Size(171, 39); + this.AddButton.TabIndex = 1; + this.AddButton.Text = "Добавить"; + this.AddButton.UseVisualStyleBackColor = true; + this.AddButton.Click += new System.EventHandler(this.AddButton_Click); + // + // ChangeButton + // + this.ChangeButton.Location = new System.Drawing.Point(615, 110); + this.ChangeButton.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); + this.ChangeButton.Name = "ChangeButton"; + this.ChangeButton.Size = new System.Drawing.Size(171, 39); + this.ChangeButton.TabIndex = 2; + this.ChangeButton.Text = "Изменить"; + this.ChangeButton.UseVisualStyleBackColor = true; + this.ChangeButton.Click += new System.EventHandler(this.ChangeButton_Click); + // + // DeleteButton + // + this.DeleteButton.Location = new System.Drawing.Point(615, 209); + this.DeleteButton.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); + this.DeleteButton.Name = "DeleteButton"; + this.DeleteButton.Size = new System.Drawing.Size(171, 39); + this.DeleteButton.TabIndex = 3; + this.DeleteButton.Text = "Удалить"; + this.DeleteButton.UseVisualStyleBackColor = true; + this.DeleteButton.Click += new System.EventHandler(this.DeleteButton_Click); + // + // UpdateButton + // + this.UpdateButton.Location = new System.Drawing.Point(615, 310); + this.UpdateButton.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); + this.UpdateButton.Name = "UpdateButton"; + this.UpdateButton.Size = new System.Drawing.Size(171, 39); + this.UpdateButton.TabIndex = 4; + this.UpdateButton.Text = "Обновить"; + this.UpdateButton.UseVisualStyleBackColor = true; + this.UpdateButton.Click += new System.EventHandler(this.UpdateButton_Click); + // + // FormJewels + // + this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 450); - this.Text = "FormJewels"; + this.ClientSize = new System.Drawing.Size(805, 563); + this.Controls.Add(this.UpdateButton); + this.Controls.Add(this.DeleteButton); + this.Controls.Add(this.ChangeButton); + this.Controls.Add(this.AddButton); + this.Controls.Add(this.DataGridView); + this.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); + this.Name = "FormJewels"; + this.Text = "Драгоценности"; + this.Load += new System.EventHandler(this.FormJewels_Load); + ((System.ComponentModel.ISupportInitialize)(this.DataGridView)).EndInit(); + this.ResumeLayout(false); + } #endregion + + private DataGridView DataGridView; + private Button AddButton; + private Button ChangeButton; + private Button DeleteButton; + private Button UpdateButton; } } \ No newline at end of file diff --git a/JewelryStore/FormJewels.cs b/JewelryStore/FormJewels.cs index dc4f263..bea74a4 100644 --- a/JewelryStore/FormJewels.cs +++ b/JewelryStore/FormJewels.cs @@ -1,4 +1,5 @@ -using System; +using Microsoft.Extensions.Logging; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; @@ -7,14 +8,114 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; - +using JewelryStore; +using JewelryStoreContracts.BindingModels; +using JewelryStoreContracts.BusinessLogicsContracts; namespace JewelryStore { public partial class FormJewels : Form //TODO { - public FormJewels() + private readonly ILogger _logger; + private readonly IJewelLogic _logic; + + public FormJewels(ILogger logger, IJewelLogic logic) { InitializeComponent(); + _logger = logger; + _logic = logic; } + + private void LoadData() + { + try + { + var list = _logic.ReadList(null); + + if (list != null) + { + DataGridView.DataSource = list; + DataGridView.Columns["Id"].Visible = false; + DataGridView.Columns["JewelName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + } + + _logger.LogInformation("Загрузка компонентов"); + + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки компонентов"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void AddButton_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormJewel)); + + if (service is FormJewel form) + { + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + private void ChangeButton_Click(object sender, EventArgs e) + { + if (DataGridView.SelectedRows.Count == 1) + { + var service = Program.ServiceProvider?.GetService(typeof(FormJewel)); + + if (service is FormJewel form) + { + form.Id = Convert.ToInt32(DataGridView.SelectedRows[0].Cells["Id"].Value); + + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + } + private void DeleteButton_Click(object sender, EventArgs e) + { + if (DataGridView.SelectedRows.Count == 1) + { + if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + int id = Convert.ToInt32(DataGridView.SelectedRows[0].Cells["Id"].Value); + _logger.LogInformation("Удаление компонента"); + + try + { + if (!_logic.Delete(new JewelBindingModel + { + Id = id + })) + { + throw new Exception("Ошибка при удалении. Дополнительная информация в логах."); + } + + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка удаления компонента"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + } + private void UpdateButton_Click(object sender, EventArgs e) + { + LoadData(); + } + + + private void FormJewels_Load(object sender, EventArgs e) + { + LoadData(); + } + } } diff --git a/JewelryStore/FormMain.cs b/JewelryStore/FormMain.cs index 284076e..3baf773 100644 --- a/JewelryStore/FormMain.cs +++ b/JewelryStore/FormMain.cs @@ -64,9 +64,9 @@ namespace JewelryStore private void драгоценностиToolStripMenuItem_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormJewel)); + var service = Program.ServiceProvider?.GetService(typeof(FormJewels)); - if (service is FormJewel form) + if (service is FormJewels form) { form.ShowDialog(); }