PIbd-22_Chernyshev_G.J._30_.../GarmentFactory/FormAddTextile.cs
2024-05-09 17:22:02 +04:00

110 lines
4.2 KiB
C#

using GarmentFactoryContracts.BindingModels;
using GarmentFactoryContracts.BusinessLogicsContracts;
using GarmentFactoryContracts.SearchModels;
using GarmentFactoryContracts.ViewModels;
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 GarmentFactoryView
{
public partial class FormAddTextile : Form
{
private readonly ILogger _logger;
private readonly ITextileLogic _logicT;
private readonly IShopLogic _logicS;
//Списки для ComboBox
private List<ShopViewModel> _shopList = new List<ShopViewModel>();
private List<TextileViewModel> _textileList = new List<TextileViewModel>();
public FormAddTextile(ILogger<FormAddTextile> logger, ITextileLogic logicT, IShopLogic logicS)
{
InitializeComponent();
_logger = logger;
_logicT = logicT;
_logicS = logicS;
}
private void FormAddTextile_Load(object sender, EventArgs e)
{
_shopList = _logicS.ReadList(null);
_textileList = _logicT.ReadList(null);
_logger.LogInformation("Загрузка списка магазинов при пополнении");
if (_shopList != null)
{
comboBoxShop.DisplayMember = "ShopName";
comboBoxShop.ValueMember = "Id";
comboBoxShop.DataSource = _shopList;
comboBoxShop.SelectedItem = null;
}
_logger.LogInformation("Загрузка списка товаров при пополнении");
if (_textileList != null)
{
comboBoxTextile.DisplayMember = "TextileName";
comboBoxTextile.ValueMember = "Id";
comboBoxTextile.DataSource = _textileList;
comboBoxTextile.SelectedItem = null;
}
}
private void ButtonSave_Click(object sender, EventArgs e)
{
if (comboBoxShop.SelectedValue == null)
{
MessageBox.Show("Выберите магазин", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (comboBoxTextile.SelectedValue == null)
{
MessageBox.Show("Выберите изделие", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (string.IsNullOrEmpty(textBoxCount.Text))
{
MessageBox.Show("Заполните поле \"Количество\"", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_logger.LogInformation("Пополнение магазина");
try
{
var operationResult = _logicS.MakeSupply(new SupplyBindingModel
{
ShopId = Convert.ToInt32(comboBoxShop.SelectedValue),
TextileId = Convert.ToInt32(comboBoxTextile.SelectedValue),
Count = Convert.ToInt32(textBoxCount.Text)
});
if (!operationResult)
{
throw new Exception("Ошибка при пополнении магазина. Дополнительная информация в логах.");
}
MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
DialogResult = DialogResult.OK;
Close();
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка поплнения магазина");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ButtonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
}
}