PIAPS_CW/WinFormsApp/FormMediaFiles.cs

118 lines
4.0 KiB
C#
Raw Normal View History

2024-06-24 16:23:42 +04:00
using BusinessLogic.BusinessLogic;
using Contracts.BindingModels;
using Contracts.BusinessLogicContracts;
using Contracts.SearchModels;
using Microsoft.Extensions.Logging;
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 WinFormsApp
{
public partial class FormMediaFiles : Form
{
2024-06-24 16:23:42 +04:00
private readonly ILogger _logger;
private readonly IMediaFileLogic _mediafileLogic;
private readonly IProductLogic _productLogic;
private string? _filename;
public FormMediaFiles(ILogger<FormMediaFiles> logger, IMediaFileLogic mediaFileLogic, IProductLogic productLogic)
{
InitializeComponent();
2024-06-24 16:23:42 +04:00
_logger = logger;
_mediafileLogic = mediaFileLogic;
_productLogic = productLogic;
_filename = null;
}
private void LoadData()
{
if (comboBoxProduct.SelectedIndex < 0) return;
var list = _mediafileLogic.ReadList(new MediaFileSearchModel()
{
ProductId = (Guid?)comboBoxProduct.SelectedValue ?? Guid.Empty,
});
panelFiles.Controls.Clear();
foreach (var item in list)
{
var imageBox = new PictureBox();
imageBox.Dock = DockStyle.Top;
imageBox.Image = new Bitmap(item.Location);
imageBox.Width = groupBox1.Width;
panelFiles.Controls.Add(imageBox);
imageBox.SizeMode = PictureBoxSizeMode.Zoom;
imageBox.Height = 400;
panelFiles.Refresh();
}
}
private void FormMediaFiles_Load(object sender, EventArgs e)
{
try
{
var list = _productLogic.ReadList(null);
if (list != null)
{
comboBoxProduct.DisplayMember = "Name";
comboBoxProduct.ValueMember = "Id";
comboBoxProduct.DataSource = list;
comboBoxProduct.SelectedItem = null;
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка загрузки списка продуктов");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void comboBoxProduct_SelectedIndexChanged(object sender, EventArgs e)
{
LoadData();
}
private void buttonAdd_Click(object sender, EventArgs e)
{
if (comboBoxProduct.SelectedIndex != -1 && !string.IsNullOrEmpty(_filename))
{
_mediafileLogic.Create(new MediaFileBindingModel()
{
Id = Guid.NewGuid(),
Name = System.IO.Path.GetFileNameWithoutExtension(_filename),
Location = _filename,
ProductId = (Guid)comboBoxProduct.SelectedValue,
});
}
}
private void buttonSelectFile_Click(object sender, EventArgs e)
{
var dialog = new OpenFileDialog();
dialog.Filter = "Image Files(*.jpg; *.png; *.jpeg; *.gif; *.bmp)|*.jpg;*.png;*.jpeg;*.gif;*.bmp";
if (dialog.ShowDialog() == DialogResult.OK)
{
_filename = dialog.FileName;
labelFile.Text = System.IO.Path.GetDirectoryName(_filename);
}
}
private void FormMediaFiles_Resize(object sender, EventArgs e)
{
panelFiles.Width = Convert.ToInt32(this.Width * 0.5);
panelFiles.Height = this.Height;
foreach (var control in this.Controls.OfType<PictureBox>())
{
control.Width = panelFiles.Width;
control.Height = Convert.ToInt32(panelFiles.Height * 0.3);
}
}
}
}