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

74 lines
2.9 KiB
C#

using ProjectGarage.Entities;
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;
namespace ProjectGarage.Forms
{
public partial class FormReplenishment : Form
{
private readonly IReplenishmentRepository _replenishmentRepository;
public FormReplenishment(IReplenishmentRepository replenishmentRepository,
IDriverRepository driverRepository, IFuelRepository fuelRepository)
{
InitializeComponent();
_replenishmentRepository = replenishmentRepository ??
throw new ArgumentNullException(nameof(replenishmentRepository));
comboBoxReplenishmentDriver.DataSource = driverRepository.ReadDrivers();
comboBoxReplenishmentDriver.DisplayMember = "FullName";
comboBoxReplenishmentDriver.ValueMember = "Id";
ColumnFuel.DataSource = fuelRepository.ReadFuels();
ColumnFuel.DisplayMember = "FuelName";
ColumnFuel.ValueMember = "Id";
}
private void ButtonReplenishmentSave_Click(object sender, EventArgs e)
{
try
{
if (dataGridViewReplenishment.RowCount < 1 ||
comboBoxReplenishmentDriver.SelectedIndex < 0)
{
throw new Exception("Имеются незаполненные поля");
}
_replenishmentRepository.CreateFuelReplenishment(FuelReplenishment.CreateOpeartion(0,
(int)comboBoxReplenishmentDriver.SelectedValue!, CreateListFuelFuelReplenishmentsFromDataGrid()));
Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ButtonReplenishmentCancel_Click(object sender, EventArgs e) => Close();
private List<FuelFuelReplenishment> CreateListFuelFuelReplenishmentsFromDataGrid()
{
var list = new List<FuelFuelReplenishment>();
foreach (DataGridViewRow row in dataGridViewReplenishment.Rows)
{
if (row.Cells["ColumnFuel"].Value == null ||
row.Cells["ColumnAmount"].Value == null)
{
continue;
}
list.Add(FuelFuelReplenishment.CreateElement(0, Convert.ToInt32(row.Cells["ColumnFuel"].Value),
Convert.ToInt32(row.Cells["ColumnAmount"].Value)));
}
return list.GroupBy(x => x.FuelId, x => x.Amount,
(id, counts) => FuelFuelReplenishment.CreateElement(0, id, counts.Sum())).ToList();
}
}
}