PIbd-21 Potapov N.S. LabWork02 Hard #9

Closed
ns.potapov wants to merge 34 commits from LabWorkHard02 into LabWork02
3 changed files with 274 additions and 11 deletions
Showing only changes of commit 1ec3a79493 - Show all commits

View File

@ -28,18 +28,164 @@
/// </summary>
private void InitializeComponent()
{
labelName = new Label();
labelAddress = new Label();
labelOpeningDate = new Label();
textBoxName = new TextBox();
textBoxAddress = new TextBox();
dateTimePickerOpeningDate = new DateTimePicker();
dataGridView = new DataGridView();
ColumnId = new DataGridViewTextBoxColumn();
ColumnName = new DataGridViewTextBoxColumn();
ColumnCount = new DataGridViewTextBoxColumn();
colorDialog1 = new ColorDialog();
buttonSave = new Button();
buttonCancel = new Button();
((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit();
SuspendLayout();
//
// labelName
//
labelName.AutoSize = true;
labelName.Location = new Point(12, 9);
labelName.Name = "labelName";
labelName.Size = new Size(80, 20);
labelName.TabIndex = 0;
labelName.Text = "Название:";
//
// labelAddress
//
labelAddress.AutoSize = true;
labelAddress.Location = new Point(12, 41);
labelAddress.Name = "labelAddress";
labelAddress.Size = new Size(54, 20);
labelAddress.TabIndex = 1;
labelAddress.Text = "Адрес:";
//
// labelOpeningDate
//
labelOpeningDate.AutoSize = true;
labelOpeningDate.Location = new Point(12, 75);
labelOpeningDate.Name = "labelOpeningDate";
labelOpeningDate.Size = new Size(113, 20);
labelOpeningDate.TabIndex = 2;
labelOpeningDate.Text = "Дата открытия:";
//
// textBoxName
//
textBoxName.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
textBoxName.Location = new Point(98, 6);
textBoxName.Name = "textBoxName";
textBoxName.Size = new Size(562, 27);
textBoxName.TabIndex = 3;
//
// textBoxAddress
//
textBoxAddress.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
textBoxAddress.Location = new Point(98, 38);
textBoxAddress.Name = "textBoxAddress";
textBoxAddress.Size = new Size(562, 27);
textBoxAddress.TabIndex = 4;
//
// dateTimePickerOpeningDate
//
dateTimePickerOpeningDate.Location = new Point(131, 71);
dateTimePickerOpeningDate.Name = "dateTimePickerOpeningDate";
dateTimePickerOpeningDate.Size = new Size(185, 27);
dateTimePickerOpeningDate.TabIndex = 5;
//
// dataGridView
//
dataGridView.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
dataGridView.Columns.AddRange(new DataGridViewColumn[] { ColumnId, ColumnName, ColumnCount });
dataGridView.Location = new Point(12, 104);
dataGridView.Name = "dataGridView";
dataGridView.RowHeadersVisible = false;
dataGridView.RowHeadersWidth = 51;
dataGridView.RowTemplate.Height = 29;
dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridView.Size = new Size(517, 334);
dataGridView.TabIndex = 6;
//
// ColumnId
//
ColumnId.HeaderText = "Id";
ColumnId.MinimumWidth = 6;
ColumnId.Name = "ColumnId";
ColumnId.Visible = false;
//
// ColumnName
//
ColumnName.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
ColumnName.HeaderText = "Изделие";
ColumnName.MinimumWidth = 6;
ColumnName.Name = "ColumnName";
//
// ColumnCount
//
ColumnCount.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;
ColumnCount.HeaderText = "Количество";
ColumnCount.MinimumWidth = 6;
ColumnCount.Name = "ColumnCount";
ColumnCount.Width = 119;
//
// buttonSave
//
buttonSave.Anchor = AnchorStyles.Top | AnchorStyles.Right;
buttonSave.Location = new Point(549, 145);
buttonSave.Name = "buttonSave";
buttonSave.Size = new Size(94, 29);
buttonSave.TabIndex = 7;
buttonSave.Text = "Сохранить";
buttonSave.UseVisualStyleBackColor = true;
//
// buttonCancel
//
buttonCancel.Anchor = AnchorStyles.Top | AnchorStyles.Right;
buttonCancel.Location = new Point(549, 219);
buttonCancel.Name = "buttonCancel";
buttonCancel.Size = new Size(94, 29);
buttonCancel.TabIndex = 8;
buttonCancel.Text = "Закрыть";
buttonCancel.UseVisualStyleBackColor = true;
//
// FormShop
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450);
ClientSize = new Size(672, 450);
Controls.Add(buttonCancel);
Controls.Add(buttonSave);
Controls.Add(dataGridView);
Controls.Add(dateTimePickerOpeningDate);
Controls.Add(textBoxAddress);
Controls.Add(textBoxName);
Controls.Add(labelOpeningDate);
Controls.Add(labelAddress);
Controls.Add(labelName);
Name = "FormShop";
Text = "Магазин";
((System.ComponentModel.ISupportInitialize)dataGridView).EndInit();
ResumeLayout(false);
PerformLayout();
}
#endregion
private Label labelName;
private Label labelAddress;
private Label labelOpeningDate;
private TextBox textBoxName;
private TextBox textBoxAddress;
private DateTimePicker dateTimePickerOpeningDate;
private DataGridView dataGridView;
private DataGridViewTextBoxColumn ColumnId;
private DataGridViewTextBoxColumn ColumnName;
private DataGridViewTextBoxColumn ColumnCount;
private ColorDialog colorDialog1;
private Button buttonSave;
private Button buttonCancel;
}
}

