исправления

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.Size = new System.Drawing.Size(174, 23);
this.NameComboBox.TabIndex = 3;
this.NameComboBox.SelectedIndexChanged += new System.EventHandler(this.NameComboBox_SelectedIndexChanged);
//
// AdressTextBox
//

View File

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