diff --git a/SoftwareInstallation/SoftwareInstallation/FormPackages.cs b/SoftwareInstallation/SoftwareInstallation/FormPackages.cs index c2ab63c..9d6c8cb 100644 --- a/SoftwareInstallation/SoftwareInstallation/FormPackages.cs +++ b/SoftwareInstallation/SoftwareInstallation/FormPackages.cs @@ -16,7 +16,7 @@ namespace SoftwareInstallationView _logic = logic; } - private void FormComponents_Load(object sender, EventArgs e) + private void FormPackages_Load(object sender, EventArgs e) { LoadData(); } diff --git a/SoftwareInstallation/SoftwareInstallation/FormStore.Designer.cs b/SoftwareInstallation/SoftwareInstallation/FormStore.Designer.cs new file mode 100644 index 0000000..45b191b --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallation/FormStore.Designer.cs @@ -0,0 +1,183 @@ +namespace SoftwareInstallationView +{ + partial class FormStore + { + /// + /// 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.StoreNameLabel = new System.Windows.Forms.Label(); + this.StoreAdressLabel = new System.Windows.Forms.Label(); + this.OpeningDateLabel = new System.Windows.Forms.Label(); + this.NameComboBox = new System.Windows.Forms.ComboBox(); + this.AdressTextBox = new System.Windows.Forms.TextBox(); + this.OpeningDateTextBox = new System.Windows.Forms.TextBox(); + this.DataGridView = new System.Windows.Forms.DataGridView(); + this.PackageName = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.PackagePrice = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.PackageCount = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.SaveButton = new System.Windows.Forms.Button(); + this.ButtonCancel = new System.Windows.Forms.Button(); + ((System.ComponentModel.ISupportInitialize)(this.DataGridView)).BeginInit(); + this.SuspendLayout(); + // + // StoreNameLabel + // + this.StoreNameLabel.AutoSize = true; + this.StoreNameLabel.Location = new System.Drawing.Point(12, 9); + this.StoreNameLabel.Name = "StoreNameLabel"; + this.StoreNameLabel.Size = new System.Drawing.Size(119, 15); + this.StoreNameLabel.TabIndex = 0; + this.StoreNameLabel.Text = "Название магазина: "; + // + // StoreAdressLabel + // + this.StoreAdressLabel.AutoSize = true; + this.StoreAdressLabel.Location = new System.Drawing.Point(12, 40); + this.StoreAdressLabel.Name = "StoreAdressLabel"; + this.StoreAdressLabel.Size = new System.Drawing.Size(100, 15); + this.StoreAdressLabel.TabIndex = 1; + this.StoreAdressLabel.Text = "Адрес магазина: "; + // + // OpeningDateLabel + // + this.OpeningDateLabel.AutoSize = true; + this.OpeningDateLabel.Location = new System.Drawing.Point(12, 72); + this.OpeningDateLabel.Name = "OpeningDateLabel"; + this.OpeningDateLabel.Size = new System.Drawing.Size(93, 15); + this.OpeningDateLabel.TabIndex = 2; + this.OpeningDateLabel.Text = "Дата открытия: "; + // + // NameComboBox + // + this.NameComboBox.FormattingEnabled = true; + this.NameComboBox.Location = new System.Drawing.Point(137, 6); + this.NameComboBox.Name = "NameComboBox"; + this.NameComboBox.Size = new System.Drawing.Size(174, 23); + this.NameComboBox.TabIndex = 3; + this.NameComboBox.SelectedIndexChanged += new System.EventHandler(this.NameComboBox_SelectedIndexChanged); + // + // AdressTextBox + // + this.AdressTextBox.Location = new System.Drawing.Point(137, 37); + this.AdressTextBox.Name = "AdressTextBox"; + this.AdressTextBox.Size = new System.Drawing.Size(174, 23); + this.AdressTextBox.TabIndex = 4; + // + // OpeningDateTextBox + // + this.OpeningDateTextBox.Location = new System.Drawing.Point(137, 69); + this.OpeningDateTextBox.Name = "OpeningDateTextBox"; + this.OpeningDateTextBox.Size = new System.Drawing.Size(174, 23); + this.OpeningDateTextBox.TabIndex = 5; + // + // DataGridView + // + this.DataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.DataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { + this.PackageName, + this.PackagePrice, + this.PackageCount}); + this.DataGridView.Location = new System.Drawing.Point(12, 109); + this.DataGridView.Name = "DataGridView"; + this.DataGridView.RowTemplate.Height = 25; + this.DataGridView.Size = new System.Drawing.Size(776, 288); + this.DataGridView.TabIndex = 6; + // + // PackageName + // + this.PackageName.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill; + this.PackageName.HeaderText = "Название изделия"; + this.PackageName.Name = "PackageName"; + // + // PackagePrice + // + this.PackagePrice.HeaderText = "Цена"; + this.PackagePrice.Name = "PackagePrice"; + // + // PackageCount + // + this.PackageCount.HeaderText = "Количество"; + this.PackageCount.Name = "PackageCount"; + // + // SaveButton + // + this.SaveButton.Location = new System.Drawing.Point(552, 403); + this.SaveButton.Name = "SaveButton"; + this.SaveButton.Size = new System.Drawing.Size(115, 35); + this.SaveButton.TabIndex = 7; + this.SaveButton.Text = "Сохранить"; + this.SaveButton.UseVisualStyleBackColor = true; + this.SaveButton.Click += new System.EventHandler(this.SaveButton_Click); + // + // ButtonCancel + // + this.ButtonCancel.Location = new System.Drawing.Point(673, 403); + this.ButtonCancel.Name = "ButtonCancel"; + this.ButtonCancel.Size = new System.Drawing.Size(115, 35); + this.ButtonCancel.TabIndex = 8; + this.ButtonCancel.Text = "Отменить"; + this.ButtonCancel.UseVisualStyleBackColor = true; + this.ButtonCancel.Click += new System.EventHandler(this.ButtonCancel_Click); + // + // FormStore + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(800, 450); + this.Controls.Add(this.ButtonCancel); + this.Controls.Add(this.SaveButton); + this.Controls.Add(this.DataGridView); + this.Controls.Add(this.OpeningDateTextBox); + this.Controls.Add(this.AdressTextBox); + this.Controls.Add(this.NameComboBox); + this.Controls.Add(this.OpeningDateLabel); + this.Controls.Add(this.StoreAdressLabel); + this.Controls.Add(this.StoreNameLabel); + this.Name = "FormStore"; + this.Text = "Изделия магазина"; + this.Load += new System.EventHandler(this.FormStore_Load); + ((System.ComponentModel.ISupportInitialize)(this.DataGridView)).EndInit(); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private Label StoreNameLabel; + private Label StoreAdressLabel; + private Label OpeningDateLabel; + private ComboBox NameComboBox; + private TextBox AdressTextBox; + private TextBox OpeningDateTextBox; + private DataGridView DataGridView; + private Button SaveButton; + private Button ButtonCancel; + private DataGridViewTextBoxColumn PackageName; + private DataGridViewTextBoxColumn PackagePrice; + private DataGridViewTextBoxColumn PackageCount; + } +} \ No newline at end of file diff --git a/SoftwareInstallation/SoftwareInstallation/FormStore.cs b/SoftwareInstallation/SoftwareInstallation/FormStore.cs new file mode 100644 index 0000000..72053c8 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallation/FormStore.cs @@ -0,0 +1,142 @@ +using Microsoft.Extensions.Logging; +using SofrwareInstallationContracts.BindingModels; +using SofrwareInstallationContracts.BusinessLogicsContracts; +using SofrwareInstallationContracts.ViewModels; +using SoftwareInstallationDataModels.Models; + +namespace SoftwareInstallationView +{ + public partial class FormStore : Form + { + private readonly List? _listStores; + private readonly IStoreLogic _logic; + private readonly ILogger _logger; + public int Id { get; set; } + + public FormStore(ILogger logger, IStoreLogic logic) + { + InitializeComponent(); + _logger = logger; + _listStores = logic.ReadList(null); + _logic = logic; + if (_listStores != null) + { + NameComboBox.DisplayMember = "StoreName"; + NameComboBox.ValueMember = "Id"; + NameComboBox.DataSource = _listStores; + NameComboBox.SelectedItem = null; + } + } + + private IStoreModel? GetStore(int id) + { + if (_listStores == null) + { + return null; + } + foreach (var elem in _listStores) + { + if (elem.Id == id) + { + return elem; + } + } + return null; + } + + private void SaveButton_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(NameComboBox.Text)) + { + MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + if (string.IsNullOrEmpty(AdressTextBox.Text)) + { + MessageBox.Show("Заполните адрес", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + _logger.LogInformation("Сохранение изделия"); + + try + { + DateTime.TryParse(OpeningDateTextBox.Text, out var dateTime); + StoreBindingModel model = new() + { + StoreName = NameComboBox.Text, + StoreAdress = AdressTextBox.Text, + OpeningDate = dateTime + }; + var vmodel = GetStore(Id); + bool operationResult = false; + + if (vmodel != null) + { + model.Id = vmodel.Id; + operationResult = _logic.Update(model); + } + else + { + operationResult = _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 void FormStore_Load(object sender, EventArgs e) + { + LoadData(); + } + private void LoadData(bool extendDate = true) + { + try + { + var model = GetStore(extendDate ? Id : Convert.ToInt32(NameComboBox.SelectedValue)); + if (model != null) + { + NameComboBox.Text = model.StoreName; + AdressTextBox.Text = model.StoreAdress; + OpeningDateTextBox.Text = Convert.ToString(model.OpeningDate); + DataGridView.Rows.Clear(); + foreach (var el in model.Packages.Values) + { + DataGridView.Rows.Add(new object[] { el.Item1.PackageName, el.Item1.Price, el.Item2 }); + } + } + _logger.LogInformation("Загрузка магазинов"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки магазинов"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + + private void NameComboBox_SelectedIndexChanged(object sender, EventArgs e) + { + LoadData(false); + } + } +} diff --git a/SoftwareInstallation/SoftwareInstallation/FormStore.resx b/SoftwareInstallation/SoftwareInstallation/FormStore.resx new file mode 100644 index 0000000..7a40f39 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallation/FormStore.resx @@ -0,0 +1,69 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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 + + + True + + + True + + + True + + \ No newline at end of file diff --git a/SoftwareInstallation/SoftwareInstallation/FormStores.Designer.cs b/SoftwareInstallation/SoftwareInstallation/FormStores.Designer.cs new file mode 100644 index 0000000..4ad91e6 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallation/FormStores.Designer.cs @@ -0,0 +1,114 @@ +namespace SoftwareInstallationView +{ + partial class FormStores + { + /// + /// 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.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, 1); + this.DataGridView.Name = "DataGridView"; + this.DataGridView.RowTemplate.Height = 25; + this.DataGridView.Size = new System.Drawing.Size(550, 447); + this.DataGridView.TabIndex = 0; + // + // AddButton + // + this.AddButton.Location = new System.Drawing.Point(585, 12); + this.AddButton.Name = "AddButton"; + this.AddButton.Size = new System.Drawing.Size(121, 40); + 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(585, 67); + this.ChangeButton.Name = "ChangeButton"; + this.ChangeButton.Size = new System.Drawing.Size(121, 40); + 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(585, 122); + this.DeleteButton.Name = "DeleteButton"; + this.DeleteButton.Size = new System.Drawing.Size(121, 40); + 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(585, 179); + this.UpdateButton.Name = "UpdateButton"; + this.UpdateButton.Size = new System.Drawing.Size(121, 40); + this.UpdateButton.TabIndex = 4; + this.UpdateButton.Text = "Обновить"; + this.UpdateButton.UseVisualStyleBackColor = true; + this.UpdateButton.Click += new System.EventHandler(this.UpdateButton_Click); + // + // FormStores + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(718, 450); + 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.Name = "FormStores"; + this.Text = "Магазины"; + this.Load += new System.EventHandler(this.FormStores_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/SoftwareInstallation/SoftwareInstallation/FormStores.cs b/SoftwareInstallation/SoftwareInstallation/FormStores.cs new file mode 100644 index 0000000..b02fe4f --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallation/FormStores.cs @@ -0,0 +1,113 @@ +using Microsoft.Extensions.Logging; +using SofrwareInstallationContracts.BindingModels; +using SofrwareInstallationContracts.BusinessLogicsContracts; + +namespace SoftwareInstallationView +{ + public partial class FormStores : Form + { + private readonly ILogger _logger; + private readonly IStoreLogic _logic; + public FormStores(ILogger logger, IStoreLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + } + + private void FormStores_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["StoreName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + DataGridView.Columns["Packages"].Visible = false; + } + + _logger.LogInformation("Загрузка магазинов"); + + } + 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 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 StoreBindingModel + { + Id = id + })) + { + throw new Exception("Ошибка при удалении. Дополнительная информация в логах."); + } + + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка удаления изделия"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + } + + private void ChangeButton_Click(object sender, EventArgs e) + { + if (DataGridView.SelectedRows.Count == 1) + { + var service = Program.ServiceProvider?.GetService(typeof(FormStore)); + + if (service is FormStore form) + { + form.Id = Convert.ToInt32(DataGridView.SelectedRows[0].Cells["Id"].Value); + + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + } + + private void AddButton_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormStore)); + + if (service is FormStore form) + { + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + } +} diff --git a/SoftwareInstallation/SoftwareInstallation/FormStores.resx b/SoftwareInstallation/SoftwareInstallation/FormStores.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallation/FormStores.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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