точно готовая 1 усл
This commit is contained in:
parent
3077f482fc
commit
bc38151b42
@ -12,129 +12,152 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
using SoftwareInstallationContracts.BindingModels;
|
||||
using System.Security.Cryptography;
|
||||
using SoftwareInstallationContracts.SearchModels;
|
||||
using SoftwareInstallationDataModels.Models;
|
||||
|
||||
namespace SoftwareInstallationView
|
||||
{
|
||||
public partial class FormShop : Form
|
||||
{
|
||||
private readonly List<ShopViewModel>? _listShops;
|
||||
private readonly IShopLogic _logic;
|
||||
/// <summary>
|
||||
/// Логгер
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
/// <summary>
|
||||
/// Бизнес-логика для магазинов
|
||||
/// </summary>
|
||||
private readonly IShopLogic _logic;
|
||||
/// <summary>
|
||||
/// Идентификатор
|
||||
/// </summary>
|
||||
private int? _id;
|
||||
public int Id { set { _id = value; } }
|
||||
/// <summary>
|
||||
/// Список изделий в магазине
|
||||
/// </summary>
|
||||
private Dictionary<int, (IPackageModel, int)> _shopPackages;
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="logger"></param>
|
||||
/// <param name="logic"></param>
|
||||
public FormShop(ILogger<FormShop> logger, IShopLogic logic)
|
||||
{
|
||||
InitializeComponent();
|
||||
_logger = logger;
|
||||
_listShops = logic.ReadList(null);
|
||||
_logic = logic;
|
||||
|
||||
_shopPackages = new Dictionary<int, (IPackageModel, int)>();
|
||||
}
|
||||
private void LoadData(bool extendDate = true)
|
||||
/// <summary>
|
||||
/// Загрузка списка изделий в магазине
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void FormShop_Load(object sender, EventArgs e)
|
||||
{
|
||||
if (_id.HasValue)
|
||||
{
|
||||
_logger.LogInformation("Загрузка магазина");
|
||||
try
|
||||
{
|
||||
var model = GetShop(extendDate ? Id : Convert.ToInt32(null));
|
||||
if (model != null)
|
||||
var view = _logic.ReadElement(new ShopSearchModel { Id = _id.Value });
|
||||
if (view != null)
|
||||
{
|
||||
textBoxShop.Text = model.Name;
|
||||
textBoxAddress.Text = model.Address;
|
||||
openingDatePicker.Text = Convert.ToString(model.DateOpening);
|
||||
dataGridView.Rows.Clear();
|
||||
foreach (var el in model.Packages.Values)
|
||||
{
|
||||
dataGridView.Rows.Add(new object[] { el.Item1.PackageName, el.Item1.Price, el.Item2 });
|
||||
textBoxShop.Text = view.Name;
|
||||
textBoxAddress.Text = view.Address;
|
||||
openingDatePicker.Text = view.DateOpening.ToString();
|
||||
_shopPackages = view.Packages ?? new Dictionary<int, (IPackageModel, int)>();
|
||||
LoadData();
|
||||
}
|
||||
}
|
||||
_logger.LogInformation("Загрузка магазинов");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка загрузки магазинов");
|
||||
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
|
||||
MessageBoxIcon.Error);
|
||||
_logger.LogError(ex, "Ошибка загрузки магазина");
|
||||
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// Кнопка "Сохранить"
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void buttonSave_Click(object sender, EventArgs e)
|
||||
{
|
||||
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(openingDatePicker.Text, out var dateTime);
|
||||
ShopBindingModel model = new()
|
||||
var model = new ShopBindingModel
|
||||
{
|
||||
Id = _id ?? 0,
|
||||
Name = textBoxShop.Text,
|
||||
Address = textBoxAddress.Text,
|
||||
DateOpening = dateTime
|
||||
DateOpening = openingDatePicker.Value.Date,
|
||||
Packages = _shopPackages
|
||||
};
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Кнопка "Отмена"
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void buttonCancel_Click(object sender, EventArgs e)
|
||||
{
|
||||
DialogResult = DialogResult.Cancel;
|
||||
Close();
|
||||
}
|
||||
private void FormShop_Load(object sender, EventArgs e)
|
||||
/// <summary>
|
||||
/// Метод загрузки изделий магазина
|
||||
/// </summary>
|
||||
private void LoadData()
|
||||
{
|
||||
LoadData();
|
||||
_logger.LogInformation("Загрузка изделий магазина");
|
||||
try
|
||||
{
|
||||
if (_shopPackages != null)
|
||||
{
|
||||
dataGridView.Rows.Clear();
|
||||
foreach (var elem in _shopPackages)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user