143 lines
4.3 KiB
C#

using CarRepairShopContracts.BusinessLogicsContracts;
using CarRepairShopContracts.SearchModels;
using CarRepairShopContracts.ViewModels;
using CarRepairShopDataModels.Models;
namespace CarRepairShop
{
public partial class FormSupply : Form
{
private readonly List<RepairViewModel>? _repairList;
private readonly List<ShopViewModel>? _shopsList;
IShopLogic _shopLogic;
IRepairLogic _repairLogic;
public int ShopId
{
get
{
return
Convert.ToInt32(comboBoxShop.SelectedValue);
}
set
{
comboBoxShop.SelectedValue = value;
}
}
public int RepairId
{
get
{
return
Convert.ToInt32(comboBoxRepair.SelectedValue);
}
set
{
comboBoxRepair.SelectedValue = value;
}
}
public IRepairModel? RepairModel
{
get
{
if (_repairList == null)
{
return null;
}
foreach (var elem in _repairList)
{
if (elem.Id == RepairId)
{
return elem;
}
}
return null;
}
}
public int Count
{
get { return Convert.ToInt32(textBoxCount.Text); }
set
{ textBoxCount.Text = value.ToString(); }
}
public FormSupply(IRepairLogic repairLogic, IShopLogic shopLogic)
{
InitializeComponent();
_shopLogic = shopLogic;
_repairLogic = repairLogic;
_repairList = repairLogic.ReadList(null);
_shopsList = shopLogic.ReadList(null);
if (_repairList != null)
{
comboBoxRepair.DisplayMember = "RepairName";
comboBoxRepair.ValueMember = "Id";
comboBoxRepair.DataSource = _repairList;
comboBoxRepair.SelectedItem = null;
}
if (_shopsList != null)
{
comboBoxShop.DisplayMember = "ShopName";
comboBoxShop.ValueMember = "Id";
comboBoxShop.DataSource = _shopsList;
comboBoxShop.SelectedItem = null;
}
}
private void SaveButton_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxCount.Text))
{
MessageBox.Show("Заполните поле Количество", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (comboBoxRepair.SelectedValue == null)
{
MessageBox.Show("Выберите ремонт", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (comboBoxShop.SelectedValue == null)
{
MessageBox.Show("Выберите магазин", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
int count = Convert.ToInt32(textBoxCount.Text);
bool res = _shopLogic.MakeSupply(
new ShopSearchModel() { Id = Convert.ToInt32(comboBoxShop.SelectedValue) },
_repairLogic.ReadElement(new() { Id = Convert.ToInt32(comboBoxRepair.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();
}
}
}