123 lines
4.8 KiB
C#
123 lines
4.8 KiB
C#
|
using LawFirmContracts.BusinessLogicsContracts;
|
|||
|
using LawFirmContracts.SearchModels;
|
|||
|
using LawFirmDataModels.Models;
|
|||
|
using Microsoft.Extensions.Logging;
|
|||
|
using LawFirmContracts.BindingModels;
|
|||
|
|
|||
|
namespace LawFirmView
|
|||
|
{
|
|||
|
public partial class FormShop : Form
|
|||
|
{
|
|||
|
private readonly ILogger _logger;
|
|||
|
private readonly IShopLogic _logic;
|
|||
|
private int? _id;
|
|||
|
private Dictionary<int, (IDocumentModel, int)> _shopDocuments;
|
|||
|
public int Id { set { _id = value; } }
|
|||
|
public FormShop(ILogger<FormShop> logger, IShopLogic logic)
|
|||
|
{
|
|||
|
InitializeComponent();
|
|||
|
_logger = logger;
|
|||
|
_logic = logic;
|
|||
|
_shopDocuments = new Dictionary<int, (IDocumentModel, int)>();
|
|||
|
}
|
|||
|
|
|||
|
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)
|
|||
|
{
|
|||
|
textBoxName.Text = view.ShopName;
|
|||
|
textBoxAddress.Text = view.Address.ToString();
|
|||
|
dateTimePickerDateOpen.Text = view.DateOpen.ToString();
|
|||
|
_shopDocuments = view.ShopDocuments ?? new Dictionary<int, (IDocumentModel, int)>();
|
|||
|
LoadData();
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
_logger.LogError(ex, "Ошибка загрузки магазина");
|
|||
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
private void LoadData()
|
|||
|
{
|
|||
|
_logger.LogInformation("Загрузка кораблей магазина");
|
|||
|
try
|
|||
|
{
|
|||
|
if (_shopDocuments != null)
|
|||
|
{
|
|||
|
dataGridView.Rows.Clear();
|
|||
|
foreach (var element in _shopDocuments)
|
|||
|
{
|
|||
|
dataGridView.Rows.Add(new object[] { element.Key, element.Value.Item1.DocumentName, element.Value.Item2 });
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
_logger.LogError(ex, "Ошибка загрузки кораблей магазина");
|
|||
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
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;
|
|||
|
}
|
|||
|
if (string.IsNullOrEmpty(dateTimePickerDateOpen.Text))
|
|||
|
{
|
|||
|
MessageBox.Show("Заполните дату", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
return;
|
|||
|
}
|
|||
|
_logger.LogInformation("Сохранение магазина");
|
|||
|
try
|
|||
|
{
|
|||
|
var model = new ShopBindingModel
|
|||
|
{
|
|||
|
Id = _id ?? 0,
|
|||
|
ShopName = textBoxName.Text,
|
|||
|
Address = textBoxAddress.Text,
|
|||
|
DateOpen = DateTime.Parse(dateTimePickerDateOpen.Text),
|
|||
|
ShopDocuments = _shopDocuments
|
|||
|
};
|
|||
|
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();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|