ISEbd-21_Koscheev.M.S.Softw.../SoftwareInstallation/SoftwareInstallationView/FormAddPackageInShop.cs
2023-04-03 18:03:56 +04:00

102 lines
3.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using SoftwareInstallationContracts.BindingModels;
using SoftwareInstallationContracts.BusinessLogicsContracts;
using SoftwareInstallationContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections;
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 SoftwareInstallationView
{
public partial class FormAddPackageInShop : Form
{
private readonly ILogger _logger;
private readonly IShopLogic _shopLogic;
private readonly IPackageLogic _packageLogic;
private readonly List<ShopViewModel>? _listShops;
private readonly List<PackageViewModel>? _listPackages;
public FormAddPackageInShop(ILogger<FormAddPackageInShop> logger, IShopLogic shopLogic, IPackageLogic packageLogic)
{
InitializeComponent();
_shopLogic = shopLogic;
_packageLogic = packageLogic;
_logger = logger;
_listShops = shopLogic.ReadList(null);
if (_listShops != null)
{
textBoxShop.DisplayMember = "Name";
textBoxShop.ValueMember = "Id";
textBoxShop.DataSource = _listShops;
textBoxShop.SelectedItem = null;
}
_listPackages = packageLogic.ReadList(null);
if (_listPackages != null)
{
textBoxPackage.DisplayMember = "PackageName";
textBoxPackage.ValueMember= "Id";
textBoxPackage.DataSource = _listPackages;
textBoxPackage.SelectedItem = null;
}
}
private void ButtonSave_Click(object sender, EventArgs e)
{
if (textBoxShop.SelectedValue == null)
{
MessageBox.Show("Выберите магазин", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (textBoxPackage.SelectedValue == null)
{
MessageBox.Show("Выберите изделие", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_logger.LogInformation("Добавление изделия в магазин");
try
{
var package = _packageLogic.ReadElement(new()
{
Id = (int)textBoxPackage.SelectedValue
});
if (package == null)
{
throw new Exception("Не найдено изделие. Дополнительная информация в логах.");
}
var resultOperation = _shopLogic.AddPackage(
model: new() { Id = (int)textBoxShop.SelectedValue },
package: package,
count: (int)numericUpDownCount.Value
);
if (!resultOperation)
{
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();
}
}
}