Изменение логики FormShop. Исправление форматирования в некоторых классах

This commit is contained in:
dasha 2023-02-14 00:26:18 +04:00
parent 65d38414ee
commit a4a2c34044
5 changed files with 139 additions and 170 deletions

View File

@ -30,16 +30,16 @@
{ {
this.buttonSave = new System.Windows.Forms.Button(); this.buttonSave = new System.Windows.Forms.Button();
this.buttonCancel = new System.Windows.Forms.Button(); this.buttonCancel = new System.Windows.Forms.Button();
this.textBoxDateOpening = new System.Windows.Forms.TextBox();
this.textBoxAddress = new System.Windows.Forms.TextBox(); this.textBoxAddress = new System.Windows.Forms.TextBox();
this.labelTime = new System.Windows.Forms.Label(); this.labelTime = new System.Windows.Forms.Label();
this.labelAddress = new System.Windows.Forms.Label(); this.labelAddress = new System.Windows.Forms.Label();
this.dataGridView = new System.Windows.Forms.DataGridView(); this.dataGridView = new System.Windows.Forms.DataGridView();
this.SushiName = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Price = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Count = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.comboBoxShop = new System.Windows.Forms.ComboBox();
this.labelShop = new System.Windows.Forms.Label(); this.labelShop = new System.Windows.Forms.Label();
this.textBoxShop = new System.Windows.Forms.TextBox();
this.dateTimePicker = new System.Windows.Forms.DateTimePicker();
this.ColumnID = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.ColumnSushiName = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.ColumnCount = new System.Windows.Forms.DataGridViewTextBoxColumn();
((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit();
this.SuspendLayout(); this.SuspendLayout();
// //
@ -65,13 +65,6 @@
this.buttonCancel.UseVisualStyleBackColor = true; this.buttonCancel.UseVisualStyleBackColor = true;
this.buttonCancel.Click += new System.EventHandler(this.ButtonCancel_Click); this.buttonCancel.Click += new System.EventHandler(this.ButtonCancel_Click);
// //
// textBoxDateOpening
//
this.textBoxDateOpening.Location = new System.Drawing.Point(386, 27);
this.textBoxDateOpening.Name = "textBoxDateOpening";
this.textBoxDateOpening.Size = new System.Drawing.Size(209, 23);
this.textBoxDateOpening.TabIndex = 15;
//
// textBoxAddress // textBoxAddress
// //
this.textBoxAddress.Location = new System.Drawing.Point(159, 27); this.textBoxAddress.Location = new System.Drawing.Point(159, 27);
@ -84,7 +77,7 @@
this.labelTime.AutoSize = true; this.labelTime.AutoSize = true;
this.labelTime.Location = new System.Drawing.Point(386, 9); this.labelTime.Location = new System.Drawing.Point(386, 9);
this.labelTime.Name = "labelTime"; this.labelTime.Name = "labelTime";
this.labelTime.Size = new System.Drawing.Size(97, 15); this.labelTime.Size = new System.Drawing.Size(87, 15);
this.labelTime.TabIndex = 13; this.labelTime.TabIndex = 13;
this.labelTime.Text = "Дата открытия"; this.labelTime.Text = "Дата открытия";
// //
@ -104,40 +97,15 @@
| System.Windows.Forms.AnchorStyles.Right))); | System.Windows.Forms.AnchorStyles.Right)));
this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { this.dataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.SushiName, this.ColumnID,
this.Price, this.ColumnSushiName,
this.Count}); this.ColumnCount});
this.dataGridView.Location = new System.Drawing.Point(12, 56); this.dataGridView.Location = new System.Drawing.Point(12, 56);
this.dataGridView.Name = "dataGridView"; this.dataGridView.Name = "dataGridView";
this.dataGridView.RowTemplate.Height = 25; this.dataGridView.RowTemplate.Height = 25;
this.dataGridView.Size = new System.Drawing.Size(581, 240); this.dataGridView.Size = new System.Drawing.Size(581, 240);
this.dataGridView.TabIndex = 11; this.dataGridView.TabIndex = 11;
// //
// SushiName
//
this.SushiName.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
this.SushiName.HeaderText = "Суши";
this.SushiName.Name = "SushiName";
//
// Price
//
this.Price.HeaderText = "Цена";
this.Price.Name = "Price";
//
// Count
//
this.Count.HeaderText = "Количество";
this.Count.Name = "Count";
//
// comboBoxShop
//
this.comboBoxShop.FormattingEnabled = true;
this.comboBoxShop.Location = new System.Drawing.Point(12, 27);
this.comboBoxShop.Name = "comboBoxShop";
this.comboBoxShop.Size = new System.Drawing.Size(141, 23);
this.comboBoxShop.TabIndex = 10;
this.comboBoxShop.SelectedIndexChanged += new System.EventHandler(this.ComboBoxShop_SelectedIndexChanged);
//
// labelShop // labelShop
// //
this.labelShop.AutoSize = true; this.labelShop.AutoSize = true;
@ -147,22 +115,54 @@
this.labelShop.TabIndex = 9; this.labelShop.TabIndex = 9;
this.labelShop.Text = "Магазин"; this.labelShop.Text = "Магазин";
// //
// textBoxShop
//
this.textBoxShop.Location = new System.Drawing.Point(12, 27);
this.textBoxShop.Name = "textBoxShop";
this.textBoxShop.Size = new System.Drawing.Size(141, 23);
this.textBoxShop.TabIndex = 18;
//
// dateTimePicker
//
this.dateTimePicker.Location = new System.Drawing.Point(386, 27);
this.dateTimePicker.Name = "dateTimePicker";
this.dateTimePicker.Size = new System.Drawing.Size(207, 23);
this.dateTimePicker.TabIndex = 19;
//
// ColumnID
//
this.ColumnID.HeaderText = "ID";
this.ColumnID.Name = "ColumnID";
this.ColumnID.Visible = false;
//
// ColumnSushiName
//
this.ColumnSushiName.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
this.ColumnSushiName.HeaderText = "Суши";
this.ColumnSushiName.Name = "ColumnSushiName";
//
// ColumnCount
//
this.ColumnCount.HeaderText = "Количество";
this.ColumnCount.Name = "ColumnCount";
//
// FormShop // FormShop
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(605, 337); this.ClientSize = new System.Drawing.Size(605, 337);
this.Controls.Add(this.dateTimePicker);
this.Controls.Add(this.textBoxShop);
this.Controls.Add(this.buttonSave); this.Controls.Add(this.buttonSave);
this.Controls.Add(this.buttonCancel); this.Controls.Add(this.buttonCancel);
this.Controls.Add(this.textBoxDateOpening);
this.Controls.Add(this.textBoxAddress); this.Controls.Add(this.textBoxAddress);
this.Controls.Add(this.labelTime); this.Controls.Add(this.labelTime);
this.Controls.Add(this.labelAddress); this.Controls.Add(this.labelAddress);
this.Controls.Add(this.dataGridView); this.Controls.Add(this.dataGridView);
this.Controls.Add(this.comboBoxShop);
this.Controls.Add(this.labelShop); this.Controls.Add(this.labelShop);
this.Name = "FormShop"; this.Name = "FormShop";
this.Text = "Магазин"; this.Text = "Магазин";
this.Load += new System.EventHandler(this.FormShop_Load);
this.Click += new System.EventHandler(this.FormShop_Load); this.Click += new System.EventHandler(this.FormShop_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit();
this.ResumeLayout(false); this.ResumeLayout(false);
@ -174,15 +174,15 @@
private Button buttonSave; private Button buttonSave;
private Button buttonCancel; private Button buttonCancel;
private TextBox textBoxDateOpening;
private TextBox textBoxAddress; private TextBox textBoxAddress;
private Label labelTime; private Label labelTime;
private Label labelAddress; private Label labelAddress;
private DataGridView dataGridView; private DataGridView dataGridView;
private DataGridViewTextBoxColumn SushiName;
private DataGridViewTextBoxColumn Price;
private DataGridViewTextBoxColumn Count;
private ComboBox comboBoxShop;
private Label labelShop; private Label labelShop;
private TextBox textBoxShop;
private DateTimePicker dateTimePicker;
private DataGridViewTextBoxColumn ColumnID;
private DataGridViewTextBoxColumn ColumnSushiName;
private DataGridViewTextBoxColumn ColumnCount;
} }
} }

