128 lines
4.9 KiB
C#
128 lines
4.9 KiB
C#
using PizzeriaDataModels.Models;
|
|
using Microsoft.Extensions.Logging;
|
|
using PizzeriaContracts.BindingModels;
|
|
using PizzeriaContracts.BusinessLogicsContracts;
|
|
using PizzeriaContracts.SearchModels;
|
|
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 PizzeriaView
|
|
{
|
|
public partial class FormShop : Form
|
|
{
|
|
private readonly ILogger _logger;
|
|
private readonly IShopLogic _logic;
|
|
private int? _id;
|
|
public int Id { set { _id = value; } }
|
|
private Dictionary<int, (IPizzaModel, int)> _ShopPizzas;
|
|
private DateTime? _openingDate = null;
|
|
public FormShop(ILogger<FormShop> logger, IShopLogic logic)
|
|
{
|
|
InitializeComponent();
|
|
_logger = logger;
|
|
_logic = logic;
|
|
_ShopPizzas = new Dictionary<int, (IPizzaModel, 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;
|
|
textBoxAdress.Text = view.Adress;
|
|
dateTimeOpen.Value = view.OpeningDate;
|
|
numericUpPizzaMaxCount.Value = view.PizzaMaxCount;
|
|
_ShopPizzas = view.ShopPizzas ?? new Dictionary<int, (IPizzaModel, int)>();
|
|
LoadData();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка загрузки магазина");
|
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void LoadData()
|
|
{
|
|
_logger.LogInformation("Загрузка пиццы в магазине");
|
|
try
|
|
{
|
|
if (_ShopPizzas != null)
|
|
{
|
|
dataGridView.Rows.Clear();
|
|
foreach (var sr in _ShopPizzas)
|
|
{
|
|
dataGridView.Rows.Add(new object[] { sr.Key, sr.Value.Item1.PizzaName, 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(textBoxAdress.Text))
|
|
{
|
|
MessageBox.Show("Заполните адрес", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
_logger.LogInformation("Сохранение магазина");
|
|
try
|
|
{
|
|
var model = new ShopBindingModel
|
|
{
|
|
Id = _id ?? 0,
|
|
ShopName = textBoxName.Text,
|
|
Adress = textBoxAdress.Text,
|
|
OpeningDate = dateTimeOpen.Value,
|
|
PizzaMaxCount = (int)numericUpPizzaMaxCount.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();
|
|
}
|
|
}
|
|
}
|