PIbd-23_Sergunov_M.A._GiftShop/GiftShop/GiftShopView/FormImplementers.cs

100 lines
2.5 KiB
C#
Raw Normal View History

2023-05-14 14:10:25 +04:00
using GiftShopContracts.BindingModels;
using GiftShopContracts.BusinessLogicsContracts;
2023-06-03 01:27:48 +04:00
using GiftShopContracts.DI;
2023-05-14 14:10:25 +04:00
using Microsoft.Extensions.Logging;
namespace GiftShopView
{
public partial class FormImplementers : Form
{
private readonly ILogger _logger;
private readonly IImplementerLogic _logic;
public FormImplementers(ILogger<FormImplementers> logger, IImplementerLogic logic)
{
InitializeComponent();
_logger = logger;
_logic = logic;
}
private void AddButton_Click(object sender, EventArgs e)
{
2023-06-03 01:27:48 +04:00
var form = DependencyManager.Instance.Resolve<FormImplementer>();
2023-05-14 14:10:25 +04:00
2023-06-03 01:27:48 +04:00
if (form.ShowDialog() == DialogResult.OK)
2023-05-14 14:10:25 +04:00
{
2023-06-03 01:27:48 +04:00
LoadData();
2023-05-14 14:10:25 +04:00
}
}
private void ChangeButton_Click(object sender, EventArgs e)
{
if (DataGridView.SelectedRows.Count == 1)
{
2023-06-03 01:27:48 +04:00
var form = DependencyManager.Instance.Resolve<FormImplementer>();
form.Id = Convert.ToInt32(DataGridView.SelectedRows[0].Cells["Id"].Value);
if (form.ShowDialog() == DialogResult.OK)
2023-05-14 14:10:25 +04:00
{
2023-06-03 01:27:48 +04:00
LoadData();
2023-05-14 14:10:25 +04:00
}
2023-06-03 01:27:48 +04:00
2023-05-14 14:10:25 +04:00
}
}
private void DeleteButton_Click(object sender, EventArgs e)
{
if (DataGridView.SelectedRows.Count == 1)
{
if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
int id = Convert.ToInt32(DataGridView.SelectedRows[0].Cells["Id"].Value);
_logger.LogInformation("Удаление исполнителя");
try
{
if (!_logic.Delete(new ImplementerBindingModel
{
Id = id
}))
{
throw new Exception("Ошибка при удалении. Дополнительная информация в логах.");
}
LoadData();
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка удаления исполнителя");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
private void UpdateButton_Click(object sender, EventArgs e)
{
LoadData();
}
private void FormImplementers_Load(object sender, EventArgs e)
{
LoadData();
}
private void LoadData()
{
try
{
2023-06-03 01:27:48 +04:00
DataGridView.FillandConfigGrid(_logic.ReadList(null));
2023-05-14 14:10:25 +04:00
_logger.LogInformation("Загрузка исполнителей");
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка загрузки исполнителей");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}