PIbd-32.-Stroev-V.M.-COP/WinFormsLibrary1/WinFormsApp1/ProductForm.cs
2024-11-14 20:27:58 +04:00

81 lines
2.4 KiB
C#

using System;
using System.Windows.Forms;
using Contracts.BindingModels;
using Contracts.BusinessLogicContracts;
using Contracts.ViewModels;
using Library15Gerimovich;
using WinFormsLibrary1;
namespace Forms
{
public partial class ProductForm : Form
{
private readonly IProductLogic _productLogic;
private readonly ICategoryLogic _categoryLogic;
private ProductViewModel _product;
private bool _isNewProduct;
public ProductForm(IProductLogic productLogic, ICategoryLogic categoryLogic, ProductViewModel product = null)
{
InitializeComponent();
_categoryLogic = categoryLogic;
_productLogic = productLogic;
_product = product;
_isNewProduct = product == null;
LoadCategories();
LoadProduct();
}
private void LoadCategories()
{
var categories = _categoryLogic.ReadList();
_categoryComboBox.ClearItems();
foreach (var category in categories)
{
_categoryComboBox.AddItem(category.Name);
}
}
private void LoadProduct()
{
if (_product != null)
{
textBoxName.Text = _product.Name;
textBoxDescription.Text = _product.Description;
_categoryComboBox.SelectedValue = _product.Category;
_countOnStorageControl.DoubleValue = _product.CountOnStorage;
}
}
private void buttonSave_Click(object sender, EventArgs e)
{
var model = new ProductBindingModel
{
Id = string.IsNullOrEmpty(_product?.Id) ? 0 : int.Parse(_product?.Id),
Name = textBoxName.Text,
Description = textBoxDescription.Text,
Category = _categoryComboBox.SelectedValue,
CountOnStorage = _countOnStorageControl.DoubleValue != null ? (int?)_countOnStorageControl.DoubleValue : null
};
if (_isNewProduct)
{
_productLogic.Create(model);
}
else
{
_productLogic.Update(model);
}
this.DialogResult = DialogResult.OK;
this.Close();
}
private void buttonCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
}
}