PIbd-23-Radaev-A.V.-GiftShop/GiftShop/GiftShopView/FormSupply.cs
Arkadiy Radaev 4b9e654056 res 2 hard
2024-05-18 17:53:11 +04:00

154 lines
4.5 KiB
C#

using GiftShopBusinessLogic.BusinessLogics;
using GiftShopContracts.BusinessLogicsContracts;
using GiftShopContracts.SearchModels;
using GiftShopContracts.ViewModels;
using GiftShopDataModels.Models;
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 GiftShopView
{
public partial class FormSupply : Form
{
private readonly List<GiftViewModel>? _GiftList;
private readonly List<ShopViewModel>? _shopsList;
IShopLogic _shopLogic;
IGiftLogic _GiftLogic;
public int ShopId
{
get
{
return
Convert.ToInt32(ShopComboBox.SelectedValue);
}
set
{
ShopComboBox.SelectedValue = value;
}
}
public int GiftId
{
get
{
return
Convert.ToInt32(GiftComboBox.SelectedValue);
}
set
{
GiftComboBox.SelectedValue = value;
}
}
public IGiftModel? GiftModel
{
get
{
if (_GiftList == null)
{
return null;
}
foreach (var elem in _GiftList)
{
if (elem.Id == GiftId)
{
return elem;
}
}
return null;
}
}
public int Count
{
get { return Convert.ToInt32(CountTextBox.Text); }
set
{ CountTextBox.Text = value.ToString(); }
}
public FormSupply(IGiftLogic GiftLogic, IShopLogic shopLogic)
{
InitializeComponent();
_shopLogic = shopLogic;
_GiftLogic = GiftLogic;
_GiftList = GiftLogic.ReadList(null);
_shopsList = shopLogic.ReadList(null);
if (_GiftList != null)
{
GiftComboBox.DisplayMember = "GiftName";
GiftComboBox.ValueMember = "Id";
GiftComboBox.DataSource = _GiftList;
GiftComboBox.SelectedItem = null;
}
if (_shopsList != null)
{
ShopComboBox.DisplayMember = "ShopName";
ShopComboBox.ValueMember = "Id";
ShopComboBox.DataSource = _shopsList;
ShopComboBox.SelectedItem = null;
}
}
private void SaveButton_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(CountTextBox.Text))
{
MessageBox.Show("Заполните поле Количество", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (GiftComboBox.SelectedValue == null)
{
MessageBox.Show("Выберите подарок", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (ShopComboBox.SelectedValue == null)
{
MessageBox.Show("Выберите магазин", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
int count = Convert.ToInt32(CountTextBox.Text);
bool res = _shopLogic.MakeSupply(
new ShopSearchModel() { Id = Convert.ToInt32(ShopComboBox.SelectedValue) },
_GiftLogic.ReadElement(new() { Id = Convert.ToInt32(GiftComboBox.SelectedValue) }),
count
);
if (!res)
{
throw new Exception("Ошибка при пополнении. Дополнительная информация в логах");
}
MessageBox.Show("Пополнение прошло успешно");
DialogResult = DialogResult.OK;
Close();
}
catch (Exception err)
{
MessageBox.Show("Ошибка пополнения");
return;
}
}
private void CancelButton_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
}
}