using Microsoft.Extensions.Logging; using RouteGuideBusinessLogics.BusinessLogics; using RouteGuideContracts.BindingModels; using RouteGuideContracts.BusinessLogicsContracts; using RouteGuideMongoDBImplement; 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 DriversTestsToolStripMenuItem_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormDriversTests)); if (service is FormDriversTests form) { form.ShowDialog(); } } /// /// Форма для тестов сущности "Транспорт" /// /// /// private void TransportTestsToolStripMenuItem_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormTransportTests)); if (service is FormTransportTests form) { form.ShowDialog(); } } /// /// Форма для тестов сущности "Маршрут" /// /// /// private void RoutesTestsToolStripMenuItem_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormRoutesTests)); if (service is FormRoutesTests form) { form.ShowDialog(); } } /// /// Форма для тестов сущности "Остановка" /// /// /// private void StopsTestsToolStripMenuItem_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormStopsTests)); if (service is FormStopsTests form) { form.ShowDialog(); } } /// /// Форма для тестов сущности "Расписание" /// /// /// private void SchedulesTestsToolStripMenuItem_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormSchedulesTests)); if (service is FormSchedulesTests 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 = 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) { object id = 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 buttonSyncData_Click(object sender, EventArgs e) { DataTransfer dataTransfer = new DataTransfer(); dataTransfer.SyncData(); } /// /// Загрузка данных /// 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); } } } }