ISEbd-22_Alimova_M.S._Confe.../Confectionery/ConfectioneryView/FormShop.cs

127 lines
4.8 KiB
C#
Raw Normal View History

2024-02-29 23:33:00 +04:00
using ConfectioneryContracts.BusinessLogicsContracts;
using ConfectioneryDataModels.Models;
using Microsoft.Extensions.Logging;
using ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.SearchModels;
using System;
2024-02-28 22:55:32 +04:00
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;
2024-02-29 23:33:00 +04:00
using Microsoft.VisualBasic.Logging;
2024-02-28 22:55:32 +04:00
namespace ConfectioneryView
{
public partial class FormShop : Form
{
2024-02-29 23:33:00 +04:00
private readonly ILogger _logger;
private readonly IShopLogic _logic;
private int? _id;
public int Id { set { _id = value; } }
private Dictionary<int, (IPastryModel, int)> _ShopPastrys;
private DateTime? _openingDate = null;
public FormShop(ILogger<FormShop> logger, IShopLogic logic)
2024-02-28 22:55:32 +04:00
{
InitializeComponent();
2024-02-29 23:33:00 +04:00
_logger = logger;
_logic = logic;
_ShopPastrys = new Dictionary<int, (IPastryModel, int)>();
}
private void FormShop_Load(object sender, EventArgs e)
{
if (_id.HasValue)
{
_logger.LogInformation("Загрузка магазина");
try
{
var view = _logic.ReadElement(new ShopSearchModel
{
Id = _id.Value
});
if (view != null)
{
textBoxName.Text = view.ShopName;
textBoxAddress.Text = view.Address;
dateTimePickerOpen.Value = view.OpeningDate;
_ShopPastrys = view.ShopPastrys ?? new Dictionary<int, (IPastryModel, int)>();
LoadData();
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка загрузки магазина");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void LoadData()
{
_logger.LogInformation("Загрузка изделий в магазине");
try
{
if (_ShopPastrys != null)
{
dataGridView.Rows.Clear();
foreach (var sr in _ShopPastrys)
{
dataGridView.Rows.Add(new object[] { sr.Key, sr.Value.Item1.PastryName, sr.Value.Item2 });
}
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка загрузки изделий магазина");
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;
}
_logger.LogInformation("Сохранение магазина");
try
{
var model = new ShopBindingModel
{
Id = _id ?? 0,
ShopName = textBoxName.Text,
Address = textBoxAddress.Text,
OpeningDate = dateTimePickerOpen.Value
};
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, "Ошибка сохранения магазина");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void buttonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
2024-02-28 22:55:32 +04:00
}
}
}