PIbd-21_MasenkinMS_SUBD_Rou.../RouteGuide/RouteGuideView/FormDrivers.cs
2024-04-23 19:38:25 +04:00

152 lines
5.1 KiB
C#

using Microsoft.Extensions.Logging;
using RouteGuideContracts.BindingModels;
using RouteGuideContracts.BusinessLogicsContracts;
using System;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace RouteGuideView
{
/// <summary>
/// Форма для вывода списка водителей
/// </summary>
public partial class FormDrivers : Form
{
/// <summary>
/// Логгер
/// </summary>
private readonly ILogger _logger;
/// <summary>
/// Бизнес-логика
/// </summary>
private readonly IDriverLogic _driverlogic;
/// <summary>
/// Конструктор
/// </summary>
public FormDrivers(ILogger<FormDrivers> logger, IDriverLogic driverLogic)
{
InitializeComponent();
_logger = logger;
_driverlogic = driverLogic;
}
/// <summary>
/// Загрузка данных
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FormDrivers_Load(object sender, EventArgs e)
{
LoadData();
}
/// <summary>
/// Кнопка "Создать"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonCreate_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormDriver));
if (service is FormDriver form)
{
if (form.ShowDialog() == DialogResult.OK)
{
LoadData();
}
}
}
/// <summary>
/// Кнопка "Изменить"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonUpdate_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count == 1)
{
var service = Program.ServiceProvider?.GetService(typeof(FormDriver));
if (service is FormDriver form)
{
form.Id = dataGridView.SelectedRows[0].Cells["Id"].Value;
if (form.ShowDialog() == DialogResult.OK)
{
LoadData();
}
}
}
}
/// <summary>
/// Кнопка "Удалить"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonDelete_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count == 1)
{
if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
object id = dataGridView.SelectedRows[0].Cells["Id"].Value;
_logger.LogInformation("Удаление сущности 'Водитель'");
try
{
if (!_driverlogic.Delete(new DriverBindingModel { Id = id }))
{
throw new Exception("Ошибка при удалении сущности 'Водитель'. Дополнительная информация в логах.");
}
LoadData();
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка удаления сущности 'Водитель'");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
/// <summary>
/// Кнопка "Обновить"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonRefresh_Click(object sender, EventArgs e)
{
LoadData();
}
/// <summary>
/// Загрузка данных
/// </summary>
private void LoadData()
{
try
{
var list = _driverlogic.ReadList(null);
if (list != null)
{
dataGridView.DataSource = list;
dataGridView.Columns["Id"].Visible = false;
dataGridView.Columns["FullName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
}
_logger.LogInformation("Загрузка списка водителей");
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка загрузки списка водителей");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}