2023-02-05 19:51:51 +04:00
|
|
|
|
using ConfectioneryContracts.BindingModels;
|
|
|
|
|
using ConfectioneryContracts.BusinessLogicsContracts;
|
2023-02-05 19:37:08 +04:00
|
|
|
|
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;
|
2023-02-05 19:51:51 +04:00
|
|
|
|
private readonly IShopLogic _shopLogic;
|
|
|
|
|
private readonly IPastryLogic _pastryLogic;
|
2023-02-05 19:37:08 +04:00
|
|
|
|
private readonly List<ShopViewModel>? _listShops;
|
|
|
|
|
private readonly List<PastryViewModel>? _listPastries;
|
|
|
|
|
|
|
|
|
|
public FormAddPastryInShop(ILogger<FormAddPastryInShop> logger, IShopLogic shopLogic, IPastryLogic pastryLogic)
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
2023-02-05 19:51:51 +04:00
|
|
|
|
_shopLogic = shopLogic;
|
|
|
|
|
_pastryLogic = pastryLogic;
|
2023-02-05 19:37:08 +04:00
|
|
|
|
_logger = logger;
|
|
|
|
|
_listShops = shopLogic.ReadList(null);
|
|
|
|
|
if (_listShops != null)
|
|
|
|
|
{
|
2023-02-05 19:51:51 +04:00
|
|
|
|
comboBoxShop.DisplayMember = "Name";
|
2023-02-05 19:37:08 +04:00
|
|
|
|
comboBoxShop.ValueMember = "Id";
|
|
|
|
|
comboBoxShop.DataSource = _listShops;
|
|
|
|
|
comboBoxShop.SelectedItem = null;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-05 19:51:51 +04:00
|
|
|
|
_listPastries = pastryLogic.ReadList(null);
|
|
|
|
|
if (_listPastries != null)
|
|
|
|
|
{
|
|
|
|
|
comboBoxPastry.DisplayMember = "PastryName";
|
|
|
|
|
comboBoxPastry.ValueMember= "Id";
|
|
|
|
|
comboBoxPastry.DataSource = _listPastries;
|
|
|
|
|
comboBoxPastry.SelectedItem = null;
|
|
|
|
|
}
|
2023-02-05 19:37:08 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ButtonSave_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
2023-02-05 19:51:51 +04:00
|
|
|
|
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);
|
|
|
|
|
}
|
2023-02-05 19:37:08 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ButtonCancel_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
2023-02-05 19:51:51 +04:00
|
|
|
|
DialogResult = DialogResult.Cancel;
|
|
|
|
|
Close();
|
2023-02-05 19:37:08 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|