105 lines
3.3 KiB
C#
105 lines
3.3 KiB
C#
using Data;
|
|
using Data.Models;
|
|
using Data.Repositories;
|
|
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 Laba3
|
|
{
|
|
public partial class ProductForm : Form
|
|
{
|
|
private readonly IProductRepository _productRepository;
|
|
private readonly IManufacturerRepository _manufacturerRepository;
|
|
private Product _product;
|
|
private bool _isNewProduct;
|
|
private const int MINL = 3;
|
|
private const int MAXL= 10;
|
|
|
|
public ProductForm(IProductRepository productRepository, IManufacturerRepository manufacturerRepository, Product product = null)
|
|
{
|
|
InitializeComponent();
|
|
_productRepository = productRepository;
|
|
_manufacturerRepository = manufacturerRepository;
|
|
_product = product;
|
|
_isNewProduct = product == null;
|
|
txtName.MinLength = MINL;
|
|
txtName.MaxLength = MAXL;
|
|
|
|
if (!_isNewProduct)
|
|
{
|
|
txtName.Text = _product.Name;
|
|
cmbManufacturer.SelectedValue= _product.ManufacturerName;
|
|
dtpDeliveryDate.Date = _product.DeliveryDate;
|
|
if (_product.Image != null)
|
|
{
|
|
using (var ms = new MemoryStream(_product.Image))
|
|
{
|
|
pbImage.Image = Image.FromStream(ms);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void btnBrowse_Click(object sender, EventArgs e)
|
|
{
|
|
using (var openFileDialog = new OpenFileDialog())
|
|
{
|
|
openFileDialog.Filter = "Image Files|*.jpg;*.jpeg;*.png;*.bmp";
|
|
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
|
{
|
|
pbImage.Image = Image.FromFile(openFileDialog.FileName);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void btnSave_Click(object sender, EventArgs e)
|
|
{
|
|
if (string.IsNullOrEmpty(txtName.Text))
|
|
{
|
|
MessageBox.Show("Name is required.");
|
|
return;
|
|
}
|
|
|
|
if (_isNewProduct)
|
|
{
|
|
_product = new Product();
|
|
_productRepository.AddProduct(_product);
|
|
}
|
|
|
|
_product.Name = txtName.Text;
|
|
_product.ManufacturerName = cmbManufacturer.SelectedValue.ToString();
|
|
_product.DeliveryDate = dtpDeliveryDate.Date;
|
|
|
|
if (pbImage.Image != null)
|
|
{
|
|
using (var ms = new MemoryStream())
|
|
{
|
|
pbImage.Image.Save(ms, pbImage.Image.RawFormat);
|
|
_product.Image = ms.ToArray();
|
|
}
|
|
}
|
|
|
|
_productRepository.UpdateProduct(_product);
|
|
DialogResult = DialogResult.OK;
|
|
Close();
|
|
}
|
|
|
|
private void ProductForm_Load(object sender, EventArgs e)
|
|
{
|
|
var manufacturers = _manufacturerRepository.GetAllManufacturers().Select(m => m.Name).ToList();
|
|
// cmbManufacturer.Data = manufacturers;
|
|
foreach (var manufacturer in manufacturers)
|
|
{
|
|
cmbManufacturer.AddItem(manufacturer);
|
|
}
|
|
}
|
|
}
|
|
}
|