using Microsoft.Extensions.Logging; using RouteGuideBusinessLogics.BusinessLogics; using RouteGuideContracts.BindingModels; using RouteGuideContracts.BusinessLogicsContracts; namespace RouteGuideView { /// /// Форма для вывода списка расписаний /// public partial class FormMain : Form { /// /// Логгер /// private readonly ILogger _logger; /// /// Бизнес-логика для расписаний /// private readonly IScheduleLogic _schedulelogic; /// /// Конструктор /// /// /// public FormMain(ILogger logger, IScheduleLogic scheduleLogic) { InitializeComponent(); _logger = logger; _schedulelogic = scheduleLogic; } /// /// Загрузка данных /// /// /// private void FormMain_Load(object sender, EventArgs e) { LoadData(); } /// /// Форма для вывода списка водителей /// /// /// private void DrviversToolStripMenuItem_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormDrivers)); if (service is FormDrivers form) { form.ShowDialog(); } } /// /// Форма для вывода списка транспорта /// /// /// private void TransportToolStripMenuItem_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormTransports)); if (service is FormTransports form) { form.ShowDialog(); } } /// /// Форма для вывода списка маршрутов /// /// /// private void RoutesToolStripMenuItem_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormRoutes)); if (service is FormRoutes form) { form.ShowDialog(); } } /// /// Форма для вывода списка остановок /// /// /// private void StopsToolStripMenuItem_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormStops)); if (service is FormStops form) { form.ShowDialog(); } } /// /// Кнопка "Создать" /// /// /// private void buttonCreate_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormSchedule)); if (service is FormSchedule form) { if (form.ShowDialog() == DialogResult.OK) { LoadData(); } } } /// /// Кнопка "Изменить" /// /// /// private void buttonUpdate_Click(object sender, EventArgs e) { if (dataGridView.SelectedRows.Count == 1) { var service = Program.ServiceProvider?.GetService(typeof(FormSchedule)); if (service is FormSchedule form) { form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); if (form.ShowDialog() == DialogResult.OK) { LoadData(); } } } } /// /// Кнопка "Удалить" /// /// /// private void buttonDelete_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 (!_schedulelogic.Delete(new ScheduleBindingModel { Id = id })) { throw new Exception("Ошибка при удалении сущности 'Расписание'. Дополнительная информация в логах."); } LoadData(); } catch (Exception ex) { _logger.LogError(ex, "Ошибка удаления сущности 'Расписание'"); MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } } /// /// Кнопка "Обновить" /// /// /// private void buttonRefresh_Click(object sender, EventArgs e) { LoadData(); } /// /// Загрузка данных /// private void LoadData() { try { var list = _schedulelogic.ReadList(null); if (list != null) { dataGridView.DataSource = list; dataGridView.Columns["Id"].Visible = false; dataGridView.Columns["RouteId"].Visible = false; dataGridView.Columns["RouteName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; } _logger.LogInformation("Загрузка списка расписаний"); } catch (Exception ex) { _logger.LogError(ex, "Ошибка загрузки списка расписаний"); MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } }