ISEbd-21_Agliullov.D.A._Con.../Confectionery/FormAddPastryInShop.cs

102 lines
3.8 KiB
C#
Raw Permalink Normal View History

using ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.BusinessLogicsContracts;
using ConfectioneryContracts.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 ConfectioneryView
{
public partial class FormAddPastryInShop : Form
{
private readonly ILogger _logger;
private readonly IShopLogic _shopLogic;
private readonly IPastryLogic _pastryLogic;
private readonly List<ShopViewModel>? _listShops;
private readonly List<PastryViewModel>? _listPastries;
public FormAddPastryInShop(ILogger<FormAddPastryInShop> logger, IShopLogic shopLogic, IPastryLogic pastryLogic)
{
InitializeComponent();
_shopLogic = shopLogic;
_pastryLogic = pastryLogic;
_logger = logger;
_listShops = shopLogic.ReadList(null);
if (_listShops != null)
{
comboBoxShop.DisplayMember = "Name";
comboBoxShop.ValueMember = "Id";
comboBoxShop.DataSource = _listShops;
comboBoxShop.SelectedItem = null;
}
_listPastries = pastryLogic.ReadList(null);
if (_listPastries != null)
{
comboBoxPastry.DisplayMember = "PastryName";
comboBoxPastry.ValueMember= "Id";
comboBoxPastry.DataSource = _listPastries;
comboBoxPastry.SelectedItem = null;
}
}
private void ButtonSave_Click(object sender, EventArgs e)
{
if (comboBoxShop.SelectedValue == null)
{
MessageBox.Show("Выберите магазин", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (comboBoxPastry.SelectedValue == null)
{
MessageBox.Show("Выберите изделие", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_logger.LogInformation("Добавление изделия в магазин");
try
{
var pastry = _pastryLogic.ReadElement(new()
{
Id = (int)comboBoxPastry.SelectedValue
});
if (pastry == null)
{
throw new Exception("Не найдено изделие. Дополнительная информация в логах.");
}
var resultOperation = _shopLogic.AddPastry(
model: new() { Id = (int)comboBoxShop.SelectedValue },
pastry: pastry,
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();
}
}
}