93 lines
3.9 KiB
C#
93 lines
3.9 KiB
C#
using ProjectAtelier.Entities;
|
|
using ProjectAtelier.Repositories;
|
|
|
|
namespace ProjectAtelier.Forms
|
|
{
|
|
public partial class FormProductMaterial : Form
|
|
{
|
|
private readonly IProductRepository _productRepository;
|
|
private readonly IProductMaterialRepository _feedReplenishmentRepository;
|
|
private int? _productId;
|
|
public int Id
|
|
{
|
|
set
|
|
{
|
|
try
|
|
{
|
|
var product = _productRepository.ReadProductById(value);
|
|
if (product == null)
|
|
{
|
|
throw new InvalidDataException(nameof(product));
|
|
}
|
|
textBoxName.Text = product.Name;
|
|
comboBoxProduct.SelectedItem = product.View;
|
|
numericUpDownCount.Value = product.CountMaterial;
|
|
_productId = value;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при полученииданных", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
public FormProductMaterial(IProductMaterialRepository materialConsumptionRepository, IProductRepository productRepository, IMaterialRepository materialRepository)
|
|
{
|
|
InitializeComponent();
|
|
_feedReplenishmentRepository = materialConsumptionRepository ?? throw new ArgumentNullException(nameof(materialConsumptionRepository));
|
|
_productRepository = productRepository ?? throw new ArgumentNullException(nameof(productRepository));
|
|
|
|
comboBoxProduct.DataSource = Enum.GetValues(typeof(ProductView));
|
|
|
|
ColumnMaterials.DataSource = materialRepository.ReadMaterials();
|
|
ColumnMaterials.DisplayMember = "Name";
|
|
ColumnMaterials.ValueMember = "Id";
|
|
}
|
|
private void buttonAdd_Click(object sender, EventArgs e)
|
|
{
|
|
if (dataGridView.RowCount < 1)
|
|
{
|
|
throw new Exception("Имеются незаполненны поля");
|
|
}
|
|
try
|
|
{
|
|
if (string.IsNullOrWhiteSpace(textBoxName.Text) || comboBoxProduct.SelectedIndex < 1)
|
|
{
|
|
throw new Exception("Имеются незаполненные поля");
|
|
}
|
|
if (_productId.HasValue)
|
|
{
|
|
_productRepository.UpdateProduct(CreateProduct(_productId.Value));
|
|
}
|
|
else
|
|
{
|
|
_productRepository.CreateProduct(CreateProduct(0));
|
|
}
|
|
Close();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
private void buttonCancel_Click(object sender, EventArgs e) => Close();
|
|
private List<ProductMaterial> CreateListMaterialFromDataGrid()
|
|
{
|
|
var list = new List<ProductMaterial>();
|
|
foreach (DataGridViewRow row in dataGridView.Rows)
|
|
{
|
|
if (row.Cells["ColumProduct"].Value == null || row.Cells["ColumMaterials"].Value == null || row.Cells["ColumnCount"].Value == null)
|
|
{
|
|
continue;
|
|
}
|
|
list.Add(ProductMaterial.CreateOperation(0, Convert.ToInt32(row.Cells["ColumnMaterial"].Value),
|
|
Convert.ToInt32(row.Cells["ColumnProduct"].Value), Convert.ToInt32(row.Cells["ColumnCount"].Value)));
|
|
}
|
|
return list;
|
|
}
|
|
private Product CreateProduct(int id) => Product.CreateEntity(id, textBoxName.Text, (ProductView)comboBoxProduct.SelectedItem!, Convert.ToInt32(numericUpDownCount.Value), CreateListMaterialFromDataGrid());
|
|
|
|
|
|
}
|
|
}
|