Вроде работает

This commit is contained in:
maxnes3 2023-04-09 19:52:16 +04:00
parent 7183057ec3
commit aeb4deafed
2 changed files with 194 additions and 199 deletions

View File

@ -28,131 +28,150 @@
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
ShopNameLabel = new Label();
ShopAdressLabel = new Label();
dateOpeningLabel = new Label();
textBoxAddress = new TextBox();
dataGridView = new DataGridView();
PackageName = new DataGridViewTextBoxColumn();
PackagePrice = new DataGridViewTextBoxColumn();
PackageCount = new DataGridViewTextBoxColumn();
buttonSave = new Button();
buttonCancel = new Button();
dateTimePicker = new DateTimePicker(); dateTimePicker = new DateTimePicker();
textBoxName = new TextBox(); textBoxName = new TextBox();
textBoxAddress = new TextBox();
labelTime = new Label();
labelAddress = new Label();
dataGridView = new DataGridView();
ColumnID = new DataGridViewTextBoxColumn();
ColumnManufactureName = new DataGridViewTextBoxColumn();
Цена = new DataGridViewTextBoxColumn();
ColumnCount = new DataGridViewTextBoxColumn();
labelShop = new Label();
buttonSave = new Button();
buttonCancel = new Button();
((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit();
SuspendLayout(); SuspendLayout();
// //
// ShopNameLabel
//
ShopNameLabel.AutoSize = true;
ShopNameLabel.Location = new Point(12, 9);
ShopNameLabel.Name = "ShopNameLabel";
ShopNameLabel.Size = new Size(119, 15);
ShopNameLabel.TabIndex = 0;
ShopNameLabel.Text = "Название магазина: ";
//
// ShopAdressLabel
//
ShopAdressLabel.AutoSize = true;
ShopAdressLabel.Location = new Point(12, 40);
ShopAdressLabel.Name = "ShopAdressLabel";
ShopAdressLabel.Size = new Size(100, 15);
ShopAdressLabel.TabIndex = 1;
ShopAdressLabel.Text = "Адрес магазина: ";
//
// dateOpeningLabel
//
dateOpeningLabel.AutoSize = true;
dateOpeningLabel.Location = new Point(12, 72);
dateOpeningLabel.Name = "dateOpeningLabel";
dateOpeningLabel.Size = new Size(93, 15);
dateOpeningLabel.TabIndex = 2;
dateOpeningLabel.Text = "Дата открытия: ";
//
// textBoxAddress
//
textBoxAddress.Location = new Point(137, 37);
textBoxAddress.Name = "textBoxAddress";
textBoxAddress.Size = new Size(174, 23);
textBoxAddress.TabIndex = 4;
//
// dataGridView
//
dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
dataGridView.Columns.AddRange(new DataGridViewColumn[] { PackageName, PackagePrice, PackageCount });
dataGridView.Location = new Point(12, 109);
dataGridView.Name = "dataGridView";
dataGridView.RowTemplate.Height = 25;
dataGridView.Size = new Size(776, 288);
dataGridView.TabIndex = 6;
//
// PackageName
//
PackageName.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
PackageName.HeaderText = "Название изделия";
PackageName.Name = "PackageName";
//
// PackagePrice
//
PackagePrice.HeaderText = "Цена";
PackagePrice.Name = "PackagePrice";
//
// PackageCount
//
PackageCount.HeaderText = "Количество";
PackageCount.Name = "PackageCount";
//
// buttonSave
//
buttonSave.Location = new Point(552, 403);
buttonSave.Name = "buttonSave";
buttonSave.Size = new Size(115, 35);
buttonSave.TabIndex = 7;
buttonSave.Text = "Сохранить";
buttonSave.UseVisualStyleBackColor = true;
buttonSave.Click += SaveButton_Click;
//
// buttonCancel
//
buttonCancel.Location = new Point(673, 403);
buttonCancel.Name = "buttonCancel";
buttonCancel.Size = new Size(115, 35);
buttonCancel.TabIndex = 8;
buttonCancel.Text = "Отменить";
buttonCancel.UseVisualStyleBackColor = true;
buttonCancel.Click += ButtonCancel_Click;
//
// dateTimePicker // dateTimePicker
// //
dateTimePicker.Location = new Point(137, 66); dateTimePicker.Location = new Point(384, 27);
dateTimePicker.Name = "dateTimePicker"; dateTimePicker.Name = "dateTimePicker";
dateTimePicker.Size = new Size(174, 23); dateTimePicker.Size = new Size(207, 23);
dateTimePicker.TabIndex = 9; dateTimePicker.TabIndex = 26;
// //
// textBoxName // textBoxName
// //
textBoxName.Location = new Point(137, 6); textBoxName.Location = new Point(10, 27);
textBoxName.Name = "textBoxName"; textBoxName.Name = "textBoxName";
textBoxName.Size = new Size(174, 23); textBoxName.Size = new Size(141, 23);
textBoxName.TabIndex = 10; textBoxName.TabIndex = 25;
//
// textBoxAddress
//
textBoxAddress.Location = new Point(158, 27);
textBoxAddress.Name = "textBoxAddress";
textBoxAddress.Size = new Size(221, 23);
textBoxAddress.TabIndex = 24;
//
// labelTime
//
labelTime.AutoSize = true;
labelTime.Location = new Point(384, 9);
labelTime.Name = "labelTime";
labelTime.Size = new Size(87, 15);
labelTime.TabIndex = 23;
labelTime.Text = "Дата открытия";
//
// labelAddress
//
labelAddress.AutoSize = true;
labelAddress.Location = new Point(158, 9);
labelAddress.Name = "labelAddress";
labelAddress.Size = new Size(40, 15);
labelAddress.TabIndex = 22;
labelAddress.Text = "Адрес";
//
// dataGridView
//
dataGridView.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
dataGridView.Columns.AddRange(new DataGridViewColumn[] { ColumnID, ColumnManufactureName, Цена, ColumnCount });
dataGridView.Location = new Point(10, 56);
dataGridView.Name = "dataGridView";
dataGridView.RowHeadersWidth = 62;
dataGridView.RowTemplate.Height = 25;
dataGridView.Size = new Size(581, 327);
dataGridView.TabIndex = 21;
//
// ColumnID
//
ColumnID.HeaderText = "ID";
ColumnID.MinimumWidth = 8;
ColumnID.Name = "ColumnID";
ColumnID.Visible = false;
ColumnID.Width = 150;
//
// ColumnManufactureName
//
ColumnManufactureName.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
ColumnManufactureName.HeaderText = "Название компьютера";
ColumnManufactureName.MinimumWidth = 8;
ColumnManufactureName.Name = "ColumnManufactureName";
//
// Цена
//
Цена.HeaderText = "Цена";
Цена.MinimumWidth = 6;
Цена.Name = "Цена";
Цена.Width = 125;
//
// ColumnCount
//
ColumnCount.HeaderText = "Количество";
ColumnCount.MinimumWidth = 8;
ColumnCount.Name = "ColumnCount";
ColumnCount.Width = 150;
//
// labelShop
//
labelShop.AutoSize = true;
labelShop.Location = new Point(10, 9);
labelShop.Name = "labelShop";
labelShop.Size = new Size(54, 15);
labelShop.TabIndex = 20;
labelShop.Text = "Магазин";
//
// buttonSave
//
buttonSave.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonSave.Location = new Point(363, 392);
buttonSave.Name = "buttonSave";
buttonSave.Size = new Size(120, 22);
buttonSave.TabIndex = 28;
buttonSave.Text = "Сохранить";
buttonSave.UseVisualStyleBackColor = true;
buttonSave.Click += ButtonSave_Click;
//
// buttonCancel
//
buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonCancel.Location = new Point(488, 392);
buttonCancel.Name = "buttonCancel";
buttonCancel.Size = new Size(103, 22);
buttonCancel.TabIndex = 27;
buttonCancel.Text = "Отмена";
buttonCancel.UseVisualStyleBackColor = true;
buttonCancel.Click += ButtonCancel_Click;
// //
// FormShop // FormShop
// //
AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450); ClientSize = new Size(616, 425);
Controls.Add(textBoxName);
Controls.Add(dateTimePicker);
Controls.Add(buttonCancel);
Controls.Add(buttonSave); Controls.Add(buttonSave);
Controls.Add(dataGridView); Controls.Add(buttonCancel);
Controls.Add(dateTimePicker);
Controls.Add(textBoxName);
Controls.Add(textBoxAddress); Controls.Add(textBoxAddress);
Controls.Add(dateOpeningLabel); Controls.Add(labelTime);
Controls.Add(ShopAdressLabel); Controls.Add(labelAddress);
Controls.Add(ShopNameLabel); Controls.Add(dataGridView);
Controls.Add(labelShop);
Margin = new Padding(3, 2, 3, 2);
Name = "FormShop"; Name = "FormShop";
Text = "Магазина"; Text = "FormShop";
Load += FormShop_Load; Load += FormShop_Load;
((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit();
ResumeLayout(false); ResumeLayout(false);
@ -161,17 +180,18 @@
#endregion #endregion
private Label ShopNameLabel;
private Label ShopAdressLabel;
private Label dateOpeningLabel;
private TextBox textBoxAddress;
private DataGridView dataGridView;
private Button buttonSave;
private Button buttonCancel;
private DataGridViewTextBoxColumn PackageName;
private DataGridViewTextBoxColumn PackagePrice;
private DataGridViewTextBoxColumn PackageCount;
private DateTimePicker dateTimePicker; private DateTimePicker dateTimePicker;
private TextBox textBoxName; private TextBox textBoxName;
private TextBox textBoxAddress;
private Label labelTime;
private Label labelAddress;
private DataGridView dataGridView;
private Label labelShop;
private Button buttonSave;
private Button buttonCancel;
private DataGridViewTextBoxColumn ColumnID;
private DataGridViewTextBoxColumn ColumnManufactureName;
private DataGridViewTextBoxColumn Цена;
private DataGridViewTextBoxColumn ColumnCount;
} }
} }

