2024-03-10 23:53:01 +04:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2024-03-11 12:09:55 +04:00
|
|
|
|
using SushiBarContracts.BindingModel;
|
2024-03-10 23:53:01 +04:00
|
|
|
|
using SushiBarContracts.BusinessLogicsContracts;
|
|
|
|
|
using SushiBarContracts.SearchModel;
|
|
|
|
|
using SushiBarDataModels.Models;
|
2024-03-10 23:03:51 +04:00
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
|
|
|
namespace SushiBarView.Shops
|
|
|
|
|
{
|
|
|
|
|
public partial class FormShop : Form
|
|
|
|
|
{
|
2024-03-10 23:53:01 +04:00
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IShopLogic _logic;
|
|
|
|
|
private int? _id;
|
|
|
|
|
public int Id
|
2024-03-10 23:03:51 +04:00
|
|
|
|
{
|
2024-03-10 23:53:01 +04:00
|
|
|
|
set { _id = value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Dictionary<int, (ISushiModel, int)> _shopSushis;
|
2024-03-11 12:09:55 +04:00
|
|
|
|
public FormShop(ILogger<FormShop> logger, IShopLogic shopLogic)
|
2024-03-10 23:53:01 +04:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_logic = shopLogic;
|
|
|
|
|
_shopSushis = new Dictionary<int, (ISushiModel, int)>();
|
2024-03-10 23:03:51 +04:00
|
|
|
|
InitializeComponent();
|
|
|
|
|
}
|
2024-03-10 23:53:01 +04:00
|
|
|
|
|
|
|
|
|
private void FormShop_Load(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (_id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Загрузка магазина");
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var view = _logic.ReadElement(new ShopSearchModel { Id = _id.Value });
|
|
|
|
|
if (view != null)
|
|
|
|
|
{
|
2024-03-11 12:09:55 +04:00
|
|
|
|
textBoxName.Text = view.ShopName;
|
2024-03-10 23:53:01 +04:00
|
|
|
|
textBoxAddress.Text = view.Address;
|
|
|
|
|
dateTimePickerDateOpening.Text = view.DateOpening.ToString();
|
|
|
|
|
_shopSushis = view.ShopSushis ?? new Dictionary<int, (ISushiModel, int)>();
|
|
|
|
|
LoadData();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка загрузки магазина");
|
|
|
|
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-11 12:09:55 +04:00
|
|
|
|
private void LoadData()
|
|
|
|
|
{
|
2024-03-10 23:53:01 +04:00
|
|
|
|
_logger.LogInformation("Загрузка изделий магазина");
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (_shopSushis != null)
|
|
|
|
|
{
|
|
|
|
|
dataGridView.Rows.Clear();
|
2024-03-11 12:09:55 +04:00
|
|
|
|
foreach (var elem in _shopSushis)
|
2024-03-10 23:53:01 +04:00
|
|
|
|
{
|
|
|
|
|
dataGridView.Rows.Add(new object[]
|
|
|
|
|
{
|
|
|
|
|
elem.Key,
|
2024-03-11 12:09:55 +04:00
|
|
|
|
elem.Value.Item1.SushiName,
|
2024-03-10 23:53:01 +04:00
|
|
|
|
elem.Value.Item2
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка загрузки изделий магазина");
|
|
|
|
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-11 12:09:55 +04:00
|
|
|
|
|
|
|
|
|
private void buttonSave_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(textBoxName.Text))
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Введите название магазина", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrEmpty(textBoxAddress.Text))
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Введите название магазина", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation($"Сохранение магазина {textBoxName.Text}");
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var model = new ShopBindingModel
|
|
|
|
|
{
|
|
|
|
|
Id = _id ?? 0,
|
|
|
|
|
ShopName = textBoxName.Text,
|
|
|
|
|
Address = textBoxAddress.Text,
|
|
|
|
|
DateOpening = dateTimePickerDateOpening.Value.Date,
|
|
|
|
|
ShopSushis = _shopSushis
|
|
|
|
|
};
|
|
|
|
|
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, "Ошибка сохранения магазина");
|
|
|
|
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void buttonCancel_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
DialogResult = DialogResult.Cancel;
|
|
|
|
|
Close();
|
|
|
|
|
}
|
2024-03-10 23:03:51 +04:00
|
|
|
|
}
|
|
|
|
|
}
|