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 = _product?.Id ?? 0, Name = textBoxName.Text, Description = textBoxDescription.Text, Category = _categoryComboBox.SelectedValue, CountOnStorage = (int)_countOnStorageControl.DoubleValue }; 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(); } } }