105 lines
3.8 KiB
C#
105 lines
3.8 KiB
C#
using GiftShopContracts.BusinessLogicsContracts;
|
||
using GiftShopContracts.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 GiftShopView
|
||
{
|
||
public partial class FormShopReplenishment : Form
|
||
{
|
||
private readonly ILogger _logger;
|
||
private readonly IShopLogic _ShopLogic;
|
||
private readonly IGiftLogic _giftLogic;
|
||
private readonly List<ShopViewModel>? _listShops;
|
||
private readonly List<GiftViewModel>? _listGifts;
|
||
public FormShopReplenishment(ILogger<FormShopReplenishment> logger, IShopLogic ShopLogic, IGiftLogic giftLogic)
|
||
{
|
||
InitializeComponent();
|
||
_ShopLogic = ShopLogic;
|
||
_giftLogic = giftLogic;
|
||
_logger = logger;
|
||
_listShops = ShopLogic.ReadList(null);
|
||
if (_listShops != null)
|
||
{
|
||
shopNameComboBox.DisplayMember = "ShopName";
|
||
shopNameComboBox.ValueMember = "Id";
|
||
shopNameComboBox.DataSource = _listShops;
|
||
shopNameComboBox.SelectedItem = null;
|
||
}
|
||
|
||
_listGifts = giftLogic.ReadList(null);
|
||
if (_listGifts != null)
|
||
{
|
||
giftNameComboBox.DisplayMember = "GiftName";
|
||
giftNameComboBox.ValueMember = "Id";
|
||
giftNameComboBox.DataSource = _listGifts;
|
||
giftNameComboBox.SelectedItem = null;
|
||
}
|
||
}
|
||
|
||
private void SaveButton_Click(object sender, EventArgs e)
|
||
{
|
||
if (shopNameComboBox.SelectedValue == null)
|
||
{
|
||
MessageBox.Show("Выберите магазин", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
return;
|
||
}
|
||
|
||
if (giftNameComboBox.SelectedValue == null)
|
||
{
|
||
MessageBox.Show("Выберите изделие", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
return;
|
||
}
|
||
|
||
_logger.LogInformation("Добавление изделия в магазин");
|
||
|
||
try
|
||
{
|
||
var gift = _giftLogic.ReadElement(new()
|
||
{
|
||
Id = (int)giftNameComboBox.SelectedValue
|
||
});
|
||
|
||
if (gift == null)
|
||
{
|
||
throw new Exception("Не найдено изделие. Дополнительная информация в логах.");
|
||
}
|
||
|
||
var resultOperation = _ShopLogic.AddGift(
|
||
model: new() { Id = (int)shopNameComboBox.SelectedValue },
|
||
gift: gift,
|
||
quantity: Convert.ToInt32(countTextBox.Text)
|
||
);
|
||
|
||
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();
|
||
}
|
||
}
|
||
}
|