Исправления

This commit is contained in:
Данияр Аглиуллов 2023-02-06 01:31:42 +04:00
parent 48a5b72bbb
commit 785d086b3f
4 changed files with 36 additions and 53 deletions

View File

@ -52,7 +52,8 @@ namespace ConfectioneryListImplement
Id = Id, Id = Id,
Name = Name, Name = Name,
Address = Address, Address = Address,
Pastries = Pastries Pastries = Pastries,
DateOpening = DateOpening,
}; };
} }
} }

View File

@ -163,6 +163,7 @@
this.Controls.Add(this.label1); this.Controls.Add(this.label1);
this.Name = "FormShop"; this.Name = "FormShop";
this.Text = "Просмотр изделий магазина"; this.Text = "Просмотр изделий магазина";
this.Load += new System.EventHandler(this.FormShop_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit();
this.ResumeLayout(false); this.ResumeLayout(false);
this.PerformLayout(); this.PerformLayout();

View File

@ -23,34 +23,21 @@ namespace ConfectioneryView
private readonly List<ShopViewModel>? _listShops; private readonly List<ShopViewModel>? _listShops;
private readonly IShopLogic _logic; private readonly IShopLogic _logic;
private readonly ILogger _logger; private readonly ILogger _logger;
private IShopModel? _currentShopModel;
public int Id public int Id
{ {
get get; set;
{
return Convert.ToInt32(comboBoxShop.SelectedValue);
}
set
{
comboBoxShop.SelectedValue = value;
}
}
public IShopModel? ShopModel
{
get => _currentShopModel;
} }
private IShopModel? GetShop() private IShopModel? GetShop()
{ {
var list = _logic.ReadList(null); if (_listShops == null)
if (list == null)
{ {
return null; return null;
} }
foreach (var elem in list) foreach (var elem in _listShops)
{ {
if (elem.Id == this.Id) if (elem.Id == Id)
{ {
return elem; return elem;
} }
@ -77,22 +64,18 @@ namespace ConfectioneryView
{ {
try try
{ {
var currentShop = _logic.ReadElement(new() { Id = this.Id}); var model = GetShop();
if (currentShop != null) if (model != null)
{ {
var vmodel = GetShop(); comboBoxShop.Text = model.Name;
if (vmodel != null) textBoxAddress.Text = model.Address;
{ textBoxDateOpening.Text = Convert.ToString(model.DateOpening);
comboBoxShop.Text = vmodel.Name;
textBoxAddress.Text = vmodel.Address;
textBoxDateOpening.Text = Convert.ToString(vmodel.DateOpening);
}
dataGridView.Rows.Clear(); dataGridView.Rows.Clear();
foreach (var el in currentShop.Pastries.Values) foreach (var el in model.Pastries.Values)
{ {
dataGridView.Rows.Add(new object[]{el.Item1.PastryName, el.Item1.Price, el.Item2 }); dataGridView.Rows.Add(new object[]{el.Item1.PastryName, el.Item1.Price, el.Item2 });
} }
} }
_logger.LogInformation("Загрузка магазинов"); _logger.LogInformation("Загрузка магазинов");
} }
@ -126,13 +109,6 @@ namespace ConfectioneryView
_logger.LogInformation("Сохранение изделия"); _logger.LogInformation("Сохранение изделия");
try try
{ {
var vmodel = GetShop();
if (vmodel != null)
{
_currentShopModel = vmodel;
return;
}
// Создаем новый магазин если не нашли такого
DateTime.TryParse(textBoxDateOpening.Text, out var dateTime); DateTime.TryParse(textBoxDateOpening.Text, out var dateTime);
ShopBindingModel model = new() ShopBindingModel model = new()
{ {
@ -140,8 +116,18 @@ namespace ConfectioneryView
Address = textBoxAddress.Text, Address = textBoxAddress.Text,
DateOpening = dateTime DateOpening = dateTime
}; };
var operationResult = _logic.Create(model); var vmodel = GetShop();
_currentShopModel = 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("Ошибка при сохранении. Дополнительная информация в логах.");
@ -163,5 +149,10 @@ namespace ConfectioneryView
DialogResult = DialogResult.Cancel; DialogResult = DialogResult.Cancel;
Close(); Close();
} }
private void FormShop_Load(object sender, EventArgs e)
{
LoadData();
}
} }
} }

View File

@ -26,7 +26,9 @@ namespace ConfectioneryView
if (list != null) if (list != null)
{ {
dataGridView.DataSource = list; dataGridView.DataSource = list;
dataGridView.Columns["Id"].Visible = false; dataGridView.Columns["Id"].Visible = false;
dataGridView.Columns["Pastries"].Visible = false;
dataGridView.Columns["Name"].AutoSizeMode = dataGridView.Columns["Name"].AutoSizeMode =
DataGridViewAutoSizeColumnMode.Fill; DataGridViewAutoSizeColumnMode.Fill;
} }
@ -57,24 +59,12 @@ namespace ConfectioneryView
var service = Program.ServiceProvider?.GetService(typeof(FormShop)); var service = Program.ServiceProvider?.GetService(typeof(FormShop));
if (service is FormShop form) if (service is FormShop form)
{ {
var id = form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value);
if (form.ShowDialog() == DialogResult.OK) if (form.ShowDialog() == DialogResult.OK)
{ {
var model = form.ShopModel; LoadData();
if (model != null)
{
_logic.Update(new()
{
Id = id,
Address = model.Address,
Name = model.Name,
DateOpening= model.DateOpening,
});
}
} }
} }
LoadData();
} }
} }
private void ButtonDel_Click(object sender, EventArgs e) private void ButtonDel_Click(object sender, EventArgs e)