исправления

This commit is contained in:
AnnZhimol 2023-03-26 14:15:48 +04:00
parent 064509309b
commit 372ebcf2f0
2 changed files with 44 additions and 67 deletions

View File

@ -80,7 +80,6 @@
this.NameComboBox.Name = "NameComboBox"; this.NameComboBox.Name = "NameComboBox";
this.NameComboBox.Size = new System.Drawing.Size(174, 23); this.NameComboBox.Size = new System.Drawing.Size(174, 23);
this.NameComboBox.TabIndex = 3; this.NameComboBox.TabIndex = 3;
this.NameComboBox.SelectedIndexChanged += new System.EventHandler(this.NameComboBox_SelectedIndexChanged);
// //
// AdressTextBox // AdressTextBox
// //

View File

@ -1,47 +1,28 @@
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using SofrwareInstallationContracts.BindingModels; using SofrwareInstallationContracts.BindingModels;
using SofrwareInstallationContracts.BusinessLogicsContracts; using SofrwareInstallationContracts.BusinessLogicsContracts;
using SofrwareInstallationContracts.SearchModels;
using SofrwareInstallationContracts.ViewModels; using SofrwareInstallationContracts.ViewModels;
using SoftwareInstallationDataModels.Models; using SoftwareInstallationDataModels.Models;
using System.Reflection;
using System.Windows.Forms;
namespace SoftwareInstallationView namespace SoftwareInstallationView
{ {
public partial class FormStore : Form public partial class FormStore : Form
{ {
private readonly List<StoreViewModel>? _listStores;
private readonly IStoreLogic _logic; private readonly IStoreLogic _logic;
private readonly ILogger _logger; private readonly ILogger _logger;
public int Id { get; set; } private int? _id;
private Dictionary<int, (IPackageModel, int)> _listStores;
public int Id { set { _id = value; } }
public FormStore(ILogger<FormStore> logger, IStoreLogic logic) public FormStore(ILogger<FormStore> logger, IStoreLogic logic)
{ {
InitializeComponent(); InitializeComponent();
_logger = logger; _logger = logger;
_listStores = logic.ReadList(null); _listStores = new();
_logic = logic; _logic = logic;
if (_listStores != null)
{
NameComboBox.DisplayMember = "StoreName";
NameComboBox.ValueMember = "Id";
NameComboBox.DataSource = _listStores;
NameComboBox.SelectedItem = null;
}
}
private IStoreModel? GetStore(int id)
{
if (_listStores == null)
{
return null;
}
foreach (var elem in _listStores)
{
if (elem.Id == id)
{
return elem;
}
}
return null;
} }
private void SaveButton_Click(object sender, EventArgs e) private void SaveButton_Click(object sender, EventArgs e)
@ -51,50 +32,34 @@ namespace SoftwareInstallationView
MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return; return;
} }
if (string.IsNullOrEmpty(AdressTextBox.Text)) if (string.IsNullOrEmpty(AdressTextBox.Text))
{ {
MessageBox.Show("Заполните адрес", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBox.Show("Заполните адрес", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return; return;
} }
_logger.LogInformation("Сохранение магазина"); _logger.LogInformation("Сохранение магазина");
try try
{ {
DateTime.TryParse(OpeningDatePicker.Text, out var dateTime); var model = new StoreBindingModel
StoreBindingModel model = new()
{ {
Id = _id ?? 0,
StoreName = NameComboBox.Text, StoreName = NameComboBox.Text,
StoreAdress = AdressTextBox.Text, StoreAdress = AdressTextBox.Text,
OpeningDate = dateTime, OpeningDate = OpeningDatePicker.Value.Date,
PackageMaxCount = (int)VolumeNumericUpDown.Value PackageMaxCount = (int)VolumeNumericUpDown.Value
}; };
var vmodel = GetStore(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);
} }
} }
@ -107,39 +72,52 @@ namespace SoftwareInstallationView
private void FormStore_Load(object sender, EventArgs e) private void FormStore_Load(object sender, EventArgs e)
{ {
LoadData(); if (_id.HasValue)
}
private void LoadData(bool extendDate = true)
{ {
_logger.LogInformation("Загрузка магазина");
try try
{ {
var model = GetStore(extendDate ? Id : Convert.ToInt32(NameComboBox.SelectedValue)); var view = _logic.ReadElement(new StoreSearchModel
if (model != null)
{ {
NameComboBox.Text = model.StoreName; Id = _id.Value
AdressTextBox.Text = model.StoreAdress; });
OpeningDatePicker.Text = Convert.ToString(model.OpeningDate); if (view != null)
VolumeNumericUpDown.Value = model.PackageMaxCount;
DataGridView.Rows.Clear();
foreach (var el in model.StorePackages.Values)
{ {
DataGridView.Rows.Add(new object[] { el.Item1.PackageName, el.Item1.Price, el.Item2 }); NameComboBox.Text = view.StoreName;
AdressTextBox.Text = view.StoreAdress;
OpeningDatePicker.Text = view.OpeningDate.ToString();
VolumeNumericUpDown.Value = view.PackageMaxCount;
_listStores = view.StorePackages ?? new Dictionary<int, (IPackageModel, 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 NameComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
LoadData(false);
} }
private void LoadData(bool extendDate = true)
{
_logger.LogInformation("Загрузка изделий магазина");
try
{
if (_listStores != null)
{
DataGridView.Rows.Clear();
foreach (var elem in _listStores)
{
DataGridView.Rows.Add(new object[] { elem.Key, elem.Value.Item1.PackageName, elem.Value.Item2 });
}
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка загрузки изделий магазина");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
} }
} }