Изменение логики 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.buttonCancel = new System.Windows.Forms.Button();
this.textBoxDateOpening = new System.Windows.Forms.TextBox();
this.textBoxAddress = new System.Windows.Forms.TextBox();
this.labelTime = new System.Windows.Forms.Label();
this.labelAddress = new System.Windows.Forms.Label();
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.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();
this.SuspendLayout();
//
@ -65,13 +65,6 @@
this.buttonCancel.UseVisualStyleBackColor = true;
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
//
this.textBoxAddress.Location = new System.Drawing.Point(159, 27);
@ -84,7 +77,7 @@
this.labelTime.AutoSize = true;
this.labelTime.Location = new System.Drawing.Point(386, 9);
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.Text = "Дата открытия";
//
@ -104,40 +97,15 @@
| System.Windows.Forms.AnchorStyles.Right)));
this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.SushiName,
this.Price,
this.Count});
this.ColumnID,
this.ColumnSushiName,
this.ColumnCount});
this.dataGridView.Location = new System.Drawing.Point(12, 56);
this.dataGridView.Name = "dataGridView";
this.dataGridView.RowTemplate.Height = 25;
this.dataGridView.Size = new System.Drawing.Size(581, 240);
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
//
this.labelShop.AutoSize = true;
@ -147,22 +115,54 @@
this.labelShop.TabIndex = 9;
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
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
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.buttonCancel);
this.Controls.Add(this.textBoxDateOpening);
this.Controls.Add(this.textBoxAddress);
this.Controls.Add(this.labelTime);
this.Controls.Add(this.labelAddress);
this.Controls.Add(this.dataGridView);
this.Controls.Add(this.comboBoxShop);
this.Controls.Add(this.labelShop);
this.Name = "FormShop";
this.Text = "Магазин";
this.Load += new System.EventHandler(this.FormShop_Load);
this.Click += new System.EventHandler(this.FormShop_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit();
this.ResumeLayout(false);
@ -174,15 +174,15 @@
private Button buttonSave;
private Button buttonCancel;
private TextBox textBoxDateOpening;
private TextBox textBoxAddress;
private Label labelTime;
private Label labelAddress;
private DataGridView dataGridView;
private DataGridViewTextBoxColumn SushiName;
private DataGridViewTextBoxColumn Price;
private DataGridViewTextBoxColumn Count;
private ComboBox comboBoxShop;
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 SushiBarContracts.BindingModels;
using SushiBarContracts.BusinessLogicsContracts;
using SushiBarContracts.ViewModels;
using SushiBarContracts.SearchModels;
using SushiBarDataModels.Models;
namespace SushiBarView
{
public partial class FormShop : Form
{
private readonly List<ShopViewModel>? _listShops;
private readonly IShopLogic _logic;
private readonly ILogger _logger;
public int Id { 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;
}
private readonly IShopLogic _logic;
private int? _id;
private Dictionary<int, (ISushiModel, int)> _shopListSushi;
public int Id { set { _id = value; } }
public FormShop(ILogger<FormShop> logger, IShopLogic logic)
{
InitializeComponent();
_logger = logger;
_listShops = logic.ReadList(null);
_logic = logic;
if (_listShops != null)
{
comboBoxShop.DisplayMember = "ShopName";
comboBoxShop.ValueMember = "Id";
comboBoxShop.DataSource = _listShops;
comboBoxShop.SelectedItem = null;
}
_shopListSushi = new();
}
private void FormShop_Load(object sender, EventArgs e)
{
LoadData();
}
private void LoadData(bool extendDate = true)
if (_id.HasValue)
{
_logger.LogInformation("Загрузка магазина");
try
{
var model = GetShop(extendDate ? Id : Convert.ToInt32(comboBoxShop.SelectedValue));
if (model != null)
var view = _logic.ReadElement(new ShopSearchModel
{
comboBoxShop.Text = model.ShopName;
textBoxAddress.Text = model.Address;
textBoxDateOpening.Text = Convert.ToString(model.DateOpening);
dataGridView.Rows.Clear();
foreach (var el in model.ListSushi.Values)
Id = _id.Value
});
if (view != null)
{
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)
{
_logger.LogError(ex, "Ошибка загрузки магазинов");
_logger.LogError(ex, "Ошибка загрузки магазина");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
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)
{
if (string.IsNullOrEmpty(comboBoxShop.Text))
if (string.IsNullOrEmpty(textBoxShop.Text))
{
MessageBox.Show("Заполните название", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (string.IsNullOrEmpty(textBoxAddress.Text))
{
MessageBox.Show("Заполните адрес", "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
MessageBox.Show("Заполните адрес", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_logger.LogInformation("Сохранение суши");
_logger.LogInformation("Сохранение магазина");
try
{
DateTime.TryParse(textBoxDateOpening.Text, out var dateTime);
ShopBindingModel model = new()
var model = new ShopBindingModel
{
ShopName = comboBoxShop.Text,
Id = _id ?? 0,
ShopName = textBoxShop.Text,
Address = textBoxAddress.Text,
DateOpening = dateTime
DateOpening = dateTimePicker.Value.Date
};
var vmodel = GetShop(Id);
bool operationResult = false;
if (vmodel != null)
{
model.Id = vmodel.Id;
operationResult = _logic.Update(model);
}
else
{
operationResult = _logic.Create(model);
}
var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model);
if (!operationResult)
{
throw new Exception("Ошибка при сохранении. Дополнительная информация в логах.");
}
MessageBox.Show("Сохранение прошло успешно", "Сообщение",
MessageBoxButtons.OK, MessageBoxIcon.Information);
MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
DialogResult = DialogResult.OK;
Close();
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка сохранения суши");
_logger.LogError(ex, "Ошибка сохранения магазина");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

View File

@ -57,13 +57,13 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</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>
</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>
</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>
</metadata>
</root>

View File

@ -13,21 +13,31 @@ namespace SushiBarListImplement.Implements
{
_source = DataListSingleton.GetInstance();
}
public ShopViewModel? Delete(ShopBindingModel model)
public List<ShopViewModel> GetFullList()
{
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];
_source.Shops.RemoveAt(i);
return element.GetViewModel;
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 null;
return result;
}
public ShopViewModel? GetElement(ShopSearchModel model)
{
if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue)
@ -45,34 +55,6 @@ namespace SushiBarListImplement.Implements
}
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)
{
model.Id = 1;
@ -91,7 +73,6 @@ namespace SushiBarListImplement.Implements
_source.Shops.Add(newShop);
return newShop.GetViewModel;
}
public ShopViewModel? Update(ShopBindingModel model)
{
foreach (var shop in _source.Shops)
@ -104,5 +85,18 @@ namespace SushiBarListImplement.Implements
}
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 int Id { get; private set; }
public string ShopName { get; private set; } = string.Empty;
public string Address { get; private set; } = string.Empty;
public DateTime DateOpening { get; private set; }
public Dictionary<int, (ISushiModel, int)> ListSushi
{
get;
private set;
} = new();
public int Id { get; private set; }
public static Shop? Create(ShopBindingModel? model)
{
if (model == null)