PIbd-21_MasenkinMS_SUBD_Rou.../RouteGuide/RouteGuideView/FormMain.cs

287 lines
7.3 KiB
C#

using Microsoft.Extensions.Logging;
using RouteGuideBusinessLogics.BusinessLogics;
using RouteGuideContracts.BindingModels;
using RouteGuideContracts.BusinessLogicsContracts;
using RouteGuideMongoDBImplement;
namespace RouteGuideView
{
/// <summary>
/// Ôîðìà äëÿ âûâîäà ñïèñêà ðàñïèñàíèé
/// </summary>
public partial class FormMain : Form
{
/// <summary>
/// Ëîããåð
/// </summary>
private readonly ILogger _logger;
/// <summary>
/// Áèçíåñ-ëîãèêà äëÿ ðàñïèñàíèé
/// </summary>
private readonly IScheduleLogic _schedulelogic;
/// <summary>
/// Êîíñòðóêòîð
/// </summary>
/// <param name="logger"></param>
/// <param name="scheduleLogic"></param>
public FormMain(ILogger<FormMain> logger, IScheduleLogic scheduleLogic)
{
InitializeComponent();
_logger = logger;
_schedulelogic = scheduleLogic;
}
/// <summary>
/// Çàãðóçêà äàííûõ
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FormMain_Load(object sender, EventArgs e)
{
LoadData();
}
/// <summary>
/// Ôîðìà äëÿ âûâîäà ñïèñêà âîäèòåëåé
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DrviversToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormDrivers));
if (service is FormDrivers form)
{
form.ShowDialog();
}
}
/// <summary>
/// Ôîðìà äëÿ âûâîäà ñïèñêà òðàíñïîðòà
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TransportToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormTransports));
if (service is FormTransports form)
{
form.ShowDialog();
}
}
/// <summary>
/// Ôîðìà äëÿ âûâîäà ñïèñêà ìàðøðóòîâ
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void RoutesToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormRoutes));
if (service is FormRoutes form)
{
form.ShowDialog();
}
}
/// <summary>
/// Ôîðìà äëÿ âûâîäà ñïèñêà îñòàíîâîê
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void StopsToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormStops));
if (service is FormStops form)
{
form.ShowDialog();
}
}
/// <summary>
/// Ôîðìà äëÿ òåñòîâ ñóùíîñòè "Âîäèòåëü"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DriversTestsToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormDriversTests));
if (service is FormDriversTests form)
{
form.ShowDialog();
}
}
/// <summary>
/// Ôîðìà äëÿ òåñòîâ ñóùíîñòè "Òðàíñïîðò"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TransportTestsToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormTransportTests));
if (service is FormTransportTests form)
{
form.ShowDialog();
}
}
/// <summary>
/// Ôîðìà äëÿ òåñòîâ ñóùíîñòè "Ìàðøðóò"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void RoutesTestsToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormRoutesTests));
if (service is FormRoutesTests form)
{
form.ShowDialog();
}
}
/// <summary>
/// Ôîðìà äëÿ òåñòîâ ñóùíîñòè "Îñòàíîâêà"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void StopsTestsToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormStopsTests));
if (service is FormStopsTests form)
{
form.ShowDialog();
}
}
/// <summary>
/// Ôîðìà äëÿ òåñòîâ ñóùíîñòè "Ðàñïèñàíèå"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SchedulesTestsToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormSchedulesTests));
if (service is FormSchedulesTests form)
{
form.ShowDialog();
}
}
/// <summary>
/// Êíîïêà "Ñîçäàòü"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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();
}
}
}
/// <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(FormSchedule));
if (service is FormSchedule 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 (!_schedulelogic.Delete(new ScheduleBindingModel { 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>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonSyncData_Click(object sender, EventArgs e)
{
DataTransfer dataTransfer = new DataTransfer();
dataTransfer.SyncData();
}
/// <summary>
/// Çàãðóçêà äàííûõ
/// </summary>
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);
}
}
}
}