125 lines
4.5 KiB
C#
125 lines
4.5 KiB
C#
|
using FlowerShopContracts.BusinessLogicsContracts;
|
|||
|
using Microsoft.Extensions.Logging;
|
|||
|
using FlowerShopDataModels.Models;
|
|||
|
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;
|
|||
|
using FlowerShopContracts.BindingModels;
|
|||
|
using FlowerShopContracts.SearchModels;
|
|||
|
|
|||
|
|
|||
|
namespace ProjectFlowerShop
|
|||
|
{
|
|||
|
public partial class ShopForm : Form
|
|||
|
{
|
|||
|
private readonly ILogger _logger;
|
|||
|
private readonly IShopLogic _logic;
|
|||
|
public int? _id;
|
|||
|
private Dictionary<int, (IFlowerModel, int)> _flowers;
|
|||
|
public ShopForm(ILogger<ShopForm> logger, IShopLogic logic)
|
|||
|
{
|
|||
|
InitializeComponent();
|
|||
|
_logger = logger;
|
|||
|
_logic = logic;
|
|||
|
}
|
|||
|
|
|||
|
private void LoadData()
|
|||
|
{
|
|||
|
_logger.LogInformation("Загрузка товаров магазина");
|
|||
|
try
|
|||
|
{
|
|||
|
if (_flowers != null)
|
|||
|
{
|
|||
|
foreach (var flower in _flowers)
|
|||
|
{
|
|||
|
DataGridView.Rows.Add(new object[] { flower.Key, flower.Value.Item1.FlowerName, flower.Value.Item1.Price, flower.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,
|
|||
|
DateOpen = DateTimePicker.Value.Date,
|
|||
|
ShopFlowers = _flowers
|
|||
|
};
|
|||
|
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();
|
|||
|
}
|
|||
|
|
|||
|
private void ShopForm_Load(object sender, EventArgs e)
|
|||
|
{
|
|||
|
if (_id.HasValue)
|
|||
|
{
|
|||
|
_logger.LogInformation("Загрузка магазина");
|
|||
|
try
|
|||
|
{
|
|||
|
var shop = _logic.ReadElement(new ShopSearchModel { Id = _id });
|
|||
|
if (shop != null)
|
|||
|
{
|
|||
|
textBoxName.Text = shop.ShopName;
|
|||
|
textBoxAddress.Text = shop.Address;
|
|||
|
DateTimePicker.Text = shop.DateOpen.ToString();
|
|||
|
_flowers = shop.ShopFlowers;
|
|||
|
}
|
|||
|
LoadData();
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
_logger.LogError(ex, "Ошибка загрузки магазина");
|
|||
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
|
|||
|
MessageBoxIcon.Error);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|