PIbd-21_MasenkinMS_SUBD_Rou.../RouteGuide/RouteGuideView/FormMain.cs
2024-04-05 02:48:52 +04:00

205 lines
6.7 KiB
C#

using Microsoft.Extensions.Logging;
using RouteGuideBusinessLogics.BusinessLogics;
using RouteGuideContracts.BindingModels;
using RouteGuideContracts.BusinessLogicsContracts;
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 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 = Convert.ToInt32(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)
{
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);
}
}
}
}
/// <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 = _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);
}
}
}
}