View File

@ -1,5 +1,6 @@
using ComputersShopContracts.BindingModels; using ComputersShopContracts.BindingModels;
using ComputersShopContracts.BusinessLogicContracts; using ComputersShopContracts.BusinessLogicContracts;
using ComputersShopContracts.SearchModels;
using ComputersShopContracts.ViewModels; using ComputersShopContracts.ViewModels;
using ComputersShopDataModels.Models; using ComputersShopDataModels.Models;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
@ -17,135 +18,109 @@ namespace ComputersShopView
{ {
public partial class FormShop : Form public partial class FormShop : Form
{ {
private readonly List<ShopViewModel>? _listShops;
private readonly IShopLogic _logic;
private readonly ILogger _logger; private readonly ILogger _logger;
public int Id { get; set; } private readonly IShopLogic _logic;
private int? _id;
private Dictionary<int, (IComputerModel, int)> _shopComputers;
public int Id { set { _id = value; } }
public FormShop(ILogger<FormShop> logger, IShopLogic logic) public FormShop(ILogger<FormShop> logger, IShopLogic logic)
{ {
InitializeComponent(); InitializeComponent();
_logger = logger; _logger = logger;
_listShops = logic.ReadList(null);
_logic = logic; _logic = logic;
if (_listShops != null) _shopComputers = new Dictionary<int, (IComputerModel, int)>();
}
private void FormShop_Load(object sender, EventArgs e)
{ {
comboBoxName.DisplayMember = "ShopName"; if (_id.HasValue)
comboBoxName.ValueMember = "Id"; {
comboBoxName.DataSource = _listShops; _logger.LogInformation("Загрузка магазина");
comboBoxName.SelectedItem = null; try
{
var view = _logic.ReadElement(new ShopSearchModel
{
Id = _id.Value
});
if (view != null)
{
textBoxName.Text = view.ShopName;
textBoxAddress.Text = view.ShopAddress.ToString();
dateTimePicker.Text = view.DateOpening.ToString();
_shopComputers = view.Computers ?? new Dictionary<int, (IComputerModel, int)>();
LoadData();
} }
} }
catch (Exception ex)
private IShopModel? GetShop(int id)
{ {
if (_listShops == null) _logger.LogError(ex, "Ошибка загрузки магазина");
{ MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
return null; MessageBoxIcon.Error);
}
foreach (var elem in _listShops)
{
if (elem.Id == id)
{
return elem;
} }
} }
return null;
} }
private void LoadData()
private void SaveButton_Click(object sender, EventArgs e)
{ {
if (string.IsNullOrEmpty(comboBoxName.Text)) _logger.LogInformation("Загрузка компонент магазина");
try
{
if (_shopComputers != null)
{
dataGridView.Rows.Clear();
foreach (var pc in _shopComputers)
{
dataGridView.Rows.Add(new object[] { pc.Key, pc.Value.Item1.ComputerName, pc.Value.Item1.Price, pc.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); MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return; return;
} }
if (string.IsNullOrEmpty(textBoxAddress.Text)) if (string.IsNullOrEmpty(textBoxAddress.Text))
{ {
MessageBox.Show("Заполните адрес", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBox.Show("Заполните цену", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return; return;
} }
_logger.LogInformation("Сохранение магазина"); _logger.LogInformation("Сохранение магазина");
try try
{ {
DateTime.TryParse(dateTimePicker.Text, out var dateTime); var model = new ShopBindingModel
ShopBindingModel model = new()
{ {
ShopName = comboBoxName.Text, Id = _id ?? 0,
ShopName = textBoxName.Text,
ShopAddress = textBoxAddress.Text, ShopAddress = textBoxAddress.Text,
DateOpening = dateTime DateOpening = dateTimePicker.Value.Date,
Computers = _shopComputers
}; };
var vmodel = GetShop(Id); var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model);
bool operationResult = false;
if (vmodel != null)
{
model.Id = vmodel.Id;
operationResult = _logic.Update(model);
}
else
{
operationResult = _logic.Create(model);
}
if (!operationResult) if (!operationResult)
{ {
throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); throw new Exception("Ошибка при сохранении. Дополнительная информация в логах.");
} }
MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
DialogResult = DialogResult.OK; DialogResult = DialogResult.OK;
Close(); Close();
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "Ошибка сохранения изделия"); _logger.LogError(ex, "Ошибка сохранения магазина");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
} }
private void ButtonCancel_Click(object sender, EventArgs e) private void ButtonCancel_Click(object sender, EventArgs e)
{ {
DialogResult = DialogResult.Cancel; DialogResult = DialogResult.Cancel;
Close(); Close();
} }
private void FormShop_Load(object sender, EventArgs e)
{
LoadData();
}
private void LoadData(bool extendDate = true)
{
try
{
var model = GetShop(extendDate ? Id : Convert.ToInt32(comboBoxName.SelectedValue));
if (model != null)
{
comboBoxName.Text = model.ShopName;
textBoxAddress.Text = model.ShopAddress;
dateTimePicker.Text = Convert.ToString(model.DateOpening);
dataGridView.Rows.Clear();
foreach (var comp in model.Computers.Values)
{
dataGridView.Rows.Add(new object[] { comp.Item1.ComputerName, comp.Item1.Price, comp.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);
}
} }
} }