View File

@ -1,131 +1,111 @@
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using SushiBarContracts.BindingModels; using SushiBarContracts.BindingModels;
using SushiBarContracts.BusinessLogicsContracts; using SushiBarContracts.BusinessLogicsContracts;
using SushiBarContracts.ViewModels; using SushiBarContracts.SearchModels;
using SushiBarDataModels.Models; using SushiBarDataModels.Models;
namespace SushiBarView namespace SushiBarView
{ {
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 IShopModel? GetShop(int id) private Dictionary<int, (ISushiModel, int)> _shopListSushi;
{ public int Id { set { _id = value; } }
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);
_logic = logic; _logic = logic;
if (_listShops != null) _shopListSushi = new();
{
comboBoxShop.DisplayMember = "ShopName";
comboBoxShop.ValueMember = "Id";
comboBoxShop.DataSource = _listShops;
comboBoxShop.SelectedItem = null;
}
} }
private void FormShop_Load(object sender, EventArgs e) private void FormShop_Load(object sender, EventArgs e)
{ {
LoadData();
} if (_id.HasValue)
private void LoadData(bool extendDate = true)
{ {
_logger.LogInformation("Загрузка магазина");
try try
{ {
var model = GetShop(extendDate ? Id : Convert.ToInt32(comboBoxShop.SelectedValue)); var view = _logic.ReadElement(new ShopSearchModel
if (model != null)
{ {
comboBoxShop.Text = model.ShopName; Id = _id.Value
textBoxAddress.Text = model.Address; });
textBoxDateOpening.Text = Convert.ToString(model.DateOpening); if (view != null)
dataGridView.Rows.Clear();
foreach (var el in model.ListSushi.Values)
{ {
dataGridView.Rows.Add(new object[] { el.Item1.SushiName, el.Item1.Price, el.Item2 }); textBoxShop.Text = view.ShopName;
textBoxAddress.Text = view.Address;
_shopListSushi = view.ListSushi ?? new Dictionary<int, (ISushiModel, int)>();
LoadData();
} }
} }
_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) private void LoadData()
{ {
LoadData(false); _logger.LogInformation("Загрузка суши магазина");
try
{
if (_shopListSushi != null)
{
dataGridView.Rows.Clear();
foreach (var elem in _shopListSushi)
{
dataGridView.Rows.Add(new object[] { elem.Key, elem.Value.Item1.SushiName, elem.Value.Item2 });
}
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка загрузки суши магазина");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
} }
private void ButtonSave_Click(object sender, EventArgs e) private void ButtonSave_Click(object sender, EventArgs e)
{ {
if (string.IsNullOrEmpty(comboBoxShop.Text)) if (string.IsNullOrEmpty(textBoxShop.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()
{ {
ShopName = comboBoxShop.Text, Id = _id ?? 0,
ShopName = textBoxShop.Text,
Address = textBoxAddress.Text, Address = textBoxAddress.Text,
DateOpening = dateTime DateOpening = dateTimePicker.Value.Date
}; };
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);
} }
} }

View File

@ -57,13 +57,13 @@
<resheader name="writer"> <resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader> </resheader>
<metadata name="SushiName.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <metadata name="ColumnID.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value> <value>True</value>
</metadata> </metadata>
<metadata name="Price.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <metadata name="ColumnSushiName.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value> <value>True</value>
</metadata> </metadata>
<metadata name="Count.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <metadata name="ColumnCount.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value> <value>True</value>
</metadata> </metadata>
</root> </root>

View File

@ -13,21 +13,31 @@ namespace SushiBarListImplement.Implements
{ {
_source = DataListSingleton.GetInstance(); _source = DataListSingleton.GetInstance();
} }
public List<ShopViewModel> GetFullList()
public ShopViewModel? Delete(ShopBindingModel model)
{ {
for (int i = 0; i < _source.Shops.Count; ++i) var result = new List<ShopViewModel>();
foreach (var shop in _source.Shops)
{ {
if (_source.Shops[i].Id == model.Id) result.Add(shop.GetViewModel);
}
return result;
}
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
{ {
var element = _source.Shops[i]; var result = new List<ShopViewModel>();
_source.Shops.RemoveAt(i); if (string.IsNullOrEmpty(model.ShopName))
return element.GetViewModel; {
return result;
}
foreach (var shop in _source.Shops)
{
if (shop.ShopName.Contains(model.ShopName ?? string.Empty))
{
result.Add(shop.GetViewModel);
} }
} }
return null; return result;
} }
public ShopViewModel? GetElement(ShopSearchModel model) public ShopViewModel? GetElement(ShopSearchModel model)
{ {
if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue) if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue)
@ -45,34 +55,6 @@ namespace SushiBarListImplement.Implements
} }
return null; return null;
} }
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
{
var result = new List<ShopViewModel>();
if (string.IsNullOrEmpty(model.ShopName))
{
return result;
}
foreach (var shop in _source.Shops)
{
if (shop.ShopName.Contains(model.ShopName ?? string.Empty))
{
result.Add(shop.GetViewModel);
}
}
return result;
}
public List<ShopViewModel> GetFullList()
{
var result = new List<ShopViewModel>();
foreach (var shop in _source.Shops)
{
result.Add(shop.GetViewModel);
}
return result;
}
public ShopViewModel? Insert(ShopBindingModel model) public ShopViewModel? Insert(ShopBindingModel model)
{ {
model.Id = 1; model.Id = 1;
@ -91,7 +73,6 @@ namespace SushiBarListImplement.Implements
_source.Shops.Add(newShop); _source.Shops.Add(newShop);
return newShop.GetViewModel; return newShop.GetViewModel;
} }
public ShopViewModel? Update(ShopBindingModel model) public ShopViewModel? Update(ShopBindingModel model)
{ {
foreach (var shop in _source.Shops) foreach (var shop in _source.Shops)
@ -104,5 +85,18 @@ namespace SushiBarListImplement.Implements
} }
return null; return null;
} }
public ShopViewModel? Delete(ShopBindingModel model)
{
for (int i = 0; i < _source.Shops.Count; ++i)
{
if (_source.Shops[i].Id == model.Id)
{
var element = _source.Shops[i];
_source.Shops.RemoveAt(i);
return element.GetViewModel;
}
}
return null;
}
} }
} }

View File

@ -6,20 +6,15 @@ namespace SushiBarListImplement.Models
{ {
public class Shop : IShopModel public class Shop : IShopModel
{ {
public int Id { get; private set; }
public string ShopName { get; private set; } = string.Empty; public string ShopName { get; private set; } = string.Empty;
public string Address { get; private set; } = string.Empty; public string Address { get; private set; } = string.Empty;
public DateTime DateOpening { get; private set; } public DateTime DateOpening { get; private set; }
public Dictionary<int, (ISushiModel, int)> ListSushi public Dictionary<int, (ISushiModel, int)> ListSushi
{ {
get; get;
private set; private set;
} = new(); } = new();
public int Id { get; private set; }
public static Shop? Create(ShopBindingModel? model) public static Shop? Create(ShopBindingModel? model)
{ {
if (model == null) if (model == null)