78 lines
2.2 KiB
C#
78 lines
2.2 KiB
C#
using GasStation.Entities.Enums;
|
|
using GasStation.Repositories;
|
|
using GasStation.Entities;
|
|
|
|
namespace GasStation.Forms;
|
|
|
|
public partial class FormProduct : Form
|
|
{
|
|
|
|
private readonly IProductRepository _productRepository;
|
|
|
|
private int? _productId;
|
|
|
|
public FormProduct(IProductRepository productRepository)
|
|
{
|
|
InitializeComponent();
|
|
_productRepository = productRepository ??
|
|
throw new ArgumentNullException(nameof(productRepository));
|
|
|
|
comboBoxProduct.DataSource = Enum.GetValues(typeof(ProductType));
|
|
}
|
|
|
|
public int ID
|
|
{
|
|
set
|
|
{
|
|
try
|
|
{
|
|
var product = _productRepository.ReadProductByID(value);
|
|
if (product == null)
|
|
{
|
|
throw new InvalidDataException(nameof(product));
|
|
}
|
|
|
|
comboBoxProduct.SelectedItem = product.ProductType;
|
|
_productId = value;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void ButtonSave_Click(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
if (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);
|
|
return;
|
|
}
|
|
}
|
|
|
|
private void ButtonCancel_Click(object sender, EventArgs e) => Close();
|
|
|
|
private Product CreateProduct(int id) =>
|
|
Product.CreateProduct(id, Convert.ToInt32(numericUpDownCost.Value),
|
|
(ProductType)comboBoxProduct.SelectedItem!);
|
|
|
|
}
|