Сданная лаб 2

This commit is contained in:
Кашин Максим 2023-03-28 09:59:30 +04:00
parent aff68c7469
commit a5815975cd
2 changed files with 85 additions and 99 deletions
PrecastConcretePlant/PrecastConcretePlant

@ -49,20 +49,19 @@
// label1 // label1
// //
this.label1.AutoSize = true; this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 9); this.label1.Location = new System.Drawing.Point(14, 12);
this.label1.Name = "label1"; this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(116, 15); this.label1.Size = new System.Drawing.Size(150, 20);
this.label1.TabIndex = 0; this.label1.TabIndex = 0;
this.label1.Text = "Название магазина:"; this.label1.Text = "Название магазина:";
// //
// comboBoxShop // comboBoxShop
// //
this.comboBoxShop.FormattingEnabled = true; this.comboBoxShop.Location = new System.Drawing.Point(14, 39);
this.comboBoxShop.Location = new System.Drawing.Point(12, 27); this.comboBoxShop.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.comboBoxShop.Name = "comboBoxShop"; this.comboBoxShop.Name = "comboBoxShop";
this.comboBoxShop.Size = new System.Drawing.Size(141, 23); this.comboBoxShop.Size = new System.Drawing.Size(138, 28);
this.comboBoxShop.TabIndex = 1; this.comboBoxShop.TabIndex = 12;
this.comboBoxShop.SelectedIndexChanged += new System.EventHandler(this.ComboBoxShop_SelectedIndexChanged);
// //
// dataGridView // dataGridView
// //
@ -74,59 +73,68 @@
this.PastryName, this.PastryName,
this.Price, this.Price,
this.Count}); this.Count});
this.dataGridView.Location = new System.Drawing.Point(12, 116); this.dataGridView.Location = new System.Drawing.Point(14, 155);
this.dataGridView.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.dataGridView.Name = "dataGridView"; this.dataGridView.Name = "dataGridView";
this.dataGridView.RowHeadersWidth = 51;
this.dataGridView.RowTemplate.Height = 25; this.dataGridView.RowTemplate.Height = 25;
this.dataGridView.Size = new System.Drawing.Size(583, 249); this.dataGridView.Size = new System.Drawing.Size(666, 332);
this.dataGridView.TabIndex = 2; this.dataGridView.TabIndex = 2;
// //
// PastryName // PastryName
// //
this.PastryName.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill; this.PastryName.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
this.PastryName.HeaderText = "Имя изделия"; this.PastryName.HeaderText = "Имя изделия";
this.PastryName.MinimumWidth = 6;
this.PastryName.Name = "PastryName"; this.PastryName.Name = "PastryName";
// //
// Price // Price
// //
this.Price.HeaderText = "Цена"; this.Price.HeaderText = "Цена";
this.Price.MinimumWidth = 6;
this.Price.Name = "Price"; this.Price.Name = "Price";
this.Price.Width = 125;
// //
// Count // Count
// //
this.Count.HeaderText = "Количество"; this.Count.HeaderText = "Количество";
this.Count.MinimumWidth = 6;
this.Count.Name = "Count"; this.Count.Name = "Count";
this.Count.Width = 125;
// //
// label2 // label2
// //
this.label2.AutoSize = true; this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(159, 9); this.label2.Location = new System.Drawing.Point(182, 12);
this.label2.Name = "label2"; this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(97, 15); this.label2.Size = new System.Drawing.Size(124, 20);
this.label2.TabIndex = 3; this.label2.TabIndex = 3;
this.label2.Text = "Адрес магазина:"; this.label2.Text = "Адрес магазина:";
// //
// label3 // label3
// //
this.label3.AutoSize = true; this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(386, 9); this.label3.Location = new System.Drawing.Point(441, 12);
this.label3.Name = "label3"; this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(100, 15); this.label3.Size = new System.Drawing.Size(126, 20);
this.label3.TabIndex = 4; this.label3.TabIndex = 4;
this.label3.Text = "Время открытия:"; this.label3.Text = "Время открытия:";
// //
// textBoxAddress // textBoxAddress
// //
this.textBoxAddress.Location = new System.Drawing.Point(159, 27); this.textBoxAddress.Location = new System.Drawing.Point(182, 36);
this.textBoxAddress.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.textBoxAddress.Name = "textBoxAddress"; this.textBoxAddress.Name = "textBoxAddress";
this.textBoxAddress.Size = new System.Drawing.Size(221, 23); this.textBoxAddress.Size = new System.Drawing.Size(252, 27);
this.textBoxAddress.TabIndex = 5; this.textBoxAddress.TabIndex = 5;
// //
// buttonCancel // buttonCancel
// //
this.buttonCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonCancel.Location = new System.Drawing.Point(492, 371); this.buttonCancel.Location = new System.Drawing.Point(562, 495);
this.buttonCancel.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.buttonCancel.Name = "buttonCancel"; this.buttonCancel.Name = "buttonCancel";
this.buttonCancel.Size = new System.Drawing.Size(103, 23); this.buttonCancel.Size = new System.Drawing.Size(118, 31);
this.buttonCancel.TabIndex = 7; this.buttonCancel.TabIndex = 7;
this.buttonCancel.Text = "Отмена"; this.buttonCancel.Text = "Отмена";
this.buttonCancel.UseVisualStyleBackColor = true; this.buttonCancel.UseVisualStyleBackColor = true;
@ -135,9 +143,10 @@
// buttonSave // buttonSave
// //
this.buttonSave.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonSave.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonSave.Location = new System.Drawing.Point(366, 372); this.buttonSave.Location = new System.Drawing.Point(418, 496);
this.buttonSave.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.buttonSave.Name = "buttonSave"; this.buttonSave.Name = "buttonSave";
this.buttonSave.Size = new System.Drawing.Size(120, 22); this.buttonSave.Size = new System.Drawing.Size(137, 29);
this.buttonSave.TabIndex = 8; this.buttonSave.TabIndex = 8;
this.buttonSave.Text = "Сохранить"; this.buttonSave.Text = "Сохранить";
this.buttonSave.UseVisualStyleBackColor = true; this.buttonSave.UseVisualStyleBackColor = true;
@ -145,32 +154,34 @@
// //
// textBoxDateOpening // textBoxDateOpening
// //
this.textBoxDateOpening.Location = new System.Drawing.Point(386, 27); this.textBoxDateOpening.Location = new System.Drawing.Point(441, 36);
this.textBoxDateOpening.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.textBoxDateOpening.Name = "textBoxDateOpening"; this.textBoxDateOpening.Name = "textBoxDateOpening";
this.textBoxDateOpening.Size = new System.Drawing.Size(200, 23); this.textBoxDateOpening.Size = new System.Drawing.Size(228, 27);
this.textBoxDateOpening.TabIndex = 9; this.textBoxDateOpening.TabIndex = 9;
// //
// ReinforcedMaxCountLable // ReinforcedMaxCountLable
// //
this.ReinforcedMaxCountLable.AutoSize = true; this.ReinforcedMaxCountLable.AutoSize = true;
this.ReinforcedMaxCountLable.Location = new System.Drawing.Point(12, 53); this.ReinforcedMaxCountLable.Location = new System.Drawing.Point(14, 71);
this.ReinforcedMaxCountLable.Name = "ReinforcedMaxCountLable"; this.ReinforcedMaxCountLable.Name = "ReinforcedMaxCountLable";
this.ReinforcedMaxCountLable.Size = new System.Drawing.Size(140, 15); this.ReinforcedMaxCountLable.Size = new System.Drawing.Size(177, 20);
this.ReinforcedMaxCountLable.TabIndex = 10; this.ReinforcedMaxCountLable.TabIndex = 10;
this.ReinforcedMaxCountLable.Text = "Вместимость магазина: "; this.ReinforcedMaxCountLable.Text = "Вместимость магазина: ";
// //
// VolumeNumericUpDown // VolumeNumericUpDown
// //
this.VolumeNumericUpDown.Location = new System.Drawing.Point(12, 71); this.VolumeNumericUpDown.Location = new System.Drawing.Point(14, 95);
this.VolumeNumericUpDown.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.VolumeNumericUpDown.Name = "VolumeNumericUpDown"; this.VolumeNumericUpDown.Name = "VolumeNumericUpDown";
this.VolumeNumericUpDown.Size = new System.Drawing.Size(141, 23); this.VolumeNumericUpDown.Size = new System.Drawing.Size(161, 27);
this.VolumeNumericUpDown.TabIndex = 11; this.VolumeNumericUpDown.TabIndex = 11;
// //
// FormShop // FormShop
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(607, 405); this.ClientSize = new System.Drawing.Size(694, 540);
this.Controls.Add(this.VolumeNumericUpDown); this.Controls.Add(this.VolumeNumericUpDown);
this.Controls.Add(this.ReinforcedMaxCountLable); this.Controls.Add(this.ReinforcedMaxCountLable);
this.Controls.Add(this.textBoxDateOpening); this.Controls.Add(this.textBoxDateOpening);
@ -182,6 +193,7 @@
this.Controls.Add(this.dataGridView); this.Controls.Add(this.dataGridView);
this.Controls.Add(this.comboBoxShop); this.Controls.Add(this.comboBoxShop);
this.Controls.Add(this.label1); this.Controls.Add(this.label1);
this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.Name = "FormShop"; this.Name = "FormShop";
this.Text = "Просмотр изделий магазина"; this.Text = "Просмотр изделий магазина";
this.Load += new System.EventHandler(this.FormShop_Load); this.Load += new System.EventHandler(this.FormShop_Load);

