179 lines
6.6 KiB
C#
179 lines
6.6 KiB
C#
using Data;
|
||
using Data.Repositories;
|
||
using View;
|
||
using System.Windows.Forms;
|
||
using Data.Models;
|
||
using Library14Petrushin;
|
||
|
||
namespace Laba3
|
||
{
|
||
public partial class MainForm : Form
|
||
{
|
||
private readonly IProductRepository _productRepository;
|
||
private readonly IManufacturerRepository _manufacturerRepository;
|
||
private SaveFileDialog saveFileDialog;
|
||
|
||
public MainForm(IProductRepository productRepository, IManufacturerRepository manufacturerRepository)
|
||
{
|
||
InitializeComponent();
|
||
_productRepository = productRepository;
|
||
_manufacturerRepository = manufacturerRepository;
|
||
InitializeOutputTableResults();
|
||
LoadProducts();
|
||
}
|
||
|
||
private void InitializeOutputTableResults()
|
||
{
|
||
outputTableResults.ConfigureColumns(new List<Library15Gerimovich.ColumnInfo>
|
||
{
|
||
new Library15Gerimovich.ColumnInfo("", 0, false, "Id"),
|
||
new Library15Gerimovich.ColumnInfo("Name", 150, true, "Name"),
|
||
new Library15Gerimovich.ColumnInfo("ManufacturerNameManufacturerName", 150, true, "ManufacturerName"),
|
||
new Library15Gerimovich.ColumnInfo("DeliveryDate", 50, true, "DeliveryDate"),
|
||
});
|
||
|
||
}
|
||
|
||
private void LoadProducts()
|
||
{
|
||
var products = _productRepository.GetAllProducts();
|
||
outputTableResults.ClearGrid();
|
||
foreach (var product in products)
|
||
{
|
||
outputTableResults.InsertValue(product);
|
||
}
|
||
}
|
||
|
||
private void addToolStripMenuItem_Click(object sender, EventArgs e)
|
||
{
|
||
var productForm = new ProductForm(_productRepository, _manufacturerRepository);
|
||
if (productForm.ShowDialog() == DialogResult.OK)
|
||
{
|
||
LoadProducts();
|
||
}
|
||
}
|
||
|
||
private void editToolStripMenuItem_Click(object sender, EventArgs e)
|
||
{
|
||
try
|
||
{
|
||
var selectedProductId = outputTableResults.GetSelectedObject<Product>().Id;
|
||
var selectedProduct = _productRepository.GetProductById(selectedProductId);
|
||
var productForm = new ProductForm(_productRepository, _manufacturerRepository, selectedProduct);
|
||
if (productForm.ShowDialog() == DialogResult.OK)
|
||
{
|
||
LoadProducts();
|
||
}
|
||
}
|
||
catch (Exception ex) {
|
||
MessageBox.Show(ex.ToString());
|
||
}
|
||
}
|
||
|
||
private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
|
||
{
|
||
var selectedProductId = outputTableResults.GetSelectedObject<Product>().Id;
|
||
if (MessageBox.Show("Are you sure you want to delete this product?", "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes)
|
||
{
|
||
_productRepository.DeleteProduct(selectedProductId);
|
||
LoadProducts();
|
||
}
|
||
}
|
||
|
||
private void manufacturersToolStripMenuItem_Click(object sender, EventArgs e)
|
||
{
|
||
var manufacturerForm = new ManufacturerForm(_manufacturerRepository);
|
||
manufacturerForm.ShowDialog();
|
||
}
|
||
|
||
private void createSimpleDocumentToolStripMenuItem_Click(object sender, EventArgs e)
|
||
{
|
||
// Получаем изображения из базы данных
|
||
var images = GetImagesFromDatabase();
|
||
|
||
if (images.Count > 0)
|
||
{
|
||
// Показываем диалоговое окно для выбора пути сохранения
|
||
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
||
{
|
||
// Создаем компонент PdfImg
|
||
var pdfImg = new Library14Petrushin.PdfImg();
|
||
|
||
// Путь для сохранения PDF-документа
|
||
string fileName = saveFileDialog.FileName;
|
||
|
||
// Заголовок документа
|
||
string documentTitle = "Product Images";
|
||
|
||
// Создаем PDF-документ
|
||
pdfImg.CreatePdfDocument(fileName, documentTitle, images);
|
||
|
||
}
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("No images found in the database.");
|
||
}
|
||
}
|
||
|
||
private void createCustomTableDocumentToolStripMenuItem_Click(object sender, EventArgs e)
|
||
{
|
||
// Реализация создания документа с настраиваемой таблицей
|
||
}
|
||
|
||
private void createChartDocumentToolStripMenuItem_Click(object sender, EventArgs e)
|
||
{
|
||
// Реализация создания документа с диаграммой
|
||
}
|
||
|
||
private void MainForm_KeyDown(object sender, KeyEventArgs e)
|
||
{
|
||
if (e.Control)
|
||
{
|
||
switch (e.KeyCode)
|
||
{
|
||
case Keys.A:
|
||
addToolStripMenuItem_Click(null, EventArgs.Empty);
|
||
break;
|
||
case Keys.U:
|
||
editToolStripMenuItem_Click(null, EventArgs.Empty);
|
||
break;
|
||
case Keys.D:
|
||
deleteToolStripMenuItem_Click(null, EventArgs.Empty);
|
||
break;
|
||
case Keys.S:
|
||
createSimpleDocumentToolStripMenuItem_Click(null, EventArgs.Empty);
|
||
break;
|
||
case Keys.T:
|
||
createCustomTableDocumentToolStripMenuItem_Click(null, EventArgs.Empty);
|
||
break;
|
||
case Keys.C:
|
||
createChartDocumentToolStripMenuItem_Click(null, EventArgs.Empty);
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
private List<ImageData> GetImagesFromDatabase()
|
||
{
|
||
var images = new List<ImageData>();
|
||
var products = _productRepository.GetAllProducts();
|
||
|
||
foreach (var product in products)
|
||
{
|
||
if (product.Image != null)
|
||
{
|
||
// Создаем временный файл для сохранения изображения
|
||
string tempFilePath = Path.GetTempFileName();
|
||
File.WriteAllBytes(tempFilePath, product.Image);
|
||
|
||
// Добавляем путь к временному файлу в список
|
||
images.Add(new ImageData { ImagePath = tempFilePath });
|
||
}
|
||
}
|
||
|
||
return images;
|
||
}
|
||
}
|
||
}
|