From 61d7f60024cc9f9a1ee6e4fa9b47eee4e3ec07a4 Mon Sep 17 00:00:00 2001 From: Oleg Shabunov Date: Wed, 21 Feb 2024 10:36:30 +0400 Subject: [PATCH] forms --- AutoWorkshop/Forms/FormComponent.Designer.cs | 3 + AutoWorkshop/Forms/FormComponent.cs | 74 +++++- AutoWorkshop/Forms/FormComponents.Designer.cs | 112 +++++++++ AutoWorkshop/Forms/FormComponents.cs | 112 +++++++++ AutoWorkshop/Forms/FormComponents.resx | 120 +++++++++ .../Forms/FormCreateOrder.Designer.cs | 145 +++++++++++ AutoWorkshop/Forms/FormCreateOrder.cs | 133 ++++++++++ AutoWorkshop/Forms/FormCreateOrder.resx | 120 +++++++++ AutoWorkshop/Forms/FormRepair.Designer.cs | 227 +++++++++++++++++ AutoWorkshop/Forms/FormRepair.cs | 237 ++++++++++++++++++ AutoWorkshop/Forms/FormRepair.resx | 120 +++++++++ .../Forms/FormRepairComponent.Designer.cs | 118 +++++++++ AutoWorkshop/Forms/FormRepairComponent.cs | 89 +++++++ AutoWorkshop/Forms/FormRepairComponent.resx | 120 +++++++++ AutoWorkshop/Program.cs | 40 ++- 15 files changed, 1767 insertions(+), 3 deletions(-) create mode 100644 AutoWorkshop/Forms/FormComponents.Designer.cs create mode 100644 AutoWorkshop/Forms/FormComponents.cs create mode 100644 AutoWorkshop/Forms/FormComponents.resx create mode 100644 AutoWorkshop/Forms/FormCreateOrder.Designer.cs create mode 100644 AutoWorkshop/Forms/FormCreateOrder.cs create mode 100644 AutoWorkshop/Forms/FormCreateOrder.resx create mode 100644 AutoWorkshop/Forms/FormRepair.Designer.cs create mode 100644 AutoWorkshop/Forms/FormRepair.cs create mode 100644 AutoWorkshop/Forms/FormRepair.resx create mode 100644 AutoWorkshop/Forms/FormRepairComponent.Designer.cs create mode 100644 AutoWorkshop/Forms/FormRepairComponent.cs create mode 100644 AutoWorkshop/Forms/FormRepairComponent.resx diff --git a/AutoWorkshop/Forms/FormComponent.Designer.cs b/AutoWorkshop/Forms/FormComponent.Designer.cs index e526b19..e944a75 100644 --- a/AutoWorkshop/Forms/FormComponent.Designer.cs +++ b/AutoWorkshop/Forms/FormComponent.Designer.cs @@ -76,6 +76,7 @@ CancelButton.TabIndex = 4; CancelButton.Text = "Отмена"; CancelButton.UseVisualStyleBackColor = true; + CancelButton.Click += ButtonCancel_Click; // // SaveButton // @@ -85,6 +86,7 @@ SaveButton.TabIndex = 5; SaveButton.Text = "Сохранить"; SaveButton.UseVisualStyleBackColor = true; + SaveButton.Click += ButtonSave_Click; // // FormComponent // @@ -99,6 +101,7 @@ Controls.Add(ComponentNameLabel); Name = "FormComponent"; Text = "Компонент"; + Load += FormComponent_Load; ResumeLayout(false); PerformLayout(); } diff --git a/AutoWorkshop/Forms/FormComponent.cs b/AutoWorkshop/Forms/FormComponent.cs index 7e1fd82..d79e3bf 100644 --- a/AutoWorkshop/Forms/FormComponent.cs +++ b/AutoWorkshop/Forms/FormComponent.cs @@ -1,5 +1,8 @@ -using AutoWorkshopContracts.BusinessLogicContracts; +using AutoWorkshopContracts.BindingModels; +using AutoWorkshopContracts.BusinessLogicContracts; +using AutoWorkshopContracts.SearchModels; using Microsoft.Extensions.Logging; +using System.Windows.Forms; namespace AutoWorkshopView.Forms { @@ -20,5 +23,74 @@ namespace AutoWorkshopView.Forms _logger = Logger; _logic = Logic; } + + private void FormComponent_Load(object sender, EventArgs e) + { + if (_id.HasValue) + { + try + { + _logger.LogInformation("Получение компонента"); + var View = _logic.ReadElement(new ComponentSearchModel + { + Id = _id.Value + }); + + if (View != null) + { + ComponentNameTextBox.Text = View.ComponentName; + ComponentPriceTextBox.Text = View.Cost.ToString(); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения компонента"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + + private void ButtonSave_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(ComponentNameTextBox.Text)) + { + MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + _logger.LogInformation("Сохранение компонента"); + + try + { + var model = new ComponentBindingModel + { + Id = _id ?? 0, + ComponentName = ComponentNameTextBox.Text, + Cost = Convert.ToDouble(ComponentPriceTextBox.Text) + }; + + 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(); + } } } diff --git a/AutoWorkshop/Forms/FormComponents.Designer.cs b/AutoWorkshop/Forms/FormComponents.Designer.cs new file mode 100644 index 0000000..c22f388 --- /dev/null +++ b/AutoWorkshop/Forms/FormComponents.Designer.cs @@ -0,0 +1,112 @@ +namespace AutoWorkshopView.Forms +{ + partial class FormComponents + { + /// + /// 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() + { + DataGridView = new DataGridView(); + AddButton = new Button(); + ChangeButton = new Button(); + DeleteButton = new Button(); + RefreshButton = new Button(); + ((System.ComponentModel.ISupportInitialize)DataGridView).BeginInit(); + SuspendLayout(); + // + // DataGridView + // + DataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + DataGridView.Location = new Point(12, 12); + DataGridView.Name = "DataGridView"; + DataGridView.Size = new Size(369, 350); + DataGridView.TabIndex = 0; + // + // AddButton + // + AddButton.Location = new Point(387, 12); + AddButton.Name = "AddButton"; + AddButton.Size = new Size(126, 23); + AddButton.TabIndex = 1; + AddButton.Text = "Добавить"; + AddButton.UseVisualStyleBackColor = true; + AddButton.Click += AddButton_Click; + // + // ChangeButton + // + ChangeButton.Location = new Point(387, 41); + ChangeButton.Name = "ChangeButton"; + ChangeButton.Size = new Size(126, 23); + ChangeButton.TabIndex = 2; + ChangeButton.Text = "Изменить"; + ChangeButton.UseVisualStyleBackColor = true; + ChangeButton.Click += ChangeButton_Click; + // + // DeleteButton + // + DeleteButton.Location = new Point(387, 70); + DeleteButton.Name = "DeleteButton"; + DeleteButton.Size = new Size(126, 23); + DeleteButton.TabIndex = 3; + DeleteButton.Text = "Удалить"; + DeleteButton.UseVisualStyleBackColor = true; + DeleteButton.Click += DeleteButton_Click; + // + // RefreshButton + // + RefreshButton.Location = new Point(387, 99); + RefreshButton.Name = "RefreshButton"; + RefreshButton.Size = new Size(126, 23); + RefreshButton.TabIndex = 4; + RefreshButton.Text = "Обновить"; + RefreshButton.UseVisualStyleBackColor = true; + RefreshButton.Click += RefreshButton_Click; + // + // ComponentsForm + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(525, 374); + Controls.Add(RefreshButton); + Controls.Add(DeleteButton); + Controls.Add(ChangeButton); + Controls.Add(AddButton); + Controls.Add(DataGridView); + Name = "ComponentsForm"; + Text = "Компоненты"; + Load += ComponentsForm_Load; + ((System.ComponentModel.ISupportInitialize)DataGridView).EndInit(); + ResumeLayout(false); + } + + #endregion + + private DataGridView DataGridView; + private Button AddButton; + private Button ChangeButton; + private Button DeleteButton; + private Button RefreshButton; + } +} \ No newline at end of file diff --git a/AutoWorkshop/Forms/FormComponents.cs b/AutoWorkshop/Forms/FormComponents.cs new file mode 100644 index 0000000..1e41122 --- /dev/null +++ b/AutoWorkshop/Forms/FormComponents.cs @@ -0,0 +1,112 @@ +using AutoWorkshopContracts.BindingModels; +using AutoWorkshopContracts.BusinessLogicContracts; +using Microsoft.Extensions.Logging; + +namespace AutoWorkshopView.Forms +{ + public partial class FormComponents : Form + { + private readonly ILogger _logger; + private readonly IComponentLogic _logic; + + public FormComponents(ILogger Logger, IComponentLogic Logic) + { + InitializeComponent(); + _logger = Logger; + _logic = Logic; + } + + private void ComponentsForm_Load(object sender, EventArgs e) + { + LoadData(); + } + + private void LoadData() + { + try + { + var List = _logic.ReadList(null); + + if (List != null) + { + DataGridView.DataSource = List; + DataGridView.Columns["Id"].Visible = false; + DataGridView.Columns["ComponentName"].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(FormComponent)); + + if (Service is FormComponent 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(FormComponent)); + if (Service is FormComponent 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 ComponentBindingModel + { + Id = id + })) + { + throw new Exception("Ошибка при удалении. Дополнительная информация в логах."); + } + + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка удаления компонента"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + } + + private void RefreshButton_Click(object sender, EventArgs e) + { + LoadData(); + } + } +} diff --git a/AutoWorkshop/Forms/FormComponents.resx b/AutoWorkshop/Forms/FormComponents.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/AutoWorkshop/Forms/FormComponents.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/AutoWorkshop/Forms/FormCreateOrder.Designer.cs b/AutoWorkshop/Forms/FormCreateOrder.Designer.cs new file mode 100644 index 0000000..a84b1ea --- /dev/null +++ b/AutoWorkshop/Forms/FormCreateOrder.Designer.cs @@ -0,0 +1,145 @@ +namespace AutoWorkshopView.Forms +{ + 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() + { + RepairComboBox = new ComboBox(); + CountTextBox = new TextBox(); + SumTextBox = new TextBox(); + SaveButton = new Button(); + CancelButton = new Button(); + RepairLabel = new Label(); + CountLabel = new Label(); + SumLabel = new Label(); + SuspendLayout(); + // + // RepairComboBox + // + RepairComboBox.FormattingEnabled = true; + RepairComboBox.Location = new Point(103, 12); + RepairComboBox.Name = "RepairComboBox"; + RepairComboBox.Size = new Size(232, 23); + RepairComboBox.TabIndex = 0; + RepairComboBox.SelectedIndexChanged += RepairComboBox_SelectedIndexChanged; + // + // CountTextBox + // + CountTextBox.Location = new Point(103, 41); + CountTextBox.Name = "CountTextBox"; + CountTextBox.Size = new Size(232, 23); + CountTextBox.TabIndex = 1; + CountTextBox.TextChanged += CountTextBox_TextChanged; + // + // SumTextBox + // + SumTextBox.Location = new Point(103, 70); + SumTextBox.Name = "SumTextBox"; + SumTextBox.ReadOnly = true; + SumTextBox.Size = new Size(232, 23); + SumTextBox.TabIndex = 2; + SumTextBox.TextChanged += SumTextBox_TextChanged; + // + // SaveButton + // + SaveButton.Location = new Point(173, 107); + SaveButton.Name = "SaveButton"; + SaveButton.Size = new Size(81, 23); + SaveButton.TabIndex = 3; + SaveButton.Text = "Сохранить"; + SaveButton.UseVisualStyleBackColor = true; + SaveButton.Click += SaveButton_Click; + // + // CancelButton + // + CancelButton.Location = new Point(260, 107); + CancelButton.Name = "CancelButton"; + CancelButton.Size = new Size(75, 23); + CancelButton.TabIndex = 4; + CancelButton.Text = "Отмена"; + CancelButton.UseVisualStyleBackColor = true; + CancelButton.Click += CancelButton_Click; + // + // RepairLabel + // + RepairLabel.AutoSize = true; + RepairLabel.Location = new Point(12, 15); + RepairLabel.Name = "RepairLabel"; + RepairLabel.Size = new Size(51, 15); + RepairLabel.TabIndex = 5; + RepairLabel.Text = "Ремонт:"; + // + // CountLabel + // + CountLabel.AutoSize = true; + CountLabel.Location = new Point(12, 44); + CountLabel.Name = "CountLabel"; + CountLabel.Size = new Size(75, 15); + CountLabel.TabIndex = 6; + CountLabel.Text = "Количество:"; + // + // SumLabel + // + SumLabel.AutoSize = true; + SumLabel.Location = new Point(12, 73); + SumLabel.Name = "SumLabel"; + SumLabel.Size = new Size(48, 15); + SumLabel.TabIndex = 7; + SumLabel.Text = "Сумма:"; + // + // FormCreateOrder + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(348, 142); + Controls.Add(SumLabel); + Controls.Add(CountLabel); + Controls.Add(RepairLabel); + Controls.Add(CancelButton); + Controls.Add(SaveButton); + Controls.Add(SumTextBox); + Controls.Add(CountTextBox); + Controls.Add(RepairComboBox); + Name = "FormCreateOrder"; + Text = "Создание заказа"; + Load += FormCreateOrder_Load; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private ComboBox RepairComboBox; + private TextBox CountTextBox; + private TextBox SumTextBox; + private Button SaveButton; + private Button CancelButton; + private Label RepairLabel; + private Label CountLabel; + private Label SumLabel; + } +} \ No newline at end of file diff --git a/AutoWorkshop/Forms/FormCreateOrder.cs b/AutoWorkshop/Forms/FormCreateOrder.cs new file mode 100644 index 0000000..1440327 --- /dev/null +++ b/AutoWorkshop/Forms/FormCreateOrder.cs @@ -0,0 +1,133 @@ +using AutoWorkshopContracts.BindingModels; +using AutoWorkshopContracts.BusinessLogicContracts; +using AutoWorkshopContracts.SearchModels; +using Microsoft.Extensions.Logging; + +namespace AutoWorkshopView.Forms +{ + public partial class FormCreateOrder : Form + { + private readonly ILogger _logger; + private readonly IRepairLogic _logicI; + private readonly IOrderLogic _logicO; + + public FormCreateOrder(ILogger Logger, IRepairLogic LogicI, IOrderLogic LogicO) + { + InitializeComponent(); + + _logger = Logger; + _logicI = LogicI; + _logicO = LogicO; + } + + private void FormCreateOrder_Load(object sender, EventArgs e) + { + _logger.LogInformation("Загрузка ремонта для заказа"); + + try + { + var list = _logicI.ReadList(null); + if (list != null) + { + RepairComboBox.DisplayMember = "RepairName"; + RepairComboBox.ValueMember = "Id"; + RepairComboBox.DataSource = list; + RepairComboBox.SelectedItem = null; + } + + _logger.LogInformation("Ремонт загружен"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки ремонта"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void CalcSum() + { + if (RepairComboBox.SelectedValue != null && !string.IsNullOrEmpty(CountTextBox.Text)) + { + try + { + int id = Convert.ToInt32(RepairComboBox.SelectedValue); + var Repair = _logicI.ReadElement(new RepairSearchModel + { + Id = id + }); + int count = Convert.ToInt32(CountTextBox.Text); + + SumTextBox.Text = Math.Round(count * (Repair?.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 (RepairComboBox.SelectedValue == null) + { + MessageBox.Show("Выберите ремонт", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + _logger.LogInformation("Создание заказа"); + + try + { + var OperationResult = _logicO.CreateOrder(new OrderBindingModel + { + RepairId = Convert.ToInt32(RepairComboBox.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 RepairComboBox_SelectedIndexChanged(object sender, EventArgs e) + { + CalcSum(); + } + } +} diff --git a/AutoWorkshop/Forms/FormCreateOrder.resx b/AutoWorkshop/Forms/FormCreateOrder.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/AutoWorkshop/Forms/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/AutoWorkshop/Forms/FormRepair.Designer.cs b/AutoWorkshop/Forms/FormRepair.Designer.cs new file mode 100644 index 0000000..b8301ef --- /dev/null +++ b/AutoWorkshop/Forms/FormRepair.Designer.cs @@ -0,0 +1,227 @@ +namespace AutoWorkshopView.Forms +{ + partial class FormRepair + { + /// + /// 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() + { + NameLabel = new Label(); + PriceLabel = new Label(); + NameTextBox = new TextBox(); + PriceTextBox = new TextBox(); + ComponentsGroupBox = new GroupBox(); + RefreshButton = new Button(); + DeleteButton = new Button(); + UpdateButton = new Button(); + AddButton = new Button(); + DataGridView = new DataGridView(); + IdColumn = new DataGridViewTextBoxColumn(); + ComponentNameColumn = new DataGridViewTextBoxColumn(); + CountColumn = new DataGridViewTextBoxColumn(); + SaveButton = new Button(); + CancelButton = new Button(); + ComponentsGroupBox.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)DataGridView).BeginInit(); + SuspendLayout(); + // + // NameLabel + // + NameLabel.AutoSize = true; + NameLabel.Location = new Point(12, 9); + NameLabel.Name = "NameLabel"; + NameLabel.Size = new Size(59, 15); + NameLabel.TabIndex = 0; + NameLabel.Text = "Название"; + // + // PriceLabel + // + PriceLabel.AutoSize = true; + PriceLabel.Location = new Point(12, 35); + PriceLabel.Name = "PriceLabel"; + PriceLabel.Size = new Size(35, 15); + PriceLabel.TabIndex = 1; + PriceLabel.Text = "Цена"; + // + // NameTextBox + // + NameTextBox.Location = new Point(77, 6); + NameTextBox.Name = "NameTextBox"; + NameTextBox.Size = new Size(237, 23); + NameTextBox.TabIndex = 2; + // + // PriceTextBox + // + PriceTextBox.Location = new Point(77, 32); + PriceTextBox.Name = "PriceTextBox"; + PriceTextBox.ReadOnly = true; + PriceTextBox.Size = new Size(100, 23); + PriceTextBox.TabIndex = 3; + // + // ComponentsGroupBox + // + ComponentsGroupBox.Controls.Add(RefreshButton); + ComponentsGroupBox.Controls.Add(DeleteButton); + ComponentsGroupBox.Controls.Add(UpdateButton); + ComponentsGroupBox.Controls.Add(AddButton); + ComponentsGroupBox.Controls.Add(DataGridView); + ComponentsGroupBox.Location = new Point(12, 64); + ComponentsGroupBox.Name = "ComponentsGroupBox"; + ComponentsGroupBox.Size = new Size(582, 287); + ComponentsGroupBox.TabIndex = 4; + ComponentsGroupBox.TabStop = false; + ComponentsGroupBox.Text = "Компоненты"; + // + // RefreshButton + // + RefreshButton.Location = new Point(481, 109); + RefreshButton.Name = "RefreshButton"; + RefreshButton.Size = new Size(75, 23); + RefreshButton.TabIndex = 4; + RefreshButton.Text = "Обновить"; + RefreshButton.UseVisualStyleBackColor = true; + RefreshButton.Click += RefreshButton_Click; + // + // DeleteButton + // + DeleteButton.Location = new Point(481, 80); + DeleteButton.Name = "DeleteButton"; + DeleteButton.Size = new Size(75, 23); + DeleteButton.TabIndex = 3; + DeleteButton.Text = "Удалить"; + DeleteButton.UseVisualStyleBackColor = true; + DeleteButton.Click += DeleteButton_Click; + // + // UpdateButton + // + UpdateButton.Location = new Point(481, 51); + UpdateButton.Name = "UpdateButton"; + UpdateButton.Size = new Size(75, 23); + UpdateButton.TabIndex = 2; + UpdateButton.Text = "Изменить"; + UpdateButton.UseVisualStyleBackColor = true; + UpdateButton.Click += UpdateButton_Click; + // + // AddButton + // + AddButton.Location = new Point(481, 22); + AddButton.Name = "AddButton"; + AddButton.Size = new Size(75, 23); + AddButton.TabIndex = 1; + AddButton.Text = "Добавить"; + AddButton.UseVisualStyleBackColor = true; + AddButton.Click += AddButton_Click; + // + // DataGridView + // + DataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + DataGridView.Columns.AddRange(new DataGridViewColumn[] { IdColumn, ComponentNameColumn, CountColumn }); + DataGridView.Location = new Point(6, 22); + DataGridView.Name = "DataGridView"; + DataGridView.RowHeadersWidth = 51; + DataGridView.Size = new Size(442, 259); + DataGridView.TabIndex = 0; + // + // IdColumn + // + IdColumn.HeaderText = "Column1"; + IdColumn.MinimumWidth = 6; + IdColumn.Name = "IdColumn"; + IdColumn.Visible = false; + IdColumn.Width = 6; + // + // ComponentNameColumn + // + ComponentNameColumn.HeaderText = "Компонент"; + ComponentNameColumn.MinimumWidth = 6; + ComponentNameColumn.Name = "ComponentNameColumn"; + ComponentNameColumn.Width = 300; + // + // CountColumn + // + CountColumn.HeaderText = "Количество"; + CountColumn.MinimumWidth = 6; + CountColumn.Name = "CountColumn"; + CountColumn.Width = 125; + // + // SaveButton + // + SaveButton.Location = new Point(438, 363); + SaveButton.Name = "SaveButton"; + SaveButton.Size = new Size(75, 23); + SaveButton.TabIndex = 5; + SaveButton.Text = "Сохранить"; + SaveButton.UseVisualStyleBackColor = true; + SaveButton.Click += SaveButton_Click; + // + // CancelButton + // + CancelButton.Location = new Point(519, 363); + CancelButton.Name = "CancelButton"; + CancelButton.Size = new Size(75, 23); + CancelButton.TabIndex = 6; + CancelButton.Text = "Отмена"; + CancelButton.UseVisualStyleBackColor = true; + // + // FormRepair + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(606, 398); + Controls.Add(CancelButton); + Controls.Add(SaveButton); + Controls.Add(ComponentsGroupBox); + Controls.Add(PriceTextBox); + Controls.Add(NameTextBox); + Controls.Add(PriceLabel); + Controls.Add(NameLabel); + Name = "FormRepair"; + Text = "Ремонт"; + Load += FormRepair_Load; + ComponentsGroupBox.ResumeLayout(false); + ((System.ComponentModel.ISupportInitialize)DataGridView).EndInit(); + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private Label NameLabel; + private Label PriceLabel; + private TextBox NameTextBox; + private TextBox PriceTextBox; + private GroupBox ComponentsGroupBox; + private Button RefreshButton; + private Button DeleteButton; + private Button UpdateButton; + private Button AddButton; + private DataGridView DataGridView; + private DataGridViewTextBoxColumn IdColumn; + private DataGridViewTextBoxColumn ComponentNameColumn; + private DataGridViewTextBoxColumn CountColumn; + private Button SaveButton; + private Button CancelButton; + } +} \ No newline at end of file diff --git a/AutoWorkshop/Forms/FormRepair.cs b/AutoWorkshop/Forms/FormRepair.cs new file mode 100644 index 0000000..422ec24 --- /dev/null +++ b/AutoWorkshop/Forms/FormRepair.cs @@ -0,0 +1,237 @@ +using AutoWorkshopContracts.BindingModels; +using AutoWorkshopContracts.BusinessLogicContracts; +using AutoWorkshopContracts.SearchModels; +using AutoWorkshopDataModels.Models; +using Microsoft.Extensions.Logging; + +namespace AutoWorkshopView.Forms +{ + public partial class FormRepair : Form + { + private readonly ILogger _logger; + + private readonly IRepairLogic _logic; + + private int? _id; + + private Dictionary _repairComponents; + + public int Id { set { _id = value; } } + + public FormRepair(ILogger Logger, IRepairLogic Logic) + { + InitializeComponent(); + _logger = Logger; + _logic = Logic; + _repairComponents = new Dictionary(); + } + + private void FormRepair_Load(object sender, EventArgs e) + { + if (_id.HasValue) + { + _logger.LogInformation("Загрузка ремонта"); + + try + { + var View = _logic.ReadElement(new RepairSearchModel + { + Id = _id.Value + }); + + if (View != null) + { + NameTextBox.Text = View.RepairName; + PriceTextBox.Text = View.Price.ToString(); + + _repairComponents = View.RepairComponents ?? new Dictionary(); + LoadData(); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки ремонта"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + + private void LoadData() + { + _logger.LogInformation("Загрузка компонентов ремонта"); + + try + { + if (_repairComponents != null) + { + DataGridView.Rows.Clear(); + foreach (var Comp in _repairComponents) + { + DataGridView.Rows.Add(new object[] { Comp.Key, Comp.Value.Item1.ComponentName, Comp.Value.Item2 }); + } + + PriceTextBox.Text = CalcPrice().ToString(); + } + } + 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(FormRepairComponent)); + + if (Service is FormRepairComponent Form) + { + if (Form.ShowDialog() == DialogResult.OK) + { + if (Form.ComponentModel == null) + { + return; + } + + _logger.LogInformation("Добавление нового компонента: {ComponentName} - {Count}", Form.ComponentModel.ComponentName, Form.Count); + + if (_repairComponents.ContainsKey(Form.Id)) + { + _repairComponents[Form.Id] = (Form.ComponentModel, + Form.Count); + } + else + { + _repairComponents.Add(Form.Id, (Form.ComponentModel, + Form.Count)); + } + + LoadData(); + } + } + } + + private void UpdateButton_Click(object sender, EventArgs e) + { + if (DataGridView.SelectedRows.Count == 1) + { + var service = Program.ServiceProvider?.GetService(typeof(FormRepairComponent)); + if (service is FormRepairComponent Form) + { + int id = Convert.ToInt32(DataGridView.SelectedRows[0].Cells[0].Value); + Form.Id = id; + Form.Count = _repairComponents[id].Item2; + + if (Form.ShowDialog() == DialogResult.OK) + { + if (Form.ComponentModel == null) + { + return; + } + + _logger.LogInformation("Изменение компонента: {ComponentName} - {Count}", Form.ComponentModel.ComponentName, Form.Count); + _repairComponents[Form.Id] = (Form.ComponentModel, Form.Count); + + LoadData(); + } + } + } + } + + private void DeleteButton_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); + + _repairComponents?.Remove(Convert.ToInt32(DataGridView.SelectedRows[0].Cells[0].Value)); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + + LoadData(); + } + } + } + + private void RefreshButton_Click(object sender, EventArgs e) + { + LoadData(); + } + + private void SaveButton_Click(object sender, EventArgs e) + { + + if (string.IsNullOrEmpty(NameTextBox.Text)) + { + MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + if (string.IsNullOrEmpty(PriceTextBox.Text)) + { + MessageBox.Show("Заполните цену", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + if (_repairComponents == null || _repairComponents.Count == 0) + { + MessageBox.Show("Заполните компоненты", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + _logger.LogInformation("Сохранение ремонта"); + + try + { + var Мodel = new RepairBindingModel + { + Id = _id ?? 0, + RepairName = NameTextBox.Text, + Price = Convert.ToDouble(PriceTextBox.Text), + RepairComponents = _repairComponents + }; + + var OperationResult = _id.HasValue ? _logic.Update(Мodel) : _logic.Create(Мodel); + + 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 _repairComponents) + { + Price += ((Elem.Value.Item1?.Cost ?? 0) * Elem.Value.Item2); + } + + return Math.Round(Price * 1.1, 2); + } + } +} diff --git a/AutoWorkshop/Forms/FormRepair.resx b/AutoWorkshop/Forms/FormRepair.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/AutoWorkshop/Forms/FormRepair.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/AutoWorkshop/Forms/FormRepairComponent.Designer.cs b/AutoWorkshop/Forms/FormRepairComponent.Designer.cs new file mode 100644 index 0000000..360d6c1 --- /dev/null +++ b/AutoWorkshop/Forms/FormRepairComponent.Designer.cs @@ -0,0 +1,118 @@ +namespace AutoWorkshopView.Forms +{ + partial class FormRepairComponent + { + /// + /// 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() + { + ComponentLabel = new Label(); + CountLabel = new Label(); + CountTextBox = new TextBox(); + ComboBox = new ComboBox(); + SaveButton = new Button(); + CancelButton = new Button(); + SuspendLayout(); + // + // ComponentLabel + // + ComponentLabel.AutoSize = true; + ComponentLabel.Location = new Point(14, 15); + ComponentLabel.Name = "ComponentLabel"; + ComponentLabel.Size = new Size(72, 15); + ComponentLabel.TabIndex = 0; + ComponentLabel.Text = "Компонент:"; + // + // CountLabel + // + CountLabel.AutoSize = true; + CountLabel.Location = new Point(14, 46); + CountLabel.Name = "CountLabel"; + CountLabel.Size = new Size(75, 15); + CountLabel.TabIndex = 1; + CountLabel.Text = "Количество:"; + // + // CountTextBox + // + CountTextBox.Location = new Point(95, 43); + CountTextBox.Name = "CountTextBox"; + CountTextBox.Size = new Size(292, 23); + CountTextBox.TabIndex = 2; + // + // ComboBox + // + ComboBox.FormattingEnabled = true; + ComboBox.Location = new Point(95, 12); + ComboBox.Name = "ComboBox"; + ComboBox.Size = new Size(292, 23); + ComboBox.TabIndex = 3; + // + // SaveButton + // + SaveButton.Location = new Point(231, 81); + SaveButton.Name = "SaveButton"; + SaveButton.Size = new Size(75, 23); + SaveButton.TabIndex = 4; + SaveButton.Text = "Сохранить"; + SaveButton.UseVisualStyleBackColor = true; + SaveButton.Click += ButtonSave_Click; + // + // CancelButton + // + CancelButton.Location = new Point(312, 81); + CancelButton.Name = "CancelButton"; + CancelButton.Size = new Size(75, 23); + CancelButton.TabIndex = 5; + CancelButton.Text = "Отмена"; + CancelButton.UseVisualStyleBackColor = true; + CancelButton.Click += ButtonCancel_Click; + // + // FormRepairComponent + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(407, 120); + Controls.Add(CancelButton); + Controls.Add(SaveButton); + Controls.Add(ComboBox); + Controls.Add(CountTextBox); + Controls.Add(CountLabel); + Controls.Add(ComponentLabel); + Name = "FormRepairComponent"; + Text = "Компонент ремонта"; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private Label ComponentLabel; + private Label CountLabel; + private TextBox CountTextBox; + private ComboBox ComboBox; + private Button SaveButton; + private Button CancelButton; + } +} diff --git a/AutoWorkshop/Forms/FormRepairComponent.cs b/AutoWorkshop/Forms/FormRepairComponent.cs new file mode 100644 index 0000000..97bc27c --- /dev/null +++ b/AutoWorkshop/Forms/FormRepairComponent.cs @@ -0,0 +1,89 @@ +using AutoWorkshopContracts.BusinessLogicContracts; +using AutoWorkshopContracts.ViewModels; +using AutoWorkshopDataModels.Models; + +namespace AutoWorkshopView.Forms +{ + public partial class FormRepairComponent : Form + { + private readonly List? _list; + + public int Id + { + get + { + return Convert.ToInt32(ComboBox.SelectedValue); + } + set + { + ComboBox.SelectedValue = value; + } + } + + public IComponentModel? ComponentModel + { + get + { + if (_list == null) + { + return null; + } + + foreach (var Elem in _list) + { + if (Elem.Id == Id) + { + return Elem; + } + } + + return null; + } + } + + public int Count + { + get { return Convert.ToInt32(CountTextBox.Text); } + set { CountTextBox.Text = value.ToString(); } + } + + public FormRepairComponent(IComponentLogic Logic) + { + InitializeComponent(); + + _list = Logic.ReadList(null); + + if (_list != null) + { + ComboBox.DisplayMember = "ComponentName"; + ComboBox.ValueMember = "Id"; + ComboBox.DataSource = _list; + ComboBox.SelectedItem = null; + } + } + + private void ButtonSave_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(CountTextBox.Text)) + { + MessageBox.Show("Заполните поле Количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + if (ComboBox.SelectedValue == null) + { + MessageBox.Show("Выберите компонент", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + DialogResult = DialogResult.OK; + Close(); + } + + private void ButtonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + } +} diff --git a/AutoWorkshop/Forms/FormRepairComponent.resx b/AutoWorkshop/Forms/FormRepairComponent.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/AutoWorkshop/Forms/FormRepairComponent.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/AutoWorkshop/Program.cs b/AutoWorkshop/Program.cs index b3e47af..f160524 100644 --- a/AutoWorkshop/Program.cs +++ b/AutoWorkshop/Program.cs @@ -1,12 +1,48 @@ +using AutoWorkshopContracts.BusinessLogicContracts; +using AutoWorkshopContracts.StoragesContracts; +using AutoWorkshopView.Forms; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; + namespace AutoWorkshopView { internal static class Program { + private static ServiceProvider? _serviceProvider; + public static ServiceProvider? ServiceProvider => _serviceProvider; + [STAThread] static void Main() { - ApplicationConfiguration.Initialize(); - Application.Run(new Form1()); + var Services = new ServiceCollection(); + ConfigureServices(Services); + + _serviceProvider = Services.BuildServiceProvider(); + + Application.Run(_serviceProvider.GetRequiredService()); + } + + private static void ConfigureServices(ServiceCollection Services) + { + Services.AddLogging(option => + { + option.SetMinimumLevel(LogLevel.Information); + option.AddNLog("nlog.config"); + }); + + Services.AddTransient(); + Services.AddTransient(); + Services.AddTransient(); + Services.AddTransient(); + Services.AddTransient(); + Services.AddTransient(); + Services.AddTransient(); + Services.AddTransient(); + Services.AddTransient(); + Services.AddTransient(); + Services.AddTransient(); + Services.AddTransient(); + Services.AddTransient(); } } }