Pibd-21_Ievlewa_MD._Precast.../PrecastConcretePlant/PrecastConcretePlantView/FormShop.cs

134 lines
5.1 KiB
C#
Raw Permalink Normal View History

2024-04-18 23:41:30 +04:00
using PrecastConcretePlantContracts.BindingModels;
using PrecastConcretePlantContracts.BusinessLogicsContracts;
using PrecastConcretePlantContracts.SearchModels;
using PrecastConcretePlantDataModels.Models;
using Microsoft.Extensions.Logging;
using PrecastConcretePlantListImplement.Models;
namespace PrecastConcretePlantView
{
public partial class FormShop : Form
{
private readonly ILogger _logger;
private readonly IShopLogic _logic;
private int? _id;
private Dictionary<int, (IReinforcedModel, int)> _shopReinforceds;
public int Id { set { _id = value; } }
public FormShop(ILogger<FormShop> logger, IShopLogic logic)
{
InitializeComponent();
_logger = logger;
_logic = logic;
_shopReinforceds = new Dictionary<int, (IReinforcedModel, int)>();
}
private void FormShop_Load(object sender, EventArgs e)
{
if (_id.HasValue)
{
_logger.LogInformation("Shop loading");
try
{
var view = _logic.ReadElement(new ShopSearchModel { Id = _id.Value });
if (view != null)
{
textBoxName.Text = view.ShopName;
textBoxAddress.Text = view.Address;
dateTimePicker.Value = view.DateOpening;
2024-05-16 21:59:54 +04:00
textBoxMaximum.Text = view.ReinforcedsMax.ToString();
2024-04-18 23:41:30 +04:00
_shopReinforceds = view.ShopReinforceds ?? new Dictionary<int, (IReinforcedModel, int)>();
LoadData();
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Shop loading error");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void LoadData()
{
_logger.LogInformation("Shop reinforceds loading");
try
{
if (_shopReinforceds != null)
{
dataGridView.Rows.Clear();
foreach (var reinforced in _shopReinforceds)
{
dataGridView.Rows.Add(new object[] { reinforced.Key, reinforced.Value.Item1.ReinforcedName, reinforced.Value.Item2 });
}
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Shop reinforceds loading error");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ButtonSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxName.Text))
{
MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (string.IsNullOrEmpty(textBoxAddress.Text))
{
MessageBox.Show("Заполните адрес", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (string.IsNullOrEmpty(dateTimePicker.Text))
{
MessageBox.Show("Заполните дату", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
2024-05-16 21:59:54 +04:00
if (string.IsNullOrEmpty(textBoxMaximum.Text))
{
MessageBox.Show("Заполните максимальное количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
2024-04-18 23:41:30 +04:00
_logger.LogInformation("Shop saving");
try
{
var model = new ShopBindingModel
{
Id = _id ?? 0,
ShopName = textBoxName.Text,
Address = textBoxAddress.Text,
DateOpening = dateTimePicker.Value,
2024-05-16 21:59:54 +04:00
ReinforcedsMax = Convert.ToInt32(textBoxMaximum.Text),
2024-04-18 23:41:30 +04:00
ShopReinforceds = _shopReinforceds
};
var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model);
if (!operationResult)
{
throw new Exception("Ошибка при сохранении. Дополнительная информация в логах.");
}
MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
DialogResult = DialogResult.OK;
Close();
}
catch (Exception ex)
{
_logger.LogError(ex, "Shop saving error");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ButtonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
}
}