PIbd-23_Baryshev_D.A._Garage/ProjectGarage/Forms/FormFuels.cs

115 lines
3.5 KiB
C#

using ProjectGarage.Repositories;
using ProjectGarage.Repositories.Implementations;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Unity;
namespace ProjectGarage.Forms
{
public partial class FormFuels : Form
{
private readonly IUnityContainer _container;
private readonly IFuelRepository _fuelRepository;
public FormFuels(IUnityContainer container, IFuelRepository fuelRepository)
{
InitializeComponent();
_container = container ?? throw new ArgumentNullException(nameof(container));
_fuelRepository = fuelRepository ?? throw new ArgumentNullException(nameof(fuelRepository));
}
private void FormFeeds_Load(object sender, EventArgs e)
{
try
{
LoadList();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка при загрузке",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ButtonAddFuel_Click(object sender, EventArgs e)
{
try
{
_container.Resolve<FormFuel>().ShowDialog();
LoadList();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка при добавлении",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ButtonUpdateFuel_Click(object sender, EventArgs e)
{
if (!TryGetIdentifierFromSelectedRow(out var findId))
{
return;
}
try
{
var form = _container.Resolve<FormFuel>();
form.Id = findId;
form.ShowDialog();
LoadList();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка при изменении",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ButtonDeleteFuel_Click(object sender, EventArgs e)
{
if (!TryGetIdentifierFromSelectedRow(out var findId))
{
return;
}
if (MessageBox.Show("Удалить запись?", "Удаление",
MessageBoxButtons.YesNo) != DialogResult.Yes)
{
return;
}
try
{
_fuelRepository.DeleteFuel(findId);
LoadList();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка при удалении",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void LoadList() => dataGridViewFuels.DataSource = _fuelRepository.ReadFuels();
private bool TryGetIdentifierFromSelectedRow(out int id)
{
id = 0;
if (dataGridViewFuels.SelectedRows.Count < 1)
{
MessageBox.Show("Нет выбранной записи", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
id = Convert.ToInt32(dataGridViewFuels.SelectedRows[0].Cells["Id"].Value);
return true;
}
}
}