@ -1,6 +1,7 @@
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using PrecastConcretePlantContracts.BindingModels; using PrecastConcretePlantContracts.BindingModels;
using PrecastConcretePlantContracts.BusinessLogicsContracts; using PrecastConcretePlantContracts.BusinessLogicsContracts;
using PrecastConcretePlantContracts.SearchModels;
using PrecastConcretePlantContracts.ViewModels; using PrecastConcretePlantContracts.ViewModels;
using PrecastConcretePlantDataModels.Models; using PrecastConcretePlantDataModels.Models;
using System; using System;
@ -19,127 +20,75 @@ namespace ConfectioneryView
{ {
public partial class FormShop : Form public partial class FormShop : Form
{ {
private readonly List<ShopViewModel>? _listShops;
private readonly IShopLogic _logic; private readonly IShopLogic _logic;
private readonly ILogger _logger; private readonly ILogger _logger;
private int? _id;
public int Id private Dictionary<int, (IReinforcedModel, int)> _listShops;
{ public int Id { set { _id = value; } }
get; set;
}
private IShopModel? GetShop(int id)
{
if (_listShops == null)
{
return null;
}
foreach (var elem in _listShops)
{
if (elem.Id == id)
{
return elem;
}
}
return null;
}
public FormShop(ILogger<FormShop> logger, IShopLogic logic) public FormShop(ILogger<FormShop> logger, IShopLogic logic)
{ {
InitializeComponent(); InitializeComponent();
_logger = logger; _logger = logger;
_listShops = logic.ReadList(null); _listShops = new();
_logic = logic; _logic = logic;
if (_listShops != null)
{
comboBoxShop.DisplayMember = "Name";
comboBoxShop.ValueMember = "Id";
comboBoxShop.DataSource = _listShops;
comboBoxShop.SelectedItem = null;
}
} }
private void LoadData(bool extendDate = true) private void LoadData(bool extendDate = true)
{ {
_logger.LogInformation("Загрузка изделий магазина");
try try
{ {
var model = GetShop(extendDate ? Id : Convert.ToInt32(comboBoxShop.SelectedValue)); if (_listShops != null)
if (model != null) {
{
comboBoxShop.Text = model.Name;
textBoxAddress.Text = model.Address;
textBoxDateOpening.Text = Convert.ToString(model.DateOpening);
VolumeNumericUpDown.Value = model.ReinforcedMaxCount;
dataGridView.Rows.Clear(); dataGridView.Rows.Clear();
foreach (var el in model.ShopReinforcedies.Values) foreach (var elem in _listShops)
{ {
dataGridView.Rows.Add(new object[]{el.Item1.ReinforcedName, el.Item1.Price, el.Item2 }); dataGridView.Rows.Add(new object[] { elem.Key, elem.Value.Item1.ReinforcedName, elem.Value.Item2 });
} }
} }
_logger.LogInformation("Загрузка магазинов");
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "Ошибка загрузки магазинов"); _logger.LogError(ex, "Ошибка загрузки изделий магазина");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBoxIcon.Error);
} }
} }
private void ComboBoxShop_SelectedIndexChanged(object sender, EventArgs e)
{
LoadData(false);
}
private void ButtonSave_Click(object sender, EventArgs e) private void ButtonSave_Click(object sender, EventArgs e)
{ {
if (string.IsNullOrEmpty(comboBoxShop.Text)) if (string.IsNullOrEmpty(comboBoxShop.Text))
{ {
MessageBox.Show("Заполните название", "Ошибка", MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBoxButtons.OK, MessageBoxIcon.Error);
return; return;
} }
if (string.IsNullOrEmpty(textBoxAddress.Text)) if (string.IsNullOrEmpty(textBoxAddress.Text))
{ {
MessageBox.Show("Заполните адрес", "Ошибка", MessageBoxButtons.OK, MessageBox.Show("Заполните адрес", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBoxIcon.Error);
return; return;
} }
_logger.LogInformation("Сохранение магазина"); _logger.LogInformation("Сохранение магазина");
try try
{ {
DateTime.TryParse(textBoxDateOpening.Text, out var dateTime); var model = new ShopBindingModel
ShopBindingModel model = new()
{ {
Id = _id ?? 0,
Name = comboBoxShop.Text, Name = comboBoxShop.Text,
Address = textBoxAddress.Text, Address = textBoxAddress.Text,
DateOpening = dateTime, DateOpening = textBoxDateOpening.Value.Date,
ReinforcedMaxCount = (int)VolumeNumericUpDown.Value ReinforcedMaxCount = (int)VolumeNumericUpDown.Value
}; };
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("Сохранение прошло успешно", "Сообщение", MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
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);
} }
} }
@ -152,7 +101,32 @@ namespace ConfectioneryView
private void FormShop_Load(object sender, EventArgs e) private void FormShop_Load(object sender, EventArgs e)
{ {
LoadData(); if (_id.HasValue)
{
_logger.LogInformation("Загрузка магазина");
try
{
var view = _logic.ReadElement(new ShopSearchModel
{
Id = _id.Value
});
if (view != null)
{
comboBoxShop.Text = view.Name;
textBoxAddress.Text = view.Address;
textBoxDateOpening.Text = view.DateOpening.ToString();
VolumeNumericUpDown.Value = view.ReinforcedMaxCount;
_listShops = view.ShopReinforcedies ?? new Dictionary<int, (IReinforcedModel, int)>();
LoadData();
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка загрузки магазина");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
} }
} }
} }