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

117 lines
3.3 KiB
C#

using GiftShopContracts.BusinessLogicsContracts;
using GiftShopContracts.SearchModels;
using GiftShopContracts.ViewModels;
using GiftShopDataModels.Models;
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 FormSell : Form
{
private readonly List<GiftViewModel>? _GiftList;
IShopLogic _shopLogic;
IGiftLogic _GiftLogic;
public FormSell(IGiftLogic GiftLogic, IShopLogic shopLogic)
{
InitializeComponent();
_shopLogic = shopLogic;
_GiftLogic = GiftLogic;
_GiftList = GiftLogic.ReadList(null);
if (_GiftList != null)
{
GiftComboBox.DisplayMember = "GiftName";
GiftComboBox.ValueMember = "Id";
GiftComboBox.DataSource = _GiftList;
GiftComboBox.SelectedItem = null;
}
}
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(); }
}
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;
}
try
{
int count = Convert.ToInt32(CountTextBox.Text);
bool res = _shopLogic.MakeSell(
_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;
}
}
}
}