View File

@ -1,20 +1,125 @@
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;
using Microsoft.Extensions.Logging;
using SecuritySystemContracts.BindingModels;
using SecuritySystemContracts.BusinessLogicsContracts;
using SecuritySystemContracts.SearchModels;
using SecuritySystemDataModels.Models;
namespace SecuritySystemView
{
public partial class FormShop : Form
{
public FormShop()
private readonly IShopLogic _logic;
private readonly ILogger _logger;
private Dictionary<int, (ISecureModel, int)> _shopSecures;
private int? _id;
public int Id { set { _id = value; } }
public FormShop(ILogger<FormShop> logger, IShopLogic logic)
{
InitializeComponent();
_logger = logger;
_logic = logic;
_shopSecures = new();
}
private void FormShop_Load(object sender, EventArgs e)
{
if (_id.HasValue)
{
_logger.LogInformation("Loading shop");
try
{
var view = _logic.ReadElement(new ShopSearchModel { Id = _id.Value });
if (view != null)
{
textBoxName.Text = view.Name;
textBoxAddress.Text = view.Address;
dateTimePickerOpeningDate.Value = view.OpeningDate;
_shopSecures = view.ShopSecures ?? new Dictionary<int, (ISecureModel, int)>();
LoadData();
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка загрузки магазина");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void LoadData()
{
_logger.LogInformation("Загрузка магазина");
try
{
if (_shopSecures != null)
{
dataGridView.Rows.Clear();
foreach (var secure in _shopSecures)
{
dataGridView.Rows.Add(new object[] { secure.Key, secure.Value.Item1.SecureName, secure.Value.Item2 });
}
}
}
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(textBoxName.Text))
{
MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (string.IsNullOrEmpty(textBoxAddress.Text))
{
MessageBox.Show("Заполните адрес", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_logger.LogInformation("Сохранение магазина");
try
{
var model = new ShopBindingModel
{
Id = _id ?? 0,
Name = textBoxName.Text,
Address = textBoxAddress.Text,
OpeningDate = dateTimePickerOpeningDate.Value.Date,
ShopSecures = _shopSecures
};
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();
}
}
}

View File

@ -117,4 +117,16 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="ColumnId.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="ColumnName.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="ColumnCount.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="colorDialog1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
</root>