2024-05-05 00:19:00 +04:00

105 lines
3.9 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.Extensions.Logging;
using PlumbingRepairContracts.BusinessLogicsContracts;
using PlumbingRepairContracts.ViewModels;
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 PlumbingRepairView
{
public partial class FormStoreReplenishment : Form
{
private readonly ILogger _logger;
private readonly IStoreLogic _storeLogic;
private readonly IWorkLogic _workLogic;
private readonly List<StoreViewModel>? _listStores;
private readonly List<WorkViewModel>? _listWorks;
public FormStoreReplenishment(ILogger<FormStoreReplenishment> logger, IStoreLogic storeLogic, IWorkLogic workLogic)
{
InitializeComponent();
_storeLogic = storeLogic;
_workLogic = workLogic;
_logger = logger;
_listStores = storeLogic.ReadList(null);
if (_listStores != null)
{
StoreNameComboBox.DisplayMember = "StoreName";
StoreNameComboBox.ValueMember = "Id";
StoreNameComboBox.DataSource = _listStores;
StoreNameComboBox.SelectedItem = null;
}
_listWorks = workLogic.ReadList(null);
if (_listWorks != null)
{
WorkNameComboBox.DisplayMember = "WorkName";
WorkNameComboBox.ValueMember = "Id";
WorkNameComboBox.DataSource = _listWorks;
WorkNameComboBox.SelectedItem = null;
}
}
private void SaveButton_Click(object sender, EventArgs e)
{
if (StoreNameComboBox.SelectedValue == null)
{
MessageBox.Show("Выберите магазин", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (WorkNameComboBox.SelectedValue == null)
{
MessageBox.Show("Выберите изделие", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_logger.LogInformation("Добавление изделия в магазин");
try
{
var work = _workLogic.ReadElement(new()
{
Id = (int)WorkNameComboBox.SelectedValue
});
if (work == null)
{
throw new Exception("Не найдено изделие. Дополнительная информация в логах.");
}
var resultOperation = _storeLogic.AddWork(
model: new() { Id = (int)StoreNameComboBox.SelectedValue },
work: work,
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();
}
}
}